From 011ce40af190c2c5bb9b3ab41f8710093af34a91 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 4 Jan 2024 16:28:08 +0100 Subject: [PATCH] solution dialog --- .../memorisationapplication/PlayScreen.kt | 25 +++++++++++++++++-- .../ui/PlayViewModel.kt | 18 ++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) 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 d10725f..fd667ff 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt @@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding +import androidx.compose.material3.AlertDialog import androidx.compose.material3.Button import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.OutlinedTextField @@ -15,6 +16,7 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.lifecycle.viewmodel.compose.viewModel @@ -39,6 +41,7 @@ fun PlayScreen( } val reponse by model.proposedAnswer val correction by model.evaluatedAnswer + var giveup by model.showAnswer val cpt by model.compteurSb if (correction != null) { @@ -54,6 +57,15 @@ fun PlayScreen( } } + if (giveup) { + SolutionDialog(question!!.reponse, model::newQuestion) + } + + // Update timer if needed + if (!model.isDelayElapsed()) { + model.updateTime(System.currentTimeMillis()) + } + Column( modifier = Modifier.padding(padding), horizontalAlignment = Alignment.CenterHorizontally @@ -74,10 +86,19 @@ fun PlayScreen( } Button( - enabled = false /* TODO: s'activer au bout de 3 secondes */, - onClick = { /*TODO*/ }) { + enabled = model.isDelayElapsed(), + onClick = { giveup = true }) { Text(text = "Voir réponse") } } } } + +@Composable +fun SolutionDialog(reponse: String, next: () -> Unit) = + AlertDialog(onDismissRequest = next, + title = { Text(text = "Solution") }, + text = { Text(text = reponse) }, + confirmButton = { + Button(onClick = next) { Text(text = "Ok") } + }) 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 b479dde..c42c8d2 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 @@ -18,11 +18,12 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { var currentQuestion = mutableStateOf(null) private var index = mutableStateOf(0) - var proposedAnswer = mutableStateOf("") var evaluatedAnswer = mutableStateOf(null) - val compteurSb = mutableStateOf(0) + private var timestampQuestion = mutableStateOf(System.currentTimeMillis()) + private var currentTime = mutableStateOf(System.currentTimeMillis()) + var showAnswer = mutableStateOf(false) fun updateQuestionList(setId: Int) { if (setId != initialId) { @@ -47,6 +48,7 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { private fun reset() { proposedAnswer.value = "" + showAnswer.value = false } fun resetAfterSb() { @@ -54,10 +56,13 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { compteurSb.value++ } - private fun newQuestion() { + fun newQuestion() { reset() index.value++ updateQuestion() + + // reset le timer uniquement en changeant de question + timestampQuestion.value = System.currentTimeMillis() } fun updateAnswer(text: String) { @@ -88,4 +93,11 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { AnswerType.BAD -> reset() } } + + fun isDelayElapsed() = currentTime.value - timestampQuestion.value >= 3000 + + fun updateTime(time: Long) { + currentTime.value = time + } + }