244 lines
7.4 KiB
Kotlin
244 lines
7.4 KiB
Kotlin
package com.example.rehabilitation.Calendare
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.util.Log
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import androidx.fragment.app.Fragment
|
|
import androidx.fragment.app.activityViewModels
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
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.Retrofit.PatientApi
|
|
import com.example.rehabilitation.Sport.DayAdapter
|
|
import com.example.rehabilitation.databinding.FragmentCalendarBinding
|
|
import com.example.sqlitework.dip.MainViewModel
|
|
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.time.LocalDate
|
|
import java.util.Timer
|
|
import kotlin.concurrent.fixedRateTimer
|
|
import kotlin.concurrent.timerTask
|
|
|
|
|
|
class CalendarFragment : Fragment(),CalendareListAdapter.Listener {
|
|
private lateinit var binding: FragmentCalendarBinding
|
|
lateinit var adapterCalendare: CalendareListAdapter
|
|
private val modelPatient: PatientViewModel by activityViewModels()//Инициализировали класс
|
|
private lateinit var patientApi: PatientApi
|
|
private lateinit var timer: Timer
|
|
|
|
val prefPatientConclusion = ConclusionPref()
|
|
|
|
|
|
|
|
//Календарь
|
|
var listCalendare:List<CalendareModel>? = null
|
|
|
|
@SuppressLint("NewApi")
|
|
|
|
//Загрузка данных для календаря
|
|
var calendare = false
|
|
|
|
//Класс проверки интеренета
|
|
val enternetCheck = EnternetCheck()
|
|
|
|
var infoVisibleCalendare = false
|
|
|
|
override fun onCreateView(
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
savedInstanceState: Bundle?
|
|
): View? {
|
|
binding = FragmentCalendarBinding.inflate(layoutInflater, container, false)
|
|
return binding.root
|
|
}
|
|
|
|
@SuppressLint("NewApi")
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
if(isAdded) {
|
|
visibleLoad()
|
|
initRcView()
|
|
initRetrofit()
|
|
APIliveCountCalendareCurrent()
|
|
|
|
|
|
modelPatient.calendareList.observe(viewLifecycleOwner){
|
|
if(listCalendare != it){
|
|
listCalendare = it
|
|
adapterCalendare.submitList(it)
|
|
}
|
|
visibleCalendare()
|
|
}
|
|
|
|
// fixedRateTimer("timer", false, 0, 10000) {
|
|
// activity?.runOnUiThread {
|
|
//
|
|
// }
|
|
// }
|
|
}
|
|
|
|
binding.btnInfoCalendare.setOnClickListener{
|
|
if(infoVisibleCalendare == false){
|
|
infoVisibleCalendare = true
|
|
binding.CVInfoCalendare.visibility = View.VISIBLE
|
|
}
|
|
else{
|
|
infoVisibleCalendare = false
|
|
binding.CVInfoCalendare.visibility = View.GONE
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fun visibleCalendare(){
|
|
binding.CLCalendare.visibility = View.VISIBLE
|
|
binding.CLLoad.visibility = View.GONE
|
|
}
|
|
|
|
fun visibleLoad(){
|
|
binding.CLCalendare.visibility = View.GONE
|
|
binding.CLLoad.visibility = View.VISIBLE
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Инициализация списка
|
|
private fun initRcView() = with(binding) {
|
|
rcView.layoutManager = GridLayoutManager(requireContext(), 1)//По вертикали будет выводить по умолчанию
|
|
adapterCalendare = CalendareListAdapter(this@CalendarFragment)
|
|
rcView.adapter = adapterCalendare
|
|
//binding.rcVIewDayList.smoothScrollToPosition(0);
|
|
}
|
|
|
|
|
|
|
|
//Инициализация запроса
|
|
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)//Экземпляр
|
|
}
|
|
|
|
//Функция получения данных для заполнения календаря
|
|
private fun APIliveCountCalendareCurrent() {
|
|
if (enternetCheck.isOnline(requireContext())) {
|
|
|
|
initRetrofit()
|
|
|
|
val Tokens = prefPatientConclusion.conclusionToken(requireContext())
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
val Calendare = patientApi.GetCalendare("Bearer $Tokens")
|
|
|
|
activity?.runOnUiThread {
|
|
|
|
|
|
//Фиксируем полученные данные
|
|
val List = Calendare.body()
|
|
val Nice = Calendare.isSuccessful
|
|
val Code = Calendare.code()
|
|
if(Code==429){
|
|
val intetn = Intent(requireContext(), Code429Activity::class.java)
|
|
|
|
startActivity(intetn)
|
|
}
|
|
else if(Code==200) {
|
|
//Если нету ошибок
|
|
if (Nice) {
|
|
//Если нету ошибок
|
|
if (List != null) {
|
|
Log.i("CalendareList_calendare_day", List.calendare_day.toString())
|
|
modelPatient.calendareList.value = List.calendare_day
|
|
}
|
|
|
|
}
|
|
}
|
|
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() = CalendarFragment()
|
|
}
|
|
|
|
override fun onClickCalendare(item: CalendareModel) {
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
APIliveCountCalendareCurrent()
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
} |