Skip to content

Commit 8aade95

Browse files
fix: demo app crashes when scrolling through color tokens with orange theme (#953)
* Handle unspecified colors in color tokens screen * Rename token illustration methods and add previews
1 parent d11f13b commit 8aade95

File tree

5 files changed

+109
-21
lines changed

5 files changed

+109
-21
lines changed

app/src/main/java/com/orange/ouds/app/ui/tokens/Token.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package com.orange.ouds.app.ui.tokens
1414

1515
import androidx.compose.runtime.Composable
1616
import androidx.compose.ui.graphics.Color
17+
import androidx.compose.ui.graphics.isUnspecified
1718
import androidx.compose.ui.res.stringResource
1819
import androidx.compose.ui.text.TextStyle
1920
import androidx.compose.ui.unit.Dp
@@ -33,7 +34,13 @@ data class Token<T>(val name: String, val relativeName: String, val value: @Comp
3334
val literalValue: String
3435
@Composable
3536
get() = when (val value = value()) {
36-
is Color -> stringResource(id = R.string.app_tokens_colorFormat_label, value.value.toString(16).substring(2, 8).uppercase())
37+
is Color -> {
38+
if (value.isUnspecified) {
39+
stringResource(id = R.string.app_tokens_color_unspecified_label)
40+
} else {
41+
stringResource(id = R.string.app_tokens_colorFormat_label, value.value.toString(16).substring(2, 8).uppercase())
42+
}
43+
}
3744
is Float -> "\u200e${value}f" // "\u200e" forces LTR display even if the app is in arabic
3845
is Dp -> "\u200e${value} dp".replace(".0.dp", "").substringBeforeLast(".dp")
3946
is TextStyle -> "\u200e${value.fontSize} sp".replace(".0.sp", "").substringBeforeLast(".sp")

app/src/main/java/com/orange/ouds/app/ui/tokens/TokenCategoryDetailScreen.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,23 +219,23 @@ private fun TokenRow(tokenProperty: TokenProperty<out TokenCategory<*>>, token:
219219

220220
@Composable
221221
private fun TokenIllustration(tokenProperty: TokenProperty<*>, token: Token<*>) = when (tokenProperty) {
222-
is TokenProperty.BorderWidth -> BorderIllustrationBox(width = token.value() as Dp)
223-
is TokenProperty.BorderRadius -> BorderIllustrationBox(shape = RoundedCornerShape(token.value() as Dp))
224-
is TokenProperty.BorderStyle -> BorderIllustrationBox(style = token.value() as OudsBorderStyle)
222+
is TokenProperty.BorderWidth -> BorderIllustration(width = token.value() as Dp)
223+
is TokenProperty.BorderRadius -> BorderIllustration(shape = RoundedCornerShape(token.value() as Dp))
224+
is TokenProperty.BorderStyle -> BorderIllustration(style = token.value() as OudsBorderStyle)
225225
is TokenProperty.ColorAction, TokenProperty.ColorAlways, TokenProperty.ColorBackground, TokenProperty.ColorBorder, TokenProperty.ColorContent,
226-
TokenProperty.ColorOverlay, TokenProperty.ColorSurface -> BorderIllustrationBox(backgroundColor = token.value() as Color)
227-
is TokenProperty.Opacity -> OpacityIllustrationBox(opacity = token.value() as Float)
228-
is TokenProperty.Elevation -> ElevationIllustrationSurface(elevation = token.value() as Dp)
229-
is TokenProperty.SizeIconDecorative -> SizeIconIllustrationBox(size = token.value() as Dp)
230-
is TokenProperty.SizeIconWithText -> SizeIconIllustrationBox(size = token.value() as Dp)
231-
is TokenProperty.SpaceColumnGap, TokenProperty.SpaceFixed, TokenProperty.SpaceScaled -> SpaceIllustrationBox(
226+
TokenProperty.ColorOverlay, TokenProperty.ColorSurface -> ColorIllustration(color = token.value() as Color)
227+
is TokenProperty.Opacity -> OpacityIllustration(opacity = token.value() as Float)
228+
is TokenProperty.Elevation -> ElevationIllustration(elevation = token.value() as Dp)
229+
is TokenProperty.SizeIconDecorative -> SizeIconIllustration(size = token.value() as Dp)
230+
is TokenProperty.SizeIconWithText -> SizeIconIllustration(size = token.value() as Dp)
231+
is TokenProperty.SpaceColumnGap, TokenProperty.SpaceFixed, TokenProperty.SpaceScaled -> SpaceIllustration(
232232
size = token.value() as Dp,
233233
contentAlignment = Alignment.Center
234234
)
235-
TokenProperty.SpacePaddingInline -> SpaceIllustrationBox(size = token.value() as Dp)
236-
TokenProperty.SpacePaddingInset -> SpacePaddingInsetIllustrationBox(size = token.value() as Dp)
237-
TokenProperty.SpacePaddingStack -> SpaceIllustrationBox(size = token.value() as Dp, orientation = SpaceOrientation.Vertical)
238-
TokenProperty.SpaceRowGap -> SpaceIllustrationBox(size = token.value() as Dp, orientation = SpaceOrientation.Vertical, contentAlignment = Alignment.Center)
235+
TokenProperty.SpacePaddingInline -> SpaceIllustration(size = token.value() as Dp)
236+
TokenProperty.SpacePaddingInset -> SpacePaddingInsetIllustration(size = token.value() as Dp)
237+
TokenProperty.SpacePaddingStack -> SpaceIllustration(size = token.value() as Dp, orientation = SpaceOrientation.Vertical)
238+
TokenProperty.SpaceRowGap -> SpaceIllustration(size = token.value() as Dp, orientation = SpaceOrientation.Vertical, contentAlignment = Alignment.Center)
239239
TokenProperty.Typography, TokenProperty.Grid -> Unit
240240
}
241241

app/src/main/java/com/orange/ouds/app/ui/tokens/TokenPropertyIllustration.kt

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
package com.orange.ouds.app.ui.tokens
1414

15+
import androidx.compose.foundation.Canvas
1516
import androidx.compose.foundation.Image
1617
import androidx.compose.foundation.background
1718
import androidx.compose.foundation.border
1819
import androidx.compose.foundation.isSystemInDarkTheme
1920
import androidx.compose.foundation.layout.Box
2021
import androidx.compose.foundation.layout.BoxScope
2122
import androidx.compose.foundation.layout.fillMaxHeight
23+
import androidx.compose.foundation.layout.fillMaxSize
2224
import androidx.compose.foundation.layout.fillMaxWidth
2325
import androidx.compose.foundation.layout.height
2426
import androidx.compose.foundation.layout.padding
@@ -30,24 +32,31 @@ import androidx.compose.runtime.Composable
3032
import androidx.compose.ui.Alignment
3133
import androidx.compose.ui.Modifier
3234
import androidx.compose.ui.draw.clip
35+
import androidx.compose.ui.geometry.Offset
3336
import androidx.compose.ui.graphics.Color
3437
import androidx.compose.ui.graphics.RectangleShape
3538
import androidx.compose.ui.graphics.Shape
39+
import androidx.compose.ui.graphics.StrokeCap
40+
import androidx.compose.ui.graphics.isUnspecified
3641
import androidx.compose.ui.res.painterResource
42+
import androidx.compose.ui.tooling.preview.PreviewLightDark
43+
import androidx.compose.ui.tooling.preview.PreviewParameter
3744
import androidx.compose.ui.unit.Dp
3845
import androidx.compose.ui.unit.dp
3946
import com.orange.ouds.app.R
47+
import com.orange.ouds.app.ui.utilities.composable.AppPreview
4048
import com.orange.ouds.core.theme.OudsBorderStyle
4149
import com.orange.ouds.core.theme.OudsTheme
4250
import com.orange.ouds.core.theme.dashedBorder
4351
import com.orange.ouds.core.theme.dottedBorder
4452
import com.orange.ouds.core.theme.takeUnlessHairline
4553
import com.orange.ouds.foundation.extensions.orElse
54+
import com.orange.ouds.foundation.utilities.BasicPreviewParameterProvider
4655

4756
private val defaultIllustrationSize = 64.dp
4857

4958
@Composable
50-
fun IllustrationBox(
59+
private fun IllustrationBox(
5160
modifier: Modifier = Modifier,
5261
backgroundColor: Color = OudsTheme.colorScheme.surface.secondary,
5362
contentAlignment: Alignment = Alignment.TopStart,
@@ -63,7 +72,7 @@ fun IllustrationBox(
6372
}
6473

6574
@Composable
66-
fun BorderIllustrationBox(
75+
fun BorderIllustration(
6776
width: Dp = 1.dp,
6877
shape: Shape = RectangleShape,
6978
style: OudsBorderStyle = OudsBorderStyle.Solid,
@@ -86,14 +95,38 @@ fun BorderIllustrationBox(
8695
}
8796

8897
@Composable
89-
fun ElevationIllustrationSurface(elevation: Dp) {
98+
fun ColorIllustration(color: Color) {
99+
IllustrationBox(
100+
modifier = Modifier.border(width = 1.dp, color = OudsTheme.colorScheme.border.emphasized),
101+
backgroundColor = if (color.isUnspecified) Color.White else color
102+
) {
103+
if (color.isUnspecified) {
104+
Canvas(
105+
modifier = Modifier
106+
.fillMaxSize()
107+
.clip(shape = RectangleShape)
108+
) {
109+
drawLine(
110+
color = Color.Red,
111+
start = Offset(x = size.width, y = 0f),
112+
end = Offset(x = 0f, y = size.height),
113+
strokeWidth = 2.dp.toPx(),
114+
cap = StrokeCap.Square
115+
)
116+
}
117+
}
118+
}
119+
}
120+
121+
@Composable
122+
fun ElevationIllustration(elevation: Dp) {
90123
Surface(shadowElevation = elevation) {
91124
IllustrationBox(backgroundColor = OudsTheme.colorScheme.background.secondary)
92125
}
93126
}
94127

95128
@Composable
96-
fun OpacityIllustrationBox(opacity: Float) {
129+
fun OpacityIllustration(opacity: Float) {
97130
val squareColor = if (isSystemInDarkTheme()) Color.White else Color.Black
98131
Box {
99132
Image(painter = painterResource(id = R.drawable.il_opacity_union), contentDescription = null)
@@ -108,7 +141,7 @@ fun OpacityIllustrationBox(opacity: Float) {
108141
}
109142

110143
@Composable
111-
fun SizeIconIllustrationBox(size: Dp) {
144+
fun SizeIconIllustration(size: Dp) {
112145
IllustrationBox(modifier = Modifier.size(80.dp), contentAlignment = Alignment.Center) {
113146
Icon(
114147
modifier = Modifier.size(size),
@@ -120,7 +153,7 @@ fun SizeIconIllustrationBox(size: Dp) {
120153
}
121154

122155
@Composable
123-
fun SpacePaddingInsetIllustrationBox(size: Dp) {
156+
fun SpacePaddingInsetIllustration(size: Dp) {
124157
IllustrationBox {
125158
Box(
126159
modifier = Modifier
@@ -138,7 +171,7 @@ fun SpacePaddingInsetIllustrationBox(size: Dp) {
138171
}
139172

140173
@Composable
141-
fun SpaceIllustrationBox(
174+
fun SpaceIllustration(
142175
size: Dp,
143176
orientation: SpaceOrientation = SpaceOrientation.Horizontal,
144177
contentAlignment: Alignment = Alignment.TopStart
@@ -162,3 +195,49 @@ fun SpaceIllustrationBox(
162195
enum class SpaceOrientation {
163196
Horizontal, Vertical
164197
}
198+
199+
@PreviewLightDark
200+
@Composable
201+
fun PreviewBorderIllustration() = AppPreview {
202+
BorderIllustration()
203+
}
204+
205+
@PreviewLightDark
206+
@Composable
207+
fun PreviewColorIllustration(@PreviewParameter(ColorIllustrationPreviewParameterProvider::class) color: Color) = AppPreview {
208+
ColorIllustration(color)
209+
}
210+
211+
private class ColorIllustrationPreviewParameterProvider : BasicPreviewParameterProvider<Color>(Color.Magenta, Color.Unspecified)
212+
213+
@PreviewLightDark
214+
@Composable
215+
fun PreviewElevationIllustration() = AppPreview {
216+
Box(modifier = Modifier.padding(10.dp)) {
217+
ElevationIllustration(8.dp)
218+
}
219+
}
220+
221+
@PreviewLightDark
222+
@Composable
223+
fun PreviewOpacityIllustration() = AppPreview {
224+
OpacityIllustration(0.5f)
225+
}
226+
227+
@PreviewLightDark
228+
@Composable
229+
fun PreviewSizeIconIllustration() = AppPreview {
230+
SizeIconIllustration(size = 32.dp)
231+
}
232+
233+
@PreviewLightDark
234+
@Composable
235+
fun PreviewSpacePaddingInsetIllustration() = AppPreview {
236+
SpacePaddingInsetIllustration(4.dp)
237+
}
238+
239+
@PreviewLightDark
240+
@Composable
241+
fun PreviewSpaceIllustration() = AppPreview {
242+
SpaceIllustration(8.dp)
243+
}

app/src/main/res/values-ar/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<!-- Tokens: color -->
5959
<string name="app_tokens_color_label">اللون</string>
6060
<string name="app_tokens_color_description_text">اللون يعزز هويتنا البصرية ويضمن التناسق عبر تجارب المنتجات.</string>
61+
<string name="app_tokens_color_unspecified_label">Unspecified</string>
6162

6263
<!-- Tokens: dimension -->
6364
<string name="app_tokens_dimension_label">الأبعاد</string>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<string name="app_tokens_color_content_label" translatable="false">Content</string>
6969
<string name="app_tokens_color_overlay_label" translatable="false">Overlay</string>
7070
<string name="app_tokens_color_surface_label" translatable="false">Surface</string>
71+
<string name="app_tokens_color_unspecified_label">Unspecified</string>
7172

7273
<!-- Tokens: dimension -->
7374
<string name="app_tokens_dimension_label">Dimension</string>

0 commit comments

Comments
 (0)