From 84aab558f677fcf5abd767698415df7fa4503e70 Mon Sep 17 00:00:00 2001 From: Xavier Molloy Date: Tue, 6 Feb 2024 12:21:42 +0100 Subject: [PATCH] fix: [ANDROAPP-5900] set initial selection to value length --- .../providers/InputFieldsProvider.kt | 5 ++- .../ui/provider/inputfield/DateProvider.kt | 4 +- .../ui/provider/inputfield/FieldProvider.kt | 42 ++++++++++++++----- .../InputsForTextValueTypeProvider.kt | 8 +++- .../inputfield/UnitIntervalInputProvider.kt | 5 ++- 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventDetails/providers/InputFieldsProvider.kt b/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventDetails/providers/InputFieldsProvider.kt index e18437e62a..4dd1678f0e 100644 --- a/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventDetails/providers/InputFieldsProvider.kt +++ b/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventDetails/providers/InputFieldsProvider.kt @@ -9,6 +9,7 @@ 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 @@ -52,9 +53,11 @@ 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) { if (uiModel.eventDate.dateValue != null) { - mutableStateOf(TextFieldValue(formatStoredDateToUI(uiModel.eventDate.dateValue) ?: "")) + mutableStateOf(TextFieldValue(formatStoredDateToUI(uiModel.eventDate.dateValue) ?: "", textSelection)) } else { mutableStateOf(TextFieldValue()) } diff --git a/form/src/main/java/org/dhis2/form/ui/provider/inputfield/DateProvider.kt b/form/src/main/java/org/dhis2/form/ui/provider/inputfield/DateProvider.kt index 36994889c9..3083b5dfc3 100644 --- a/form/src/main/java/org/dhis2/form/ui/provider/inputfield/DateProvider.kt +++ b/form/src/main/java/org/dhis2/form/ui/provider/inputfield/DateProvider.kt @@ -8,6 +8,7 @@ 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 @@ -37,11 +38,12 @@ 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( if (fieldUiModel.value != null) { - TextFieldValue(formatStoredDateToUI(fieldUiModel.value!!, fieldUiModel.valueType)) + TextFieldValue(formatStoredDateToUI(fieldUiModel.value!!, fieldUiModel.valueType), textSelection) } else { TextFieldValue() }, diff --git a/form/src/main/java/org/dhis2/form/ui/provider/inputfield/FieldProvider.kt b/form/src/main/java/org/dhis2/form/ui/provider/inputfield/FieldProvider.kt index dd56aecfe4..625a51a7e5 100644 --- a/form/src/main/java/org/dhis2/form/ui/provider/inputfield/FieldProvider.kt +++ b/form/src/main/java/org/dhis2/form/ui/provider/inputfield/FieldProvider.kt @@ -23,6 +23,7 @@ import androidx.compose.ui.geometry.Rect import androidx.compose.ui.geometry.Size import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.TextFieldValue import kotlinx.coroutines.launch @@ -448,8 +449,10 @@ private fun ProvideIntegerPositive( focusManager: FocusManager, onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputPositiveInteger( @@ -487,8 +490,10 @@ private fun ProvideIntegerPositiveOrZero( onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputPositiveIntegerOrZero( @@ -526,8 +531,10 @@ private fun ProvidePercentage( onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputPercentage( @@ -565,8 +572,10 @@ private fun ProvideNumber( onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputNumber( @@ -605,8 +614,9 @@ private fun ProvideIntegerNegative( onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value?.replace("-", "") ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value?.replace("-", "") ?: "", textSelection)) } InputNegativeInteger( @@ -644,8 +654,10 @@ private fun ProvideLongText( onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputLongText( @@ -684,8 +696,9 @@ private fun ProvideLetter( onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputLetter( @@ -723,8 +736,9 @@ private fun ProvideInteger( onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputInteger( @@ -762,8 +776,10 @@ private fun ProvideEmail( focusManager: FocusManager, onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputEmail( @@ -811,8 +827,10 @@ private fun ProvideInputPhoneNumber( onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputPhoneNumber( @@ -860,8 +878,10 @@ private fun ProvideInputLink( onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputLink( diff --git a/form/src/main/java/org/dhis2/form/ui/provider/inputfield/InputsForTextValueTypeProvider.kt b/form/src/main/java/org/dhis2/form/ui/provider/inputfield/InputsForTextValueTypeProvider.kt index 4b52d9cb97..9d069fdea0 100644 --- a/form/src/main/java/org/dhis2/form/ui/provider/inputfield/InputsForTextValueTypeProvider.kt +++ b/form/src/main/java/org/dhis2/form/ui/provider/inputfield/InputsForTextValueTypeProvider.kt @@ -8,6 +8,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusManager +import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.input.TextFieldValue import org.dhis2.form.extensions.autocompleteList import org.dhis2.form.extensions.inputState @@ -72,8 +73,9 @@ private fun ProvideQRInput( focusManager: FocusManager, onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputQRCode( @@ -170,8 +172,10 @@ private fun ProvideBarcodeInput( focusManager: FocusManager, onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputBarCode( diff --git a/form/src/main/java/org/dhis2/form/ui/provider/inputfield/UnitIntervalInputProvider.kt b/form/src/main/java/org/dhis2/form/ui/provider/inputfield/UnitIntervalInputProvider.kt index 8f033dca80..eb7dfa93f8 100644 --- a/form/src/main/java/org/dhis2/form/ui/provider/inputfield/UnitIntervalInputProvider.kt +++ b/form/src/main/java/org/dhis2/form/ui/provider/inputfield/UnitIntervalInputProvider.kt @@ -7,6 +7,7 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.input.TextFieldValue import org.dhis2.form.extensions.inputState import org.dhis2.form.extensions.legend @@ -22,8 +23,10 @@ fun ProvideUnitIntervalInput( intentHandler: (FormIntent) -> Unit, onNextClicked: () -> Unit, ) { + val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0) + var value by remember(fieldUiModel.value) { - mutableStateOf(TextFieldValue(fieldUiModel.value ?: "")) + mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection)) } InputUnitInterval( modifier = modifier.fillMaxWidth(),