diff --git a/app/src/main/java/net/novagamestudios/kaffeekasse/ui/login/LoginDevice.kt b/app/src/main/java/net/novagamestudios/kaffeekasse/ui/login/LoginDevice.kt index dc5c4536bd8af5eb98895a29cb7fe206fe3824fe..1ff6ba75aaadd281fa3f062b7ab5e6610f37f362 100644 --- a/app/src/main/java/net/novagamestudios/kaffeekasse/ui/login/LoginDevice.kt +++ b/app/src/main/java/net/novagamestudios/kaffeekasse/ui/login/LoginDevice.kt @@ -335,19 +335,112 @@ private fun ConfirmLogoutDeviceDialog( onDismiss: () -> Unit, onConfirm: () -> Unit, modifier: Modifier = Modifier -) = AlertDialog( - onDismissRequest = onDismiss, - confirmButton = { - Button(onClick = onConfirm) { Text("Ausloggen") } - }, - modifier, - dismissButton = { - TextButton(onClick = onDismiss) { Text("Abbrechen") } - }, - text = { - Text("Möchtest du das Gerät wirklich ausloggen?") +) { + val toasts = remember { ToastsState() } + Toasts(toasts) + + val target = 42 + val minSolution = -10 + val maxSolution = 10 + + fun newRandomEquation() = randomEquation( + target = target, + minSolution = minSolution, + maxSolution = maxSolution + ) + + var equationAndSolution by remember { + mutableStateOf(newRandomEquation()) } -) + val (equation, solution) = equationAndSolution + + var userAnswer by remember { mutableStateOf<Float?>(null) } + var userAnswerIncorrect by remember { mutableStateOf(false) } + + fun submitUserAnswer() { + if (userAnswer?.toInt() == solution) { + onConfirm() + } else { + userAnswerIncorrect = true + } + } + + AlertDialog( + onDismissRequest = onDismiss, + confirmButton = { + Button( + onClick = { submitUserAnswer() }, + enabled = userAnswer != null + ) { Text("Ausloggen") } + }, + modifier, + dismissButton = { + TextButton(onClick = onDismiss) { Text("Abbrechen") } + }, + icon = { + Icon(Icons.Rounded.Warning, "Achtung") + }, + title = { + Text("Gerät wirklich ausloggen?") + }, + text = { + ColumnCenter(Modifier.width(IntrinsicSize.Max)) { + Text("Bitte löse die folgende Gleichung, um fortzufahren:") + Box(Modifier.fillMaxWidth()) { + Text( + text = equation, + modifier = Modifier.padding(24.dp).align(Alignment.Center), + style = MaterialTheme.typography.titleMedium.copy( + fontFamily = FontFamily.Serif + ) + ) + IconButton( + onClick = { + equationAndSolution = newRandomEquation() + userAnswer = null + userAnswerIncorrect = false + }, + modifier = Modifier.align(Alignment.CenterEnd) + ) { + Icon(Icons.Default.Refresh, "Neue Gleichung") + } + } + var currentInput by remember { mutableStateOf("") } + + LaunchedEffect(userAnswer) { + if (currentInput.toIntOrNull() != userAnswer?.toInt()) { + currentInput = "${userAnswer?.toInt() ?: ""}" + } + } + + OutlinedTextField( + value = currentInput, + onValueChange = { + val sanitized = it.filter { c -> c.isDigit() || c in setOf('-', '+', '.', ',') } + currentInput = sanitized + userAnswer = sanitized.toIntOrNull()?.toFloat() + userAnswerIncorrect = false + }, + modifier = Modifier.defaultMinSize(minWidth = 1.dp), + textStyle = LocalTextStyle.current.copy( + fontFamily = FontFamily.Serif + ), + placeholder = { Text("?") }, + prefix = { + Text("x = ", fontFamily = FontFamily.Serif) + }, + isError = userAnswerIncorrect || (currentInput.isNotEmpty() && userAnswer == null), + keyboardOptions = KeyboardOptions( + keyboardType = KeyboardType.Number, + imeAction = ImeAction.Done + ), + keyboardActions = KeyboardActions(onDone = { submitUserAnswer() }) + ) + } + }, + iconContentColor = MaterialTheme.colorScheme.error + ) +}