From 61593d66f6cd660626588aee02842e513679aa80 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 4 Jan 2024 16:03:18 +0100 Subject: [PATCH] =?UTF-8?q?snackbar=20validation=20r=C3=A9ponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../memorisationapplication/MainActivity.kt | 9 +++-- .../memorisationapplication/PlayScreen.kt | 35 +++++++++++-------- .../memorisationapplication/ui/AnswerType.kt | 2 +- .../ui/PlayViewModel.kt | 28 +++++++++++---- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt index e23b3f7..8f28db0 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt @@ -14,10 +14,13 @@ import androidx.compose.material3.CenterAlignedTopAppBar import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold +import androidx.compose.material3.SnackbarHost +import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview @@ -59,8 +62,10 @@ fun MainScreenActivityPreview() { @Composable fun MainScreen() { val navController = rememberNavController() + val snackbarHostState = remember { SnackbarHostState() } - Scaffold(topBar = { TopBar() }, bottomBar = { BottomBar(navController) }) { padding -> + Scaffold(topBar = { TopBar() }, bottomBar = { BottomBar(navController) }, + snackbarHost = { SnackbarHost(snackbarHostState) }) { padding -> NavHost( navController = navController, startDestination = HOME, @@ -79,7 +84,7 @@ fun MainScreen() { it.arguments?.getString(PLAY_SET_ARG)?.let { idSet -> PlayScreen( padding, - navController, + snackbarHostState, idSet.toInt() ) } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt index d4f8192..d10725f 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt @@ -1,6 +1,5 @@ package fr.uparis.diamantkennel.memorisationapplication -import android.util.Log import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues @@ -10,14 +9,15 @@ import androidx.compose.foundation.layout.padding import androidx.compose.material3.Button import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.SnackbarDuration +import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext import androidx.lifecycle.viewmodel.compose.viewModel -import androidx.navigation.NavController import fr.uparis.diamantkennel.memorisationapplication.ui.AnswerType import fr.uparis.diamantkennel.memorisationapplication.ui.PlayViewModel @@ -25,7 +25,7 @@ import fr.uparis.diamantkennel.memorisationapplication.ui.PlayViewModel @Composable fun PlayScreen( padding: PaddingValues, - navController: NavController, + snackbarHostState: SnackbarHostState, idSet: Int, model: PlayViewModel = viewModel() ) { @@ -33,8 +33,6 @@ fun PlayScreen( model.setId.value = idSet model.updateQuestionList(idSet) - val context = LocalContext.current - val question by model.currentQuestion if (question == null) { return @@ -42,14 +40,18 @@ fun PlayScreen( val reponse by model.proposedAnswer val correction by model.evaluatedAnswer - if (correction == AnswerType.GOOD) { - Log.d("1312", "Bonne réponse !") - model.newQuestion() - } - - if (correction == AnswerType.BAD) { - Log.d("1312", "Mauvaise réponse !") - model.reset() + val cpt by model.compteurSb + if (correction != null) { + LaunchedEffect(cpt) { + model.sbUpdate() + snackbarHostState.showSnackbar( + when (correction!!) { + AnswerType.GOOD -> "Bonnne réponse !" + AnswerType.BAD -> "Mauvaise réponse !" + }, duration = SnackbarDuration.Short + ) + model.resetAfterSb() + } } Column( @@ -64,7 +66,10 @@ fun PlayScreen( ) Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { - Button(onClick = model::checkAnswer) { + Button( + enabled = reponse.isNotBlank() && correction == null, + onClick = model::checkAnswer + ) { Text(text = "Répondre") } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/AnswerType.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/AnswerType.kt index d09b197..77fd1f1 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/AnswerType.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/AnswerType.kt @@ -1,5 +1,5 @@ package fr.uparis.diamantkennel.memorisationapplication.ui enum class AnswerType { - NONE, GOOD, BAD + GOOD, BAD } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/PlayViewModel.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/PlayViewModel.kt index 86a15d5..b479dde 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/PlayViewModel.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/PlayViewModel.kt @@ -20,7 +20,9 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { private var index = mutableStateOf(0) var proposedAnswer = mutableStateOf("") - var evaluatedAnswer = mutableStateOf(AnswerType.NONE) + var evaluatedAnswer = mutableStateOf(null) + + val compteurSb = mutableStateOf(0) fun updateQuestionList(setId: Int) { if (setId != initialId) { @@ -32,9 +34,10 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { } private fun updateQuestion() { - viewModelScope.launch(Dispatchers.IO) { + viewModelScope.launch { questions.collect { questionList -> if (index.value >= questionList.size) { + /* Fin des questions */ index.value = 0 } currentQuestion.value = questionList[index.value] @@ -42,14 +45,18 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { } } - fun reset() { + private fun reset() { proposedAnswer.value = "" - evaluatedAnswer.value = AnswerType.NONE } - fun newQuestion() { + fun resetAfterSb() { + evaluatedAnswer.value = null + compteurSb.value++ + } + + private fun newQuestion() { reset() - index.value += 1 + index.value++ updateQuestion() } @@ -57,6 +64,8 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { proposedAnswer.value = text } + /** Permet de calculer la similarité entre 2 string. + * Renvoie un pourcentage entre 0 et 1. */ private fun calcSimilarite(str1: String, str2: String): Float { val set1 = str1.lowercase().toSet() val set2 = str2.lowercase().toSet() @@ -73,5 +82,10 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { } } - + fun sbUpdate() { + when (evaluatedAnswer.value!!) { + AnswerType.GOOD -> newQuestion() + AnswerType.BAD -> reset() + } + } }