day3_commit2_added_qr_scan

This commit is contained in:
Terebov_Maksim 2025-02-20 12:43:28 +03:00
parent 9818c97148
commit cb5fbdd634
4 changed files with 29 additions and 15 deletions

View File

@ -1,10 +1,10 @@
package ru.myitschool.work.ui.main
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import kotlinx.coroutines.launch
import ru.myitschool.work.R
import ru.myitschool.work.api.ApiService
@ -39,23 +39,22 @@ class MainFragment : Fragment(R.layout.fragment_main) {
}
private fun setupUI() {
// Инициализация UI компонентов, если необходимо
binding.refresh.setOnClickListener {
fetchUserInfo()
}
binding.scanQrCode?.setOnClickListener {
// Здесь вы можете использовать навигацию для перехода к экрану сканирования QR-кода
findNavController().navigate(R.id.qrScanFragment) // Убедитесь, что у вас есть правильный ID для навигации
}
}
private fun fetchUserInfo() {
lifecycleScope.launch {
val login = SessionManager.userLogin ?: return@launch
val authHeader = SessionManager.getAuthHeader() // Получаем заголовок авторизации
Log.d("MainFragment", "Fetching user info for login: $login with authHeader: $authHeader")
val authHeader = SessionManager.getAuthHeader()
val response = apiService.getUserInfo(login, authHeader)
Log.d("MainFragment", "Response code: ${response.code()}") // Логируем код ответа
if (response.isSuccessful) {
val employeeData = response.body()
employeeData?.let {
@ -66,9 +65,12 @@ class MainFragment : Fragment(R.layout.fragment_main) {
binding.fullname.visibility = View.VISIBLE
binding.position.visibility = View.VISIBLE
binding.lastEntry.visibility = View.VISIBLE
// Показываем кнопку "Сканировать QR-код" после успешного получения данных
binding.scanQrCode?.visibility = View.VISIBLE
}
} else {
binding.error.text = "Ошибка получения данных: ${response.message()}"
binding.error.text = "Ошибка получения данных"
binding.error.visibility = View.VISIBLE
}
}

View File

@ -19,7 +19,9 @@ import ru.myitschool.work.core.Constants
import ru.myitschool.work.databinding.FragmentQrScanResultBinding
class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
private lateinit var binding: FragmentQrScanResultBinding
private var _binding: FragmentQrScanResultBinding? = null
private val binding get() = _binding!!
private lateinit var apiService: ApiService
override fun onCreateView(
@ -27,7 +29,7 @@ class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentQrScanResultBinding.inflate(inflater, container, false)
_binding = FragmentQrScanResultBinding.inflate(inflater, container, false)
apiService = Retrofit.Builder()
.baseUrl(Constants.SERVER_ADDRESS)
.addConverterFactory(GsonConverterFactory.create())
@ -42,6 +44,7 @@ class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
val qrData = QrScanDestination.getDataIfExist(requireArguments())
if (qrData != null) {
binding.result.text = "Результат сканирования: $qrData" // Отображаем результат сканирования
sendRequestToServer(qrData)
} else {
binding.result.text = "Вход был отменён/Operation was cancelled"
@ -55,12 +58,10 @@ class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
private fun sendRequestToServer(qrData: String) {
lifecycleScope.launch {
try {
// Проверяем, что userLogin не равен null
val login = SessionManager.userLogin
if (login != null) {
// Создаем объект OpenDoorRequest с логином и кодом
val openDoorRequest = OpenDoorRequest(login, qrData.toLong()) // Преобразуем qrData в Long, если это необходимо
val response = apiService.openDoor(openDoorRequest) // Теперь передаем только openDoorRequest
val response = apiService.openDoor(openDoorRequest)
if (response.isSuccessful) {
binding.result.text = "Успешно/Success"
} else {
@ -74,4 +75,9 @@ class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
}
}
}
override fun onDestroyView() {
_binding = null
super.onDestroyView()
}
}

View File

@ -47,4 +47,11 @@
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/scan_qr_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Сканировать QR-код"
android:visibility="gone" />
</LinearLayout>

View File

@ -27,5 +27,4 @@
android:textColor="@android:color/white"
android:padding="12dp"
android:layout_marginTop="24dp" />
</LinearLayout>