snackbar validation réponse

This commit is contained in:
Mylloon 2024-01-04 16:03:18 +01:00
parent f75d844633
commit 61593d66f6
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 49 additions and 25 deletions

View file

@ -14,10 +14,13 @@ import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
@ -59,8 +62,10 @@ fun MainScreenActivityPreview() {
@Composable @Composable
fun MainScreen() { fun MainScreen() {
val navController = rememberNavController() 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( NavHost(
navController = navController, navController = navController,
startDestination = HOME, startDestination = HOME,
@ -79,7 +84,7 @@ fun MainScreen() {
it.arguments?.getString(PLAY_SET_ARG)?.let { idSet -> it.arguments?.getString(PLAY_SET_ARG)?.let { idSet ->
PlayScreen( PlayScreen(
padding, padding,
navController, snackbarHostState,
idSet.toInt() idSet.toInt()
) )
} }

View file

@ -1,6 +1,5 @@
package fr.uparis.diamantkennel.memorisationapplication package fr.uparis.diamantkennel.memorisationapplication
import android.util.Log
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues 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.Button
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import fr.uparis.diamantkennel.memorisationapplication.ui.AnswerType import fr.uparis.diamantkennel.memorisationapplication.ui.AnswerType
import fr.uparis.diamantkennel.memorisationapplication.ui.PlayViewModel import fr.uparis.diamantkennel.memorisationapplication.ui.PlayViewModel
@ -25,7 +25,7 @@ import fr.uparis.diamantkennel.memorisationapplication.ui.PlayViewModel
@Composable @Composable
fun PlayScreen( fun PlayScreen(
padding: PaddingValues, padding: PaddingValues,
navController: NavController, snackbarHostState: SnackbarHostState,
idSet: Int, idSet: Int,
model: PlayViewModel = viewModel() model: PlayViewModel = viewModel()
) { ) {
@ -33,8 +33,6 @@ fun PlayScreen(
model.setId.value = idSet model.setId.value = idSet
model.updateQuestionList(idSet) model.updateQuestionList(idSet)
val context = LocalContext.current
val question by model.currentQuestion val question by model.currentQuestion
if (question == null) { if (question == null) {
return return
@ -42,14 +40,18 @@ fun PlayScreen(
val reponse by model.proposedAnswer val reponse by model.proposedAnswer
val correction by model.evaluatedAnswer val correction by model.evaluatedAnswer
if (correction == AnswerType.GOOD) { val cpt by model.compteurSb
Log.d("1312", "Bonne réponse !") if (correction != null) {
model.newQuestion() LaunchedEffect(cpt) {
model.sbUpdate()
snackbarHostState.showSnackbar(
when (correction!!) {
AnswerType.GOOD -> "Bonnne réponse !"
AnswerType.BAD -> "Mauvaise réponse !"
}, duration = SnackbarDuration.Short
)
model.resetAfterSb()
} }
if (correction == AnswerType.BAD) {
Log.d("1312", "Mauvaise réponse !")
model.reset()
} }
Column( Column(
@ -64,7 +66,10 @@ fun PlayScreen(
) )
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
Button(onClick = model::checkAnswer) { Button(
enabled = reponse.isNotBlank() && correction == null,
onClick = model::checkAnswer
) {
Text(text = "Répondre") Text(text = "Répondre")
} }

View file

@ -1,5 +1,5 @@
package fr.uparis.diamantkennel.memorisationapplication.ui package fr.uparis.diamantkennel.memorisationapplication.ui
enum class AnswerType { enum class AnswerType {
NONE, GOOD, BAD GOOD, BAD
} }

View file

@ -20,7 +20,9 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) {
private var index = mutableStateOf(0) private var index = mutableStateOf(0)
var proposedAnswer = mutableStateOf("") var proposedAnswer = mutableStateOf("")
var evaluatedAnswer = mutableStateOf(AnswerType.NONE) var evaluatedAnswer = mutableStateOf<AnswerType?>(null)
val compteurSb = mutableStateOf(0)
fun updateQuestionList(setId: Int) { fun updateQuestionList(setId: Int) {
if (setId != initialId) { if (setId != initialId) {
@ -32,9 +34,10 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) {
} }
private fun updateQuestion() { private fun updateQuestion() {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch {
questions.collect { questionList -> questions.collect { questionList ->
if (index.value >= questionList.size) { if (index.value >= questionList.size) {
/* Fin des questions */
index.value = 0 index.value = 0
} }
currentQuestion.value = questionList[index.value] currentQuestion.value = questionList[index.value]
@ -42,14 +45,18 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) {
} }
} }
fun reset() { private fun reset() {
proposedAnswer.value = "" proposedAnswer.value = ""
evaluatedAnswer.value = AnswerType.NONE
} }
fun newQuestion() { fun resetAfterSb() {
evaluatedAnswer.value = null
compteurSb.value++
}
private fun newQuestion() {
reset() reset()
index.value += 1 index.value++
updateQuestion() updateQuestion()
} }
@ -57,6 +64,8 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) {
proposedAnswer.value = text 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 { private fun calcSimilarite(str1: String, str2: String): Float {
val set1 = str1.lowercase().toSet() val set1 = str1.lowercase().toSet()
val set2 = str2.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()
}
}
} }