142 lines
4.9 KiB
Kotlin
142 lines
4.9 KiB
Kotlin
package com.example.rehabilitation.Progress
|
|
|
|
import android.content.Context
|
|
import android.content.SharedPreferences
|
|
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.LinearLayoutManager
|
|
import com.example.rehabilitation.Pref.ConclusionPref
|
|
import com.example.rehabilitation.Retrofit.PatientApi
|
|
import com.example.rehabilitation.databinding.FragmentProgresBinding
|
|
import com.example.rehabilitation.listCalendare
|
|
import com.example.rehabilitation.listProgressCheck
|
|
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 org.json.JSONObject
|
|
import retrofit2.Retrofit
|
|
import retrofit2.converter.gson.GsonConverterFactory
|
|
|
|
class ProgresFragment : Fragment() {
|
|
private lateinit var binding: FragmentProgresBinding
|
|
private val model: MainViewModel by activityViewModels()//Инициализировали класс
|
|
|
|
//Хранинение токена
|
|
private var prefToken: SharedPreferences? = null
|
|
private var Token = ""
|
|
private lateinit var patientApi: PatientApi
|
|
lateinit var adapterProgress: ProgressAdapter
|
|
var view_progress = false
|
|
|
|
val prefPatientConclusion = ConclusionPref()
|
|
|
|
override fun onCreateView(
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
savedInstanceState: Bundle?
|
|
): View? {
|
|
binding = FragmentProgresBinding.inflate(layoutInflater, container, false)
|
|
return binding.root
|
|
}
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
super.onViewCreated(view, savedInstanceState)
|
|
prefToken = activity?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
|
Token = prefToken?.getString("token", "")!!
|
|
|
|
model.liveProgressCheckList.observe(viewLifecycleOwner) {//viewLifecycleOwner - следит за циклом жизни fragment
|
|
adapterProgress.submitList(it)//Напрямую переадем созданный список в adapter(ProductAdapter)
|
|
visible1()
|
|
}
|
|
|
|
initRetrofit()
|
|
initRCView()
|
|
visibleLoad()
|
|
AddProgresSportDayList()
|
|
}
|
|
|
|
|
|
//Функция получения данных для заполнения списка прогресса
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Инициализация подлючения к серверу
|
|
fun AddProgresSportDayList() {
|
|
if (view_progress == false) {
|
|
view_progress = true
|
|
visibleLoad()
|
|
initRetrofit()
|
|
val Tokens = prefPatientConclusion.conclusionToken(requireContext())
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
val progress = patientApi.ProgressPatientCourses("Bearer $Tokens")
|
|
|
|
activity?.runOnUiThread {
|
|
|
|
//Фиксируем полученные данные
|
|
val progressMes = progress.body()
|
|
val progressMesCode = progress.isSuccessful()
|
|
//Если нету ошибок
|
|
if (progressMesCode) {
|
|
model.liveProgressCheckList.value = progressMes?.search_check_sport
|
|
binding.txtLoadProgres.visibility = View.GONE
|
|
}
|
|
else{
|
|
binding.txtLoadProgres.visibility = View.VISIBLE
|
|
visible1()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
private fun initRCView() = with(binding) {
|
|
rcViewProgress.layoutManager = LinearLayoutManager(
|
|
requireContext(),
|
|
LinearLayoutManager.VERTICAL,
|
|
false
|
|
)//По вертикали будет выводить по умолчанию
|
|
adapterProgress = ProgressAdapter()
|
|
rcViewProgress.adapter = adapterProgress
|
|
}
|
|
|
|
fun visible1() {
|
|
binding.CLMainProgres.visibility = View.VISIBLE
|
|
binding.CLLoad.visibility = View.GONE
|
|
}
|
|
|
|
|
|
fun visibleLoad() {
|
|
binding.CLMainProgres.visibility = View.GONE
|
|
binding.CLLoad.visibility = View.VISIBLE
|
|
}
|
|
|
|
companion object {
|
|
fun newInstance() = ProgresFragment()
|
|
}
|
|
|
|
} |