From 8d87535627df42c5f7872893215d4dd5464e9b16 Mon Sep 17 00:00:00 2001 From: Florent Maitre Date: Fri, 8 Sep 2023 12:29:09 +0200 Subject: [PATCH] [#624] Add new convenience methods for code implementation --- .../components/appbars/top/ComponentTopAppBar.kt | 6 +++--- .../app/ui/components/banners/ComponentBanners.kt | 6 +++--- .../bottomnavigation/ComponentBottomNavigation.kt | 4 ++-- .../app/ui/components/buttons/ButtonsContained.kt | 5 ++--- .../app/ui/components/buttons/ButtonsOutlined.kt | 2 +- .../ods/app/ui/components/buttons/ButtonsText.kt | 5 ++--- .../components/buttons/ButtonsTextToggleGroup.kt | 2 +- .../ui/components/buttons/icons/ButtonsIcon.kt | 2 +- .../components/buttons/icons/ButtonsIconToggle.kt | 4 ++-- .../buttons/icons/ButtonsIconToggleGroup.kt | 2 +- .../ods/app/ui/components/cards/CardHorizontal.kt | 2 +- .../app/ui/components/dialogs/ComponentDialog.kt | 4 ++-- .../ods/app/ui/components/menus/MenuDropdown.kt | 2 +- .../ui/components/menus/MenuExposedDropdown.kt | 2 +- .../navigationdrawers/ComponentModalDrawers.kt | 8 ++++---- .../ods/app/ui/components/textfields/TextField.kt | 2 +- .../ui/utilities/composable/CodeImplementation.kt | 15 ++++++++++++++- changelog.md | 1 + 18 files changed, 43 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/orange/ods/app/ui/components/appbars/top/ComponentTopAppBar.kt b/app/src/main/java/com/orange/ods/app/ui/components/appbars/top/ComponentTopAppBar.kt index 824632300..aa4f6b628 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/appbars/top/ComponentTopAppBar.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/appbars/top/ComponentTopAppBar.kt @@ -109,15 +109,15 @@ fun ComponentTopAppBar(variant: Variant) { title(context.getString(R.string.component_app_bars_top_regular)) if (isNavigationIconEnabled) { - classInstance("navigationIcon", OdsTopAppBarNavigationIcon::class.java) { - simple("imageVector", "") + classInstance("navigationIcon") { + imageVector() contentDescription(context.getString(R.string.top_app_bar_back_icon_desc)) } } list("actions") { repeat(actionCount.value) { - classInstance(OdsTopAppBarActionButton::class.java) { + classInstance { onClick() painter() contentDescription("icon description") diff --git a/app/src/main/java/com/orange/ods/app/ui/components/banners/ComponentBanners.kt b/app/src/main/java/com/orange/ods/app/ui/components/banners/ComponentBanners.kt index 0f33b1c4c..c1e6c791d 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/banners/ComponentBanners.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/banners/ComponentBanners.kt @@ -143,18 +143,18 @@ fun ComponentBanners() { exhaustiveParameters = false, parameters = { string("message", if (hasTwoTextLines) recipe.description else recipe.title) - classInstance("firstButton", OdsBannerButton::class.java) { + classInstance("firstButton") { text(context.getString(R.string.component_banner_dismiss)) onClick() } if (hasImage) { - classInstance("image", OdsBannerImage::class.java) { + classInstance("image") { painter() contentDescription("") } } if (hasSecondButton) { - classInstance("secondButton", OdsBannerButton::class.java) { + classInstance("secondButton") { text(context.getString(R.string.component_banner_detail)) onClick() } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/bottomnavigation/ComponentBottomNavigation.kt b/app/src/main/java/com/orange/ods/app/ui/components/bottomnavigation/ComponentBottomNavigation.kt index 361039399..6cc4879b9 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/bottomnavigation/ComponentBottomNavigation.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/bottomnavigation/ComponentBottomNavigation.kt @@ -98,8 +98,8 @@ fun ComponentBottomNavigation() { parameters = { list("items") { navigationItems.take(selectedNavigationItemCount.value).forEach { item -> - classInstance(OdsBottomNavigationItem::class.java) { - classInstance("icon", OdsBottomNavigationItemIcon::class.java) { + classInstance { + classInstance("icon") { painter() contentDescription("") } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsContained.kt b/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsContained.kt index 31dff263c..eafac28ef 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsContained.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsContained.kt @@ -37,7 +37,6 @@ import com.orange.ods.compose.component.button.OdsButton import com.orange.ods.compose.component.button.OdsButtonIcon import com.orange.ods.compose.component.button.OdsButtonStyle import com.orange.ods.compose.theme.OdsDisplaySurface -import com.orange.ods.extension.fullName @Composable fun ButtonsContained(customizationState: ButtonCustomizationState) { @@ -84,10 +83,10 @@ fun ButtonsContained(customizationState: ButtonCustomizationState) { name = OdsComposable.OdsButton.name, exhaustiveParameters = false, parameters = { - simple("style", buttonStyle.value.fullName) + enum("style", buttonStyle.value) if (hasFullScreenWidth) fillMaxWidth() if (hasLeadingIcon) { - classInstance("icon", OdsButtonIcon::class.java) { + classInstance("icon") { painter() } } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsOutlined.kt b/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsOutlined.kt index 8197bda6c..d06de67d4 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsOutlined.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsOutlined.kt @@ -63,7 +63,7 @@ fun ButtonsOutlined(customizationState: ButtonCustomizationState) { parameters = { if (hasFullScreenWidth) fillMaxWidth() if (hasLeadingIcon) { - classInstance("icon", OdsButtonIcon::class.java) { + classInstance("icon") { painter() } } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsText.kt b/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsText.kt index 1b9ab8f15..753627a24 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsText.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsText.kt @@ -37,7 +37,6 @@ import com.orange.ods.compose.component.button.OdsButtonIcon import com.orange.ods.compose.component.button.OdsTextButton import com.orange.ods.compose.component.button.OdsTextButtonStyle import com.orange.ods.compose.theme.OdsDisplaySurface -import com.orange.ods.extension.fullName @Composable fun ButtonsText(customizationState: ButtonCustomizationState) { @@ -79,10 +78,10 @@ fun ButtonsText(customizationState: ButtonCustomizationState) { name = OdsComposable.OdsTextButton.name, exhaustiveParameters = false, parameters = { - simple("style", textButtonStyle.value.fullName) + enum("style", textButtonStyle.value) if (hasFullScreenWidth) fillMaxWidth() if (hasLeadingIcon) { - classInstance("icon", OdsButtonIcon::class.java) { + classInstance("icon") { painter() } } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsTextToggleGroup.kt b/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsTextToggleGroup.kt index 86d7fe558..7edd3f2a0 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsTextToggleGroup.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/buttons/ButtonsTextToggleGroup.kt @@ -79,7 +79,7 @@ fun ButtonsTextToggleButtonsRow(customizationState: ButtonCustomizationState) { parameters = { list("textToggleButtons") { textToggleButtons.take(toggleCount.value).forEach { item -> - classInstance(OdsTextToggleButtonsRowItem::class.java) { + classInstance { text(item.text) enabled(customizationState.isEnabled) } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIcon.kt b/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIcon.kt index ebbccc681..22dd69105 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIcon.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIcon.kt @@ -74,7 +74,7 @@ fun ButtonsIcon(customizationState: ButtonIconCustomizationState) { name = OdsComposable.OdsIconButton.name, exhaustiveParameters = false, parameters = { - classInstance("icon", OdsIconButtonIcon::class.java) { + classInstance("icon") { painter() contentDescription("") } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIconToggle.kt b/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIconToggle.kt index be3045b9c..3c820db39 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIconToggle.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIconToggle.kt @@ -82,11 +82,11 @@ fun ButtonsIconToggle(customizationState: ButtonIconCustomizationState) { name = OdsComposable.OdsIconToggleButton.name, exhaustiveParameters = false, parameters = { - classInstance("uncheckedIcon", OdsIconButtonIcon::class.java) { + classInstance("uncheckedIcon") { painter() contentDescription("") } - classInstance("checkedIcon", OdsIconButtonIcon::class.java) { + classInstance("checkedIcon") { painter() contentDescription("") } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIconToggleGroup.kt b/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIconToggleGroup.kt index 5238d1629..462f6654e 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIconToggleGroup.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/buttons/icons/ButtonsIconToggleGroup.kt @@ -82,7 +82,7 @@ fun ButtonsIconToggleGroup(customizationState: ButtonIconCustomizationState) { parameters = { list("icons") { repeat(toggleCount.value) { - classInstance(OdsIconToggleButtonsRowIcon::class.java) { + classInstance { painter() contentDescription("") if (!isEnabled) enabled(false) diff --git a/app/src/main/java/com/orange/ods/app/ui/components/cards/CardHorizontal.kt b/app/src/main/java/com/orange/ods/app/ui/components/cards/CardHorizontal.kt index 4112debd3..c10ad70bb 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/cards/CardHorizontal.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/cards/CardHorizontal.kt @@ -77,7 +77,7 @@ fun CardHorizontal(customizationState: CardCustomizationState) { name = OdsComposable.OdsHorizontalCard.name, exhaustiveParameters = false, parameters = { - simple("imagePosition", imagePosition.value.name) + enum("imagePosition", imagePosition.value) title(recipe.title) image() if (hasSubtitle) subtitle(recipe.subtitle) diff --git a/app/src/main/java/com/orange/ods/app/ui/components/dialogs/ComponentDialog.kt b/app/src/main/java/com/orange/ods/app/ui/components/dialogs/ComponentDialog.kt index efdd0bc92..823a97625 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/dialogs/ComponentDialog.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/dialogs/ComponentDialog.kt @@ -76,13 +76,13 @@ fun ComponentDialog() { exhaustiveParameters = false, parameters = { simple("text", "") - classInstance("confirmButton", OdsAlertDialogButton::class.java) { + classInstance("confirmButton") { text(confirmButtonText) onClick() } if (customizationState.isTitleChecked) string("title", recipe.title) if (customizationState.isDismissButtonChecked) { - classInstance("dismissButton", OdsAlertDialogButton::class.java) { + classInstance("dismissButton") { text(dismissButtonText) onClick() } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/menus/MenuDropdown.kt b/app/src/main/java/com/orange/ods/app/ui/components/menus/MenuDropdown.kt index daea4424d..d039e322e 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/menus/MenuDropdown.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/menus/MenuDropdown.kt @@ -123,7 +123,7 @@ fun MenuDropdown() { lambda("onDismissRequest") list("items") { recipes.take(2).forEachIndexed { index, recipe -> - classInstance(OdsDropdownMenuItem::class.java) { + classInstance { string("text", recipe.title) if (hasIcons && recipe.iconResId != null) icon() if (hasDividerExample && index == dividerIndex) stringRepresentation("divider", true) diff --git a/app/src/main/java/com/orange/ods/app/ui/components/menus/MenuExposedDropdown.kt b/app/src/main/java/com/orange/ods/app/ui/components/menus/MenuExposedDropdown.kt index cc3446bf8..3be6a9b75 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/menus/MenuExposedDropdown.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/menus/MenuExposedDropdown.kt @@ -105,7 +105,7 @@ fun MenuExposedDropdown() { string("label", label) list("items") { items.forEach { item -> - classInstance(OdsExposedDropdownMenuItem::class.java) { + classInstance { string("label", item.label) if (hasIcons) simple("iconResId", "") } diff --git a/app/src/main/java/com/orange/ods/app/ui/components/navigationdrawers/ComponentModalDrawers.kt b/app/src/main/java/com/orange/ods/app/ui/components/navigationdrawers/ComponentModalDrawers.kt index 806105800..be0736645 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/navigationdrawers/ComponentModalDrawers.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/navigationdrawers/ComponentModalDrawers.kt @@ -177,7 +177,7 @@ fun ComponentModalDrawers() { name = OdsComposable.OdsModalDrawer.name, exhaustiveParameters = false, parameters = { - classInstance("drawerHeader", OdsModalDrawerHeader::class.java) { + classInstance("drawerHeader") { title(title) image() stringRepresentation("imageDisplayType", imageDisplayType) @@ -186,15 +186,15 @@ fun ComponentModalDrawers() { list("drawerContentList") { if (isContentExampleChecked) { if (hasLabel) { - classInstance(OdsModalDrawerSectionLabel::class.java) { + classInstance { label("Section") } } - classInstance(OdsModalDrawerListItem::class.java) { + classInstance { icon() simple("text", "") } - if (hasDivider) classInstance(OdsModalDrawerDivider::class.java) + if (hasDivider) classInstance() } } simple("selectedItem", "") diff --git a/app/src/main/java/com/orange/ods/app/ui/components/textfields/TextField.kt b/app/src/main/java/com/orange/ods/app/ui/components/textfields/TextField.kt index 380fe32b7..5b8caad99 100644 --- a/app/src/main/java/com/orange/ods/app/ui/components/textfields/TextField.kt +++ b/app/src/main/java/com/orange/ods/app/ui/components/textfields/TextField.kt @@ -133,7 +133,7 @@ fun TextFieldCodeImplementationColumn( lambda("onValueChange") label(label) placeholder(placeholder) - classInstance("keyboardOptions", KeyboardOptions::class.java) { + classInstance("keyboardOptions") { simple("capitalization", capitalizationValue) stringRepresentation("keyboardType", softKeyboardType.value.keyboardType) stringRepresentation("imeAction", softKeyboardAction.value.imeAction) diff --git a/app/src/main/java/com/orange/ods/app/ui/utilities/composable/CodeImplementation.kt b/app/src/main/java/com/orange/ods/app/ui/utilities/composable/CodeImplementation.kt index e4d101d0a..13eb35007 100644 --- a/app/src/main/java/com/orange/ods/app/ui/utilities/composable/CodeImplementation.kt +++ b/app/src/main/java/com/orange/ods/app/ui/utilities/composable/CodeImplementation.kt @@ -31,10 +31,13 @@ import com.orange.ods.app.ui.UiFramework import com.orange.ods.compose.component.menu.OdsExposedDropdownMenu import com.orange.ods.compose.component.menu.OdsExposedDropdownMenuItem import com.orange.ods.compose.theme.OdsTheme +import com.orange.ods.extension.fullName import com.orange.ods.extension.orElse const val IconPainterValue = "" const val ImagePainterValue = "" +const val PainterValue = "" +const val VectorValue = "" const val CardTextValue = "" private abstract class CodeParameter(val name: String) { @@ -53,6 +56,7 @@ private open class StringParameter(name: String, textValue: String) : SimplePara private open class LambdaParameter(name: String) : SimpleParameter(name, "{ }") private class FloatParameter(name: String, value: Float) : SimpleParameter(name, value.toString().plus("f")) private class MutableStateParameter(name: String, stateValue: String) : SimpleParameter(name, "remember { mutableStateOf($stateValue) }") +private class EnumParameter(name: String, value: T) : SimpleParameter(name, value.fullName) where T : Enum private class ComposableParameter(name: String, val value: @Composable () -> Unit) : CodeParameter(name) { override val code @@ -92,8 +96,9 @@ open class Function(val name: String, val parameters: ParametersBuilder.() -> Un private sealed class PredefinedParameter { object Icon : SimpleParameter("icon", IconPainterValue) - object Painter : SimpleParameter("painter", IconPainterValue) object Image : SimpleParameter("image", ImagePainterValue) + object Painter : SimpleParameter("painter", PainterValue) + object ImageVector : SimpleParameter("imageVector", VectorValue) object CardText : SimpleParameter("cardText", CardTextValue) object FillMaxWidth : SimpleParameter("modifier", "Modifier.fillMaxWidth()") @@ -262,6 +267,9 @@ class ListParameterValueBuilder { private val functions = mutableListOf() + inline fun classInstance(noinline parameters: ParametersBuilder.() -> Unit = {}) = + classInstance(T::class.java, parameters) + fun classInstance(clazz: Class<*>, parameters: ParametersBuilder.() -> Unit = {}) = apply { functions.add(ClassInstance(clazz, parameters)) } fun function(functionName: String, parameters: ParametersBuilder.() -> Unit = {}) = apply { functions.add(Function(functionName, parameters)) } @@ -282,7 +290,11 @@ class ParametersBuilder { fun lambda(name: String) = add(LambdaParameter(name)) fun float(name: String, value: Float) = add(FloatParameter(name, value)) fun mutableState(name: String, stateValue: String) = add(MutableStateParameter(name, stateValue)) + fun > enum(name: String, value: T) = add(EnumParameter(name, value)) fun composable(name: String, value: @Composable () -> Unit) = add(ComposableParameter(name, value)) + inline fun classInstance(name: String, noinline parameters: ParametersBuilder.() -> Unit) = + classInstance(name, T::class.java, parameters) + fun classInstance(name: String, clazz: Class<*>, parameters: ParametersBuilder.() -> Unit) = add(ClassInstanceParameter(name, ClassInstance(clazz, parameters))) @@ -293,6 +305,7 @@ class ParametersBuilder { fun icon() = add(PredefinedParameter.Icon) fun painter() = add(PredefinedParameter.Painter) fun image() = add(PredefinedParameter.Image) + fun imageVector() = add(PredefinedParameter.ImageVector) fun cardText() = add(PredefinedParameter.CardText) fun fillMaxWidth() = add(PredefinedParameter.FillMaxWidth) diff --git a/changelog.md b/changelog.md index a3e1ba988..1153bf4fc 100644 --- a/changelog.md +++ b/changelog.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - \[App\] Add list item component ([#555](https://github.com/Orange-OpenSource/ods-android/issues/555)) - \[App\] Add toggle text buttons group component ([#530](https://github.com/Orange-OpenSource/ods-android/issues/530)) +- \[App\] Add new convenience methods for code implementation ([#624](https://github.com/Orange-OpenSource/ods-android/issues/624)) - \[Lib\] Add `OdsTextToggleButtonsRow` component ([#530](https://github.com/Orange-OpenSource/ods-android/issues/530)) ### Changed