From 79af05bc08eccee7834542d35b4b4b6fd2074269 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 29 Aug 2023 16:38:21 +0200 Subject: [PATCH] Use buildAnnotatedStringWithStyledPart and remove copied code. --- .../preferences/AnalyticsPreferencesView.kt | 45 +++---------------- .../designsystem/text/AnnotatedStrings.kt | 10 +++++ 2 files changed, 16 insertions(+), 39 deletions(-) diff --git a/features/analytics/api/src/main/kotlin/io/element/android/features/analytics/api/preferences/AnalyticsPreferencesView.kt b/features/analytics/api/src/main/kotlin/io/element/android/features/analytics/api/preferences/AnalyticsPreferencesView.kt index e5b40ee8a9..42775ef83f 100644 --- a/features/analytics/api/src/main/kotlin/io/element/android/features/analytics/api/preferences/AnalyticsPreferencesView.kt +++ b/features/analytics/api/src/main/kotlin/io/element/android/features/analytics/api/preferences/AnalyticsPreferencesView.kt @@ -16,26 +16,24 @@ package io.element.android.features.analytics.api.preferences -import androidx.annotation.StringRes import androidx.compose.foundation.text.ClickableText import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString -import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter import io.element.android.features.analytics.api.AnalyticsOptInEvents import io.element.android.libraries.designsystem.components.list.ListItemContent import io.element.android.libraries.designsystem.preview.ElementPreviewDark import io.element.android.libraries.designsystem.preview.ElementPreviewLight +import io.element.android.libraries.designsystem.text.buildAnnotatedStringWithStyledPart import io.element.android.libraries.designsystem.theme.components.ListItem import io.element.android.libraries.designsystem.theme.components.Text -import io.element.android.libraries.theme.LinkColor import io.element.android.libraries.ui.strings.CommonStrings +private const val LINK_TAG = "link" + @Composable fun AnalyticsPreferencesView( state: AnalyticsPreferencesState, @@ -47,10 +45,10 @@ fun AnalyticsPreferencesView( } val firstPart = stringResource(id = CommonStrings.screen_analytics_settings_help_us_improve, state.applicationName) - val secondPart = buildAnnotatedStringWithColoredPart( + val secondPart = buildAnnotatedStringWithStyledPart( CommonStrings.screen_analytics_settings_read_terms, CommonStrings.screen_analytics_settings_read_terms_content_link, - link = state.policyUrl, + tagAndLink = LINK_TAG to state.policyUrl, ) val subtitle = buildAnnotatedString { append(firstPart) @@ -67,7 +65,7 @@ fun AnalyticsPreferencesView( text = subtitle, onClick = { subtitle - .getStringAnnotations("link", it, it) + .getStringAnnotations(LINK_TAG, it, it) .firstOrNull()?.let { stringAnnotation -> onOpenAnalyticsPolicy(stringAnnotation.item) } @@ -81,37 +79,6 @@ fun AnalyticsPreferencesView( ) } -// TODO Use buildAnnotatedStringWithStyledPart. -@Composable -fun buildAnnotatedStringWithColoredPart( - @StringRes fullTextRes: Int, - @StringRes coloredTextRes: Int, - color: Color = LinkColor, - underline: Boolean = true, - link: String? = null, -) = buildAnnotatedString { - val coloredPart = stringResource(coloredTextRes) - val fullText = stringResource(fullTextRes, coloredPart) - val startIndex = fullText.indexOf(coloredPart) - append(fullText) - addStyle( - style = SpanStyle( - color = color, - textDecoration = if (underline) TextDecoration.Underline else null - ), - start = startIndex, - end = startIndex + coloredPart.length, - ) - if (link != null) { - addStringAnnotation( - tag = "link", - annotation = link, - start = startIndex, - end = startIndex + coloredPart.length - ) - } -} - @Preview @Composable internal fun AnalyticsPreferencesViewLightPreview(@PreviewParameter(AnalyticsPreferencesStateProvider::class) state: AnalyticsPreferencesState) = diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/text/AnnotatedStrings.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/text/AnnotatedStrings.kt index 779b7e7053..04a872a1cf 100644 --- a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/text/AnnotatedStrings.kt +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/text/AnnotatedStrings.kt @@ -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? = 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 + ) + } } /**