Skip to content

Commit

Permalink
dhonti/ANDROID-15405-postercard: Update top actions management in code
Browse files Browse the repository at this point in the history
  • Loading branch information
dhonti-axpe committed Nov 27, 2024
1 parent ef9ca70 commit f1f1d21
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
}
}
Expand Down Expand Up @@ -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<TopActionData>? = null,
val firstTopAction: TopActionData? = null,
val secondTopAction: TopActionData? = null
)

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ fun PosterCard(
title: String? = null,
subtitle: String? = null,
description: String? = null,
topActionsList: List<TopActionData>? = 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,
Expand All @@ -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(
Expand All @@ -64,20 +67,18 @@ fun PosterCard(
customContent = customContent
)
}
if (topActionsList.areLoaded()) {
if (anyTopActionsLoaded) {
PosterCardTopActions(
modifier = Modifier.align(alignment = Alignment.TopCenter),
topActionsList = topActionsList!!
firstTopAction = firstTopAction,
secondTopAction = secondTopAction
)
}
}
}
}
}

private fun List<TopActionData>?.areLoaded(): Boolean =
!this.isNullOrEmpty() && size <= 2

enum class PosterCardAspectRatio(val ratio: Float) {
AR_AUTO(ratio = Float.NaN),
AR_1_1(ratio = 1f),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,26 @@ import androidx.compose.ui.unit.dp
import com.telefonica.mistica.compose.theme.MisticaTheme

@Composable
internal fun PosterCardTopActions(modifier: Modifier = Modifier, topActionsList: List<TopActionData>) {
internal fun PosterCardTopActions(
modifier: Modifier = Modifier,
firstTopAction: TopActionData?,
secondTopAction: TopActionData?
) {
Row(
modifier = modifier
.fillMaxWidth()
.padding(top = 16.dp, start = 16.dp, end = 16.dp, bottom = 8.dp),
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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit f1f1d21

Please sign in to comment.