Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [ANDROAPP-5888] Disallow leading zero input from keyBoard #3938

Merged
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 @@ -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
Expand Down
35 changes: 27 additions & 8 deletions compose-table/src/main/java/org/dhis2/composetable/ui/TextInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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.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
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 @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
Loading