solution dialog

This commit is contained in:
Mylloon 2024-01-04 16:28:08 +01:00
parent 61593d66f6
commit 011ce40af1
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 38 additions and 5 deletions

View file

@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
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
@ -15,6 +16,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
@ -39,6 +41,7 @@ fun PlayScreen(
} }
val reponse by model.proposedAnswer val reponse by model.proposedAnswer
val correction by model.evaluatedAnswer val correction by model.evaluatedAnswer
var giveup by model.showAnswer
val cpt by model.compteurSb val cpt by model.compteurSb
if (correction != null) { 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( Column(
modifier = Modifier.padding(padding), modifier = Modifier.padding(padding),
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
@ -74,10 +86,19 @@ fun PlayScreen(
} }
Button( Button(
enabled = false /* TODO: s'activer au bout de 3 secondes */, enabled = model.isDelayElapsed(),
onClick = { /*TODO*/ }) { onClick = { giveup = true }) {
Text(text = "Voir réponse") 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") }
})

View file

@ -18,11 +18,12 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) {
var currentQuestion = mutableStateOf<Question?>(null) var currentQuestion = mutableStateOf<Question?>(null)
private var index = mutableStateOf(0) private var index = mutableStateOf(0)
var proposedAnswer = mutableStateOf("") var proposedAnswer = mutableStateOf("")
var evaluatedAnswer = mutableStateOf<AnswerType?>(null) var evaluatedAnswer = mutableStateOf<AnswerType?>(null)
val compteurSb = mutableStateOf(0) val compteurSb = mutableStateOf(0)
private var timestampQuestion = mutableStateOf(System.currentTimeMillis())
private var currentTime = mutableStateOf(System.currentTimeMillis())
var showAnswer = mutableStateOf(false)
fun updateQuestionList(setId: Int) { fun updateQuestionList(setId: Int) {
if (setId != initialId) { if (setId != initialId) {
@ -47,6 +48,7 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) {
private fun reset() { private fun reset() {
proposedAnswer.value = "" proposedAnswer.value = ""
showAnswer.value = false
} }
fun resetAfterSb() { fun resetAfterSb() {
@ -54,10 +56,13 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) {
compteurSb.value++ compteurSb.value++
} }
private fun newQuestion() { fun newQuestion() {
reset() reset()
index.value++ index.value++
updateQuestion() updateQuestion()
// reset le timer uniquement en changeant de question
timestampQuestion.value = System.currentTimeMillis()
} }
fun updateAnswer(text: String) { fun updateAnswer(text: String) {
@ -88,4 +93,11 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) {
AnswerType.BAD -> reset() AnswerType.BAD -> reset()
} }
} }
fun isDelayElapsed() = currentTime.value - timestampQuestion.value >= 3000
fun updateTime(time: Long) {
currentTime.value = time
}
} }