Skip to content

Commit

Permalink
Merge pull request #600 from Orange-OpenSource/597-update-odscard-api
Browse files Browse the repository at this point in the history
597 - Update OdsCard APIs
  • Loading branch information
paulinea authored Sep 21, 2023
2 parents c0c7805 + 361ef97 commit 0e8cb9d
Show file tree
Hide file tree
Showing 29 changed files with 589 additions and 473 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.ui.res.stringResource
import com.orange.ods.app.R
import com.orange.ods.app.ui.LocalMainTopAppBarManager
import com.orange.ods.app.ui.utilities.DrawableManager
import com.orange.ods.compose.component.card.OdsCardImage
import com.orange.ods.compose.component.card.OdsSmallCard
import com.orange.ods.extension.orElse

Expand Down Expand Up @@ -67,12 +68,13 @@ private fun RowScope.ComponentCard(component: Component, onComponentClick: (Long
OdsSmallCard(
modifier = Modifier.weight(0.5f),
title = stringResource(id = component.titleRes),
image = painterResource(id = smallImageResId.orElse { imageResId }),
imageBackgroundColor = Color(Component.ImageBackgroundColor),
imageContentScale = ContentScale.Fit,
imageAlignment = component.imageAlignment,
onCardClick = {
onComponentClick(component.id)
},
image = OdsCardImage(
painterResource(id = smallImageResId.orElse { imageResId }),
"",
component.imageAlignment,
ContentScale.Fit,
Color(Component.ImageBackgroundColor)
),
onClick = { onComponentClick(component.id) },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ fun rememberCardCustomizationState(
thumbnailChecked: MutableState<Boolean> = rememberSaveable { mutableStateOf(true) },
textChecked: MutableState<Boolean> = rememberSaveable { mutableStateOf(true) },
subtitleChecked: MutableState<Boolean> = rememberSaveable { mutableStateOf(true) },
actionButtonCount: MutableState<Int> = rememberSaveable { mutableStateOf(CardCustomizationState.MinActionButtonCount) },
buttonCount: MutableState<Int> = rememberSaveable { mutableStateOf(CardCustomizationState.MinButtonCount) },
dividerChecked: MutableState<Boolean> = rememberSaveable { mutableStateOf(true) },
imagePosition: MutableState<OdsHorizontalCardImagePosition> = rememberSaveable { mutableStateOf(OdsHorizontalCardImagePosition.Start) }
) =
remember(clickable, thumbnailChecked, textChecked, subtitleChecked, actionButtonCount, dividerChecked, imagePosition) {
CardCustomizationState(clickable, thumbnailChecked, textChecked, subtitleChecked, actionButtonCount, dividerChecked, imagePosition)
remember(clickable, thumbnailChecked, textChecked, subtitleChecked, buttonCount, dividerChecked, imagePosition) {
CardCustomizationState(clickable, thumbnailChecked, textChecked, subtitleChecked, buttonCount, dividerChecked, imagePosition)
}

class CardCustomizationState(
val clickable: MutableState<Boolean>,
val thumbnailChecked: MutableState<Boolean>,
val textChecked: MutableState<Boolean>,
val subtitleChecked: MutableState<Boolean>,
val actionButtonCount: MutableState<Int>,
val buttonCount: MutableState<Int>,
val dividerChecked: MutableState<Boolean>,
val imagePosition: MutableState<OdsHorizontalCardImagePosition>
) {

companion object {
const val MinActionButtonCount = 0
const val MaxActionButtonCount = 2
const val MinButtonCount = 0
const val MaxButtonCount = 2
}

val isClickable
Expand All @@ -58,12 +58,12 @@ class CardCustomizationState(
val hasSubtitle
get() = subtitleChecked.value

val hasButton1
get() = actionButtonCount.value > 0
val hasFirstButton
get() = buttonCount.value > 0

val hasButton2
get() = actionButtonCount.value > 1
val hasSecondButton
get() = buttonCount.value > 1

val hasDivider
get() = if (!hasButton1) false else dividerChecked.value
get() = if (!hasFirstButton) false else dividerChecked.value
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package com.orange.ods.app.ui.components.cards

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
Expand All @@ -31,67 +30,77 @@ import com.orange.ods.app.ui.utilities.DrawableManager
import com.orange.ods.app.ui.utilities.composable.CodeImplementationColumn
import com.orange.ods.app.ui.utilities.composable.FunctionCallCode
import com.orange.ods.compose.OdsComposable
import com.orange.ods.compose.component.card.OdsCardButton
import com.orange.ods.compose.component.card.OdsCardImage
import com.orange.ods.compose.component.card.OdsHorizontalCard

@Composable
fun CardHorizontal(customizationState: CardCustomizationState) {
val context = LocalContext.current
val recipes = LocalRecipes.current
val recipe = rememberSaveable { recipes.filter { it.description.isNotBlank() }.random() }

with(customizationState) {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(state = rememberScrollState())
.padding(dimensionResource(id = com.orange.ods.R.dimen.spacing_m))
.verticalScroll(state = rememberScrollState()),
) {
val button1Text = stringResource(id = R.string.component_element_first_button)
val button2Text = stringResource(id = R.string.component_element_second_button)
val firstButtonText = stringResource(id = R.string.component_element_first_button)
val secondButtonText = stringResource(id = R.string.component_element_second_button)
val cardText = stringResource(id = R.string.component_card_element_card)

OdsHorizontalCard(
title = recipe.title,
image = rememberAsyncImagePainter(
model = recipe.imageUrl,
placeholder = painterResource(id = DrawableManager.getPlaceholderResId()),
error = painterResource(id = DrawableManager.getPlaceholderResId(error = true))
image = OdsCardImage(
rememberAsyncImagePainter(
model = recipe.imageUrl,
placeholder = painterResource(id = DrawableManager.getPlaceholderResId()),
error = painterResource(id = DrawableManager.getPlaceholderResId(error = true))
),
""
),
subtitle = if (hasSubtitle) recipe.subtitle else null,
text = if (hasText) recipe.description else null,
onCardClick = if (isClickable) {
onClick = if (isClickable) {
{ clickOnElement(context, cardText) }
} else null,
button1Text = if (hasButton1) button1Text else null,
onButton1Click = { clickOnElement(context, button1Text) },
button2Text = if (hasButton2) button2Text else null,
onButton2Click = { clickOnElement(context, button2Text) },
firstButton = if (hasFirstButton) OdsCardButton(firstButtonText) { clickOnElement(context, firstButtonText) } else null,
secondButton = if (hasSecondButton) OdsCardButton(secondButtonText) { clickOnElement(context, secondButtonText) } else null,
imagePosition = imagePosition.value,
dividerEnabled = hasDivider
divider = hasDivider
)

Spacer(modifier = Modifier.padding(top = dimensionResource(com.orange.ods.R.dimen.spacing_s)))

CodeImplementationColumn {
CodeImplementationColumn(
modifier = Modifier.padding(top = dimensionResource(com.orange.ods.R.dimen.spacing_s))
) {
FunctionCallCode(
name = OdsComposable.OdsHorizontalCard.name,
exhaustiveParameters = false,
parameters = {
simple("imagePosition", imagePosition.value.name)
title(recipe.title)
image()
classInstance("image", OdsCardImage::class.java) {
painter()
contentDescription("")
}
if (hasSubtitle) subtitle(recipe.subtitle)
if (hasText) cardText()
if (isClickable) onCardClick()
if (hasButton1) {
button1Text(button1Text)
onButton1Click()
if (isClickable) onClick()
if (hasFirstButton) {
classInstance("firstButton", OdsCardButton::class.java) {
text(firstButtonText)
onClick()
}
}
if (hasButton2) {
button2Text(button2Text)
onButton2Click()
if (hasSecondButton) {
classInstance("secondButton", OdsCardButton::class.java) {
text(secondButtonText)
onClick()
}
}
if (!hasDivider && (hasButton1 || hasButton2)) stringRepresentation("dividerEnabled", hasDivider)
simple("imagePosition", imagePosition.value.name)
if (!hasDivider && (hasFirstButton || hasSecondButton)) stringRepresentation("divider", hasDivider)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
Expand All @@ -34,6 +33,7 @@ import com.orange.ods.app.ui.utilities.DrawableManager
import com.orange.ods.app.ui.utilities.composable.CodeImplementationColumn
import com.orange.ods.app.ui.utilities.composable.FunctionCallCode
import com.orange.ods.compose.OdsComposable
import com.orange.ods.compose.component.card.OdsCardImage
import com.orange.ods.compose.component.card.OdsSmallCard

@Composable
Expand All @@ -46,8 +46,8 @@ fun CardSmall(customizationState: CardCustomizationState) {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(state = rememberScrollState())
.padding(dimensionResource(id = com.orange.ods.R.dimen.spacing_m))
.verticalScroll(state = rememberScrollState()),
) {
Row(
modifier = Modifier
Expand All @@ -58,31 +58,37 @@ fun CardSmall(customizationState: CardCustomizationState) {

OdsSmallCard(
modifier = Modifier.weight(0.5f),
image = rememberAsyncImagePainter(
model = recipe.imageUrl,
placeholder = painterResource(id = DrawableManager.getPlaceholderResId()),
error = painterResource(id = DrawableManager.getPlaceholderResId(error = true))
image = OdsCardImage(
rememberAsyncImagePainter(
model = recipe.imageUrl,
placeholder = painterResource(id = DrawableManager.getPlaceholderResId()),
error = painterResource(id = DrawableManager.getPlaceholderResId(error = true)),
),
""
),
title = recipe.title,
subtitle = if (subtitleChecked.value) recipe.subtitle else null,
onCardClick = if (isClickable) {
onClick = if (isClickable) {
{ clickOnElement(context, cardText) }
} else null
)
Box(modifier = Modifier.weight(0.5f))
}

Spacer(modifier = Modifier.padding(top = dimensionResource(com.orange.ods.R.dimen.spacing_s)))

CodeImplementationColumn {
CodeImplementationColumn(
modifier = Modifier.padding(top = dimensionResource(com.orange.ods.R.dimen.spacing_s))
) {
FunctionCallCode(
name = OdsComposable.OdsSmallCard.name,
exhaustiveParameters = false,
parameters = {
title(recipe.title)
image()
classInstance("image", OdsCardImage::class.java) {
painter()
contentDescription("")
}
if (hasSubtitle) subtitle(recipe.subtitle)
if (isClickable) onCardClick()
if (isClickable) onClick()
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package com.orange.ods.app.ui.components.cards

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
Expand All @@ -33,6 +32,9 @@ import com.orange.ods.app.ui.utilities.DrawableManager
import com.orange.ods.app.ui.utilities.composable.CodeImplementationColumn
import com.orange.ods.app.ui.utilities.composable.FunctionCallCode
import com.orange.ods.compose.OdsComposable
import com.orange.ods.compose.component.card.OdsCardButton
import com.orange.ods.compose.component.card.OdsCardImage
import com.orange.ods.compose.component.card.OdsCardThumbnail
import com.orange.ods.compose.component.card.OdsVerticalHeaderFirstCard

@Composable
Expand All @@ -45,11 +47,11 @@ fun CardVerticalHeaderFirst(customizationState: CardCustomizationState) {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(state = rememberScrollState())
.padding(dimensionResource(id = com.orange.ods.R.dimen.spacing_m))
.verticalScroll(state = rememberScrollState()),
) {
val button1Text = stringResource(id = R.string.component_element_first_button)
val button2Text = stringResource(id = R.string.component_element_second_button)
val firstButtonText = stringResource(id = R.string.component_element_first_button)
val secondButtonText = stringResource(id = R.string.component_element_second_button)
val cardText = stringResource(id = R.string.component_card_element_card)
val imagePainter = rememberAsyncImagePainter(
model = ImageRequest.Builder(context)
Expand All @@ -62,39 +64,49 @@ fun CardVerticalHeaderFirst(customizationState: CardCustomizationState) {

OdsVerticalHeaderFirstCard(
title = recipe.title,
image = imagePainter,
thumbnail = if (hasThumbnail) imagePainter else null,
image = OdsCardImage(imagePainter, ""),
thumbnail = if (hasThumbnail) OdsCardThumbnail(imagePainter, "") else null,
subtitle = if (hasSubtitle) recipe.subtitle else null,
text = if (hasText) recipe.description else null,
onCardClick = if (isClickable) {
onClick = if (isClickable) {
{ clickOnElement(context, cardText) }
} else null,
button1Text = if (hasButton1) button1Text else null,
onButton1Click = { clickOnElement(context, button1Text) },
button2Text = if (hasButton2) button2Text else null,
onButton2Click = { clickOnElement(context, button2Text) }
firstButton = if (hasFirstButton) OdsCardButton(firstButtonText) { clickOnElement(context, firstButtonText) } else null,
secondButton = if (hasSecondButton) OdsCardButton(secondButtonText) { clickOnElement(context, secondButtonText) } else null,
)

Spacer(modifier = Modifier.padding(top = dimensionResource(com.orange.ods.R.dimen.spacing_s)))

CodeImplementationColumn {
CodeImplementationColumn(
modifier = Modifier.padding(top = dimensionResource(com.orange.ods.R.dimen.spacing_s))
) {
FunctionCallCode(
name = OdsComposable.OdsVerticalHeaderFirstCard.name,
exhaustiveParameters = false,
parameters = {
title(recipe.title)
image()
if (hasThumbnail) simple("thumbnail", "<thumbnail painter>")
classInstance("image", OdsCardImage::class.java) {
painter()
contentDescription("")
}
if (hasThumbnail) {
classInstance("thumbnail", OdsCardThumbnail::class.java) {
painter()
contentDescription("")
}
}
if (hasSubtitle) subtitle(recipe.subtitle)
if (hasText) cardText()
if (isClickable) onCardClick()
if (hasButton1) {
button1Text(button1Text)
onButton1Click()
if (isClickable) onClick()
if (hasFirstButton) {
classInstance("firstButton", OdsCardButton::class.java) {
text(firstButtonText)
onClick()
}
}
if (hasButton2) {
button2Text(button2Text)
onButton2Click()
if (hasSecondButton) {
classInstance("secondButton", OdsCardButton::class.java) {
text(secondButtonText)
onClick()
}
}
}
)
Expand Down
Loading

0 comments on commit 0e8cb9d

Please sign in to comment.