From 8601b6e15e5b6fba0bda4027aee34cc67753c761 Mon Sep 17 00:00:00 2001 From: omaeewa Date: Mon, 26 Aug 2024 10:17:17 +0300 Subject: [PATCH 1/2] Refactor: Remove duplicate code in PIN screen --- pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt | 38 +++++ .../pin/ui/create/compose/CreatePinScreen.kt | 71 ++------- .../pin/ui/reset/compose/ResetPinScreen.kt | 141 +++--------------- 3 files changed, 67 insertions(+), 183 deletions(-) create mode 100644 pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt diff --git a/pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt b/pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt new file mode 100644 index 0000000..94ee3b3 --- /dev/null +++ b/pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt @@ -0,0 +1,38 @@ +package ua.gov.diia.pin.ui + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.remember +import ua.gov.diia.ui_base.R +import ua.gov.diia.ui_base.components.DiiaResourceIcon +import ua.gov.diia.ui_base.components.atom.text.textwithparameter.TextWithParametersData +import ua.gov.diia.ui_base.components.infrastructure.DataActionWrapper +import ua.gov.diia.ui_base.components.infrastructure.utils.resource.UiText +import ua.gov.diia.ui_base.components.molecule.header.TitleGroupMlcData +import ua.gov.diia.ui_base.components.organism.header.TopGroupOrgData +import ua.gov.diia.ui_base.components.organism.tile.NumButtonTileOrganismData + +@Composable +inline fun List.asSnapshotStateList() = + remember { mutableStateListOf(*this.toTypedArray()) } + +fun getPinTestData(topGroupText: String, textWithParameters: String, pinLength: Int = 4) = listOf( + TopGroupOrgData( + titleGroupMlcData = TitleGroupMlcData( + heroText = UiText.DynamicString(topGroupText), + leftNavIcon = TitleGroupMlcData.LeftNavIcon( + code = DiiaResourceIcon.BACK.code, + accessibilityDescription = UiText.StringResource(R.string.accessibility_back_button), + action = DataActionWrapper( + type = "back", + subtype = null, + resource = null + ) + ) + ) + ), + TextWithParametersData( + text = UiText.DynamicString(textWithParameters) + ), + NumButtonTileOrganismData(pinLength = pinLength) +) \ No newline at end of file diff --git a/pin/src/main/java/ua/gov/diia/pin/ui/create/compose/CreatePinScreen.kt b/pin/src/main/java/ua/gov/diia/pin/ui/create/compose/CreatePinScreen.kt index 3428ad1..3685a28 100644 --- a/pin/src/main/java/ua/gov/diia/pin/ui/create/compose/CreatePinScreen.kt +++ b/pin/src/main/java/ua/gov/diia/pin/ui/create/compose/CreatePinScreen.kt @@ -3,8 +3,6 @@ package ua.gov.diia.pin.ui.create.compose import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.runtime.Composable -import androidx.compose.runtime.mutableStateListOf -import androidx.compose.runtime.remember import androidx.compose.runtime.snapshots.SnapshotStateList import androidx.compose.ui.Modifier import androidx.compose.ui.draw.paint @@ -12,14 +10,11 @@ import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import androidx.constraintlayout.compose.ConstraintLayout +import ua.gov.diia.pin.ui.asSnapshotStateList +import ua.gov.diia.pin.ui.getPinTestData import ua.gov.diia.ui_base.R -import ua.gov.diia.ui_base.components.DiiaResourceIcon -import ua.gov.diia.ui_base.components.atom.text.textwithparameter.TextWithParametersData -import ua.gov.diia.ui_base.components.infrastructure.DataActionWrapper import ua.gov.diia.ui_base.components.infrastructure.UIElementData import ua.gov.diia.ui_base.components.infrastructure.event.UIAction -import ua.gov.diia.ui_base.components.infrastructure.utils.resource.UiText -import ua.gov.diia.ui_base.components.molecule.header.TitleGroupMlcData import ua.gov.diia.ui_base.components.molecule.text.TextLabelMlc import ua.gov.diia.ui_base.components.molecule.text.TextLabelMlcData import ua.gov.diia.ui_base.components.organism.header.TopGroupOrg @@ -89,66 +84,24 @@ fun CreatePinScreen( } } +private val testData = getPinTestData( + topGroupText = "Повторіть код з 4 цифр", + textWithParameters = "Цей код ви будете вводити для входу у застосунок Дія.", + pinLength = 5 +) + @Composable @Preview fun CreatePinScreenPreview() { - val _uiData = remember { mutableStateListOf() } - val uiData: SnapshotStateList = _uiData - _uiData.add( - TopGroupOrgData( - titleGroupMlcData = TitleGroupMlcData( - heroText = UiText.DynamicString("Повторіть код з 4 цифр"), - leftNavIcon = TitleGroupMlcData.LeftNavIcon( - code = DiiaResourceIcon.BACK.code, - accessibilityDescription = UiText.StringResource(R.string.accessibility_back_button), - action = DataActionWrapper( - type = "back", - subtype = null, - resource = null - ) - ) - ) - ) - ) - _uiData.add( - TextLabelMlcData( - text = UiText.DynamicString("Цей код ви будете вводити для входу у застосунок Дія.") - ) - ) - _uiData.add( - NumButtonTileOrganismData() - ) + val uiData = testData.asSnapshotStateList() + CreatePinScreen(data = uiData, onUIAction = { }) } @Composable @Preview(name = "phone", device = "spec:shape=Normal,width=360,height=640,unit=dp,dpi=480") fun CreatePinScreenPreview_small_screen() { - val _uiData = remember { mutableStateListOf() } - val uiData: SnapshotStateList = _uiData - _uiData.add( - TopGroupOrgData( - titleGroupMlcData = TitleGroupMlcData( - heroText = UiText.DynamicString("Повторіть код з 4 цифр"), - leftNavIcon = TitleGroupMlcData.LeftNavIcon( - code = DiiaResourceIcon.BACK.code, - accessibilityDescription = UiText.StringResource(R.string.accessibility_back_button), - action = DataActionWrapper( - type = "back", - subtype = null, - resource = null - ) - ) - ) - ) - ) - _uiData.add( - TextWithParametersData( - text = UiText.DynamicString("Цей код ви будете вводити для входу у застосунок Дія.") - ) - ) - _uiData.add( - NumButtonTileOrganismData(pinLength = 5) - ) + val uiData = testData.asSnapshotStateList() + CreatePinScreen(data = uiData, onUIAction = { }) } \ No newline at end of file diff --git a/pin/src/main/java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt b/pin/src/main/java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt index 0a289a3..49ed843 100644 --- a/pin/src/main/java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt +++ b/pin/src/main/java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt @@ -2,31 +2,15 @@ package ua.gov.diia.pin.ui.reset.compose import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.runtime.Composable -import androidx.compose.runtime.mutableStateListOf -import androidx.compose.runtime.remember import androidx.compose.runtime.snapshots.SnapshotStateList import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.paint -import androidx.compose.ui.layout.ContentScale -import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview -import androidx.constraintlayout.compose.ConstraintLayout -import ua.gov.diia.ui_base.R -import ua.gov.diia.ui_base.components.DiiaResourceIcon -import ua.gov.diia.ui_base.components.infrastructure.DataActionWrapper +import ua.gov.diia.pin.ui.asSnapshotStateList +import ua.gov.diia.pin.ui.create.compose.CreatePinScreen +import ua.gov.diia.pin.ui.getPinTestData import ua.gov.diia.ui_base.components.infrastructure.UIElementData import ua.gov.diia.ui_base.components.infrastructure.event.UIAction -import ua.gov.diia.ui_base.components.infrastructure.utils.resource.UiText -import ua.gov.diia.ui_base.components.molecule.header.TitleGroupMlcData -import ua.gov.diia.ui_base.components.molecule.text.TextLabelMlc -import ua.gov.diia.ui_base.components.molecule.text.TextLabelMlcData -import ua.gov.diia.ui_base.components.organism.header.TopGroupOrg -import ua.gov.diia.ui_base.components.organism.header.TopGroupOrgData -import ua.gov.diia.ui_base.components.organism.tile.NumButtonTileOrganism -import ua.gov.diia.ui_base.components.organism.tile.NumButtonTileOrganismData -import ua.gov.diia.ui_base.components.provideTestTagsAsResourceId import ua.gov.diia.ui_base.components.subatomic.loader.TridentLoaderWithUIBlocking @@ -38,123 +22,32 @@ fun ResetPinScreen( onUIAction: (UIAction) -> Unit ) { Box(modifier = Modifier.fillMaxSize()) { - ConstraintLayout( - modifier = modifier - .fillMaxSize() - .paint( - painterResource(id = R.drawable.bg_blue_yellow_gradient), - contentScale = ContentScale.FillBounds - ) - .safeDrawingPadding() - .provideTestTagsAsResourceId() - ) { - val title = createRef() - val numButton = createRef() - val descriptionText = createRef() - data.forEach { item -> - if (item is TopGroupOrgData) { - TopGroupOrg( - modifier = modifier - .constrainAs(title) { - start.linkTo(parent.start) - end.linkTo(parent.end) - top.linkTo(parent.top) - }, - data = item, - onUIAction = onUIAction - ) - } - - if (item is TextLabelMlcData) { - TextLabelMlc( - modifier = modifier - .constrainAs(descriptionText) { - top.linkTo(title.bottom) - }, - data = item, - onUIAction = onUIAction - ) - } - if (item is NumButtonTileOrganismData) { - NumButtonTileOrganism( - modifier = Modifier - .constrainAs(numButton) { - linkTo( - top = descriptionText.bottom, - bottom = parent.bottom, - bias = 0.3f - ) - }, - data = item, - onUIAction = onUIAction - ) - } - } - } + CreatePinScreen( + modifier = modifier, + data = data, + onUIAction = onUIAction + ) TridentLoaderWithUIBlocking(contentLoaded = contentLoaded) } } +private val testData = getPinTestData( + topGroupText = "Повторіть код з 4 цифр", + textWithParameters = "Щоб впевнитися, що це ви змінюєте код для входу.", +) + @Composable @Preview fun ResetPinScreenPreview() { - val _uiData = remember { mutableStateListOf() } - val uiData: SnapshotStateList = _uiData - _uiData.add( - TopGroupOrgData( - titleGroupMlcData = TitleGroupMlcData( - heroText = UiText.DynamicString("Повторіть код з 4 цифр"), - leftNavIcon = TitleGroupMlcData.LeftNavIcon( - code = DiiaResourceIcon.BACK.code, - accessibilityDescription = UiText.StringResource(R.string.accessibility_back_button), - action = DataActionWrapper( - type = "back", - subtype = null, - resource = null - ) - ) - ) - ) - ) - _uiData.add( - TextLabelMlcData( - text = UiText.DynamicString("Щоб впевнитися, що це ви змінюєте код для входу.") - ) - ) - _uiData.add( - NumButtonTileOrganismData() - ) + val uiData = testData.asSnapshotStateList() + ResetPinScreen(data = uiData, contentLoaded = "" to true, onUIAction = { }) } @Composable @Preview(name = "phone", device = "spec:shape=Normal,width=360,height=640,unit=dp,dpi=480") fun ResetPinScreenPreview_small_screen() { - val _uiData = remember { mutableStateListOf() } - val uiData: SnapshotStateList = _uiData - _uiData.add( - TopGroupOrgData( - titleGroupMlcData = TitleGroupMlcData( - heroText = UiText.DynamicString("Повторіть код з 4 цифр"), - leftNavIcon = TitleGroupMlcData.LeftNavIcon( - code = DiiaResourceIcon.BACK.code, - accessibilityDescription = UiText.StringResource(R.string.accessibility_back_button), - action = DataActionWrapper( - type = "back", - subtype = null, - resource = null - ) - ) - ) - ) - ) - _uiData.add( - TextLabelMlcData( - text = UiText.DynamicString("Щоб впевнитися, що це ви змінюєте код для входу.") - ) - ) - _uiData.add( - NumButtonTileOrganismData() - ) + val uiData = testData.asSnapshotStateList() + ResetPinScreen(data = uiData, contentLoaded = "" to true, onUIAction = { }) } \ No newline at end of file From 901918d5a8f7e4ddfc77b3d11312070e61867adb Mon Sep 17 00:00:00 2001 From: omaeewa Date: Mon, 26 Aug 2024 10:18:08 +0300 Subject: [PATCH 2/2] Refactor: Remove duplicate code in PIN screen --- pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt | 6 +++--- .../ua/gov/diia/pin/ui/create/compose/CreatePinScreen.kt | 4 ++-- .../java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt b/pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt index 94ee3b3..0ce2252 100644 --- a/pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt +++ b/pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt @@ -16,10 +16,10 @@ import ua.gov.diia.ui_base.components.organism.tile.NumButtonTileOrganismData inline fun List.asSnapshotStateList() = remember { mutableStateListOf(*this.toTypedArray()) } -fun getPinTestData(topGroupText: String, textWithParameters: String, pinLength: Int = 4) = listOf( +fun getPinTestData(headerText: String, bodyText: String, pinLength: Int = 4) = listOf( TopGroupOrgData( titleGroupMlcData = TitleGroupMlcData( - heroText = UiText.DynamicString(topGroupText), + heroText = UiText.DynamicString(headerText), leftNavIcon = TitleGroupMlcData.LeftNavIcon( code = DiiaResourceIcon.BACK.code, accessibilityDescription = UiText.StringResource(R.string.accessibility_back_button), @@ -32,7 +32,7 @@ fun getPinTestData(topGroupText: String, textWithParameters: String, pinLength: ) ), TextWithParametersData( - text = UiText.DynamicString(textWithParameters) + text = UiText.DynamicString(bodyText) ), NumButtonTileOrganismData(pinLength = pinLength) ) \ No newline at end of file diff --git a/pin/src/main/java/ua/gov/diia/pin/ui/create/compose/CreatePinScreen.kt b/pin/src/main/java/ua/gov/diia/pin/ui/create/compose/CreatePinScreen.kt index 3685a28..5f3b05f 100644 --- a/pin/src/main/java/ua/gov/diia/pin/ui/create/compose/CreatePinScreen.kt +++ b/pin/src/main/java/ua/gov/diia/pin/ui/create/compose/CreatePinScreen.kt @@ -85,8 +85,8 @@ fun CreatePinScreen( } private val testData = getPinTestData( - topGroupText = "Повторіть код з 4 цифр", - textWithParameters = "Цей код ви будете вводити для входу у застосунок Дія.", + headerText = "Повторіть код з 4 цифр", + bodyText = "Цей код ви будете вводити для входу у застосунок Дія.", pinLength = 5 ) diff --git a/pin/src/main/java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt b/pin/src/main/java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt index 49ed843..5af8527 100644 --- a/pin/src/main/java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt +++ b/pin/src/main/java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt @@ -32,8 +32,8 @@ fun ResetPinScreen( } private val testData = getPinTestData( - topGroupText = "Повторіть код з 4 цифр", - textWithParameters = "Щоб впевнитися, що це ви змінюєте код для входу.", + headerText = "Повторіть код з 4 цифр", + bodyText = "Щоб впевнитися, що це ви змінюєте код для входу.", ) @Composable