Skip to content

Commit

Permalink
fix: [ANDROAPP-6628] do not allow schedule when date has error
Browse files Browse the repository at this point in the history
Signed-off-by: Manu Muñoz <[email protected]>
  • Loading branch information
mmmateos committed Dec 18, 2024
1 parent b642569 commit 00dc859
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ data class EventDate(
val scheduleInterval: Int = 0,
val allowFutureDates: Boolean = true,
val periodType: PeriodType? = null,
)
val error: Boolean = false,
) {
val isValid = !dateValue.isNullOrEmpty() && !error
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ data class EventInputDateUiModel(
val showField: Boolean = true,
val is24HourFormat: Boolean = true,
val selectableDates: SelectableDates? = null,
val onError: (() -> Unit)? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import kotlinx.datetime.LocalDate
import org.dhis2.R
import org.dhis2.commons.extensions.inDateRange
import org.dhis2.commons.extensions.inOrgUnit
Expand Down Expand Up @@ -100,8 +101,12 @@ fun ProvideInputDate(
modifier = modifier.testTag(INPUT_EVENT_INITIAL_DATE),
onValueChanged = {
value = it ?: TextFieldValue()
state = getInputShellStateBasedOnValue(it?.text)
it?.let { it1 -> manageActionBasedOnValue(uiModel, it1.text) }
it?.let { dateValue ->
manageActionBasedOnValue(
uiModel = uiModel,
dateString = dateValue.text,
)
}
},
onFocusChanged = { focused ->
if (!focused && !isValid(value.text) && state == InputShellState.FOCUSED) {
Expand All @@ -123,25 +128,31 @@ fun isValidDateFormat(dateString: String): Boolean {
}
}

fun getInputShellStateBasedOnValue(dateString: String?): InputShellState {
dateString?.let {
return if (!isValidDateFormat(it)) {
InputShellState.ERROR
} else {
InputShellState.FOCUSED
}
}
return InputShellState.FOCUSED
}

fun manageActionBasedOnValue(uiModel: EventInputDateUiModel, dateString: String) {
if (dateString.isEmpty()) {
uiModel.onClear?.invoke()
} else if (isValidDateFormat(dateString)) {
formatUIDateToStored(dateString)?.let { dateValues ->
uiModel.onDateSelected(dateValues)
if (uiModel.selectableDates?.let { dateValues.isInRange(it) } == true) {
uiModel.onDateSelected(dateValues)
} else {
uiModel.onError?.invoke()
}
}
} else {
uiModel.onError?.invoke()
}
}

fun InputDateValues.isInRange(selectableDates: SelectableDates): Boolean {
val format = LocalDate.Format {
dayOfMonth()
monthNumber()
year()
}
val date = LocalDate(year, month, day)
return format.parse(selectableDates.initialDate) <= date &&
format.parse(selectableDates.endDate) >= date
}

private fun isValid(valueString: String) = valueString.length == 8
Expand Down Expand Up @@ -171,7 +182,7 @@ fun formatUIDateToStored(dateValue: String?): InputDateValues? {
return if (dateValue?.length != 10) {
null
} else {
val date = kotlinx.datetime.LocalDate.Formats.ISO.parse(dateValue)
val date = LocalDate.Formats.ISO.parse(dateValue)
InputDateValues(date.dayOfMonth, date.monthNumber, date.year)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private fun ButtonBlock(
modifier = Modifier.fillMaxWidth(),
style = ButtonStyle.FILLED,
enabled = !scheduleNew ||
!date.dateValue.isNullOrEmpty() &&
date.isValid &&
catCombo.isCompleted,
text = buttonTitle(scheduleNew),
onClick = {
Expand All @@ -166,7 +166,7 @@ private fun ButtonBlock(
Button(
modifier = Modifier.fillMaxWidth(),
style = ButtonStyle.FILLED,
enabled = !date.dateValue.isNullOrEmpty(),
enabled = date.isValid,
text = stringResource(R.string.enter_event, eventLabel),
onClick = {
viewModel.enterEvent(launchMode)
Expand Down Expand Up @@ -272,6 +272,7 @@ fun ProvideScheduleNewEventForm(
onDateClick = {},
onDateSelected = { viewModel.onDateSet(it.year, it.month, it.day) },
onClear = { viewModel.onClearEventReportDate() },
onError = { viewModel.onDateError() },
),
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.dhis2.commons.bindings.enrollment
Expand Down Expand Up @@ -228,6 +229,12 @@ class SchedulingViewModel(
)
}

fun onDateError() {
_eventDate.update {
it.copy(error = true)
}
}

fun setUpCategoryCombo(categoryOption: Pair<String, String?>? = null) {
viewModelScope.launch {
configureEventCatCombo(categoryOption)
Expand Down

0 comments on commit 00dc859

Please sign in to comment.