Skip to content

Commit 83c6e8d

Browse files
committed
Add rich text for bullet list
1 parent 8afa331 commit 83c6e8d

2 files changed

Lines changed: 91 additions & 11 deletions

File tree

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

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ 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
5455
import androidx.compose.ui.text.TextStyle
5556
import androidx.compose.ui.text.style.TextAlign
5657
import androidx.compose.ui.tooling.preview.Preview
@@ -60,6 +61,7 @@ import androidx.compose.ui.unit.LayoutDirection
6061
import androidx.compose.ui.unit.dp
6162
import com.orange.ouds.core.R
6263
import com.orange.ouds.core.component.OudsBulletListUnorderedAsset.Bullet.extraParameters
64+
import com.orange.ouds.core.component.common.text.OudsAnnotatedBulletListLabel
6365
import com.orange.ouds.core.component.content.OudsComponentContent
6466
import com.orange.ouds.core.component.content.OudsComponentIcon
6567
import com.orange.ouds.core.component.content.OudsPolymorphicComponentContent
@@ -149,6 +151,36 @@ class OudsBulletListBuilder internal constructor() {
149151
subListType: OudsBulletListType? = null,
150152
subListTextStyle: OudsBulletListTextStyle? = null,
151153
builder: (OudsBulletListBuilder.() -> Unit)? = null
154+
) {
155+
item(label as CharSequence, subListType, subListTextStyle, builder)
156+
}
157+
158+
/**
159+
* Adds an item to the bullet list.
160+
*
161+
* This function can also define a nested sub-list by providing a `builder` lambda.
162+
*
163+
* @param label The annotated text content of the list item.
164+
* @param subListType The specific [OudsBulletListType] for the nested sub-list, if any.
165+
* If `null`, the type is inherited from the parent list.
166+
* @param subListTextStyle The specific [OudsBulletListTextStyle] for the nested sub-list, if any.
167+
* If `null`, the text style is inherited from the parent list.
168+
* @param builder A lambda scope for defining nested list items.
169+
*/
170+
fun item(
171+
label: OudsAnnotatedBulletListLabel,
172+
subListType: OudsBulletListType? = null,
173+
subListTextStyle: OudsBulletListTextStyle? = null,
174+
builder: (OudsBulletListBuilder.() -> Unit)? = null
175+
) {
176+
item(label.annotatedString, subListType, subListTextStyle, builder)
177+
}
178+
179+
private fun item(
180+
label: CharSequence,
181+
subListType: OudsBulletListType? = null,
182+
subListTextStyle: OudsBulletListTextStyle? = null,
183+
builder: (OudsBulletListBuilder.() -> Unit)? = null
152184
) {
153185
val subListItems = builder?.let { OudsBulletListBuilder().apply(it).build() }.orEmpty()
154186
items.add(BulletListItem(label, subListType, subListTextStyle, subListItems))
@@ -247,16 +279,18 @@ private fun OudsBulletListItem(
247279
OudsBulletListFontSize.BodyLarge -> OudsTheme.sizes.maxWidth.type.body.large
248280
OudsBulletListFontSize.BodyMedium -> OudsTheme.sizes.maxWidth.type.body.medium
249281
}
250-
Text(
251-
modifier = Modifier
252-
.fillMaxHeight()
253-
.wrapContentHeight() // Allows to center the text vertically when its height is smaller than the row height
254-
.widthIn(max = textMaxWidth)
255-
.clearAndSetSemantics {},
256-
text = item.label,
257-
style = currentTextStyle.toTextStyle(),
258-
color = OudsTheme.colorScheme.content.default
259-
)
282+
val textModifier = Modifier
283+
.fillMaxHeight()
284+
.wrapContentHeight() // Allows to center the text vertically when its height is smaller than the row height
285+
.widthIn(max = textMaxWidth)
286+
.clearAndSetSemantics {}
287+
val text = item.label
288+
val textStyle = currentTextStyle.toTextStyle()
289+
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)
293+
}
260294
}
261295

262296
if (item.subListItems.isNotEmpty()) {
@@ -284,7 +318,7 @@ private fun OudsBulletListItem(
284318
}
285319

286320
internal data class BulletListItem(
287-
val label: String,
321+
val label: CharSequence,
288322
val subListType: OudsBulletListType?,
289323
val subListTextStyle: OudsBulletListTextStyle?,
290324
val subListItems: List<BulletListItem> = emptyList()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Software Name: OUDS Android
3+
* SPDX-FileCopyrightText: Copyright (c) Orange SA
4+
* SPDX-License-Identifier: MIT
5+
*
6+
* This software is distributed under the MIT license,
7+
* the text of which is available at https://opensource.org/license/MIT/
8+
* or see the "LICENSE" file for more details.
9+
*
10+
* Software description: Android library of reusable graphical components
11+
*/
12+
13+
package com.orange.ouds.core.component.common.text
14+
15+
import androidx.compose.ui.text.AnnotatedString
16+
17+
class OudsAnnotatedBulletListLabel internal constructor(annotatedString: AnnotatedString) :
18+
OudsAnnotatedString<OudsAnnotatedBulletListLabel>(annotatedString) {
19+
20+
class Builder(capacity: Int = 16) :
21+
OudsAnnotatedString.Builder<OudsAnnotatedBulletListLabel>(capacity, OudsAnnotatedBulletListLabel::class.java),
22+
StrongBuilder, LinkBuilder {
23+
24+
constructor(text: String) : this() {
25+
append(text)
26+
}
27+
28+
constructor(text: OudsAnnotatedHelperText) : this() {
29+
append(text)
30+
}
31+
32+
override fun addStrong(start: Int, end: Int) = addStrongImpl(start, end)
33+
34+
override fun pushStrong(): Int = pushStrongImpl()
35+
36+
override fun addLink(url: OudsLinkAnnotation.Url, start: Int, end: Int) = addLinkImpl(url, start, end)
37+
38+
override fun addLink(clickable: OudsLinkAnnotation.Clickable, start: Int, end: Int) = addLinkImpl(clickable, start, end)
39+
40+
override fun pushLink(link: OudsLinkAnnotation): Int = pushLinkImpl(link)
41+
}
42+
}
43+
44+
fun buildOudsAnnotatedBulletListLabel(builder: (OudsAnnotatedBulletListLabel.Builder).() -> Unit): OudsAnnotatedBulletListLabel {
45+
return buildOudsAnnotatedString<OudsAnnotatedBulletListLabel, OudsAnnotatedBulletListLabel.Builder>(builder)
46+
}

0 commit comments

Comments
 (0)