2024-01-29 10:59:13 +03:00

173 lines
6.2 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.example.rehabilitation.Setting
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import com.example.admin.Toast.showCustomInfoToast
import com.example.rehabilitation.Auth.AuthFragment
import com.example.rehabilitation.Auth.AuthorizationActivity
import com.example.rehabilitation.CodeError.Code429Activity
import com.example.rehabilitation.CodeError.Code500Activity
import com.example.rehabilitation.Enternet.EnternetActivity
import com.example.rehabilitation.Enternet.EnternetCheck
import com.example.rehabilitation.Feedback.FeedbackFragment
import com.example.rehabilitation.MainFragment
import com.example.rehabilitation.Pref.ClearPref
import com.example.rehabilitation.Pref.ConclusionPref
import com.example.rehabilitation.R
import com.example.rehabilitation.Retrofit.PatientApi
import com.example.rehabilitation.databinding.FragmentSettingBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class SettingFragment : Fragment() {
private lateinit var binding: FragmentSettingBinding
private var Token = ""
val prefPatientClear = ClearPref()
val prefPatientConclusion = ConclusionPref()
private lateinit var patientApi: PatientApi
//Класс проверки интеренета
val enternetCheck = EnternetCheck()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentSettingBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (isAdded()) {
binding.btnExitSetting.setOnClickListener {
activity?.supportFragmentManager?.beginTransaction()
?.replace(R.id.CLMain, MainFragment.newInstance())
//?.addToBackStack(null)
?.commit()
}
binding.cvFeedback.setOnClickListener {
activity?.supportFragmentManager?.beginTransaction()
?.replace(R.id.CLMain, FeedbackFragment.newInstance())
//?.addToBackStack(null)
?.commit()
}
//Выход из аккаунта
binding.cvExitPatient.setOnClickListener {
createAlеrtDialogExitAuth()
}
}
}
//Диалоговое окно
private fun createAlеrtDialogExitAuth() {
val builder = AlertDialog.Builder(requireContext())
builder.setTitle("Выход")
builder.setMessage("Вы уверены что хотите выйти из аккаунта")
builder.setNegativeButton("Назад") { dialogInterface, i ->
}
builder.setPositiveButton("Подтвердить") { dialogInterface, i ->
Logout()
}
builder.show()
}
//Инициализация подлючения к серверу
private fun initRetrofit() {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient
.Builder()
.addInterceptor(interceptor)
.build()
val retrofit = Retrofit.Builder()
.baseUrl("https://rehabilitation.vmeda.org/api/")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
patientApi = retrofit.create(PatientApi::class.java)
}
//Получения списка пациентов
fun Logout() {
if (enternetCheck.isOnline(requireContext())) {
initRetrofit()
val Tokens = prefPatientConclusion.conclusionToken(requireContext())
CoroutineScope(Dispatchers.IO).launch {
val listProduct = patientApi.LogoutPatient("Bearer $Tokens")
activity?.runOnUiThread {
//Фиксируем полученные данные
val List = listProduct.body()
val Nice = listProduct.isSuccessful
val Code = listProduct.code()
if(Code==429){
val intetn = Intent(requireContext(), Code429Activity::class.java)
startActivity(intetn)
}
else if(Code==200) {
//Если нету ошибок
if (Nice) {
//Если нету ошибок
if (List != null) {
Toast(requireContext()).showCustomInfoToast(List?.message.toString(), requireActivity())
prefPatientClear.clearToken(requireContext())
activity?.supportFragmentManager?.beginTransaction()
?.replace(R.id.CLMain, AuthFragment.newInstance())
?.commit()
}
}
}
else if (Code == 500) {
val intetn = Intent(requireContext(), Code500Activity::class.java)
startActivity(intetn)
}
else if (Code == 401) {
val intetn = Intent(requireContext(), AuthorizationActivity::class.java)
activity?.finish()
startActivity(intetn)
}
}
}
} else {
val intetn = Intent(requireContext(), EnternetActivity::class.java)
activity?.finish()
startActivity(intetn)
}
}
companion object {
fun newInstance() = SettingFragment()
}
}