From 86b4339734ef0ac0040316040e0b8378e7117e32 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 4 Jan 2024 17:25:11 +0100 Subject: [PATCH] fix some crashes related to playground, add edit button in playground --- README.md | 8 +-- .../memorisationapplication/MainActivity.kt | 1 + .../memorisationapplication/PlayScreen.kt | 64 +++++++++++++------ .../ui/PlayViewModel.kt | 23 +++++-- 4 files changed, 64 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index af819be..e755cb9 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,14 @@ - [x] Créer des questions - [x] Supprimer des questions - [x] Choisir / Sélection - - [ ] Commencer un jeu + - [x] Commencer un jeu - [ ] Reprendre la progression d'un jeu - [ ] Afficher les statistiques de tous les jeux - Dans un jeu - - [ ] Consulter la réponse à une question trop difficile + - [x] Consulter la réponse à une question trop difficile - [ ] Choisir le statut d'une question - - [ ] Modifier la question - - [ ] Supprimer la question + - [x] Modifier la question + - [x] Supprimer la question - [ ] Afficher les statistiques du jeu de question en cours - [ ] Notification - une fois par jour 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 8f28db0..84d16d9 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt @@ -84,6 +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 f812f18..cda2ba5 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt @@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.material3.AlertDialog @@ -19,7 +20,11 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel +import androidx.navigation.NavController import fr.uparis.diamantkennel.memorisationapplication.ui.AnswerType import fr.uparis.diamantkennel.memorisationapplication.ui.PlayViewModel @@ -27,6 +32,7 @@ import fr.uparis.diamantkennel.memorisationapplication.ui.PlayViewModel @Composable fun PlayScreen( padding: PaddingValues, + navController: NavController, snackbarHostState: SnackbarHostState, idSet: Int, model: PlayViewModel = viewModel() @@ -35,9 +41,6 @@ fun PlayScreen( model.updateQuestionList(idSet) val question by model.currentQuestion - if (question == null) { - return - } val reponse by model.proposedAnswer val correction by model.evaluatedAnswer var giveup by model.showAnswer @@ -56,12 +59,12 @@ fun PlayScreen( } } - if (giveup) { + if (giveup && question != null) { SolutionDialog(question!!.reponse, model::newQuestion) } // Update timer if needed - if (!model.isDelayElapsed()) { + if (!model.isDelayElapsed() && question != null) { model.updateTime(System.currentTimeMillis()) } @@ -69,27 +72,46 @@ fun PlayScreen( modifier = Modifier.padding(padding), horizontalAlignment = Alignment.CenterHorizontally ) { - Text(text = question!!.enonce) - OutlinedTextField( - value = reponse, - label = { Text(text = "Réponse") }, - onValueChange = model::updateAnswer - ) + if (question == null) { + Text("Ce set n'a aucune question", fontSize = 30.sp, textAlign = TextAlign.Center) + } else { + Text(text = question!!.enonce, fontSize = 30.sp, textAlign = TextAlign.Center) - Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { - Button( - enabled = reponse.isNotBlank() && correction == null, - onClick = model::checkAnswer + Spacer(modifier = Modifier.padding(top = 20.dp)) + + OutlinedTextField( + value = reponse, + label = { Text(text = "Réponse") }, + onValueChange = model::updateAnswer + ) + + Spacer(modifier = Modifier.padding(top = 20.dp)) + + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceEvenly ) { - Text(text = "Répondre") - } + Button( + enabled = reponse.isNotBlank() && correction == null, + onClick = model::checkAnswer + ) { + Text(text = "Répondre") + } - Button( - enabled = model.isDelayElapsed(), - onClick = { giveup = true }) { - Text(text = "Voir réponse") + Button( + enabled = model.isDelayElapsed(), + onClick = { giveup = true }) { + Text(text = "Voir réponse") + } } } + + Spacer(modifier = Modifier.weight(1f)) + + Button( + onClick = { navController.navigate("$MODIFY_SET/$idSet") }) { + Text(text = "Modifier le set") + } } } 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 8b70420..9687d3f 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 @@ -24,7 +24,7 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { fun updateQuestionList(setId: Int) { if (currentQuestion.value == null) { - viewModelScope.launch(Dispatchers.IO) { + viewModelScope.launch(Dispatchers.Main) { dao.loadQuestions(setId).collect { questionList -> questions.value = questionList.shuffled() updateQuestion() @@ -34,11 +34,15 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { } private fun updateQuestion() { - if (index.value >= questions.value.size) { - /* Fin des questions */ - index.value = 0 + if (questions.value.isEmpty()) { + currentQuestion.value = null + } else { + if (index.value >= questions.value.size) { + /* Fin des questions */ + index.value = 0 + } + currentQuestion.value = questions.value[index.value] } - currentQuestion.value = questions.value[index.value] } private fun reset() { @@ -84,8 +88,13 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { fun sbUpdate() { when (evaluatedAnswer.value!!) { - AnswerType.GOOD -> newQuestion() - AnswerType.BAD -> reset() + AnswerType.GOOD -> { + newQuestion() + } + + AnswerType.BAD -> { + reset() + } } }