1- @file:OptIn(ExperimentalMaterialApi ::class )
1+ @file:OptIn(ExperimentalMaterialApi ::class , ExperimentalLayoutApi :: class )
22
33package com.inspiredandroid.linuxcommandbibliotheca.ui.screens.commanddetail
44
55import android.text.style.StyleSpan
66import android.text.style.UnderlineSpan
77import androidx.compose.foundation.clickable
8+ import androidx.compose.foundation.layout.Arrangement
89import androidx.compose.foundation.layout.Column
10+ import androidx.compose.foundation.layout.ExperimentalLayoutApi
11+ import androidx.compose.foundation.layout.FlowRow
912import androidx.compose.foundation.layout.Spacer
1013import androidx.compose.foundation.layout.fillMaxSize
1114import androidx.compose.foundation.layout.height
1215import androidx.compose.foundation.layout.padding
1316import androidx.compose.foundation.lazy.LazyColumn
1417import androidx.compose.foundation.lazy.items
18+ import androidx.compose.material.Chip
1519import androidx.compose.material.ExperimentalMaterialApi
1620import androidx.compose.material.ListItem
1721import androidx.compose.material.MaterialTheme
1822import androidx.compose.material.Text
1923import androidx.compose.runtime.Composable
24+ import androidx.compose.runtime.remember
2025import androidx.compose.ui.Modifier
2126import androidx.compose.ui.text.AnnotatedString
2227import androidx.compose.ui.text.SpanStyle
@@ -29,6 +34,7 @@ import androidx.compose.ui.unit.sp
2934import androidx.core.text.HtmlCompat
3035import com.inspiredandroid.linuxcommandbibliotheca.ui.composables.CommandView
3136import com.linuxcommandlibrary.shared.CommandElement
37+ import com.linuxcommandlibrary.shared.databaseHelper
3238import databases.CommandSection
3339import org.koin.androidx.compose.koinViewModel
3440import org.koin.core.parameter.parametersOf
@@ -92,41 +98,87 @@ private fun CommandSectionColumn(
9298 )
9399
94100 if (isCollapsed) {
95- if (section.title == " TLDR" ) {
96- Column (
97- modifier = Modifier .padding(start = 16 .dp, end = 16 .dp),
98- ) {
99- val tldrParts = section.content.split(" <b>" )
100- tldrParts.forEachIndexed { index, s ->
101- val split = s.split(" </b>" )
102- if (split.size > 1 ) {
103- Text (
104- text = split[0 ],
105- fontSize = 16 .sp,
106- style = MaterialTheme .typography.subtitle1,
107- fontWeight = FontWeight .Bold ,
108- )
101+ when (section.title) {
102+ " TLDR" -> {
103+ Column (
104+ modifier = Modifier .padding(horizontal = 16 .dp),
105+ ) {
106+ val tldrParts = section.content.split(" <b>" )
107+ tldrParts.forEachIndexed { index, s ->
108+ val split = s.split(" </b>" )
109+ if (split.size > 1 ) {
110+ Text (
111+ text = split[0 ],
112+ fontSize = 15 .sp,
113+ style = MaterialTheme .typography.subtitle1,
114+ fontWeight = FontWeight .Bold ,
115+ )
109116
110- val command = " $ " + split[1 ].replace(" <br>" , " " ).replace(" `" , " " )
111- CommandView (command, listOf (CommandElement .Text (command)), onNavigate)
117+ val command = " $ " + split[1 ].replace(" <br>" , " " ).replace(" `" , " " )
118+ CommandView (
119+ command = command,
120+ elements = listOf (CommandElement .Text (command)),
121+ onNavigate = onNavigate,
122+ )
123+ }
124+ if (index != tldrParts.lastIndex) {
125+ Spacer (Modifier .height(6 .dp))
126+ }
112127 }
113- if (index != tldrParts.lastIndex) {
114- Spacer (Modifier .height(8 .dp))
128+ }
129+ }
130+ " SEE ALSO" -> {
131+ val commands = remember {
132+ getCommands(section.content)
133+ }
134+ if (commands.isNotEmpty()) {
135+ FlowRow (
136+ modifier = Modifier .padding(horizontal = 16 .dp),
137+ horizontalArrangement = Arrangement .spacedBy(8 .dp),
138+ ) {
139+ commands.forEach { name ->
140+ Chip (onClick = {
141+ onNavigate(" command?commandName=$name " )
142+ }) {
143+ Text (
144+ text = name,
145+ color = MaterialTheme .colors.onSurface,
146+ )
147+ }
148+ }
115149 }
150+ } else {
151+ // fallback
152+ Text (
153+ modifier = Modifier .padding(horizontal = 16 .dp),
154+ text = section.content.toAnnotatedString(),
155+ color = MaterialTheme .colors.onSurface,
156+ )
116157 }
117158 }
118- } else {
119- ListItem (text = {
159+ else -> {
120160 Text (
161+ modifier = Modifier .padding(horizontal = 16 .dp),
121162 text = section.content.toAnnotatedString(),
122163 color = MaterialTheme .colors.onSurface,
123164 )
124- })
165+ }
125166 }
126167 }
127168}
128169
129- fun String.toAnnotatedString (): AnnotatedString {
170+ private fun getCommands (input : String ): List <String > {
171+ val commands = input.split(" ," ).map { it.trim() }
172+
173+ return commands
174+ .map { command ->
175+ command.replace(Regex (" \\ (\\ d+\\ )$" ), " " ).trim()
176+ }.filter {
177+ databaseHelper.getCommand(it) != null
178+ }
179+ }
180+
181+ private fun String.toAnnotatedString (): AnnotatedString {
130182 val spanned = HtmlCompat .fromHtml(this , HtmlCompat .FROM_HTML_MODE_LEGACY )
131183
132184 return buildAnnotatedString {
0 commit comments