diff --git a/.idea/misc.xml b/.idea/misc.xml index 8978d23..9f71c83 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,7 @@ + - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/java/it/edu/cassandroferminervi/flowschool/remote/ApiService.kt b/app/src/main/java/it/edu/cassandroferminervi/flowschool/remote/ApiService.kt index 8464aad..4fd8c60 100644 --- a/app/src/main/java/it/edu/cassandroferminervi/flowschool/remote/ApiService.kt +++ b/app/src/main/java/it/edu/cassandroferminervi/flowschool/remote/ApiService.kt @@ -1,6 +1,7 @@ package it.edu.cassandroferminervi.flowschool.remote import it.edu.cassandroferminervi.flowschool.remote.dto.LoginResult +import it.edu.cassandroferminervi.flowschool.remote.dto.PresenceResult import it.edu.cassandroferminervi.flowschool.remote.dto.TeacherInfoResult import retrofit2.Response import retrofit2.http.Field @@ -17,14 +18,17 @@ interface ApiService { @FormUrlEncoded @POST("presenza.php") - suspend fun postPresence(@Field("token") token: String): Response + suspend fun postPresence( + @Field("token") token: String, + @Field("codice") qrcode: String + ): Response @FormUrlEncoded @POST("lista.php") suspend fun postTeacherList(@Field("token") token: String): Response> @FormUrlEncoded - @POST("ricerca.php") + @POST("ottieni_prof.php") suspend fun postTeacherInfo( @Field("token") token: String, @Field("profId") teacherId: Int diff --git a/app/src/main/java/it/edu/cassandroferminervi/flowschool/remote/dto/PresenceResult.kt b/app/src/main/java/it/edu/cassandroferminervi/flowschool/remote/dto/PresenceResult.kt new file mode 100644 index 0000000..ab4c06f --- /dev/null +++ b/app/src/main/java/it/edu/cassandroferminervi/flowschool/remote/dto/PresenceResult.kt @@ -0,0 +1,6 @@ +package it.edu.cassandroferminervi.flowschool.remote.dto + +import androidx.annotation.Keep + +@Keep +data class PresenceResult(val success: Boolean) diff --git a/app/src/main/java/it/edu/cassandroferminervi/flowschool/screens/CameraScreen.kt b/app/src/main/java/it/edu/cassandroferminervi/flowschool/screens/CameraScreen.kt index edededb..0cead8f 100644 --- a/app/src/main/java/it/edu/cassandroferminervi/flowschool/screens/CameraScreen.kt +++ b/app/src/main/java/it/edu/cassandroferminervi/flowschool/screens/CameraScreen.kt @@ -14,10 +14,11 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.viewinterop.AndroidView import androidx.core.content.ContextCompat +import com.ramcosta.composedestinations.navigation.DestinationsNavigator import it.edu.cassandroferminervi.flowschool.util.QrAnalyzer @Composable -fun CameraScreen() { +fun CameraScreen(navigator: DestinationsNavigator, token: String) { val localContext = LocalContext.current val lifecycleOwner = LocalLifecycleOwner.current val cameraProviderFuture = remember { @@ -37,7 +38,7 @@ fun CameraScreen() { val imageAnalysis = ImageAnalysis.Builder().build() imageAnalysis.setAnalyzer( ContextCompat.getMainExecutor(context), - QrAnalyzer(context) + QrAnalyzer(context, navigator, token) ) runCatching { diff --git a/app/src/main/java/it/edu/cassandroferminervi/flowschool/screens/PermissionScreen.kt b/app/src/main/java/it/edu/cassandroferminervi/flowschool/screens/PermissionScreen.kt index 7b9b18a..30af8cf 100644 --- a/app/src/main/java/it/edu/cassandroferminervi/flowschool/screens/PermissionScreen.kt +++ b/app/src/main/java/it/edu/cassandroferminervi/flowschool/screens/PermissionScreen.kt @@ -12,15 +12,16 @@ import com.google.accompanist.permissions.isGranted import com.google.accompanist.permissions.rememberPermissionState import com.google.accompanist.permissions.shouldShowRationale import com.ramcosta.composedestinations.annotation.Destination +import com.ramcosta.composedestinations.navigation.DestinationsNavigator @OptIn(ExperimentalPermissionsApi::class) @Destination @Composable -fun PermissionScreen(token: String) { +fun PermissionScreen(navigator: DestinationsNavigator, token: String) { val cameraPermissionState = rememberPermissionState(android.Manifest.permission.CAMERA) if (cameraPermissionState.status.isGranted) { - CameraScreen() + CameraScreen(navigator, token) } else { Box( contentAlignment = Alignment.Center, diff --git a/app/src/main/java/it/edu/cassandroferminervi/flowschool/util/QrAnalyzer.kt b/app/src/main/java/it/edu/cassandroferminervi/flowschool/util/QrAnalyzer.kt index 6b3f67a..3f5093a 100644 --- a/app/src/main/java/it/edu/cassandroferminervi/flowschool/util/QrAnalyzer.kt +++ b/app/src/main/java/it/edu/cassandroferminervi/flowschool/util/QrAnalyzer.kt @@ -2,6 +2,7 @@ package it.edu.cassandroferminervi.flowschool.util import android.annotation.SuppressLint import android.content.Context +import android.content.Intent import android.widget.Toast import androidx.camera.core.ImageAnalysis import androidx.camera.core.ImageProxy @@ -9,8 +10,14 @@ import com.google.mlkit.vision.barcode.BarcodeScannerOptions import com.google.mlkit.vision.barcode.BarcodeScanning import com.google.mlkit.vision.barcode.common.Barcode import com.google.mlkit.vision.common.InputImage +import com.ramcosta.composedestinations.HomeScreenDestination +import com.ramcosta.composedestinations.navigation.DestinationsNavigator +import it.edu.cassandroferminervi.flowschool.remote.RetrofitInstance +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch -class QrAnalyzer(private val context: Context) : ImageAnalysis.Analyzer { +class QrAnalyzer(private val context: Context, val navigator: DestinationsNavigator, val token: String) : ImageAnalysis.Analyzer { private val options = BarcodeScannerOptions.Builder() .setBarcodeFormats(Barcode.FORMAT_QR_CODE) .build() @@ -29,7 +36,19 @@ class QrAnalyzer(private val context: Context) : ImageAnalysis.Analyzer { barcode?.takeIf { it.isNotEmpty() } ?.mapNotNull { it.rawValue } ?.joinToString(",") - ?.let { Toast.makeText(context, it, Toast.LENGTH_SHORT).show() } + ?.let { + Toast.makeText(context, it, Toast.LENGTH_SHORT).show() + CoroutineScope(Dispatchers.Main).launch { + val response = RetrofitInstance.api.postPresence(token, it) + val resBody = response.body() + + if (resBody != null && resBody.success) { + navigator.navigate(HomeScreenDestination(token)) + } else { + Toast.makeText(context, "Errore", Toast.LENGTH_LONG).show() + } + } + } }.addOnCompleteListener { imageProxy.close() }