Skip to content

Commit

Permalink
Split color key tokens into nested objects
Browse files Browse the repository at this point in the history
  • Loading branch information
florentmaitre committed Nov 20, 2024
1 parent ad0256c commit d6d0df6
Show file tree
Hide file tree
Showing 12 changed files with 689 additions and 421 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun ComponentsScreen() {
.padding(top = OudsSpaceKeyToken.Fixed.Medium.value)
.width(OudsGridKeyToken.Margin.value)
.height(OudsGridKeyToken.ColumnGap.value)
.background(OudsColorKeyToken.Background.BrandPrimary.value)
.background(OudsColorKeyToken.Background.Brand.Primary.value)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ sealed class TokenCategory<T>(
TokenProperty.ColorBorder,
TokenProperty.ColorBrand,
TokenProperty.ColorContent,
TokenProperty.ColorDecorative,
TokenProperty.ColorElevation,
TokenProperty.ColorGradient,
TokenProperty.ColorDecorative
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fun TokenCategoryDetailScreen(tokenCategory: TokenCategory<*>, onSubcategoryClic
}

@Composable
private fun TokenIllustration(tokenProperty: TokenProperty<*>, token: Token<Any>) = when (tokenProperty) {
private fun TokenIllustration(tokenProperty: TokenProperty<*>, token: Token<*>) = when (tokenProperty) {
is TokenProperty.BorderWidth -> BorderIllustrationBox(width = token.value as Dp)
is TokenProperty.BorderRadius -> BorderIllustrationBox(shape = RoundedCornerShape(token.value as Dp))
is TokenProperty.BorderStyle -> BorderIllustrationBox(style = token.value as OudsBorderStyle)
Expand Down Expand Up @@ -194,7 +194,10 @@ private fun TokenPropertyHeader(tokenProperty: TokenProperty<*>, modifier: Modif
@Suppress("UNCHECKED_CAST")
when (tokenProperty.categoryClass) {
TokenCategory.Grid::class -> GridHeader(modifier = modifier)
TokenCategory.Dimension.Space::class -> SpaceHeader(modifier = modifier, spaceTokenProperty = tokenProperty as TokenProperty<TokenCategory.Dimension.Space>)
TokenCategory.Dimension.Space::class -> SpaceHeader(
modifier = modifier,
spaceTokenProperty = tokenProperty as TokenProperty<TokenCategory.Dimension.Space>
)
else -> {}
}
}
Expand Down
58 changes: 42 additions & 16 deletions app/src/main/java/com/orange/ouds/app/ui/tokens/TokenProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import kotlin.reflect.KClass

sealed class TokenProperty<T>(
@StringRes val nameRes: Int?,
val tokens: @Composable () -> List<Token<Any>>,
val tokens: @Composable () -> List<Token<*>>,
val categoryClass: KClass<T>
) where T : TokenCategory<T> {

Expand All @@ -52,55 +52,55 @@ sealed class TokenProperty<T>(

data object ColorAction : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_action_label,
tokens = { OudsColorKeyToken.Action.entries.map { Token(it.name, it.value) } },
tokens = { OudsColorKeyToken.Action::class.getTokens<OudsColorKeyToken>() },
categoryClass = TokenCategory.Color::class
)

data object ColorAlways : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_always_label,
tokens = { OudsColorKeyToken.Always.entries.map { Token(it.name, it.value) } },
tokens = { OudsColorKeyToken.Always::class.getTokens<OudsColorKeyToken>() },
categoryClass = TokenCategory.Color::class
)

data object ColorBackground : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_background_label,
tokens = { OudsColorKeyToken.Background.entries.map { Token(it.name, it.value) } },
tokens = { OudsColorKeyToken.Background::class.getTokens<OudsColorKeyToken>() },
categoryClass = TokenCategory.Color::class
)

data object ColorBorder : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_border_label,
tokens = { OudsColorKeyToken.Border.entries.map { Token(it.name, it.value) } },
tokens = { OudsColorKeyToken.Border::class.getTokens<OudsColorKeyToken>() },
categoryClass = TokenCategory.Color::class
)

data object ColorBrand : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_brand_label,
tokens = { OudsColorKeyToken.Brand.entries.map { Token(it.name, it.value) } },
tokens = { OudsColorKeyToken.Brand::class.getTokens<OudsColorKeyToken>() },
categoryClass = TokenCategory.Color::class
)

data object ColorContent : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_content_label,
tokens = { OudsColorKeyToken.Content.entries.map { Token(it.name, it.value) } },
tokens = { OudsColorKeyToken.Content::class.getTokens<OudsColorKeyToken>() },
categoryClass = TokenCategory.Color::class
)

data object ColorDecorative : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_decorative_label,
tokens = { OudsColorKeyToken.Decorative::class.getTokens<OudsColorKeyToken>() },
categoryClass = TokenCategory.Color::class
)

data object ColorElevation : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_elevation_label,
tokens = { OudsColorKeyToken.Elevation.entries.map { Token(it.name, it.value) } },
tokens = { OudsColorKeyToken.Elevation::class.getTokens<OudsColorKeyToken>() },
categoryClass = TokenCategory.Color::class
)

data object ColorGradient : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_gradient_label,
tokens = { OudsColorKeyToken.Gradient.entries.map { Token(it.name, it.value) } },
categoryClass = TokenCategory.Color::class
)

data object ColorDecorative : TokenProperty<TokenCategory.Color>(
nameRes = R.string.app_tokens_color_decorative_label,
tokens = { OudsColorKeyToken.Decorative.entries.map { Token(it.name, it.value) } },
tokens = { OudsColorKeyToken.Gradient::class.getTokens<OudsColorKeyToken>() },
categoryClass = TokenCategory.Color::class
)

Expand Down Expand Up @@ -217,4 +217,30 @@ sealed class TokenProperty<T>(
tokens = { OudsTypographyKeyToken.entries.map { Token(it.name, it.value) } },
categoryClass = TokenCategory.Typography::class
)
}
}

@PublishedApi
internal fun KClass<*>.getRelativeName(parent: KClass<*>): String {
return qualifiedName.orEmpty().removePrefix("${parent.qualifiedName.orEmpty()}.")
}

@PublishedApi
internal inline fun <reified T> KClass<*>.getNestedObjects(): List<T> {
return getNestedClassesRecursive().mapNotNull { it.objectInstance }.filterIsInstance<T>()
}

@PublishedApi
internal fun KClass<*>.getNestedClassesRecursive(): List<KClass<*>> {
return nestedClasses + nestedClasses.flatMap { it.getNestedClassesRecursive() }
}

@Composable
inline fun <reified T : Any> KClass<*>.getTokens(): List<Token<*>> {
return getNestedObjects<T>().mapNotNull { keyToken ->
val value = when (keyToken) {
is OudsColorKeyToken -> keyToken.value
else -> null
}
value?.let { Token(keyToken::class.getRelativeName(this).removeSuffix(".Companion"), it) }
}.sortedBy { it.name }
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private fun SpaceHeaderContent(spaceTokenProperty: TokenProperty<TokenCategory.D
}

val modifier = Modifier
.dashedBorder(width = dashedBorderWidth, color = OudsColorKeyToken.Content.DefaultOnBgEmphasized.value)
.dashedBorder(width = dashedBorderWidth, color = OudsColorKeyToken.Content.Default.OnBgEmphasized.value)
.padding(all = dashedBorderWidth)
.background(color = OudsColorKeyToken.Always.Info.value)
.padding(externalSpaceValues)
Expand Down Expand Up @@ -180,7 +180,7 @@ private fun SpaceHeaderText(spaceTokenProperty: TokenProperty<TokenCategory.Dime
Text(
modifier = modifier.background(color = OudsColorKeyToken.Background.Emphasized.value),
text = stringResource(id = textResId),
color = OudsColorKeyToken.Content.DefaultOnBgEmphasized.value,
color = OudsColorKeyToken.Content.Default.OnBgEmphasized.value,
style = OudsTypographyKeyToken.BodyDefaultMedium.value
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fun DetailScreenHeader(
Column {
Image(
painter = painterResource(imageRes),
colorFilter = ColorFilter.tint(OudsColorKeyToken.Content.DefaultOnBgEmphasized.value),
colorFilter = ColorFilter.tint(OudsColorKeyToken.Content.Default.OnBgEmphasized.value),
contentDescription = null,
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fun LargeCard(
Column(modifier = Modifier.background(OudsColorKeyToken.Elevation.Raised.value)) {
Image(
painter = painterResource(imageRes),
colorFilter = ColorFilter.tint(OudsColorKeyToken.Content.DefaultOnBgEmphasized.value),
colorFilter = ColorFilter.tint(OudsColorKeyToken.Content.Default.OnBgEmphasized.value),
contentDescription = null,
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fun OudsButton(
modifier = modifier,
text = text,
style = labelStyle.value,
color = OudsColorKeyToken.Content.OnActionPrimaryEnabled.value
color = OudsColorKeyToken.Content.OnAction.Primary.Enabled.value
)
}
}
Expand Down
Loading

0 comments on commit d6d0df6

Please sign in to comment.