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

Refactor: Remove duplicate code in PIN screen #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions pin/src/main/java/ua/gov/diia/pin/ui/Utils.kt
Original file line number Diff line number Diff line change
@@ -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 <reified T> List<T>.asSnapshotStateList() =
remember { mutableStateListOf(*this.toTypedArray()) }

fun getPinTestData(headerText: String, bodyText: String, pinLength: Int = 4) = listOf(
TopGroupOrgData(
titleGroupMlcData = TitleGroupMlcData(
heroText = UiText.DynamicString(headerText),
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(bodyText)
),
NumButtonTileOrganismData(pinLength = pinLength)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@ 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
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
Expand Down Expand Up @@ -89,66 +84,24 @@ fun CreatePinScreen(
}
}

private val testData = getPinTestData(
headerText = "Повторіть код з 4 цифр",
bodyText = "Цей код ви будете вводити для входу у застосунок Дія.",
pinLength = 5
)

@Composable
@Preview
fun CreatePinScreenPreview() {
val _uiData = remember { mutableStateListOf<UIElementData>() }
val uiData: SnapshotStateList<UIElementData> = _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<UIElementData>() }
val uiData: SnapshotStateList<UIElementData> = _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 = { })
}
141 changes: 17 additions & 124 deletions pin/src/main/java/ua/gov/diia/pin/ui/reset/compose/ResetPinScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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(
headerText = "Повторіть код з 4 цифр",
bodyText = "Щоб впевнитися, що це ви змінюєте код для входу.",
)

@Composable
@Preview
fun ResetPinScreenPreview() {
val _uiData = remember { mutableStateListOf<UIElementData>() }
val uiData: SnapshotStateList<UIElementData> = _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<UIElementData>() }
val uiData: SnapshotStateList<UIElementData> = _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 = { })
}