From b9577f2b9941249c185eb4ee5e1b961b49cf65f5 Mon Sep 17 00:00:00 2001 From: Pauline Auvray Date: Fri, 2 Feb 2024 17:11:42 +0100 Subject: [PATCH] [#804] Provide OdsHorizontalCard XML version --- .../xml/component/card/OdsHorizontalCard.kt | 87 +++++++++++++++++++ .../ods/xml/utilities/extension/OdsCardExt.kt | 31 +++++++ lib-xml/src/main/res/values/attrs.xml | 14 +++ .../compose/component/card/OdsCardsCommon.kt | 4 +- 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 lib-xml/src/main/java/com/orange/ods/xml/component/card/OdsHorizontalCard.kt create mode 100644 lib-xml/src/main/java/com/orange/ods/xml/utilities/extension/OdsCardExt.kt diff --git a/lib-xml/src/main/java/com/orange/ods/xml/component/card/OdsHorizontalCard.kt b/lib-xml/src/main/java/com/orange/ods/xml/component/card/OdsHorizontalCard.kt new file mode 100644 index 000000000..dd9ead1f6 --- /dev/null +++ b/lib-xml/src/main/java/com/orange/ods/xml/component/card/OdsHorizontalCard.kt @@ -0,0 +1,87 @@ +/* + * Software Name: Orange Design System + * SPDX-FileCopyrightText: Copyright (c) Orange SA + * SPDX-License-Identifier: MIT + * + * This software is distributed under the MIT licence, + * the text of which is available at https://opensource.org/license/MIT/ + * or see the "LICENSE" file for more details. + * + * Software description: Android library of reusable graphical components + */ + +package com.orange.ods.xml.component.card + +import android.content.Context +import android.graphics.drawable.Drawable +import android.util.AttributeSet +import androidx.appcompat.content.res.AppCompatResources +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import androidx.core.content.withStyledAttributes +import androidx.databinding.BindingAdapter +import com.google.accompanist.drawablepainter.rememberDrawablePainter +import com.orange.ods.compose.component.card.OdsCard +import com.orange.ods.compose.component.card.OdsHorizontalCard +import com.orange.ods.extension.ifNotNull +import com.orange.ods.xml.R +import com.orange.ods.xml.component.OdsAbstractComposeView +import com.orange.ods.xml.utilities.extension.fromXmlAttrValue +import com.orange.ods.xml.utilities.extension.getResourceIdOrNull + +class OdsHorizontalCard @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : OdsAbstractComposeView(context, attrs) { + + var image by mutableStateOf(null) + var imageContentDescription by mutableStateOf("") + var title by mutableStateOf("") + var subtitle by mutableStateOf(null) + var text by mutableStateOf(null) + var firstButtonText by mutableStateOf(null) + var onFirstButtonClick by mutableStateOf<(() -> Unit)?>(null) + var secondButtonText by mutableStateOf(null) + var onSecondButtonClick by mutableStateOf<(() -> Unit)?>(null) + var imagePosition by mutableStateOf(OdsCard.Image.Position.Start) + + init { + context.withStyledAttributes(attrs, R.styleable.OdsHorizontalCard) { + image = getResourceIdOrNull(R.styleable.OdsHorizontalCard_image)?.let { AppCompatResources.getDrawable(context, it) } + imageContentDescription = getString(R.styleable.OdsHorizontalCard_imageContentDescription).orEmpty() + title = getString(R.styleable.OdsHorizontalCard_title).orEmpty() + subtitle = getString(R.styleable.OdsHorizontalCard_subtitle) + text = getString(R.styleable.OdsHorizontalCard_text) + firstButtonText = getString(R.styleable.OdsHorizontalCard_firstButtonText) + secondButtonText = getString(R.styleable.OdsHorizontalCard_secondButtonText) + imagePosition = OdsCard.Image.Position.fromXmlAttrValue(getInteger(R.styleable.OdsHorizontalCard_odsHorizontalCardImagePosition, 0)) + } + } + + @Composable + override fun OdsContent() { + OdsHorizontalCard( + title = title, + image = OdsCard.Image(rememberDrawablePainter(drawable = image), imageContentDescription), + subtitle = subtitle, + text = text, + firstButton = ifNotNull(firstButtonText, onFirstButtonClick) { text, onClick -> + OdsCard.Button(text, onClick) + }, + secondButton = ifNotNull(secondButtonText, onSecondButtonClick) { text, onClick -> + OdsCard.Button(text, onClick) + }, + imagePosition = imagePosition + ) + + } + +} + +internal object OdsHorizontalCardBindingAdapter { + + @JvmStatic + @BindingAdapter("odsHorizontalCardImagePosition") + fun com.orange.ods.xml.component.card.OdsHorizontalCard.setOdsHorizontalCardImagePosition(position: OdsCard.Image.Position) { + this.imagePosition = position + } +} diff --git a/lib-xml/src/main/java/com/orange/ods/xml/utilities/extension/OdsCardExt.kt b/lib-xml/src/main/java/com/orange/ods/xml/utilities/extension/OdsCardExt.kt new file mode 100644 index 000000000..e5fab758e --- /dev/null +++ b/lib-xml/src/main/java/com/orange/ods/xml/utilities/extension/OdsCardExt.kt @@ -0,0 +1,31 @@ +/* + * Software Name: Orange Design System + * SPDX-FileCopyrightText: Copyright (c) Orange SA + * SPDX-License-Identifier: MIT + * + * This software is distributed under the MIT licence, + * the text of which is available at https://opensource.org/license/MIT/ + * or see the "LICENSE" file for more details. + * + * Software description: Android library of reusable graphical components + */ + +package com.orange.ods.xml.utilities.extension + +import com.orange.ods.compose.component.card.OdsCard + +/** + * @return [OdsCard.Image.Position] associated to the provided [xmlId] + * BE CAREFUL: If the enum values change you have to update associated XML attributes in the lib-xml. + */ +fun OdsCard.Image.Position.Companion.fromXmlAttrValue(xmlId: Int): OdsCard.Image.Position = OdsCard.Image.Position.entries[xmlId] + +/** + * XML enum value corresponding to this [OdsCard.Image.Position] + * BE CAREFUL: As there is no way to access XML enum names directly, if an enum name change, you have to update this method. + */ +val OdsCard.Image.Position.xmlEnumValue + get() = when (this) { + OdsCard.Image.Position.Start -> "start" + OdsCard.Image.Position.End -> "end" + } \ No newline at end of file diff --git a/lib-xml/src/main/res/values/attrs.xml b/lib-xml/src/main/res/values/attrs.xml index cfc8f6a0e..1f0e85694 100644 --- a/lib-xml/src/main/res/values/attrs.xml +++ b/lib-xml/src/main/res/values/attrs.xml @@ -84,4 +84,18 @@ + + + + + + + + + + + + + + diff --git a/lib/src/main/java/com/orange/ods/compose/component/card/OdsCardsCommon.kt b/lib/src/main/java/com/orange/ods/compose/component/card/OdsCardsCommon.kt index d1296c827..5ad30adcd 100644 --- a/lib/src/main/java/com/orange/ods/compose/component/card/OdsCardsCommon.kt +++ b/lib/src/main/java/com/orange/ods/compose/component/card/OdsCardsCommon.kt @@ -153,7 +153,9 @@ object OdsCard { } enum class Position { - Start, End + Start, End; + + companion object } }