Skip to content

Commit

Permalink
Merge pull request #3485 from dhis2/ANDROAPP-5900-Some-values-are-und…
Browse files Browse the repository at this point in the history
…erlined-after-saving-an-event-or-registration-form

fix: [ANDROAPP-5900] Adapt Input fields to TextFieldValue usage
  • Loading branch information
andresmr authored Feb 6, 2024
2 parents 69520d9 + 84aab55 commit 4ce45c2
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
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 org.dhis2.R
import org.dhis2.commons.resources.ResourceManager
Expand Down Expand Up @@ -51,8 +53,14 @@ fun ProvideInputDate(
) {
if (uiModel.showField) {
Spacer(modifier = Modifier.height(16.dp))
val textSelection = TextRange(if (uiModel.eventDate.dateValue != null) uiModel.eventDate.dateValue.length else 0)

var value by remember(uiModel.eventDate.dateValue) {
mutableStateOf(uiModel.eventDate.dateValue?.let { formatStoredDateToUI(it) })
if (uiModel.eventDate.dateValue != null) {
mutableStateOf(TextFieldValue(formatStoredDateToUI(uiModel.eventDate.dateValue) ?: "", textSelection))
} else {
mutableStateOf(TextFieldValue())
}
}

var state by remember {
Expand All @@ -62,25 +70,21 @@ fun ProvideInputDate(
InputDateTime(
title = uiModel.eventDate.label ?: "",
allowsManualInput = uiModel.allowsManualInput,
value = value,
inputTextFieldValue = value,
actionIconType = DateTimeActionIconType.DATE,
onActionClicked = uiModel.onDateClick,
state = state,
visualTransformation = DateTransformation(),
onValueChanged = {
value = it
state = getInputShellStateBasedOnValue(it)
manageActionBasedOnValue(uiModel, it)
state = getInputShellStateBasedOnValue(it.text)
manageActionBasedOnValue(uiModel, it.text)
},
isRequired = uiModel.required,
modifier = modifier.testTag(INPUT_EVENT_INITIAL_DATE),
onFocusChanged = { focused ->
if (!focused) {
value?.let {
if (!isValid(it)) {
state = InputShellState.ERROR
}
}
if (!focused && !isValid(value.text)) {
state = InputShellState.ERROR
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import org.dhis2.commons.date.DateUtils
import org.dhis2.commons.extensions.toDate
import org.dhis2.form.extensions.inputState
Expand Down Expand Up @@ -36,22 +38,29 @@ fun ProvideInputDate(
ValueType.TIME -> DateTimeActionIconType.TIME to TimeTransformation()
else -> DateTimeActionIconType.DATE to DateTransformation()
}
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(fieldUiModel.value?.let { formatStoredDateToUI(it, fieldUiModel.valueType) })
mutableStateOf(
if (fieldUiModel.value != null) {
TextFieldValue(formatStoredDateToUI(fieldUiModel.value!!, fieldUiModel.valueType), textSelection)
} else {
TextFieldValue()
},
)
}

InputDateTime(
title = fieldUiModel.label,
value = value,
inputTextFieldValue = value,
actionIconType = actionType,
onActionClicked = {
when (actionType) {
DateTimeActionIconType.DATE -> uiEventHandler.invoke(
RecyclerViewUiEvents.OpenCustomCalendar(
uid = fieldUiModel.uid,
label = fieldUiModel.label,
date = value?.toDate(),
date = value.text.toDate(),
allowFutureDates = fieldUiModel.allowFutureDates ?: true,
isDateTime = false,
),
Expand All @@ -61,7 +70,7 @@ fun ProvideInputDate(
RecyclerViewUiEvents.OpenTimePicker(
uid = fieldUiModel.uid,
label = fieldUiModel.label,
date = formatUIDateToStored(value, fieldUiModel.valueType)?.let {
date = formatUIDateToStored(value.text, fieldUiModel.valueType)?.let {
DateUtils.timeFormat().parse(it)
},
isDateTime = false,
Expand All @@ -72,7 +81,7 @@ fun ProvideInputDate(
RecyclerViewUiEvents.OpenCustomCalendar(
uid = fieldUiModel.uid,
label = fieldUiModel.label,
date = formatUIDateToStored(value, fieldUiModel.valueType)?.let {
date = formatUIDateToStored(value.text, fieldUiModel.valueType)?.let {
DateUtils.databaseDateFormatNoSeconds().parse(it)
},
allowFutureDates = fieldUiModel.allowFutureDates ?: true,
Expand All @@ -81,7 +90,7 @@ fun ProvideInputDate(
)
}
},
modifier = modifier.semantics { contentDescription = formatStoredDateToUI(value ?: "", fieldUiModel.valueType) },
modifier = modifier.semantics { contentDescription = formatStoredDateToUI(value.text, fieldUiModel.valueType) },
state = fieldUiModel.inputState(),
legendData = fieldUiModel.legend(),
supportingText = fieldUiModel.supportingText(),
Expand All @@ -94,7 +103,7 @@ fun ProvideInputDate(
intentHandler.invoke(
FormIntent.OnTextChange(
uid = fieldUiModel.uid,
value = formatUIDateToStored(it, fieldUiModel.valueType),
value = formatUIDateToStored(it.text, fieldUiModel.valueType),
valueType = fieldUiModel.valueType,
allowFutureDates = fieldUiModel.allowFutureDates ?: true,
),
Expand Down
Loading

0 comments on commit 4ce45c2

Please sign in to comment.