Code revision
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package com.example.ichwillheim
|
||||
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
const val SETTINGS_PIN = "1234"
|
||||
|
||||
@Composable
|
||||
fun PinScreen(
|
||||
onCorrectPin: () -> Unit,
|
||||
onCancel: () -> Unit
|
||||
) {
|
||||
BackHandler { onCancel() }
|
||||
|
||||
var pin by remember { mutableStateOf("") }
|
||||
var error by remember { mutableStateOf(false) }
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(32.dp),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Text(stringResource(R.string.pin_screen_title), style = MaterialTheme.typography.headlineSmall)
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = pin,
|
||||
onValueChange = { input ->
|
||||
if (input.length <= 4 && input.all { it.isDigit() }) {
|
||||
pin = input
|
||||
error = false
|
||||
}
|
||||
},
|
||||
label = { Text(stringResource(R.string.field_pin)) },
|
||||
visualTransformation = PasswordVisualTransformation(),
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.NumberPassword),
|
||||
isError = error,
|
||||
supportingText = { if (error) Text(stringResource(R.string.pin_error_incorrect)) }
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
|
||||
Row {
|
||||
TextButton(onClick = onCancel) { Text(stringResource(R.string.action_cancel)) }
|
||||
Spacer(modifier = Modifier.width(16.dp))
|
||||
Button(onClick = {
|
||||
if (pin == SETTINGS_PIN) {
|
||||
onCorrectPin()
|
||||
} else {
|
||||
error = true
|
||||
pin = ""
|
||||
}
|
||||
}) {
|
||||
Text(stringResource(R.string.action_confirm))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user