From 0b65c44da120b8a6c60f958103e91ea139894580 Mon Sep 17 00:00:00 2001 From: Lukas Barragan Torres Date: Thu, 4 May 2023 12:22:42 +0200 Subject: [PATCH] added error inputfield --- .../common/composable/TextFieldComposable.kt | 97 ++++++++++++++++++- 1 file changed, 93 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/be/ugent/sel/studeez/common/composable/TextFieldComposable.kt b/app/src/main/java/be/ugent/sel/studeez/common/composable/TextFieldComposable.kt index 47dbb0b..8d158be 100644 --- a/app/src/main/java/be/ugent/sel/studeez/common/composable/TextFieldComposable.kt +++ b/app/src/main/java/be/ugent/sel/studeez/common/composable/TextFieldComposable.kt @@ -1,11 +1,11 @@ package be.ugent.sel.studeez.common.composable import androidx.annotation.StringRes +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.material.Icon -import androidx.compose.material.IconButton -import androidx.compose.material.OutlinedTextField -import androidx.compose.material.Text +import androidx.compose.material.* import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Email import androidx.compose.material.icons.filled.Lock @@ -14,10 +14,15 @@ import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.VisualTransformation +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp import be.ugent.sel.studeez.common.ext.fieldModifier +import be.ugent.sel.studeez.resources +import kotlin.math.sin import be.ugent.sel.studeez.R.drawable as AppIcon import be.ugent.sel.studeez.R.string as AppText @@ -85,6 +90,90 @@ fun EmailField( ) } +@Composable +fun LabeledNumberInputField( + value: Int, + onNewValue: (Int) -> Unit, + @StringRes label: Int, + singleLine: Boolean = false +) { + var number by remember { mutableStateOf(value) } + OutlinedTextField( + value = number.toString(), + singleLine = singleLine, + label = { Text(resources().getString(label)) }, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), + onValueChange = {typedInt -> + val isNumber = typedInt.matches(Regex("[1-9]+\\d*]")) + if (isNumber) { + number = typedInt.toInt() + onNewValue(typedInt.toInt()) + } + } + ) +} + +@Composable +fun LabeledErrorTextField( + modifier: Modifier = Modifier, + initialValue: String, + @StringRes label: Int, + singleLine: Boolean = false, + errorText: String, + keyboardType: KeyboardType, + predicate: (String) -> Boolean, + onNewCorrectValue: (String) -> Unit +) { + var value by remember { + mutableStateOf(initialValue) + } + + var isValid by remember { + mutableStateOf(predicate(value)) + } + + Column { + OutlinedTextField( + modifier = modifier.fieldModifier(), + value = value, + onValueChange = { newText -> + value = newText + isValid = predicate(value) + if (isValid) { + onNewCorrectValue(newText) + } + }, + singleLine = singleLine, + label = { Text(text = resources().getString(label)) }, + isError = !isValid, + keyboardOptions = KeyboardOptions( + keyboardType = keyboardType, + imeAction = ImeAction.Done + ) + ) + + if (!isValid) { + Text( + modifier = Modifier.padding(start = 16.dp), + text = errorText, + color = MaterialTheme.colors.error + ) + } + } +} + +fun isNumber(string: String): Boolean { + return string.matches(Regex("[1-9]+\\d*")) +} + + + + @Preview(showBackground = true) + @Composable + fun IntInputPreview() { + LabeledNumberInputField(value = 1, onNewValue = {}, label = AppText.email) + } + @Composable fun PasswordField( value: String,