Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix annotatedStringResource with args don't render annotation style #887

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import androidx.core.text.buildSpannedString
import androidx.core.text.parseAsHtml
import androidx.core.text.toHtml
import com.adevinta.spark.PreviewTheme
import com.adevinta.spark.R
import com.adevinta.spark.SparkTheme
import com.adevinta.spark.tokens.SparkColors
import com.adevinta.spark.tokens.SparkTypography

// FIXME: There is no official way to do this yet so we're waiting for
// https://issuetracker.google.com/issues/139320238 to be fixed
import kotlinx.collections.immutable.PersistentMap
import kotlinx.collections.immutable.persistentMapOf

/**
* Load a annotated string resource with formatting.
Expand All @@ -80,17 +80,25 @@ import com.adevinta.spark.tokens.SparkTypography
* @return the [AnnotatedString] data associated with the resource
*/
@Composable
public fun annotatedStringResource(@StringRes id: Int, vararg formatArgs: Any): AnnotatedString {
public fun annotatedStringResource(@StringRes id: Int, formatArgs: PersistentMap<String, String>): AnnotatedString {
val resources = resources()
val density = LocalDensity.current
val colors = SparkTheme.colors
val typography = SparkTheme.typography
return remember(id, formatArgs) {
val text = resources.getText(id, *formatArgs)
text.asAnnotatedString(density, colors, typography)
resources.buildSpannedStringWithArgs(id, formatArgs).asAnnotatedString(density, colors, typography)
}
}

private fun Resources.buildSpannedStringWithArgs(
@StringRes id: Int,
args: PersistentMap<String, String>,
): SpannedString = buildSpannedString {
append(getText(id))
getSpans(0, length, Annotation::class.java).filterIsInstance<Annotation>().filter { it.key == "variable" }
.forEach { replace(getSpanStart(it), getSpanEnd(it), args.getValue(it.value)) }
}

/**
* Load a annotated string resource.
*
Expand Down Expand Up @@ -163,22 +171,6 @@ internal fun resources(): Resources {
return LocalContext.current.resources
}

/**
* The framework `getText()` method doesn't support formatting arguments, so we need to do it ourselves.
*
* Unfortunately `toHtml()` doesn't support the `<annotation>` tag so we loose this span as we need to convert it to a
* [String] to be able to use `String.format()`.
*/
internal fun Resources.getText(@StringRes id: Int, vararg args: Any): CharSequence {
val escapedArgs = args.map {
if (it is Spanned) it.toHtmlWithoutParagraphs() else it
}.toTypedArray()
val spannedString = SpannedString(getText(id))
val htmlResource = spannedString.toHtmlWithoutParagraphs()
val formattedHtml = String.format(htmlResource, *escapedArgs)
return formattedHtml.parseAsHtml()
}

internal fun Resources.getQuantityText(@PluralsRes id: Int, quantity: Int, vararg args: Any): CharSequence {
val escapedArgs = args.map {
if (it is Spanned) it.toHtmlWithoutParagraphs() else it
Expand Down Expand Up @@ -275,6 +267,11 @@ private fun AnnotatedStringResourcePreview() {
Text(
text = annotatedStringResource(R.string.spark_annotatedStringResource_test),
)
Text(annotatedStringResource(R.string.spark_annotatedStringResource_test_args, "Spark"))
Text(
text = annotatedStringResource(
R.string.spark_annotatedStringResource_test_args,
persistentMapOf("who" to "Bob"),
),
)
}
}
2 changes: 1 addition & 1 deletion spark/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</plurals>

<string name="spark_annotatedStringResource_test">Les <b>Meilleures</b> <annotation typography="large">pratiques</annotation> pour les textes sur <i><annotation color="main">Android</annotation></i></string>
<string name="spark_annotatedStringResource_test_args">Les <b>Meilleures</b> <annotation typography="large">pratiques</annotation> <p dir="rtl">pour les %s sur</p> <p dir="ltrr"><i><annotation color="main">Android</annotation></i></p></string>
<string name="spark_annotatedStringResource_test_args">Salut, <b><annotation variable="who" typography="large" color="main">who</annotation></b>!</string>

<!--Rating-->
<string name="spark_textfield_content_description">Obligatoire</string>
Expand Down
2 changes: 1 addition & 1 deletion spark/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</plurals>

<string name="spark_annotatedStringResource_test"><b>Best</b> <annotation typography="large">practices</annotation> <p dir="rtl">for text on</p> <i><annotation color="main">Android</annotation></i></string>
<string name="spark_annotatedStringResource_test_args"><b>Best</b> <annotation typography="large">practices</annotation> <p dir="rtl">for %s on</p> <i><annotation color="main">Android</annotation></i></string>
<string name="spark_annotatedStringResource_test_args">Hello, <b><annotation variable="who" typography="large" color="main">who</annotation></b>!</string>

<!--Rating-->
<string name="spark_textfield_content_description">Mandatory</string>
Expand Down
Loading