snackbar validation réponse
This commit is contained in:
parent
f75d844633
commit
61593d66f6
4 changed files with 49 additions and 25 deletions
|
@ -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()
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package fr.uparis.diamantkennel.memorisationapplication.ui
|
||||
|
||||
enum class AnswerType {
|
||||
NONE, GOOD, BAD
|
||||
GOOD, BAD
|
||||
}
|
||||
|
|
|
@ -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<AnswerType?>(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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue