Skip to content

Commit

Permalink
[#624] Add new convenience methods for code implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
florentmaitre committed Sep 20, 2023
1 parent c0c7805 commit 69b9dfe
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<image vector>")
classInstance<OdsTopAppBarNavigationIcon>("navigationIcon") {
imageVector()
contentDescription(context.getString(R.string.top_app_bar_back_icon_desc))
}
}

list("actions") {
repeat(actionCount.value) {
classInstance(OdsTopAppBarActionButton::class.java) {
classInstance<OdsTopAppBarActionButton> {
onClick()
painter()
contentDescription("icon description")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@ fun ComponentBanners() {
exhaustiveParameters = false,
parameters = {
string("message", if (hasTwoTextLines) recipe.description else recipe.title)
classInstance("firstButton", OdsBannerButton::class.java) {
classInstance<OdsBannerButton>("firstButton") {
text(context.getString(R.string.component_banner_dismiss))
onClick()
}
if (hasImage) {
classInstance("image", OdsBannerImage::class.java) {
classInstance<OdsBannerImage>("image") {
painter()
contentDescription("")
}
}
if (hasSecondButton) {
classInstance("secondButton", OdsBannerButton::class.java) {
classInstance<OdsBannerButton>("secondButton") {
text(context.getString(R.string.component_banner_detail))
onClick()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OdsBottomNavigationItem> {
classInstance<OdsBottomNavigationItemIcon>("icon") {
painter()
contentDescription("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<OdsButtonIcon>("icon") {
painter()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fun ButtonsOutlined(customizationState: ButtonCustomizationState) {
parameters = {
if (hasFullScreenWidth) fillMaxWidth()
if (hasLeadingIcon) {
classInstance("icon", OdsButtonIcon::class.java) {
classInstance<OdsButtonIcon>("icon") {
painter()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<OdsButtonIcon>("icon") {
painter()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fun ButtonsTextToggleButtonsRow(customizationState: ButtonCustomizationState) {
parameters = {
list("textToggleButtons") {
textToggleButtons.take(toggleCount.value).forEach { item ->
classInstance(OdsTextToggleButtonsRowItem::class.java) {
classInstance<OdsTextToggleButtonsRowItem> {
text(item.text)
enabled(customizationState.isEnabled)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fun ButtonsIcon(customizationState: ButtonIconCustomizationState) {
name = OdsComposable.OdsIconButton.name,
exhaustiveParameters = false,
parameters = {
classInstance("icon", OdsIconButtonIcon::class.java) {
classInstance<OdsIconButtonIcon>("icon") {
painter()
contentDescription("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ fun ButtonsIconToggle(customizationState: ButtonIconCustomizationState) {
name = OdsComposable.OdsIconToggleButton.name,
exhaustiveParameters = false,
parameters = {
classInstance("uncheckedIcon", OdsIconButtonIcon::class.java) {
classInstance<OdsIconButtonIcon>("uncheckedIcon") {
painter()
contentDescription("")
}
classInstance("checkedIcon", OdsIconButtonIcon::class.java) {
classInstance<OdsIconButtonIcon>("checkedIcon") {
painter()
contentDescription("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fun ButtonsIconToggleGroup(customizationState: ButtonIconCustomizationState) {
parameters = {
list("icons") {
repeat(toggleCount.value) {
classInstance(OdsIconToggleButtonsRowIcon::class.java) {
classInstance<OdsIconToggleButtonsRowIcon> {
painter()
contentDescription("")
if (!isEnabled) enabled(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ fun ComponentDialog() {
exhaustiveParameters = false,
parameters = {
simple("text", "<dialog text>")
classInstance("confirmButton", OdsAlertDialogButton::class.java) {
classInstance<OdsAlertDialogButton>("confirmButton") {
text(confirmButtonText)
onClick()
}
if (customizationState.isTitleChecked) string("title", recipe.title)
if (customizationState.isDismissButtonChecked) {
classInstance("dismissButton", OdsAlertDialogButton::class.java) {
classInstance<OdsAlertDialogButton>("dismissButton") {
text(dismissButtonText)
onClick()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fun MenuDropdown() {
lambda("onDismissRequest")
list("items") {
recipes.take(2).forEachIndexed { index, recipe ->
classInstance(OdsDropdownMenuItem::class.java) {
classInstance<OdsDropdownMenuItem> {
string("text", recipe.title)
if (hasIcons && recipe.iconResId != null) icon()
if (hasDividerExample && index == dividerIndex) stringRepresentation("divider", true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fun MenuExposedDropdown() {
string("label", label)
list("items") {
items.forEach { item ->
classInstance(OdsExposedDropdownMenuItem::class.java) {
classInstance<OdsExposedDropdownMenuItem> {
string("label", item.label)
if (hasIcons) simple("iconResId", "<drawable id>")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fun ComponentModalDrawers() {
name = OdsComposable.OdsModalDrawer.name,
exhaustiveParameters = false,
parameters = {
classInstance("drawerHeader", OdsModalDrawerHeader::class.java) {
classInstance<OdsModalDrawerHeader>("drawerHeader") {
title(title)
image()
stringRepresentation("imageDisplayType", imageDisplayType)
Expand All @@ -191,15 +191,15 @@ fun ComponentModalDrawers() {
list("drawerContentList") {
if (isContentExampleChecked) {
if (hasLabel) {
classInstance(OdsModalDrawerSectionLabel::class.java) {
classInstance<OdsModalDrawerSectionLabel> {
label("Section")
}
}
classInstance(OdsModalDrawerListItem::class.java) {
classInstance<OdsModalDrawerListItem> {
icon()
simple("text", "<item label>")
}
if (hasDivider) classInstance(OdsModalDrawerDivider::class.java)
if (hasDivider) classInstance<OdsModalDrawerDivider>()
}
}
simple("selectedItem", "<OdsModalDrawerItem>")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fun TextFieldCodeImplementationColumn(
lambda("onValueChange")
label(label)
placeholder(placeholder)
classInstance("keyboardOptions", KeyboardOptions::class.java) {
classInstance<KeyboardOptions>("keyboardOptions") {
simple("capitalization", capitalizationValue)
stringRepresentation("keyboardType", softKeyboardType.value.keyboardType)
stringRepresentation("imeAction", softKeyboardAction.value.imeAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<icon painter>"
const val ImagePainterValue = "<image painter>"
const val PainterValue = "<painter>"
const val VectorValue = "<vector>"
const val CardTextValue = "<card text>"

private abstract class CodeParameter(val name: String) {
Expand All @@ -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<T>(name: String, value: T) : SimpleParameter(name, value.fullName) where T : Enum<T>

private class ComposableParameter(name: String, val value: @Composable () -> Unit) : CodeParameter(name) {
override val code
Expand Down Expand Up @@ -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()")

Expand Down Expand Up @@ -262,6 +267,9 @@ class ListParameterValueBuilder {

private val functions = mutableListOf<Function>()

inline fun <reified T> 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)) }
Expand All @@ -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 <T : Enum<T>> enum(name: String, value: T) = add(EnumParameter<T>(name, value))
fun composable(name: String, value: @Composable () -> Unit) = add(ComposableParameter(name, value))
inline fun <reified T> 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)))

Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 69b9dfe

Please sign in to comment.