Use buildAnnotatedStringWithStyledPart and remove copied code.
This commit is contained in:
parent
ff47629f6c
commit
79af05bc08
2 changed files with 16 additions and 39 deletions
|
|
@ -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) =
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue