From da700227439f358240cc20b6cf978fab0de0168f Mon Sep 17 00:00:00 2001 From: Siddharth Agarwal Date: Tue, 25 Jun 2024 15:43:11 +0530 Subject: [PATCH 1/5] Fix `InputAge` crash on language change --- .../designsystem/component/AgeFieldHelper.kt | 32 +++++++++---------- .../ui/designsystem/component/InputAge.kt | 7 ++-- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/AgeFieldHelper.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/AgeFieldHelper.kt index 19df36d23..fc74fa51e 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/AgeFieldHelper.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/AgeFieldHelper.kt @@ -18,18 +18,18 @@ import org.hisp.dhis.mobile.ui.designsystem.theme.SurfaceColor * * @param orientation: Controls how the radio buttons will be displayed, HORIZONTAL for rows or * VERTICAL for columns. - * @param optionSelected: controls which item is selected. + * @param optionSelected: controls which [TimeUnitValues] is selected. * @param enabled: manages the enabled state - * @param onClick: is a callback to notify which item has changed into the block. + * @param onClick: is a callback to notify when [TimeUnitValues] is changed. * @param modifier: optional modifier. */ @Composable internal fun TimeUnitSelector( orientation: Orientation, - optionSelected: String, + optionSelected: TimeUnitValues, modifier: Modifier = Modifier, enabled: Boolean = true, - onClick: (RadioButtonData) -> Unit, + onClick: (TimeUnitValues) -> Unit, ) { val backgroundColor = if (enabled) { SurfaceColor.Surface @@ -37,6 +37,14 @@ internal fun TimeUnitSelector( SurfaceColor.DisabledSurface } + val options = TimeUnitValues.entries.map { + RadioButtonData(it.name, optionSelected == it, enabled, provideStringResource(it.value)) + } + + var selectedOption by remember { + mutableStateOf(options.find { it.selected } ?: options[0]) + } + RowComponentContainer( modifier = modifier .background(color = backgroundColor, Shape.SmallBottom) @@ -45,18 +53,10 @@ internal fun TimeUnitSelector( end = Spacing.Spacing8, ), ) { - val options = TimeUnitValues.values().map { - RadioButtonData(it.value, optionSelected == it.value, enabled, provideStringResource(it.value)) - } - val selectedItem = options.find { - it.selected - } - var currentItem by remember { - mutableStateOf(selectedItem ?: options[0]) - } - RadioButtonBlock(orientation, options, currentItem) { - currentItem = it - onClick.invoke(it) + RadioButtonBlock(orientation, options, selectedOption) { + selectedOption = it + val selectedTimeUnit = TimeUnitValues.entries.first { it.name == selectedOption.uid } + onClick.invoke(selectedTimeUnit) } } } diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAge.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAge.kt index 3bfbedc99..cfb936182 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAge.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAge.kt @@ -218,12 +218,9 @@ fun InputAge( modifier = Modifier.fillMaxWidth() .testTag("INPUT_AGE_TIME_UNIT_SELECTOR"), orientation = Orientation.HORIZONTAL, - optionSelected = YEARS.value, + optionSelected = YEARS, enabled = uiModel.state != InputShellState.DISABLED, - onClick = { itemData -> - val timeUnit = TimeUnitValues.entries - .first { it.value.contains(itemData.textInput!!, ignoreCase = true) } - + onClick = { timeUnit -> uiModel.onValueChanged.invoke(uiModel.inputType.copy(unit = timeUnit)) }, ) From f1ebc299d6c12ace024fb0a9812276e786bc5e6a Mon Sep 17 00:00:00 2001 From: Siddharth Agarwal Date: Tue, 25 Jun 2024 16:11:26 +0530 Subject: [PATCH 2/5] Add age unit selection change test for `InputAge` component --- .../ui/designsystem/component/InputAgeTest.kt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt index fbffaf18c..aa243af37 100644 --- a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt +++ b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt @@ -6,6 +6,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.performTextClearance import androidx.compose.ui.test.performTextInput import org.junit.Rule import org.junit.Test @@ -176,4 +177,39 @@ class InputAgeTest { rule.onNodeWithTag("INPUT_AGE_SUPPORTING_TEXT").assertExists() } + @Test + fun changingAgeTimeUnitShouldWorkProperly() { + var inputType by mutableStateOf(AgeInputType.Age.EMPTY) + + rule.setContent { + InputAge( + InputAgeModel( + title = "Label", + inputType = inputType, + onValueChanged = { + inputType = it + }, + ), + + ) + } + + rule.onNodeWithTag("INPUT_AGE_TIME_UNIT_SELECTOR").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_YEARS").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_MONTHS").assertExists() + rule.onNodeWithTag("RADIO_BUTTON_DAYS").assertExists() + + rule.onNodeWithTag("RADIO_BUTTON_MONTHS").performClick() + rule.onNodeWithTag("INPUT_AGE_TEXT_FIELD").performTextInput("11") + val newInputMonthType = inputType as AgeInputType.Age + assert(newInputMonthType.value.text == "11") + assert(newInputMonthType.unit == TimeUnitValues.MONTHS) + + rule.onNodeWithTag("RADIO_BUTTON_DAYS").performClick() + rule.onNodeWithTag("INPUT_AGE_TEXT_FIELD").performTextClearance() + rule.onNodeWithTag("INPUT_AGE_TEXT_FIELD").performTextInput("28") + val newInputDaysType = inputType as AgeInputType.Age + assert(newInputDaysType.value.text == "28") + assert(newInputDaysType.unit == TimeUnitValues.DAYS) + } } From bb2267ebcb26dd99dad07f7c12439a6404a639fc Mon Sep 17 00:00:00 2001 From: Siddharth Agarwal Date: Tue, 25 Jun 2024 18:07:10 +0530 Subject: [PATCH 3/5] Fix lint error --- .../mobile/ui/designsystem/component/InputAgeTest.kt | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt index aa243af37..27e093254 100644 --- a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt +++ b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt @@ -28,7 +28,6 @@ class InputAgeTest { // no-op }, ), - ) } @@ -50,7 +49,6 @@ class InputAgeTest { // no-op }, ), - ) } @@ -73,7 +71,6 @@ class InputAgeTest { inputType = it }, ), - ) } @@ -94,7 +91,6 @@ class InputAgeTest { // no-op }, ), - ) } @@ -117,7 +113,6 @@ class InputAgeTest { inputType = it }, ), - ) } @@ -139,7 +134,6 @@ class InputAgeTest { inputType = it }, ), - ) } @@ -190,8 +184,7 @@ class InputAgeTest { inputType = it }, ), - - ) + ) } rule.onNodeWithTag("INPUT_AGE_TIME_UNIT_SELECTOR").assertExists() From 9cbc14e5a72d6598f22ee44702cbd9b91e3dc04d Mon Sep 17 00:00:00 2001 From: Siddharth Agarwal Date: Fri, 28 Jun 2024 14:24:23 +0530 Subject: [PATCH 4/5] Fix translations for `InputAge` helper text --- .../org/hisp/dhis/mobile/ui/designsystem/component/InputAge.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAge.kt b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAge.kt index cfb936182..16a5ac9fb 100644 --- a/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAge.kt +++ b/designsystem/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAge.kt @@ -157,7 +157,7 @@ fun InputAge( .testTag("INPUT_AGE_TEXT_FIELD") .fillMaxWidth(), inputTextValue = getTextFieldValue(uiModel.inputType), - helper = helperText, + helper = if (helperText != null) provideStringResource(helperText).lowercase() else null, isSingleLine = true, helperStyle = helperStyle, onInputChanged = { newText -> From 31b1bc87393c6797a5ab5f05186c213ea786a9cb Mon Sep 17 00:00:00 2001 From: Xavier Molloy Date: Thu, 4 Jul 2024 11:11:19 +0200 Subject: [PATCH 5/5] fix: [ANDROAPP-6218] ktlint fix --- .../hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt index 27e093254..c3dbd2a6f 100644 --- a/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt +++ b/designsystem/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/InputAgeTest.kt @@ -171,6 +171,7 @@ class InputAgeTest { rule.onNodeWithTag("INPUT_AGE_SUPPORTING_TEXT").assertExists() } + @Test fun changingAgeTimeUnitShouldWorkProperly() { var inputType by mutableStateOf(AgeInputType.Age.EMPTY)