Skip to content

Commit

Permalink
fix: [ANDROAPP-5888] Disallow leading zero input from keyBoard
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimolloy committed Dec 18, 2024
1 parent fe8afe2 commit 9c02de8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data class TextInputModel(
val selection: TextRange? = null,
val error: String? = null,
val warning: String? = null,
val acceptZeroValues: Boolean = true,
private val clearable: Boolean = false,
) {
fun showClearButton() = clearable && currentValue?.isNotEmpty() == true
Expand Down
42 changes: 33 additions & 9 deletions compose-table/src/main/java/org/dhis2/composetable/ui/TextInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,8 @@ private fun TextInputContent(
}
},
value = textFieldValueState,
onValueChange = {
textFieldValueState = it
onTextChanged(
textInputModel.copy(
currentValue = it.text,
selection = it.selection,
error = null,
),
)
onValueChange = { newValue ->
textFieldValueState = manageOnValueChanged(textFieldValueState, newValue, onTextChanged, textInputModel)
},
textStyle = TextStyle.Default.copy(
fontSize = 12.sp,
Expand Down Expand Up @@ -271,6 +264,32 @@ private fun TextInputContent(
}
}

fun manageOnValueChanged(textFieldValueState: TextFieldValue, newValue: TextFieldValue, onTextChanged: (TextInputModel) -> Unit, textInputModel: TextInputModel): TextFieldValue {
return if (!textInputModel.acceptZeroValues) {
if (isAValidPositiveInteger(newValue.text)) {
onTextChanged(
textInputModel.copy(
currentValue = newValue.text,
selection = newValue.selection,
error = null,
),
)
newValue
} else {
textFieldValueState
}
} else {
onTextChanged(
textInputModel.copy(
currentValue = newValue.text,
selection = newValue.selection,
error = null,
),
)
newValue
}
}

@Composable
private fun dividerColor(hasError: Boolean, hasWarning: Boolean, hasFocus: Boolean) = when {
hasError -> LocalTableColors.current.errorColor
Expand Down Expand Up @@ -382,6 +401,11 @@ fun DefaultTextInputErrorStatusPreview() {
)
}

private fun isAValidPositiveInteger(input: String): Boolean {
return POSITIVE_INTEGER_REGEX.toRegex().matches(input)
}

const val POSITIVE_INTEGER_REGEX = "^(?!0)\\d*"
const val INPUT_TEST_TAG = "INPUT_TEST_TAG"
const val INPUT_TEST_FIELD_TEST_TAG = "INPUT_TEST_FIELD_TEST_TAG"
const val INPUT_ERROR_MESSAGE_TEST_TAG = "INPUT_ERROR_MESSAGE_TEST_TAG"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ fun HomeScreen(
) { targetIndex ->
when (targetIndex) {
BottomNavigation.ANALYTICS.id -> {
DHIS2Theme() {}
AnalyticsScreen(
viewModel = viewModel,
backAction = { manageStockViewModel.onHandleBackNavigation() },
themeColor = themeColor,
modifier = Modifier.padding(paddingValues),
scaffoldState = scaffoldState,
supportFragmentManager = supportFragmentManager,
)
DHIS2Theme {
AnalyticsScreen(
viewModel = viewModel,
backAction = { manageStockViewModel.onHandleBackNavigation() },
themeColor = themeColor,
modifier = Modifier.padding(paddingValues),
scaffoldState = scaffoldState,
supportFragmentManager = supportFragmentManager,
)
}
}

BottomNavigation.DATA_ENTRY.id -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ class ManageStockViewModel @Inject constructor(
currentValue = cell.value,
keyboardInputType = KeyboardInputType.NumberPassword(),
error = stockEntry?.errorMessage,
acceptZeroValues = false,
)
}

Expand Down

0 comments on commit 9c02de8

Please sign in to comment.