Skip to content

Commit 07a52ca

Browse files
committed
fromHtml compat function
1 parent b9ea6b5 commit 07a52ca

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

app/src/main/java/com/anysoftkeyboard/janus/app/ui/TranslateScreen.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.anysoftkeyboard.janus.app.ui
22

3+
import android.os.Build
34
import android.text.Html
45
import android.text.method.LinkMovementMethod
56
import android.widget.TextView
@@ -38,6 +39,15 @@ import com.anysoftkeyboard.janus.app.ui.data.UiTranslation
3839
import com.anysoftkeyboard.janus.app.viewmodels.TranslateViewModel
3940
import com.anysoftkeyboard.janus.network.SearchResult
4041

42+
private fun setHtmlToText(view: TextView, snippet: String) {
43+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
44+
view.text = Html.fromHtml(snippet, Html.FROM_HTML_MODE_COMPACT)
45+
} else {
46+
@Suppress("DEPRECATION")
47+
view.text = Html.fromHtml(snippet)
48+
}
49+
}
50+
4151
@Composable
4252
fun TranslateScreen(viewModel: TranslateViewModel) {
4353
var text by remember { mutableStateOf("") }
@@ -92,7 +102,7 @@ fun SearchResultItem(result: SearchResult, onClick: () -> Unit) {
92102
factory = { context ->
93103
TextView(context).apply { movementMethod = LinkMovementMethod.getInstance() }
94104
},
95-
update = { it.text = Html.fromHtml(result.snippet, Html.FROM_HTML_MODE_COMPACT) })
105+
update = { setHtmlToText(it, result.snippet) })
96106
}
97107
}
98108
}
@@ -132,9 +142,7 @@ fun TranslationCard(translation: UiTranslation) {
132142
factory = { context ->
133143
TextView(context).apply { movementMethod = LinkMovementMethod.getInstance() }
134144
},
135-
update = {
136-
it.text = Html.fromHtml(translation.shortDescription ?: "", Html.FROM_HTML_MODE_COMPACT)
137-
})
145+
update = { setHtmlToText(it, translation.shortDescription ?: "") })
138146
IconButton(onClick = { /* TODO */ }) {
139147
Icon(imageVector = translation.favoriteIcon, contentDescription = "Favorite")
140148
}

0 commit comments

Comments
 (0)