day3_commit2_added_qr_scan
This commit is contained in:
parent
9818c97148
commit
cb5fbdd634
@ -1,10 +1,10 @@
|
|||||||
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 androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.navigation.fragment.findNavController
|
||||||
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
|
||||||
@ -39,23 +39,22 @@ class MainFragment : Fragment(R.layout.fragment_main) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun setupUI() {
|
private fun setupUI() {
|
||||||
// Инициализация UI компонентов, если необходимо
|
|
||||||
binding.refresh.setOnClickListener {
|
binding.refresh.setOnClickListener {
|
||||||
fetchUserInfo()
|
fetchUserInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
binding.scanQrCode?.setOnClickListener {
|
||||||
|
// Здесь вы можете использовать навигацию для перехода к экрану сканирования QR-кода
|
||||||
|
findNavController().navigate(R.id.qrScanFragment) // Убедитесь, что у вас есть правильный ID для навигации
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchUserInfo() {
|
private fun fetchUserInfo() {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
val login = SessionManager.userLogin ?: return@launch
|
val login = SessionManager.userLogin ?: return@launch
|
||||||
val authHeader = 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 {
|
||||||
@ -66,9 +65,12 @@ class MainFragment : Fragment(R.layout.fragment_main) {
|
|||||||
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
|
||||||
|
|
||||||
|
// Показываем кнопку "Сканировать QR-код" после успешного получения данных
|
||||||
|
binding.scanQrCode?.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
binding.error.text = "Ошибка получения данных: ${response.message()}"
|
binding.error.text = "Ошибка получения данных"
|
||||||
binding.error.visibility = View.VISIBLE
|
binding.error.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,9 @@ import ru.myitschool.work.core.Constants
|
|||||||
import ru.myitschool.work.databinding.FragmentQrScanResultBinding
|
import ru.myitschool.work.databinding.FragmentQrScanResultBinding
|
||||||
|
|
||||||
class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
|
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
|
private lateinit var apiService: ApiService
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
@ -27,7 +29,7 @@ class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
|
|||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View {
|
): View {
|
||||||
binding = FragmentQrScanResultBinding.inflate(inflater, container, false)
|
_binding = FragmentQrScanResultBinding.inflate(inflater, container, false)
|
||||||
apiService = Retrofit.Builder()
|
apiService = Retrofit.Builder()
|
||||||
.baseUrl(Constants.SERVER_ADDRESS)
|
.baseUrl(Constants.SERVER_ADDRESS)
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
@ -42,6 +44,7 @@ class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
|
|||||||
|
|
||||||
val qrData = QrScanDestination.getDataIfExist(requireArguments())
|
val qrData = QrScanDestination.getDataIfExist(requireArguments())
|
||||||
if (qrData != null) {
|
if (qrData != null) {
|
||||||
|
binding.result.text = "Результат сканирования: $qrData" // Отображаем результат сканирования
|
||||||
sendRequestToServer(qrData)
|
sendRequestToServer(qrData)
|
||||||
} else {
|
} else {
|
||||||
binding.result.text = "Вход был отменён/Operation was cancelled"
|
binding.result.text = "Вход был отменён/Operation was cancelled"
|
||||||
@ -55,12 +58,10 @@ class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
|
|||||||
private fun sendRequestToServer(qrData: String) {
|
private fun sendRequestToServer(qrData: String) {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
try {
|
try {
|
||||||
// Проверяем, что userLogin не равен null
|
|
||||||
val login = SessionManager.userLogin
|
val login = SessionManager.userLogin
|
||||||
if (login != null) {
|
if (login != null) {
|
||||||
// Создаем объект OpenDoorRequest с логином и кодом
|
|
||||||
val openDoorRequest = OpenDoorRequest(login, qrData.toLong()) // Преобразуем qrData в Long, если это необходимо
|
val openDoorRequest = OpenDoorRequest(login, qrData.toLong()) // Преобразуем qrData в Long, если это необходимо
|
||||||
val response = apiService.openDoor(openDoorRequest) // Теперь передаем только openDoorRequest
|
val response = apiService.openDoor(openDoorRequest)
|
||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) {
|
||||||
binding.result.text = "Успешно/Success"
|
binding.result.text = "Успешно/Success"
|
||||||
} else {
|
} else {
|
||||||
@ -74,4 +75,9 @@ class QrResult : Fragment(R.layout.fragment_qr_scan_result) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroyView() {
|
||||||
|
_binding = null
|
||||||
|
super.onDestroyView()
|
||||||
|
}
|
||||||
}
|
}
|
@ -47,4 +47,11 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
android:layout_weight="1" />
|
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>
|
</LinearLayout>
|
||||||
|
@ -27,5 +27,4 @@
|
|||||||
android:textColor="@android:color/white"
|
android:textColor="@android:color/white"
|
||||||
android:padding="12dp"
|
android:padding="12dp"
|
||||||
android:layout_marginTop="24dp" />
|
android:layout_marginTop="24dp" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
Loading…
x
Reference in New Issue
Block a user