Use buildAnnotatedStringWithStyledPart and remove copied code.
This commit is contained in:
parent
03864b3eb0
commit
88786cdda6
2 changed files with 16 additions and 39 deletions
|
|
@ -16,26 +16,24 @@
|
||||||
|
|
||||||
package io.element.android.features.analytics.api.preferences
|
package io.element.android.features.analytics.api.preferences
|
||||||
|
|
||||||
import androidx.annotation.StringRes
|
|
||||||
import androidx.compose.foundation.text.ClickableText
|
import androidx.compose.foundation.text.ClickableText
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.text.SpanStyle
|
|
||||||
import androidx.compose.ui.text.buildAnnotatedString
|
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.Preview
|
||||||
import androidx.compose.ui.tooling.preview.PreviewParameter
|
import androidx.compose.ui.tooling.preview.PreviewParameter
|
||||||
import io.element.android.features.analytics.api.AnalyticsOptInEvents
|
import io.element.android.features.analytics.api.AnalyticsOptInEvents
|
||||||
import io.element.android.libraries.designsystem.components.list.ListItemContent
|
import io.element.android.libraries.designsystem.components.list.ListItemContent
|
||||||
import io.element.android.libraries.designsystem.preview.ElementPreviewDark
|
import io.element.android.libraries.designsystem.preview.ElementPreviewDark
|
||||||
import io.element.android.libraries.designsystem.preview.ElementPreviewLight
|
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.ListItem
|
||||||
import io.element.android.libraries.designsystem.theme.components.Text
|
import io.element.android.libraries.designsystem.theme.components.Text
|
||||||
import io.element.android.libraries.theme.LinkColor
|
|
||||||
import io.element.android.libraries.ui.strings.CommonStrings
|
import io.element.android.libraries.ui.strings.CommonStrings
|
||||||
|
|
||||||
|
private const val LINK_TAG = "link"
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun AnalyticsPreferencesView(
|
fun AnalyticsPreferencesView(
|
||||||
state: AnalyticsPreferencesState,
|
state: AnalyticsPreferencesState,
|
||||||
|
|
@ -47,10 +45,10 @@ fun AnalyticsPreferencesView(
|
||||||
}
|
}
|
||||||
|
|
||||||
val firstPart = stringResource(id = CommonStrings.screen_analytics_settings_help_us_improve, state.applicationName)
|
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,
|
||||||
CommonStrings.screen_analytics_settings_read_terms_content_link,
|
CommonStrings.screen_analytics_settings_read_terms_content_link,
|
||||||
link = state.policyUrl,
|
tagAndLink = LINK_TAG to state.policyUrl,
|
||||||
)
|
)
|
||||||
val subtitle = buildAnnotatedString {
|
val subtitle = buildAnnotatedString {
|
||||||
append(firstPart)
|
append(firstPart)
|
||||||
|
|
@ -67,7 +65,7 @@ fun AnalyticsPreferencesView(
|
||||||
text = subtitle,
|
text = subtitle,
|
||||||
onClick = {
|
onClick = {
|
||||||
subtitle
|
subtitle
|
||||||
.getStringAnnotations("link", it, it)
|
.getStringAnnotations(LINK_TAG, it, it)
|
||||||
.firstOrNull()?.let { stringAnnotation ->
|
.firstOrNull()?.let { stringAnnotation ->
|
||||||
onOpenAnalyticsPolicy(stringAnnotation.item)
|
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
|
@Preview
|
||||||
@Composable
|
@Composable
|
||||||
internal fun AnalyticsPreferencesViewLightPreview(@PreviewParameter(AnalyticsPreferencesStateProvider::class) state: AnalyticsPreferencesState) =
|
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 color the color to apply to the string
|
||||||
* @param underline whether to underline the string
|
* @param underline whether to underline the string
|
||||||
* @param bold whether to bold 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
|
@Composable
|
||||||
fun buildAnnotatedStringWithStyledPart(
|
fun buildAnnotatedStringWithStyledPart(
|
||||||
|
|
@ -67,6 +68,7 @@ fun buildAnnotatedStringWithStyledPart(
|
||||||
color: Color = LinkColor,
|
color: Color = LinkColor,
|
||||||
underline: Boolean = true,
|
underline: Boolean = true,
|
||||||
bold: Boolean = false,
|
bold: Boolean = false,
|
||||||
|
tagAndLink: Pair<String, String>? = null,
|
||||||
) = buildAnnotatedString {
|
) = buildAnnotatedString {
|
||||||
val coloredPart = stringResource(coloredTextRes)
|
val coloredPart = stringResource(coloredTextRes)
|
||||||
val fullText = stringResource(fullTextRes, coloredPart)
|
val fullText = stringResource(fullTextRes, coloredPart)
|
||||||
|
|
@ -81,6 +83,14 @@ fun buildAnnotatedStringWithStyledPart(
|
||||||
start = startIndex,
|
start = startIndex,
|
||||||
end = startIndex + coloredPart.length,
|
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