Skip to content

Commit

Permalink
[#540] Display OdsButton XML code implementation for primary version
Browse files Browse the repository at this point in the history
  • Loading branch information
paulinea committed Jan 22, 2024
1 parent 6f31f65 commit 04e4bb1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ import androidx.compose.ui.res.stringResource
import com.orange.ods.app.R
import com.orange.ods.app.databinding.OdsButtonBinding
import com.orange.ods.app.ui.UiFramework
import com.orange.ods.app.ui.utilities.code.CodeBackgroundColumn
import com.orange.ods.app.ui.utilities.code.CodeImplementationColumn
import com.orange.ods.app.ui.utilities.code.FunctionCallCode
import com.orange.ods.app.ui.utilities.code.XmlViewTag
import com.orange.ods.app.ui.utilities.composable.Title
import com.orange.ods.compose.OdsComposable
import com.orange.ods.compose.component.button.OdsButton
import com.orange.ods.compose.theme.OdsDisplaySurface
import com.orange.ods.xml.utilities.extension.getXmlEnumValue

@Composable
fun ButtonsContained(customizationState: ButtonCustomizationState) {

with(customizationState) {
val buttonText = stringResource(if (isEnabled) R.string.component_state_enabled else R.string.component_state_disabled)

Column(
modifier = Modifier
.verticalScroll(rememberScrollState())
Expand All @@ -57,6 +61,7 @@ fun ButtonsContained(customizationState: ButtonCustomizationState) {
}
}
ContainedButton(
text = buttonText,
style = buttonStyle.value,
leadingIcon = hasLeadingIcon,
enabled = isEnabled,
Expand All @@ -67,6 +72,7 @@ fun ButtonsContained(customizationState: ButtonCustomizationState) {

InvertedBackgroundColumn {
ContainedButton(
text = buttonText,
style = buttonStyle.value,
leadingIcon = hasLeadingIcon,
enabled = isEnabled,
Expand All @@ -75,36 +81,57 @@ fun ButtonsContained(customizationState: ButtonCustomizationState) {
)
}

CodeImplementationColumn(modifier = Modifier.padding(horizontal = dimensionResource(id = com.orange.ods.R.dimen.screen_horizontal_margin))) {
FunctionCallCode(
name = OdsComposable.OdsButton.name,
exhaustiveParameters = false,
parameters = {
enum("style", buttonStyle.value)
if (hasFullScreenWidth) fillMaxWidth()
if (hasLeadingIcon) {
classInstance<OdsButton.Icon>("icon") {
painter()
CodeImplementationColumn(
modifier = Modifier.padding(horizontal = dimensionResource(id = com.orange.ods.R.dimen.screen_horizontal_margin)),
composeContent = {
FunctionCallCode(
name = OdsComposable.OdsButton.name,
exhaustiveParameters = false,
parameters = {
text(buttonText)
enum("style", buttonStyle.value)
if (hasFullScreenWidth) fillMaxWidth()
if (hasLeadingIcon) {
classInstance<OdsButton.Icon>("icon") {
painter()
}
}
if (!isEnabled) enabled(false)
}
if (!isEnabled) enabled(false)
)
},
xmlContent = {
CodeBackgroundColumn {
XmlViewTag(
clazz = com.orange.ods.xml.component.button.OdsButton::class.java,
xmlAttributes = {
id("ods_button")
layoutWidth(hasFullScreenWidth)
layoutHeight()
appAttr("text", buttonText)
if (hasLeadingIcon) drawableAppAttr("icon", "icon")
if (!isEnabled) disabledAppAttr()
if (buttonStyle.value != OdsButton.Style.Default) appAttr("odsButtonStyle", buttonStyle.value.getXmlEnumValue())
appAttr("odsDisplaySurface", "light")
}
)
}
)
}
}
)
}
}
}

@Composable
private fun ContainedButton(
text: String,
style: OdsButton.Style,
leadingIcon: Boolean,
enabled: Boolean,
fullScreenWidth: Boolean,
displaySurface: OdsDisplaySurface = OdsDisplaySurface.Default
) {
val context = LocalContext.current
val text = stringResource(if (enabled) R.string.component_state_enabled else R.string.component_state_disabled)
val iconId = R.drawable.ic_coffee

Box(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object PredefinedXmlAttribute {

open class AppAttr(name: String, value: String) : XmlAttribute("app:$name", value)
class DrawableAppAttr(name: String, drawableName: String) : AppAttr(name, "@drawable/$drawableName")
class DisabledAppAttr : AppAttr("enabled", "false")
}

@DslMarker
Expand All @@ -61,10 +62,10 @@ class XmlAttributesBuilder {
fun layoutWidth(matchParent: Boolean = false) = add(PredefinedXmlAttribute.LayoutWidth(matchParent))
fun appAttr(name: String, value: String) = add(PredefinedXmlAttribute.AppAttr(name, value))
fun drawableAppAttr(name: String, drawableName: String) = add(PredefinedXmlAttribute.DrawableAppAttr(name, drawableName))
fun disabledAppAttr() = add(PredefinedXmlAttribute.DisabledAppAttr())

@Composable
fun Build() = xmlAttributes.forEach { it.code() }
}


private fun getLayoutParamValue(matchParent: Boolean) = if (matchParent) "match_parent" else "wrap_content"
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ import com.orange.ods.compose.component.button.OdsButton

/**
* @return [OdsButton.Style] associated to the provided [xmlId]
* BE CAREFUL: If the enum values change you have to update associated XML attributes in the lib-xml
* BE CAREFUL: If the enum values change you have to update associated XML attributes in the lib-xml.
*/
fun OdsButton.Style.Companion.fromXmlAttrValue(xmlId: Int): OdsButton.Style = OdsButton.Style.entries[xmlId]
fun OdsButton.Style.Companion.fromXmlAttrValue(xmlId: Int): OdsButton.Style = OdsButton.Style.entries[xmlId]

/**
* @return the XML enum value corresponding to this [OdsButton.Style]
* BE CAREFUL: As there is no way to access XML enum names directly, if an enum name change, you have to update this method.
*/
fun OdsButton.Style.getXmlEnumValue() = when (this) {
OdsButton.Style.Default -> "standard"
OdsButton.Style.Primary -> "primary"
OdsButton.Style.FunctionalPositive -> "functional_positive"
OdsButton.Style.FunctionalNegative -> "functional_negative"
}
1 change: 1 addition & 0 deletions lib-xml/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<attr name="icon" />
<attr name="text" />
<attr name="enabled" />
<!-- BE CAREFUL: If this enum change, don't forget to update associated extension -->
<attr name="odsButtonStyle" format="enum">
<enum name="standard" value="0" />
<enum name="primary" value="1" />
Expand Down

0 comments on commit 04e4bb1

Please sign in to comment.