245 lines
8.7 KiB
Kotlin
245 lines
8.7 KiB
Kotlin
package com.example.rehabilitation.Appeals
|
||
|
||
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 androidx.fragment.app.FragmentActivity
|
||
import androidx.fragment.app.activityViewModels
|
||
import com.example.rehabilitation.Adapter.VpAdapter
|
||
import com.example.rehabilitation.Appeals.TabLayout.NewAppealsFragment
|
||
import com.example.rehabilitation.Appeals.TabLayout.OldAppealsFragment
|
||
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.PatientViewModel
|
||
import com.example.rehabilitation.Pref.ConclusionPref
|
||
import com.example.rehabilitation.R
|
||
import com.example.rehabilitation.Retrofit.PatientApi
|
||
import com.example.rehabilitation.databinding.FragmentAppealsBinding
|
||
import com.google.android.material.tabs.TabLayoutMediator
|
||
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
|
||
import java.util.Timer
|
||
import kotlin.concurrent.fixedRateTimer
|
||
|
||
|
||
class AppealsFragment : Fragment() {
|
||
private lateinit var binding: FragmentAppealsBinding
|
||
|
||
private val modelPatient: PatientViewModel by activityViewModels()
|
||
private lateinit var patientApi: PatientApi
|
||
private lateinit var timer: Timer
|
||
|
||
val prefPatientConclusion = ConclusionPref()
|
||
|
||
//Класс проверки интеренета
|
||
val enternetCheck = EnternetCheck()
|
||
|
||
private val tList = listOf(
|
||
"новые",
|
||
"обработанные",
|
||
)
|
||
|
||
//Список с фрагментами для переключения
|
||
private val flist = listOf(
|
||
NewAppealsFragment.newInstance(),
|
||
OldAppealsFragment.newInstance(),
|
||
)
|
||
|
||
override fun onCreateView(
|
||
inflater: LayoutInflater, container: ViewGroup?,
|
||
savedInstanceState: Bundle?
|
||
): View? {
|
||
binding = FragmentAppealsBinding.inflate(layoutInflater, container, false)
|
||
return binding.root
|
||
}
|
||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||
super.onViewCreated(view, savedInstanceState)
|
||
|
||
init()
|
||
|
||
binding.btnAddMessage.setOnClickListener {
|
||
activity?.supportFragmentManager?.beginTransaction()
|
||
?.replace(R.id.CLAppeals, CreateMessageFragment.newInstance())?.commit()
|
||
binding.ConstrainLayoutProductEdit.visibility = View.GONE
|
||
binding.CLAppeals.visibility = View.VISIBLE
|
||
}
|
||
|
||
// fixedRateTimer("timer", false, 0, 10000) {
|
||
// activity?.runOnUiThread {
|
||
//
|
||
// }
|
||
// }
|
||
}
|
||
|
||
|
||
private fun getOldAppeals() {
|
||
initRetrofit()
|
||
val Tokens = prefPatientConclusion.conclusionToken(requireContext())
|
||
CoroutineScope(Dispatchers.IO).launch {
|
||
val listAppeals = patientApi.GetAppealsPatientOld("Bearer $Tokens")
|
||
requireActivity().runOnUiThread {
|
||
|
||
//Фиксируем полученные данные
|
||
val List = listAppeals.body()
|
||
val Nice = listAppeals.isSuccessful
|
||
val Code = listAppeals.code()
|
||
if(Code==429){
|
||
val intetn = Intent(requireContext(), Code429Activity::class.java)
|
||
|
||
startActivity(intetn)
|
||
}
|
||
else if(Code==200) {
|
||
//Если нету ошибок
|
||
if (Nice) {
|
||
//Если нету ошибок
|
||
if (List != null) {
|
||
if(List.appeals_old != null){
|
||
modelPatient.appealsOldList.value = List.appeals_old
|
||
}
|
||
}
|
||
|
||
}
|
||
getNewAppeals()
|
||
}
|
||
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)
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
//Получение необработанных сообщений
|
||
private fun getNewAppeals() {
|
||
if (enternetCheck.isOnline(requireContext())) {
|
||
initRetrofit()
|
||
val Tokens = prefPatientConclusion.conclusionToken(requireContext())
|
||
CoroutineScope(Dispatchers.IO).launch {
|
||
val listAppeals = patientApi.GetAppealsPatientNew("Bearer $Tokens")
|
||
requireActivity().runOnUiThread {
|
||
|
||
//Фиксируем полученные данные
|
||
val List = listAppeals.body()
|
||
val Nice = listAppeals.isSuccessful
|
||
val Code = listAppeals.code()
|
||
if(Code==429){
|
||
val intetn = Intent(requireContext(), Code429Activity::class.java)
|
||
|
||
startActivity(intetn)
|
||
}
|
||
else if(Code==200) {
|
||
//Если нету ошибок
|
||
if (Nice) {
|
||
//Если нету ошибок
|
||
if (List != null) {
|
||
if(List.appeals_new != null){
|
||
modelPatient.appealsNewList.value = List.appeals_new
|
||
}
|
||
}
|
||
}
|
||
}
|
||
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)
|
||
}
|
||
|
||
}
|
||
|
||
//Инициализируем Retrofit
|
||
private fun initRetrofit() {
|
||
val interceptor = HttpLoggingInterceptor()
|
||
interceptor.level = HttpLoggingInterceptor.Level.BODY
|
||
|
||
val client = OkHttpClient
|
||
.Builder()
|
||
.addInterceptor(interceptor)
|
||
.build()
|
||
|
||
val retrofit = Retrofit.Builder()
|
||
.baseUrl("http://mobileapp.vmeda.org/api/")
|
||
.client(client)
|
||
.addConverterFactory(GsonConverterFactory.create())
|
||
.build()
|
||
|
||
patientApi = retrofit.create(PatientApi::class.java)
|
||
|
||
}
|
||
|
||
//Функция подключения переключения
|
||
private fun init() = with(binding) {
|
||
val adapter = VpAdapter(activity as FragmentActivity, flist)
|
||
vpProduct.adapter = adapter
|
||
|
||
//Переключения (связываем таблаяут(переключатель) с viewpager, чтобы переключать фрагменты)
|
||
TabLayoutMediator(tabLayoutProduct, vpProduct) { tab, pos ->
|
||
tab.text =
|
||
tList[pos]//tab - нажатая кнопка, pos - позиция кнопки, tList[pos] - передаем название по полученной позиции
|
||
}.attach()// attach() - чтобы все переключалось, а не вывадило постоянно один экран
|
||
|
||
//Изменения цвета в зависомости на каком из tabLayout вы находитесь
|
||
binding.tabLayoutProduct.setTabTextColors(getResources().getColor(R.color.black),
|
||
getResources().getColor(R.color.white));
|
||
}
|
||
|
||
companion object {
|
||
|
||
fun newInstance() = AppealsFragment()
|
||
}
|
||
|
||
|
||
override fun onResume() {
|
||
super.onResume()
|
||
checkForUpdates(true)
|
||
}
|
||
|
||
override fun onStop() {
|
||
super.onStop()
|
||
timer.cancel()
|
||
timer.purge()
|
||
}
|
||
|
||
private fun checkForUpdates(daemonIsTrue: Boolean) {
|
||
|
||
timer = fixedRateTimer("default", daemonIsTrue, 0, 15000) {
|
||
activity?.runOnUiThread {
|
||
getOldAppeals()
|
||
|
||
}
|
||
}
|
||
|
||
}
|
||
} |