Skip to content

Validate Price and Quantity as typing value. #88

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import java.text.NumberFormat
*/
class ItemEntryViewModel(private val itemsRepository: ItemsRepository) : ViewModel() {

companion object {
private val DECIMAL_REGEX = "^\\d*\\.?\\d*$".toRegex()
private val NUMBER_REGEX = "^\\d*$".toRegex()
}

/**
* Holds current item ui state
*/
Expand All @@ -40,8 +45,24 @@ class ItemEntryViewModel(private val itemsRepository: ItemsRepository) : ViewMod
* a validation for input values.
*/
fun updateUiState(itemDetails: ItemDetails) {
itemUiState =
ItemUiState(itemDetails = itemDetails, isEntryValid = validateInput(itemDetails))
if (
validateDecimalInputText(itemDetails.price)
.and(validateNumberInputText(itemDetails.quantity))
) {
itemUiState =
ItemUiState(
itemDetails = itemDetails,
isEntryValid = validateInput(itemDetails)
)
}
}

private fun validateDecimalInputText(inputText: String): Boolean {
return inputText.matches(DECIMAL_REGEX)
}

private fun validateNumberInputText(inputText: String): Boolean {
return inputText.matches(NUMBER_REGEX)
}

/**
Expand Down