Skip to content

Commit

Permalink
Fixed crash when a resource string was not styled as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecowboy committed Nov 23, 2023
1 parent 3bb9665 commit e89df3b
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.nononsenseapps.feeder.ui.compose.text

import android.content.res.Resources
import android.text.Annotation
import android.text.SpannedString
import android.text.Spanned
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
Expand All @@ -24,20 +24,22 @@ fun resources(): Resources {
@Composable
fun annotatedStringResource(@StringRes id: Int): AnnotatedString {
val resources = resources()
val text = resources.getText(id) as SpannedString
val text = resources.getText(id)

return buildAnnotatedString {
this.append(text.toString())

for (annotation in text.getSpans<Annotation>()) {
when (annotation.key) {
"style" -> {
getSpanStyle(annotation.value)?.let { spanStyle ->
addStyle(
spanStyle,
text.getSpanStart(annotation),
text.getSpanEnd(annotation),
)
if (text is Spanned) {
for (annotation in text.getSpans<Annotation>()) {
when (annotation.key) {
"style" -> {
getSpanStyle(annotation.value)?.let { spanStyle ->
addStyle(
spanStyle,
text.getSpanStart(annotation),
text.getSpanEnd(annotation),
)
}
}
}
}
Expand Down

0 comments on commit e89df3b

Please sign in to comment.