From 9c02de8ce1c2bfbfaed8ff6038e985cada5972d9 Mon Sep 17 00:00:00 2001 From: Xavier Molloy Date: Wed, 18 Dec 2024 12:10:36 +0100 Subject: [PATCH] fix: [ANDROAPP-5888] Disallow leading zero input from keyBoard --- .../composetable/model/TextInputModel.kt | 1 + .../org/dhis2/composetable/ui/TextInput.kt | 42 +++++++++++++++---- .../rtsm/ui/home/screens/HomeScreen.kt | 19 +++++---- .../ui/managestock/ManageStockViewModel.kt | 1 + 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/compose-table/src/main/java/org/dhis2/composetable/model/TextInputModel.kt b/compose-table/src/main/java/org/dhis2/composetable/model/TextInputModel.kt index fc3d10d08f..dd1d239eda 100644 --- a/compose-table/src/main/java/org/dhis2/composetable/model/TextInputModel.kt +++ b/compose-table/src/main/java/org/dhis2/composetable/model/TextInputModel.kt @@ -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 diff --git a/compose-table/src/main/java/org/dhis2/composetable/ui/TextInput.kt b/compose-table/src/main/java/org/dhis2/composetable/ui/TextInput.kt index de38acc3d6..d3ae53bee7 100644 --- a/compose-table/src/main/java/org/dhis2/composetable/ui/TextInput.kt +++ b/compose-table/src/main/java/org/dhis2/composetable/ui/TextInput.kt @@ -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, @@ -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 @@ -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" diff --git a/stock-usecase/src/main/java/org/dhis2/android/rtsm/ui/home/screens/HomeScreen.kt b/stock-usecase/src/main/java/org/dhis2/android/rtsm/ui/home/screens/HomeScreen.kt index a551e0b902..f3d948d255 100644 --- a/stock-usecase/src/main/java/org/dhis2/android/rtsm/ui/home/screens/HomeScreen.kt +++ b/stock-usecase/src/main/java/org/dhis2/android/rtsm/ui/home/screens/HomeScreen.kt @@ -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 -> { diff --git a/stock-usecase/src/main/java/org/dhis2/android/rtsm/ui/managestock/ManageStockViewModel.kt b/stock-usecase/src/main/java/org/dhis2/android/rtsm/ui/managestock/ManageStockViewModel.kt index d7b70a3461..8c9d48bd04 100644 --- a/stock-usecase/src/main/java/org/dhis2/android/rtsm/ui/managestock/ManageStockViewModel.kt +++ b/stock-usecase/src/main/java/org/dhis2/android/rtsm/ui/managestock/ManageStockViewModel.kt @@ -329,6 +329,7 @@ class ManageStockViewModel @Inject constructor( currentValue = cell.value, keyboardInputType = KeyboardInputType.NumberPassword(), error = stockEntry?.errorMessage, + acceptZeroValues = false, ) }