day3_commit1_fixed_mainfragment

This commit is contained in:
Terebov_Maksim 2025-02-20 12:18:31 +03:00
parent 194a1dc466
commit 9818c97148

View File

@ -1,31 +1,25 @@
package ru.myitschool.work.ui.main package ru.myitschool.work.ui.main
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.View import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import ru.myitschool.work.R import ru.myitschool.work.R
import ru.myitschool.work.api.ApiService import ru.myitschool.work.api.ApiService
import ru.myitschool.work.api.AccessLog import ru.myitschool.work.api.EmployeeData
import ru.myitschool.work.core.Constants import ru.myitschool.work.core.Constants
import ru.myitschool.work.databinding.FragmentMainBinding import ru.myitschool.work.databinding.FragmentMainBinding
import ru.myitschool.work.SessionManager
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory import retrofit2.converter.gson.GsonConverterFactory
import ru.myitschool.work.SessionManager
class MainFragment : Fragment(R.layout.fragment_main) { class MainFragment : Fragment(R.layout.fragment_main) {
private var _binding: FragmentMainBinding? = null private var _binding: FragmentMainBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
private lateinit var apiService: ApiService private lateinit var apiService: ApiService
private lateinit var accessLogAdapter: AccessLogAdapter
private val accessLogs = mutableListOf<AccessLog>()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -42,55 +36,44 @@ class MainFragment : Fragment(R.layout.fragment_main) {
setupUI() setupUI()
fetchUserInfo() fetchUserInfo()
fetchAccessLogs()
} }
private fun setupUI() { private fun setupUI() {
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) // Инициализация UI компонентов, если необходимо
accessLogAdapter = AccessLogAdapter(accessLogs)
binding.recyclerView.adapter = accessLogAdapter
binding.refresh.setOnClickListener { binding.refresh.setOnClickListener {
fetchUserInfo() fetchUserInfo()
fetchAccessLogs()
} }
} }
private fun fetchUserInfo() { private fun fetchUserInfo() {
lifecycleScope.launch { lifecycleScope.launch {
val login = SessionManager.userLogin ?: return@launch val login = SessionManager.userLogin ?: return@launch
val authHeader = "Basic ${SessionManager.getAuthHeader()}" val authHeader = SessionManager.getAuthHeader() // Получаем заголовок авторизации
Log.d("MainFragment", "Fetching user info for login: $login with authHeader: $authHeader")
val response = apiService.getUserInfo(login, authHeader) val response = apiService.getUserInfo(login, authHeader)
Log.d("MainFragment", "Response code: ${response.code()}") // Логируем код ответа
if (response.isSuccessful) { if (response.isSuccessful) {
val employeeData = response.body() val employeeData = response.body()
employeeData?.let { employeeData?.let {
binding.fullname.text = it.name binding.fullname.text = it.name
binding.position.text = it.position binding.position.text = it.position
binding.lastEntry.text = it.lastVisit binding.lastEntry.text = it.lastVisit
// Здесь можно установить изображение пользователя, если оно доступно
binding.photo.visibility = View.VISIBLE binding.photo.visibility = View.VISIBLE
binding.fullname.visibility = View.VISIBLE binding.fullname.visibility = View.VISIBLE
binding.position.visibility = View.VISIBLE binding.position.visibility = View.VISIBLE
binding.lastEntry.visibility = View.VISIBLE binding.lastEntry.visibility = View.VISIBLE
} }
} else { } else {
binding.error.text = "Ошибка получения данных" binding.error.text = "Ошибка получения данных: ${response.message()}"
binding.error.visibility = View.VISIBLE binding.error.visibility = View.VISIBLE
} }
} }
} }
private fun fetchAccessLogs() {
// Здесь вы можете реализовать логику для получения данных о проходах
// Например, вы можете сделать запрос к API для получения списка AccessLog
// Для примера, добавим несколько фиктивных данных
accessLogs.clear()
accessLogs.add(AccessLog("2024-02-31 08:31", "Reader 1", "карта"))
accessLogs.add(AccessLog("2024-02-31 09:00", "Reader 2", "смартфон"))
accessLogAdapter.notifyDataSetChanged()
}
override fun onDestroyView() { override fun onDestroyView() {
_binding = null _binding = null
super.onDestroyView() super.onDestroyView()