22
33package com.inspiredandroid.linuxcommandbibliotheca.ui.screens.commanddetail
44
5- import android.widget.TextView
5+ import android.text.style.StyleSpan
6+ import android.text.style.UnderlineSpan
67import androidx.compose.foundation.clickable
78import androidx.compose.foundation.layout.Column
89import androidx.compose.foundation.layout.Spacer
@@ -17,11 +18,14 @@ import androidx.compose.material.MaterialTheme
1718import androidx.compose.material.Text
1819import androidx.compose.runtime.Composable
1920import androidx.compose.ui.Modifier
20- import androidx.compose.ui.graphics.toArgb
21+ import androidx.compose.ui.text.AnnotatedString
22+ import androidx.compose.ui.text.SpanStyle
23+ import androidx.compose.ui.text.buildAnnotatedString
24+ import androidx.compose.ui.text.font.FontStyle
2125import androidx.compose.ui.text.font.FontWeight
26+ import androidx.compose.ui.text.style.TextDecoration
2227import androidx.compose.ui.unit.dp
2328import androidx.compose.ui.unit.sp
24- import androidx.compose.ui.viewinterop.AndroidView
2529import androidx.core.text.HtmlCompat
2630import com.inspiredandroid.linuxcommandbibliotheca.ui.composables.CommandView
2731import com.linuxcommandlibrary.shared.CommandElement
@@ -57,15 +61,21 @@ fun CommandDetailScreen(
5761 items = viewModel.sections,
5862 key = { it.id },
5963 ) { section ->
60- CommandSectionColumn (viewModel, section, onNavigate)
64+ CommandSectionColumn (
65+ section = section,
66+ isCollapsed = viewModel.isGroupCollapsed(section.id),
67+ onToggleCollapse = { id -> viewModel.toggleCollapse(id) },
68+ onNavigate = onNavigate,
69+ )
6170 }
6271 }
6372}
6473
6574@Composable
6675private fun CommandSectionColumn (
67- viewModel : CommandDetailViewModel ,
6876 section : CommandSection ,
77+ isCollapsed : Boolean ,
78+ onToggleCollapse : (Long ) -> Unit ,
6979 onNavigate : (String ) -> Unit ,
7080) {
7181 ListItem (
@@ -77,11 +87,11 @@ private fun CommandSectionColumn(
7787 )
7888 },
7989 modifier = Modifier .clickable {
80- viewModel.toggleCollapse (section.id)
90+ onToggleCollapse (section.id)
8191 },
8292 )
8393
84- if (viewModel.isGroupCollapsed(section.id) ) {
94+ if (isCollapsed ) {
8595 if (section.title == " TLDR" ) {
8696 Column (
8797 modifier = Modifier .padding(start = 16 .dp, end = 16 .dp),
@@ -106,17 +116,39 @@ private fun CommandSectionColumn(
106116 }
107117 }
108118 } else {
109- val color = MaterialTheme .colors.onSurface.toArgb()
110119 ListItem (text = {
111- AndroidView (factory = { context ->
112- TextView (context).apply {
113- val content =
114- HtmlCompat .fromHtml(section.content, HtmlCompat .FROM_HTML_MODE_LEGACY )
115- text = content.dropLastWhile { it.isWhitespace() }
116- setTextColor(color)
117- }
118- })
120+ Text (
121+ text = section.content.toAnnotatedString(),
122+ color = MaterialTheme .colors.onSurface,
123+ )
119124 })
120125 }
121126 }
122127}
128+
129+ fun String.toAnnotatedString (): AnnotatedString {
130+ val spanned = HtmlCompat .fromHtml(this , HtmlCompat .FROM_HTML_MODE_LEGACY )
131+
132+ return buildAnnotatedString {
133+ append(spanned.toString().trim(' \n ' , ' ' ))
134+
135+ spanned.getSpans(0 , spanned.length, Any ::class .java).forEach { span ->
136+ val start = spanned.getSpanStart(span)
137+ val end = spanned.getSpanEnd(span)
138+
139+ when (span) {
140+ is StyleSpan -> when (span.style) {
141+ android.graphics.Typeface .BOLD -> {
142+ addStyle(SpanStyle (fontWeight = FontWeight .Bold ), start, end)
143+ }
144+ android.graphics.Typeface .ITALIC -> {
145+ addStyle(SpanStyle (fontStyle = FontStyle .Italic ), start, end)
146+ }
147+ }
148+ is UnderlineSpan -> {
149+ addStyle(SpanStyle (textDecoration = TextDecoration .Underline ), start, end)
150+ }
151+ }
152+ }
153+ }
154+ }
0 commit comments