Skip to content

Commit

Permalink
[#804] Provide OdsHorizontalCard XML version
Browse files Browse the repository at this point in the history
  • Loading branch information
paulinea committed Feb 2, 2024
1 parent 2ccc160 commit b9577f2
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<Drawable?>(null)
var imageContentDescription by mutableStateOf("")
var title by mutableStateOf("")
var subtitle by mutableStateOf<String?>(null)
var text by mutableStateOf<String?>(null)
var firstButtonText by mutableStateOf<String?>(null)
var onFirstButtonClick by mutableStateOf<(() -> Unit)?>(null)
var secondButtonText by mutableStateOf<String?>(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
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
14 changes: 14 additions & 0 deletions lib-xml/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,18 @@
<attr name="displaySurface" />
</declare-styleable>

<declare-styleable name="OdsHorizontalCard">
<attr name="title" format="string" />
<attr name="subtitle" format="string" />
<attr name="text" />
<attr name="image" />
<attr name="imageContentDescription" />
<attr name="firstButtonText" />
<attr name="secondButtonText" />
<!-- BE CAREFUL: If this enum change, don't forget to update associated extension -->
<attr name="odsHorizontalCardImagePosition" format="enum">
<enum name="start" value="0" />
<enum name="end" value="1" />
</attr>
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ object OdsCard {
}

enum class Position {
Start, End
Start, End;

companion object
}
}

Expand Down

0 comments on commit b9577f2

Please sign in to comment.