Skip to content

Commit e89df3b

Browse files
committed
Fixed crash when a resource string was not styled as expected
1 parent 3bb9665 commit e89df3b

File tree

1 file changed

+13
-11
lines changed
  • app/src/main/java/com/nononsenseapps/feeder/ui/compose/text

1 file changed

+13
-11
lines changed

app/src/main/java/com/nononsenseapps/feeder/ui/compose/text/Extensions.kt

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.nononsenseapps.feeder.ui.compose.text
22

33
import android.content.res.Resources
44
import android.text.Annotation
5-
import android.text.SpannedString
5+
import android.text.Spanned
66
import androidx.annotation.StringRes
77
import androidx.compose.runtime.Composable
88
import androidx.compose.runtime.ReadOnlyComposable
@@ -24,20 +24,22 @@ fun resources(): Resources {
2424
@Composable
2525
fun annotatedStringResource(@StringRes id: Int): AnnotatedString {
2626
val resources = resources()
27-
val text = resources.getText(id) as SpannedString
27+
val text = resources.getText(id)
2828

2929
return buildAnnotatedString {
3030
this.append(text.toString())
3131

32-
for (annotation in text.getSpans<Annotation>()) {
33-
when (annotation.key) {
34-
"style" -> {
35-
getSpanStyle(annotation.value)?.let { spanStyle ->
36-
addStyle(
37-
spanStyle,
38-
text.getSpanStart(annotation),
39-
text.getSpanEnd(annotation),
40-
)
32+
if (text is Spanned) {
33+
for (annotation in text.getSpans<Annotation>()) {
34+
when (annotation.key) {
35+
"style" -> {
36+
getSpanStyle(annotation.value)?.let { spanStyle ->
37+
addStyle(
38+
spanStyle,
39+
text.getSpanStart(annotation),
40+
text.getSpanEnd(annotation),
41+
)
42+
}
4143
}
4244
}
4345
}

0 commit comments

Comments
 (0)