Use buildAnnotatedStringWithStyledPart and remove copied code.

This commit is contained in:
Benoit Marty 2023-08-29 16:38:21 +02:00
parent ff47629f6c
commit 79af05bc08
2 changed files with 16 additions and 39 deletions

View file

@ -59,6 +59,7 @@ fun String.toAnnotatedString(): AnnotatedString = buildAnnotatedString {
* @param color the color to apply to the string
* @param underline whether to underline the string
* @param bold whether to bold the string
* @param tagAndLink an optional pair of tag and link to add to the styled part of the string, as StringAnnotation
*/
@Composable
fun buildAnnotatedStringWithStyledPart(
@ -67,6 +68,7 @@ fun buildAnnotatedStringWithStyledPart(
color: Color = LinkColor,
underline: Boolean = true,
bold: Boolean = false,
tagAndLink: Pair<String, String>? = null,
) = buildAnnotatedString {
val coloredPart = stringResource(coloredTextRes)
val fullText = stringResource(fullTextRes, coloredPart)
@ -81,6 +83,14 @@ fun buildAnnotatedStringWithStyledPart(
start = startIndex,
end = startIndex + coloredPart.length,
)
if (tagAndLink != null) {
addStringAnnotation(
tag = tagAndLink.first,
annotation = tagAndLink.second,
start = startIndex,
end = startIndex + coloredPart.length
)
}
}
/**