Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import hu.toliver.vinotes.R
import hu.toliver.vinotes.domain.WineYears
import hu.toliver.vinotes.domain.model.Wine
import hu.toliver.vinotes.domain.model.enums.WineColour
import hu.toliver.vinotes.domain.model.enums.WineSweetness
Expand Down Expand Up @@ -76,6 +77,7 @@ fun WineFormSheet(
var description by rememberSaveable(formKey) { mutableStateOf(editingWine?.description ?: "") }

var showDeleteDialog by rememberSaveable(formKey) { mutableStateOf(false) }
var showYearError by rememberSaveable(formKey) { mutableStateOf(false) }

val isDirty = isWineFormDirty(
editingWine = editingWine,
Expand Down Expand Up @@ -170,13 +172,27 @@ fun WineFormSheet(
}

item {
val yearErrorMessage = stringResource(
R.string.invalid_wine_year,
WineYears.MIN_YEAR,
WineYears.maxYear(),
)
OutlinedTextField(
value = year,
onValueChange = { year = it },
onValueChange = {
year = filterWineYearInput(it)
showYearError = false
},
label = { Text(stringResource(R.string.year_req)) },
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
singleLine = true,
isError = showYearError,
supportingText = if (showYearError) {
{ Text(yearErrorMessage) }
} else {
null
},
)
}

Expand Down Expand Up @@ -287,10 +303,14 @@ fun WineFormSheet(

item {
val isValid = name.isNotBlank() && producer.isNotBlank()
&& year.toIntOrNull() != null && grape.isNotBlank()
&& isWineFormYearInRange(year) && grape.isNotBlank()

Button(
onClick = {
if (!isWineFormYearInRange(year)) {
showYearError = true
return@Button
}
val wine = Wine(
id = editingWine?.id ?: UUID.randomUUID().toString(),
name = name,
Expand All @@ -310,11 +330,6 @@ fun WineFormSheet(
image = editingWine?.image ?: "",
)
onSave(wine, isNew)
scope.launch {
allowSheetHide = true
sheetState.hide()
allowSheetHide = false
}
},
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package hu.toliver.vinotes.ui.screen.winelist.components

import hu.toliver.vinotes.domain.WineYears

fun filterWineYearInput(input: String): String = input.filter(Char::isDigit)

fun isWineFormYearInRange(yearText: String): Boolean {
val year = yearText.toIntOrNull() ?: return false
return WineYears.isValid(year)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package hu.toliver.vinotes.ui.screen.winelist.components

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class WineFormYearTest {

@Test
fun filterWineYearInput_stripsNonDigits() {
assertEquals("2020", filterWineYearInput("2020"))
assertEquals("1", filterWineYearInput("-1"))
assertEquals("1800", filterWineYearInput("18ab00"))
assertEquals("", filterWineYearInput("abc"))
}

@Test
fun isWineFormYearInRange_acceptsValidVintage() {
assertTrue(isWineFormYearInRange("2020"))
}

@Test
fun isWineFormYearInRange_rejectsOutOfRange() {
assertFalse(isWineFormYearInRange("1800"))
assertFalse(isWineFormYearInRange("1"))
}

@Test
fun isWineFormYearInRange_rejectsNonNumeric() {
assertFalse(isWineFormYearInRange(""))
assertFalse(isWineFormYearInRange("abc"))
}
}
Loading