Skip to content

Commit 74da04d

Browse files
committed
Add preview for rich text in alert message
1 parent 83c6e8d commit 74da04d

46 files changed

Lines changed: 299 additions & 127 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core-test/src/main/java/com/orange/ouds/core/test/OudsAlertMessageTest.kt

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,31 @@
1313
package com.orange.ouds.core.test
1414

1515
import com.orange.ouds.core.utilities.OudsPreviewableComponent
16+
import org.junit.experimental.runners.Enclosed
1617
import org.junit.runner.RunWith
1718
import org.junit.runners.Parameterized
1819

19-
@RunWith(Parameterized::class)
20-
internal class OudsAlertMessageTest(parameter: Any) : OudsComponentSnapshotTest(
21-
OudsPreviewableComponent.AlertMessage,
22-
parameter,
23-
OudsComponentTestSuite.theme,
24-
heightDp = OudsPreviewableComponent.AlertMessage.PreviewHeightDp
25-
) {
20+
@RunWith(Enclosed::class)
21+
internal class OudsAlertMessageTest {
2622

27-
companion object {
28-
@JvmStatic
29-
@Parameterized.Parameters
30-
internal fun data() = OudsPreviewableComponent.AlertMessage.parameters
23+
@RunWith(Parameterized::class)
24+
class Default(parameter: Any) : OudsComponentSnapshotTest(
25+
OudsPreviewableComponent.AlertMessage.Default,
26+
parameter,
27+
OudsComponentTestSuite.theme,
28+
heightDp = OudsPreviewableComponent.AlertMessage.Default.PreviewHeightDp
29+
) {
30+
31+
companion object {
32+
@JvmStatic
33+
@Parameterized.Parameters
34+
internal fun data() = OudsPreviewableComponent.AlertMessage.Default.parameters
35+
}
3136
}
32-
}
37+
38+
class WithRichText : OudsComponentSnapshotTest(
39+
OudsPreviewableComponent.AlertMessage.WithRichText,
40+
null,
41+
OudsComponentTestSuite.theme
42+
)
43+
}

core/src/main/java/com/orange/ouds/core/component/OudsAlertMessage.kt

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ import androidx.compose.ui.platform.LocalConfiguration
4545
import androidx.compose.ui.res.painterResource
4646
import androidx.compose.ui.res.stringResource
4747
import androidx.compose.ui.semantics.semantics
48-
import androidx.compose.ui.text.AnnotatedString
4948
import androidx.compose.ui.tooling.preview.Preview
5049
import androidx.compose.ui.tooling.preview.PreviewParameter
5150
import androidx.compose.ui.unit.dp
5251
import com.orange.ouds.core.R
5352
import com.orange.ouds.core.component.common.text.OudsAnnotatedAlertMessageBulletListLabel
5453
import com.orange.ouds.core.component.common.text.OudsAnnotatedAlertMessageDescription
54+
import com.orange.ouds.core.component.common.text.OudsAnnotatedString
55+
import com.orange.ouds.core.component.common.text.OudsLinkAnnotation
56+
import com.orange.ouds.core.component.common.text.buildOudsAnnotatedAlertMessageBulletListLabel
57+
import com.orange.ouds.core.component.common.text.buildOudsAnnotatedAlertMessageDescription
58+
import com.orange.ouds.core.component.common.text.withLink
59+
import com.orange.ouds.core.component.common.text.withStrong
5560
import com.orange.ouds.core.component.content.OudsComponentContent
5661
import com.orange.ouds.core.theme.LocalDrawableResources
5762
import com.orange.ouds.core.theme.LocalThemeSettings
@@ -60,6 +65,7 @@ import com.orange.ouds.core.theme.takeUnlessHairline
6065
import com.orange.ouds.core.theme.value
6166
import com.orange.ouds.core.utilities.OudsPreview
6267
import com.orange.ouds.core.utilities.OudsPreviewDevice
68+
import com.orange.ouds.core.utilities.OudsPreviewLightDark
6369
import com.orange.ouds.core.utilities.OudsPreviewableComponent
6470
import com.orange.ouds.core.utilities.getPreviewTheme
6571
import com.orange.ouds.foundation.extensions.orElse
@@ -239,12 +245,12 @@ private fun OudsAlertMessage(
239245
style = OudsTheme.typography.label.moderate.large
240246
)
241247
val descriptionModifier = Modifier.widthIn(max = OudsTheme.sizes.maxWidth.type.label.medium)
242-
val descriptionText = annotatedDescription?.annotatedString.orElse { description }
243248
val descriptionColor = status.contentColor
244249
val descriptionStyle = OudsTheme.typography.label.default.medium
245-
when (descriptionText) {
246-
is AnnotatedString -> Text(modifier = descriptionModifier, text = descriptionText, color = descriptionColor, style = descriptionStyle)
247-
is String -> Text(modifier = descriptionModifier, text = descriptionText, color = descriptionColor, style = descriptionStyle)
250+
if (annotatedDescription != null) {
251+
Text(modifier = descriptionModifier, text = annotatedDescription.annotatedString, color = descriptionColor, style = descriptionStyle)
252+
} else if (description != null) {
253+
Text(modifier = descriptionModifier, text = description, color = descriptionColor, style = descriptionStyle)
248254
}
249255
annotatedBulletList.orElse { bulletList }?.let { list ->
250256
Column(verticalArrangement = Arrangement.spacedBy(spaceRowGapBullet.value)) {
@@ -477,17 +483,17 @@ private fun OudsAlertMessageBulletListItem(label: CharSequence, color: Color) {
477483
.widthIn(max = OudsTheme.sizes.maxWidth.type.label.medium)
478484
val style = OudsTheme.typography.label.default.medium
479485
when (label) {
480-
is AnnotatedString -> Text(modifier = modifier, text = label, color = color, style = style)
486+
is OudsAnnotatedString<*> -> Text(modifier = modifier, text = label.annotatedString, color = color, style = style)
481487
is String -> Text(modifier = modifier, text = label, color = color, style = style)
482488
}
483489
}
484490
}
485491

486-
@Preview(name = "Light", heightDp = OudsPreviewableComponent.AlertMessage.PreviewHeightDp, device = OudsPreviewDevice)
492+
@Preview(name = "Light", heightDp = OudsPreviewableComponent.AlertMessage.Default.PreviewHeightDp, device = OudsPreviewDevice)
487493
@Preview(
488494
name = "Dark",
489495
uiMode = UI_MODE_NIGHT_YES or UI_MODE_TYPE_NORMAL,
490-
heightDp = OudsPreviewableComponent.AlertMessage.PreviewHeightDp,
496+
heightDp = OudsPreviewableComponent.AlertMessage.Default.PreviewHeightDp,
491497
device = OudsPreviewDevice
492498
)
493499
@Composable
@@ -527,6 +533,42 @@ internal fun PreviewOudsAlertMessage(
527533
}
528534
}
529535

536+
@OudsPreviewLightDark
537+
@Composable
538+
@Suppress("PreviewShouldNotBeCalledRecursively")
539+
private fun PreviewOudsAlertMessageWithRichText() {
540+
PreviewOudsAlertMessageWithRichText(theme = getPreviewTheme(), darkThemeEnabled = isSystemInDarkTheme())
541+
}
542+
543+
@Composable
544+
internal fun PreviewOudsAlertMessageWithRichText(
545+
theme: OudsThemeContract,
546+
darkThemeEnabled: Boolean
547+
) = OudsPreview(theme = theme, darkThemeEnabled = darkThemeEnabled) {
548+
val description = buildOudsAnnotatedAlertMessageDescription {
549+
append("Here is a description that contains a ")
550+
withStrong { append("strong") }
551+
append(" text and a ")
552+
withLink(OudsLinkAnnotation.Clickable("")) { append("link") }
553+
}
554+
val bulletList = listOf(
555+
buildOudsAnnotatedAlertMessageBulletListLabel { append("Bullet 1") },
556+
buildOudsAnnotatedAlertMessageBulletListLabel {
557+
append("Bullet 2 is a bullet that contains a ")
558+
withStrong { append("strong") }
559+
append(" text and a ")
560+
withLink(OudsLinkAnnotation.Clickable("link")) { append("link") }
561+
},
562+
buildOudsAnnotatedAlertMessageBulletListLabel { append("Bullet 3") }
563+
)
564+
OudsAlertMessage(
565+
modifier = Modifier.padding(all = 10.dp),
566+
label = "Label",
567+
description = description,
568+
bulletList = bulletList
569+
)
570+
}
571+
530572
internal data class OudsAlertMessagePreviewParameter(
531573
val hasIcon: Boolean = false,
532574
val description: String? = null,

core/src/main/java/com/orange/ouds/core/component/OudsBulletList.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import androidx.compose.ui.res.stringResource
5151
import androidx.compose.ui.semantics.clearAndSetSemantics
5252
import androidx.compose.ui.semantics.contentDescription
5353
import androidx.compose.ui.semantics.semantics
54-
import androidx.compose.ui.text.AnnotatedString
5554
import androidx.compose.ui.text.TextStyle
5655
import androidx.compose.ui.text.style.TextAlign
5756
import androidx.compose.ui.tooling.preview.Preview
@@ -152,7 +151,7 @@ class OudsBulletListBuilder internal constructor() {
152151
subListTextStyle: OudsBulletListTextStyle? = null,
153152
builder: (OudsBulletListBuilder.() -> Unit)? = null
154153
) {
155-
item(label as CharSequence, subListType, subListTextStyle, builder)
154+
item(label, null, subListType, subListTextStyle, builder)
156155
}
157156

158157
/**
@@ -173,17 +172,18 @@ class OudsBulletListBuilder internal constructor() {
173172
subListTextStyle: OudsBulletListTextStyle? = null,
174173
builder: (OudsBulletListBuilder.() -> Unit)? = null
175174
) {
176-
item(label.annotatedString, subListType, subListTextStyle, builder)
175+
item(null, label, subListType, subListTextStyle, builder)
177176
}
178177

179178
private fun item(
180-
label: CharSequence,
179+
label: String?,
180+
annotatedLabel: OudsAnnotatedBulletListLabel?,
181181
subListType: OudsBulletListType? = null,
182182
subListTextStyle: OudsBulletListTextStyle? = null,
183183
builder: (OudsBulletListBuilder.() -> Unit)? = null
184184
) {
185185
val subListItems = builder?.let { OudsBulletListBuilder().apply(it).build() }.orEmpty()
186-
items.add(BulletListItem(label, subListType, subListTextStyle, subListItems))
186+
items.add(BulletListItem(label, annotatedLabel, subListType, subListTextStyle, subListItems))
187187
}
188188

189189
internal fun build(): List<BulletListItem> = items
@@ -284,12 +284,12 @@ private fun OudsBulletListItem(
284284
.wrapContentHeight() // Allows to center the text vertically when its height is smaller than the row height
285285
.widthIn(max = textMaxWidth)
286286
.clearAndSetSemantics {}
287-
val text = item.label
288287
val textStyle = currentTextStyle.toTextStyle()
289288
val textColor = OudsTheme.colorScheme.content.default
290-
when (text) {
291-
is AnnotatedString -> Text(modifier = textModifier, text = text, style = textStyle, color = textColor)
292-
is String -> Text(modifier = textModifier, text = text, style = textStyle, color = textColor)
289+
if (item.annotatedLabel != null) {
290+
Text(modifier = textModifier, text = item.annotatedLabel.annotatedString, style = textStyle, color = textColor)
291+
} else if (item.label != null) {
292+
Text(modifier = textModifier, text = item.label, style = textStyle, color = textColor)
293293
}
294294
}
295295

@@ -318,7 +318,8 @@ private fun OudsBulletListItem(
318318
}
319319

320320
internal data class BulletListItem(
321-
val label: CharSequence,
321+
val label: String?,
322+
val annotatedLabel: OudsAnnotatedBulletListLabel?,
322323
val subListType: OudsBulletListType?,
323324
val subListTextStyle: OudsBulletListTextStyle?,
324325
val subListItems: List<BulletListItem> = emptyList()

core/src/main/java/com/orange/ouds/core/component/OudsControlItem.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import androidx.compose.ui.res.painterResource
3939
import androidx.compose.ui.semantics.clearAndSetSemantics
4040
import androidx.compose.ui.semantics.error
4141
import androidx.compose.ui.semantics.semantics
42-
import androidx.compose.ui.text.AnnotatedString
4342
import androidx.compose.ui.unit.Dp
4443
import androidx.compose.ui.unit.dp
4544
import androidx.compose.ui.unit.max
@@ -52,7 +51,6 @@ import com.orange.ouds.core.theme.OudsTheme
5251
import com.orange.ouds.core.theme.value
5352
import com.orange.ouds.core.utilities.CheckedContent
5453
import com.orange.ouds.core.utilities.getPreviewEnumEntry
55-
import com.orange.ouds.foundation.extensions.orElse
5654
import com.orange.ouds.foundation.utilities.BasicPreviewParameterProvider
5755

5856
/**
@@ -265,12 +263,12 @@ private fun ErrorMessageText(error: OudsError, edgeToEdge: Boolean) {
265263
.clearAndSetSemantics {
266264
error(error.message)
267265
}
268-
val text = error.annotatedMessage?.annotatedString.orElse { error.message }
269266
val style = OudsTheme.typography.label.default.medium
270267
val color = OudsTheme.colorScheme.content.status.negative
271-
when (text) {
272-
is AnnotatedString -> Text(modifier = modifier, text = text, style = style, color = color)
273-
is String -> Text(modifier = modifier, text = text, style = style, color = color)
268+
if (error.annotatedMessage != null) {
269+
Text(modifier = modifier, text = error.annotatedMessage.annotatedString, style = style, color = color)
270+
} else {
271+
Text(modifier = modifier, text = error.message, style = style, color = color)
274272
}
275273
}
276274
}

core/src/main/java/com/orange/ouds/core/component/OudsLink.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private fun OudsLink(
207207
val monochrome = LocalColorMode.current?.monochrome == true
208208
val contentColor = rememberInteractionColor(interactionState = interactionState) { linkInteractionState ->
209209
val linkState = getLinkState(enabled = enabled, interactionState = linkInteractionState)
210-
contentColor(state = linkState, monochrome = monochrome)
210+
linkContentColor(state = linkState, monochrome = monochrome)
211211
}
212212

213213
val chevronColor = rememberInteractionColor(interactionState = interactionState) { linkInteractionState ->
@@ -310,7 +310,7 @@ private fun getLinkState(enabled: Boolean, interactionState: InteractionState):
310310
}
311311

312312
@Composable
313-
private fun contentColor(state: OudsLinkState, monochrome: Boolean): Color {
313+
internal fun linkContentColor(state: OudsLinkState, monochrome: Boolean): Color {
314314
return if (monochrome) {
315315
with(OudsTheme.componentsTokens.linkMonochrome) {
316316
when (state) {
@@ -338,7 +338,7 @@ private fun contentColor(state: OudsLinkState, monochrome: Boolean): Color {
338338
private fun chevronColor(state: OudsLinkState, monochrome: Boolean): Color {
339339
return with(OudsTheme.componentsTokens.link) {
340340
if (monochrome) {
341-
contentColor(state = state, monochrome = true)
341+
linkContentColor(state = state, monochrome = true)
342342
} else {
343343
when (state) {
344344
OudsLinkState.Enabled -> colorChevronEnabled.value
@@ -434,7 +434,7 @@ open class OudsLinkIcon private constructor(
434434
get() = extraParameters.tint
435435
}
436436

437-
private enum class OudsLinkState {
437+
internal enum class OudsLinkState {
438438
Enabled, Hovered, Pressed, Disabled, Focused
439439
}
440440

core/src/main/java/com/orange/ouds/core/component/OudsTextInput.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import androidx.compose.ui.semantics.contentDescription
6262
import androidx.compose.ui.semantics.error
6363
import androidx.compose.ui.semantics.hideFromAccessibility
6464
import androidx.compose.ui.semantics.semantics
65-
import androidx.compose.ui.text.AnnotatedString
6665
import androidx.compose.ui.text.TextLayoutResult
6766
import androidx.compose.ui.text.input.ImeAction
6867
import androidx.compose.ui.text.input.KeyboardType
@@ -1219,14 +1218,12 @@ internal fun OudsTextInputHelperTextErrorMessage(
12191218
}
12201219
val textStyle = OudsTheme.typography.label.default.medium
12211220
val textColor = if (hasError) OudsTheme.colorScheme.content.status.negative else decorativeContentColor(enabled = enabled)
1222-
val text = if (hasError) {
1223-
error.annotatedMessage?.annotatedString.orElse { error.message }
1224-
} else {
1225-
annotatedHelperText?.annotatedString.orElse { helperText.orEmpty() }
1226-
}
1227-
when (text) {
1228-
is AnnotatedString -> Text(modifier = textModifier, text = text, style = textStyle, color = textColor)
1229-
is String -> Text(modifier = textModifier, text = text, style = textStyle, color = textColor)
1221+
val annotatedText = if (hasError) error.annotatedMessage else annotatedHelperText
1222+
val text = if (hasError) error.message else helperText
1223+
if (annotatedText != null) {
1224+
Text(modifier = textModifier, text = annotatedText.annotatedString, style = textStyle, color = textColor)
1225+
} else if (text != null) {
1226+
Text(modifier = textModifier, text = text, style = textStyle, color = textColor)
12301227
}
12311228
}
12321229
}

core/src/main/java/com/orange/ouds/core/component/common/text/OudsAnnotatedAlertMessageBulletListLabel.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
package com.orange.ouds.core.component.common.text
1414

15+
import androidx.compose.runtime.Composable
1516
import androidx.compose.ui.text.AnnotatedString
1617

1718
class OudsAnnotatedAlertMessageBulletListLabel internal constructor(annotatedString: AnnotatedString) :
@@ -29,18 +30,24 @@ class OudsAnnotatedAlertMessageBulletListLabel internal constructor(annotatedStr
2930
append(text)
3031
}
3132

32-
override fun addStrong(start: Int, end: Int) = addStrongImpl(start, end)
33+
@Composable
34+
override fun AddStrong(start: Int, end: Int) = AddStrongImpl(start, end)
3335

36+
@Composable
3437
override fun pushStrong(): Int = pushStrongImpl()
3538

36-
override fun addLink(url: OudsLinkAnnotation.Url, start: Int, end: Int) = addLinkImpl(url, start, end)
39+
@Composable
40+
override fun AddLink(url: OudsLinkAnnotation.Url, start: Int, end: Int) = AddLinkImpl(url, start, end)
3741

38-
override fun addLink(clickable: OudsLinkAnnotation.Clickable, start: Int, end: Int) = addLinkImpl(clickable, start, end)
42+
@Composable
43+
override fun AddLink(clickable: OudsLinkAnnotation.Clickable, start: Int, end: Int) = AddLinkImpl(clickable, start, end)
3944

45+
@Composable
4046
override fun pushLink(link: OudsLinkAnnotation): Int = pushLinkImpl(link)
4147
}
4248
}
4349

44-
fun buildOudsAnnotatedAlertMessageBulletListLabel(builder: (OudsAnnotatedAlertMessageBulletListLabel.Builder).() -> Unit): OudsAnnotatedAlertMessageBulletListLabel {
50+
@Composable
51+
fun buildOudsAnnotatedAlertMessageBulletListLabel(builder: @Composable ((OudsAnnotatedAlertMessageBulletListLabel.Builder).() -> Unit)): OudsAnnotatedAlertMessageBulletListLabel {
4552
return buildOudsAnnotatedString<OudsAnnotatedAlertMessageBulletListLabel, OudsAnnotatedAlertMessageBulletListLabel.Builder>(builder)
4653
}

core/src/main/java/com/orange/ouds/core/component/common/text/OudsAnnotatedAlertMessageDescription.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
package com.orange.ouds.core.component.common.text
1414

15+
import androidx.compose.runtime.Composable
1516
import androidx.compose.ui.text.AnnotatedString
1617

1718
class OudsAnnotatedAlertMessageDescription internal constructor(annotatedString: AnnotatedString) :
@@ -29,18 +30,24 @@ class OudsAnnotatedAlertMessageDescription internal constructor(annotatedString:
2930
append(text)
3031
}
3132

32-
override fun addStrong(start: Int, end: Int) = addStrongImpl(start, end)
33+
@Composable
34+
override fun AddStrong(start: Int, end: Int) = AddStrongImpl(start, end)
3335

36+
@Composable
3437
override fun pushStrong(): Int = pushStrongImpl()
3538

36-
override fun addLink(url: OudsLinkAnnotation.Url, start: Int, end: Int) = addLinkImpl(url, start, end)
39+
@Composable
40+
override fun AddLink(url: OudsLinkAnnotation.Url, start: Int, end: Int) = AddLinkImpl(url, start, end)
3741

38-
override fun addLink(clickable: OudsLinkAnnotation.Clickable, start: Int, end: Int) = addLinkImpl(clickable, start, end)
42+
@Composable
43+
override fun AddLink(clickable: OudsLinkAnnotation.Clickable, start: Int, end: Int) = AddLinkImpl(clickable, start, end)
3944

45+
@Composable
4046
override fun pushLink(link: OudsLinkAnnotation): Int = pushLinkImpl(link)
4147
}
4248
}
4349

44-
fun buildOudsAnnotatedAlertMessageString(builder: (OudsAnnotatedAlertMessageDescription.Builder).() -> Unit): OudsAnnotatedAlertMessageDescription {
50+
@Composable
51+
fun buildOudsAnnotatedAlertMessageDescription(builder: @Composable ((OudsAnnotatedAlertMessageDescription.Builder).() -> Unit)): OudsAnnotatedAlertMessageDescription {
4552
return buildOudsAnnotatedString<OudsAnnotatedAlertMessageDescription, OudsAnnotatedAlertMessageDescription.Builder>(builder)
4653
}

0 commit comments

Comments
 (0)