From f1f1d21c7e7760b07d35c19df2b70df84537f464 Mon Sep 17 00:00:00 2001 From: David Hontiveros Gordo Date: Wed, 27 Nov 2024 12:29:20 +0100 Subject: [PATCH] dhonti/ANDROID-15405-postercard: Update top actions management in code --- .../ui/compose/components/PosterCards.kt | 18 ++++++++---------- .../compose/card/postercard/PosterCard.kt | 15 ++++++++------- .../card/postercard/PosterCardPreviews.kt | 7 +++---- .../card/postercard/PosterCardTopActions.kt | 15 +++++++++++---- .../mistica/compose/card/postercard/README.md | 14 ++------------ 5 files changed, 32 insertions(+), 37 deletions(-) diff --git a/catalog/src/main/java/com/telefonica/mistica/catalog/ui/compose/components/PosterCards.kt b/catalog/src/main/java/com/telefonica/mistica/catalog/ui/compose/components/PosterCards.kt index 615421def..74994342c 100644 --- a/catalog/src/main/java/com/telefonica/mistica/catalog/ui/compose/components/PosterCards.kt +++ b/catalog/src/main/java/com/telefonica/mistica/catalog/ui/compose/components/PosterCards.kt @@ -143,12 +143,13 @@ fun PosterCards() { title = title.getOrNullIfEmpty(), subtitle = subtitle.getOrNullIfEmpty(), description = description.getOrNullIfEmpty(), + firstTopAction = topActionsType.info?.firstTopAction, + secondTopAction = topActionsType.info?.secondTopAction, customContent = { if (withAdditionalContent) { AdditionalContent() } - }, - topActionsList = topActionsType.info?.topActionsList + } ) } } @@ -193,23 +194,20 @@ private enum class TopActionsType(val info: PosterCardTopActionInfo? = null) { NONE, ONE_ACTION_DISMISS( info = PosterCardTopActionInfo( - topActionsList = listOf( - TopActionData(iconRes = R.drawable.ic_close_regular) - ) + firstTopAction = TopActionData(iconRes = R.drawable.ic_close_regular) ) ), TWO_ACTIONS( info = PosterCardTopActionInfo( - topActionsList = listOf( - TopActionData(iconRes = R.drawable.icn_visibility), - TopActionData(iconRes = R.drawable.ic_close_regular) - ) + firstTopAction = TopActionData(iconRes = R.drawable.icn_visibility), + secondTopAction = TopActionData(iconRes = R.drawable.ic_close_regular) ) ) } private data class PosterCardTopActionInfo( - val topActionsList: List? = null, + val firstTopAction: TopActionData? = null, + val secondTopAction: TopActionData? = null ) @Composable diff --git a/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCard.kt b/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCard.kt index 1c1c2e293..444409540 100644 --- a/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCard.kt +++ b/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCard.kt @@ -28,10 +28,13 @@ fun PosterCard( title: String? = null, subtitle: String? = null, description: String? = null, - topActionsList: List? = null, + firstTopAction: TopActionData? = null, + secondTopAction: TopActionData? = null, onClickAction: (() -> Unit)? = null, customContent: (@Composable () -> Unit)? = null, ) { + val anyTopActionsLoaded = firstTopAction!=null || secondTopAction!=null + BoxWithConstraints(modifier = modifier) { androidx.compose.material.Card( elevation = 0.dp, @@ -51,7 +54,7 @@ fun PosterCard( modifier = Modifier.align(alignment = Alignment.BottomCenter), verticalArrangement = Arrangement.Bottom ) { - if (topActionsList.areLoaded()) { + if (anyTopActionsLoaded) { Spacer(modifier = Modifier.height(40.dp)) } PosterCardMainContent( @@ -64,10 +67,11 @@ fun PosterCard( customContent = customContent ) } - if (topActionsList.areLoaded()) { + if (anyTopActionsLoaded) { PosterCardTopActions( modifier = Modifier.align(alignment = Alignment.TopCenter), - topActionsList = topActionsList!! + firstTopAction = firstTopAction, + secondTopAction = secondTopAction ) } } @@ -75,9 +79,6 @@ fun PosterCard( } } -private fun List?.areLoaded(): Boolean = - !this.isNullOrEmpty() && size <= 2 - enum class PosterCardAspectRatio(val ratio: Float) { AR_AUTO(ratio = Float.NaN), AR_1_1(ratio = 1f), diff --git a/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCardPreviews.kt b/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCardPreviews.kt index 922cca3a0..b8cf7a4e1 100644 --- a/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCardPreviews.kt +++ b/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCardPreviews.kt @@ -42,10 +42,8 @@ internal fun PosterCardSolidColor() { internal fun PosterCardGradientColor() { MisticaTheme(brand = TuBrand) { PosterCard( - topActionsList = listOf( - TopActionData(iconRes = R.drawable.icn_visibility), - TopActionData(iconRes = R.drawable.ic_close_regular) - ), + firstTopAction = TopActionData(iconRes = R.drawable.icn_visibility), + secondTopAction = TopActionData(iconRes = R.drawable.ic_close_regular), aspectRatio = PosterCardAspectRatio.AR_16_9, backgroundType = PosterCardBackgroundType.Color( brush = Brush.verticalGradient(colors = listOf(Color.Magenta, Color.Red)), @@ -65,6 +63,7 @@ internal fun PosterCardGradientColor() { internal fun PosterCardImage() { MisticaTheme(brand = TuBrand) { PosterCard( + firstTopAction = TopActionData(iconRes = R.drawable.icn_visibility), modifier = Modifier.fillMaxWidth(), aspectRatio = PosterCardAspectRatio.AR_16_9, backgroundType = PosterCardBackgroundType.Image(imageResource = R.drawable.sample_background), diff --git a/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCardTopActions.kt b/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCardTopActions.kt index ea950ed79..066e6c917 100644 --- a/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCardTopActions.kt +++ b/library/src/main/java/com/telefonica/mistica/compose/card/postercard/PosterCardTopActions.kt @@ -26,7 +26,11 @@ import androidx.compose.ui.unit.dp import com.telefonica.mistica.compose.theme.MisticaTheme @Composable -internal fun PosterCardTopActions(modifier: Modifier = Modifier, topActionsList: List) { +internal fun PosterCardTopActions( + modifier: Modifier = Modifier, + firstTopAction: TopActionData?, + secondTopAction: TopActionData? +) { Row( modifier = modifier .fillMaxWidth() @@ -34,11 +38,14 @@ internal fun PosterCardTopActions(modifier: Modifier = Modifier, topActionsList: horizontalArrangement = Arrangement.End, verticalAlignment = Alignment.CenterVertically ) { - topActionsList.forEachIndexed { index, topActionData -> - TopAction(topActionData = topActionData) - if (index != topActionsList.lastIndex) { + firstTopAction?.let { + TopAction(topActionData = it) + } + secondTopAction?.let { + if (firstTopAction != null) { Spacer(modifier = Modifier.width(16.dp)) } + TopAction(topActionData = it) } } } diff --git a/library/src/main/java/com/telefonica/mistica/compose/card/postercard/README.md b/library/src/main/java/com/telefonica/mistica/compose/card/postercard/README.md index 11526276a..3a166775b 100644 --- a/library/src/main/java/com/telefonica/mistica/compose/card/postercard/README.md +++ b/library/src/main/java/com/telefonica/mistica/compose/card/postercard/README.md @@ -12,18 +12,8 @@ In addition, up to a maximum of 2 actions can be defined, which will be displaye ```kotlin PosterCard( - topActionsList = listOf( - TopActionData( - iconRes = R.drawable.icn_visibility, - backgroundColor = MisticaTheme.colors.backgroundAlternative, - iconTint = MisticaTheme.colors.brand, - ), - TopActionData( - iconRes = R.drawable.ic_close_regular, - backgroundColor = MisticaTheme.colors.backgroundAlternative, - iconTint = MisticaTheme.colors.brand, - ) - ), + firstTopAction = TopActionData(iconRes = R.drawable.icn_visibility), + secondTopAction = TopActionData(iconRes = R.drawable.ic_close_regular), aspectRatio = PosterCardAspectRatio.AR_16_9, backgroundType = PosterCardBackgroundType.Color(brush = SolidColor(MisticaTheme.colors.background), inverseDisplay = false), headline = Tag(content = "Headline"),