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-5900] Adapt Input fields to TextFieldValue usage #3485

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 @@ -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
Loading