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] annotatedStringResource with args don't render annotation styles #253

Closed
soulcramer opened this issue Mar 9, 2023 · 2 comments · Fixed by #887
Closed

[Fix] annotatedStringResource with args don't render annotation styles #253

soulcramer opened this issue Mar 9, 2023 · 2 comments · Fixed by #887
Assignees
Labels
Bug Bug to fix

Comments

@soulcramer
Copy link
Contributor

Currently we're converting the spanned string from getText into html to be able to insert the args in their format placeholder and then convert this html back to a spanned string. Sadly this doesn't support the annotation tag as it's not part of the html spec.

I found this lib that might help us resolve this 🤔

@soulcramer soulcramer added the Bug Bug to fix label Mar 9, 2023
@soulcramer soulcramer added this to the Backlog milestone Mar 9, 2023
@thomas-leguellec thomas-leguellec removed this from the Backlog milestone Mar 22, 2023
@thomas-leguellec thomas-leguellec added the ✍️ To refine The ticket must to be refine during "Refinement meeting" label Mar 23, 2023
@soulcramer soulcramer added this to the Backlog milestone Mar 27, 2023
@thomas-leguellec thomas-leguellec added ✅ Ready All the needed informations are detailed within the ticket to include it in another sprint and removed ✍️ To refine The ticket must to be refine during "Refinement meeting" labels Apr 5, 2023
@thomas-leguellec thomas-leguellec added Bug Bug to fix and removed Bug Bug to fix labels Apr 18, 2023
@thomas-leguellec thomas-leguellec modified the milestones: Backlog, Q3 Jun 28, 2023
@thomas-leguellec thomas-leguellec modified the milestones: Q3 Backlog, Backlog Aug 22, 2023
@thomas-leguellec thomas-leguellec changed the title annotatedStringResource with args don't render annotation styles [Fix] annotatedStringResource with args don't render annotation styles Aug 29, 2023
@SimonMarquis
Copy link
Member

SimonMarquis commented Dec 21, 2023

@soulcramer, happy to report that this solution works pretty well:

<string name="hello">Hello, <b><annotation variable="who" typography="large" color="main">who</annotation></b>!</string>
@Composable
public fun annotatedStringResource(@StringRes id: Int, args: PersistentMap<String, String>): AnnotatedString {
    val resources = resources()
    val density = LocalDensity.current
    val colors = SparkTheme.colors
    val typography = SparkTheme.typography
    return remember(id, values) {
        resources
            .buildSpannedStringWithArgs(id, args)
            .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)) }
}
@Preview
@Composable
private fun AnnotatedStringResourcePreview() {
    PreviewTheme {
        Text(
            text = annotatedStringResource(
                R.string.spark_annotatedStringResource_test,
                persistentMapOf("who" to "Bob"),
            ),
        )
    }
}
image

@soulcramer soulcramer self-assigned this Jan 4, 2024
@soulcramer soulcramer modified the milestones: Backlog, Q4 - Sprint 7 Jan 4, 2024
@soulcramer
Copy link
Contributor Author

Nice! I'll add it then 🥳

@kazaky kazaky assigned kazaky and unassigned soulcramer Jan 4, 2024
@thomas-leguellec thomas-leguellec removed the ✅ Ready All the needed informations are detailed within the ticket to include it in another sprint label Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug to fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants