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..c60837de8e 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 regex: Regex? = null, 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..e238fb70d0 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 @@ -209,14 +209,7 @@ private fun TextInputContent( }, value = textFieldValueState, onValueChange = { - textFieldValueState = it - onTextChanged( - textInputModel.copy( - currentValue = it.text, - selection = it.selection, - error = null, - ), - ) + textFieldValueState = manageOnValueChanged(textFieldValueState, it, 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.regex != null) { + if (textInputModel.regex.matches(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 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..b23e5458cd 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..fbf9e99bfc 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 @@ -50,6 +50,7 @@ import org.dhis2.composetable.model.TableCell import org.dhis2.composetable.model.TextInputModel import org.dhis2.composetable.model.ValidationResult import org.hisp.dhis.android.core.program.ProgramRuleActionType +import org.hisp.dhis.mobile.ui.designsystem.component.model.RegExValidations import org.hisp.dhis.rules.models.RuleEffect import org.jetbrains.annotations.NotNull import java.util.Collections @@ -329,9 +330,17 @@ class ManageStockViewModel @Inject constructor( currentValue = cell.value, keyboardInputType = KeyboardInputType.NumberPassword(), error = stockEntry?.errorMessage, + regex = getRegExBasedOnTransactionType(), ) } + private fun getRegExBasedOnTransactionType(): Regex? { + return when (transaction.value?.transactionType) { + TransactionType.CORRECTION -> null + else -> RegExValidations.POSITIVE_INTEGER.regex + } + } + fun onSaveValueChange(cell: TableCell) { viewModelScope.launch( dispatcherProvider.io(),