fix some crashes related to playground, add edit button in playground

This commit is contained in:
Mylloon 2024-01-04 17:25:11 +01:00
parent 547dd051be
commit 86b4339734
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 64 additions and 32 deletions

View file

@ -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

View file

@ -84,6 +84,7 @@ fun MainScreen() {
it.arguments?.getString(PLAY_SET_ARG)?.let { idSet ->
PlayScreen(
padding,
navController,
snackbarHostState,
idSet.toInt()
)

View file

@ -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")
}
}
}

View file

@ -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()
}
}
}