added error input field for name and discription

This commit is contained in:
Lukas Barragan Torres 2023-05-10 12:46:30 +02:00
parent d6496cb8ad
commit b4166386a5

View file

@ -13,8 +13,10 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue 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.compose.ui.text.input.KeyboardType
import be.ugent.sel.studeez.R import be.ugent.sel.studeez.R
import be.ugent.sel.studeez.common.composable.BasicButton import be.ugent.sel.studeez.common.composable.BasicButton
import be.ugent.sel.studeez.common.composable.LabeledErrorTextField
import be.ugent.sel.studeez.common.composable.LabelledInputField import be.ugent.sel.studeez.common.composable.LabelledInputField
import be.ugent.sel.studeez.common.ext.basicButton import be.ugent.sel.studeez.common.ext.basicButton
import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo
@ -28,8 +30,14 @@ abstract class AbstractTimerFormScreen(private val timerInfo: TimerInfo) {
extraButton: @Composable () -> Unit extraButton: @Composable () -> Unit
) { ) {
var name by remember { mutableStateOf(timerInfo.name) } var name by remember { mutableStateOf(timerInfo.name) }
val isNameValid = remember { mutableStateOf(false) }
val hasEditedName = remember { mutableStateOf(true) }
var description by remember { mutableStateOf(timerInfo.description) } var description by remember { mutableStateOf(timerInfo.description) }
val isDescriptionValid = remember { mutableStateOf(false) }
val hasEditedDescription = remember { mutableStateOf(true) }
// This shall rerun whenever name and description change // This shall rerun whenever name and description change
timerInfo.name = name timerInfo.name = name
@ -47,18 +55,30 @@ abstract class AbstractTimerFormScreen(private val timerInfo: TimerInfo) {
) { ) {
// Fields that every timer shares (ommited id) // Fields that every timer shares (ommited id)
LabelledInputField( LabeledErrorTextField(
value = name, initialValue = timerInfo.name,
onNewValue = { name = it },
label = R.string.name, label = R.string.name,
) errorText = AppText.name_error,
isValid = isNameValid,
isFirst = hasEditedName,
keyboardType = KeyboardType.Text,
predicate = { it.isNotBlank() }
) { correctName ->
name = correctName
}
LabelledInputField( LabeledErrorTextField(
value = description, initialValue = timerInfo.description,
onNewValue = { description = it }, label = R.string.description,
label = AppText.description, errorText = AppText.description_error,
singleLine = false isValid = isDescriptionValid,
) isFirst = hasEditedDescription,
singleLine= false,
keyboardType = KeyboardType.Text,
predicate = { it.isNotBlank() }
) { correctName ->
description = correctName
}
ExtraFields() ExtraFields()
@ -66,7 +86,12 @@ abstract class AbstractTimerFormScreen(private val timerInfo: TimerInfo) {
Column { Column {
BasicButton(R.string.save, Modifier.basicButton()) { BasicButton(R.string.save, Modifier.basicButton()) {
onSaveClick(timerInfo) if (isNameValid.value && isDescriptionValid.value) {
onSaveClick(timerInfo)
} else {
hasEditedName.value = false
hasEditedDescription.value = false
}
} }
extraButton() extraButton()
} }