valid and first booleans in map

This commit is contained in:
Lukas Barragan Torres 2023-05-13 17:26:19 +02:00
parent e8f2d71df3
commit eb28fa4ae4

View file

@ -6,43 +6,37 @@ import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
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.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import be.ugent.sel.studeez.R
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.ext.basicButton
import be.ugent.sel.studeez.common.snackbar.SnackbarManager
import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo
import be.ugent.sel.studeez.R.string as AppText
abstract class AbstractTimerFormScreen(private val timerInfo: TimerInfo) {
protected val valids = mutableMapOf(
"name" to mutableStateOf(textPredicate(timerInfo.name)),
"description" to mutableStateOf(textPredicate(timerInfo.description))
)
protected val firsts = mutableMapOf(
"name" to mutableStateOf(true),
"description" to mutableStateOf(true)
)
@Composable
operator fun invoke(
onSaveClick: (TimerInfo) -> Unit,
extraButton: @Composable () -> Unit
) {
var name by remember { mutableStateOf(timerInfo.name) }
val isNameValid = remember { mutableStateOf(false) }
val hasEditedName = remember { mutableStateOf(true) }
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
timerInfo.name = name
timerInfo.description = description
Column(
verticalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
@ -59,25 +53,25 @@ abstract class AbstractTimerFormScreen(private val timerInfo: TimerInfo) {
initialValue = timerInfo.name,
label = R.string.name,
errorText = AppText.name_error,
isValid = isNameValid,
isFirst = hasEditedName,
isValid = valids.getValue("name"),
isFirst = firsts.getValue("name"),
keyboardType = KeyboardType.Text,
predicate = { it.isNotBlank() }
) { correctName ->
name = correctName
timerInfo.name = correctName
}
LabeledErrorTextField(
initialValue = timerInfo.description,
label = R.string.description,
errorText = AppText.description_error,
isValid = isDescriptionValid,
isFirst = hasEditedDescription,
isValid = valids.getValue("description"),
isFirst = firsts.getValue("description"),
singleLine= false,
keyboardType = KeyboardType.Text,
predicate = { it.isNotBlank() }
predicate = { textPredicate(it) }
) { correctName ->
description = correctName
timerInfo.description = correctName
}
ExtraFields()
@ -86,11 +80,11 @@ abstract class AbstractTimerFormScreen(private val timerInfo: TimerInfo) {
Column {
BasicButton(R.string.save, Modifier.basicButton()) {
if (isNameValid.value && isDescriptionValid.value) {
if (valids.all { it.component2().value }) { // All fields are valid
onSaveClick(timerInfo)
} else {
hasEditedName.value = false
hasEditedDescription.value = false
firsts.map { it.component2().value = false } // dont mask error because its not been filled out yet
SnackbarManager.showMessage(AppText.fill_out_error)
}
}
extraButton()
@ -98,6 +92,10 @@ abstract class AbstractTimerFormScreen(private val timerInfo: TimerInfo) {
}
}
private fun textPredicate(text: String): Boolean {
return text.isNotBlank()
}
@Composable
open fun ExtraFields() {
// By default no extra fields, unless overwritten by subclass.