Skip to content

Commit 339c5c0

Browse files
committed
Move screen contents into separate content composable
1 parent f06f0b6 commit 339c5c0

11 files changed

Lines changed: 140 additions & 47 deletions

File tree

common/src/commonMain/kotlin/com/linuxcommandlibrary/shared/MarkdownModels.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package com.linuxcommandlibrary.shared
22

3+
import kotlinx.collections.immutable.ImmutableList
4+
35
/**
46
* Data class for parsed tip from markdown.
57
*/
68
data class TipInfo(
79
val id: Long,
810
val title: String,
9-
val sections: List<TipSectionElement>,
11+
val sections: ImmutableList<TipSectionElement>,
1012
)
1113

1214
/**
1315
* Data class for parsed basic category from markdown.
1416
*/
1517
data class BasicInfo(
1618
val title: String,
17-
val groups: List<BasicGroup>,
19+
val groups: ImmutableList<BasicGroup>,
1820
)
1921

2022
/**
@@ -23,7 +25,7 @@ data class BasicInfo(
2325
data class BasicGroup(
2426
val id: Long,
2527
val description: String,
26-
val sections: List<TipSectionElement>,
28+
val sections: ImmutableList<TipSectionElement>,
2729
)
2830

2931
/**
@@ -32,7 +34,7 @@ data class BasicGroup(
3234
data class CommandInfo(
3335
val name: String,
3436
val description: String?,
35-
val sections: List<CommandSectionInfo>,
37+
val sections: ImmutableList<CommandSectionInfo>,
3638
)
3739

3840
/**
@@ -41,7 +43,7 @@ data class CommandInfo(
4143
data class CommandSectionInfo(
4244
val title: String,
4345
val content: String,
44-
val elements: List<TipSectionElement>,
46+
val elements: ImmutableList<TipSectionElement>,
4547
)
4648

4749
/**

common/src/commonMain/kotlin/com/linuxcommandlibrary/shared/MarkdownParser.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.linuxcommandlibrary.shared
22

3+
import kotlinx.collections.immutable.ImmutableList
34
import kotlinx.collections.immutable.toImmutableList
45

56
/**
@@ -10,7 +11,7 @@ object MarkdownParser {
1011
/**
1112
* Parse text with **bold** and _italic_ formatting into TextElement list.
1213
*/
13-
fun parseTextWithBold(text: String): List<TextElement> {
14+
fun parseTextWithBold(text: String): ImmutableList<TextElement> {
1415
val elements = mutableListOf<TextElement>()
1516
val boldRegex = Regex("""\*\*([^*]+)\*\*""")
1617
val italicRegex = Regex("""_([^_]+)_""")
@@ -55,7 +56,7 @@ object MarkdownParser {
5556
}
5657
}
5758

58-
return elements
59+
return elements.toImmutableList()
5960
}
6061

6162
/**
@@ -106,7 +107,7 @@ object MarkdownParser {
106107
* Parse markdown content into sections suitable for display.
107108
* Handles bold text, code blocks (triple backticks), and blockquotes.
108109
*/
109-
fun parseMarkdownContent(content: String): List<TipSectionElement> {
110+
fun parseMarkdownContent(content: String): ImmutableList<TipSectionElement> {
110111
val sections = mutableListOf<TipSectionElement>()
111112
val lines = content.lines()
112113
var i = 0
@@ -184,7 +185,7 @@ object MarkdownParser {
184185
}
185186
}
186187

187-
return sections
188+
return sections.toImmutableList()
188189
}
189190

190191
/**
@@ -195,16 +196,16 @@ object MarkdownParser {
195196

196197
// Parse header row (first row)
197198
val headerLine = lines[0]
198-
val headers = splitTableRow(headerLine).map { parseTableCellContent(it) }
199+
val headers = splitTableRow(headerLine).map { parseTableCellContent(it) }.toImmutableList()
199200

200201
// Skip separator row (second row with dashes) and parse data rows
201202
val dataRows = lines.drop(2).mapNotNull { line ->
202203
// Skip separator rows (contain only |, -, and spaces)
203204
if (line.trim().replace(Regex("[|\\-\\s]"), "").isEmpty()) {
204205
return@mapNotNull null
205206
}
206-
splitTableRow(line).map { parseTableCellContent(it) }
207-
}
207+
splitTableRow(line).map { parseTableCellContent(it) }.toImmutableList()
208+
}.toImmutableList()
208209

209210
if (headers.isEmpty()) return null
210211

@@ -214,7 +215,7 @@ object MarkdownParser {
214215
/**
215216
* Parse content within a table cell, handling bold, italic, inline code, and man links.
216217
*/
217-
fun parseTableCellContent(cell: String): List<TextElement> {
218+
fun parseTableCellContent(cell: String): ImmutableList<TextElement> {
218219
val elements = mutableListOf<TextElement>()
219220
var remaining = cell
220221

@@ -273,7 +274,7 @@ object MarkdownParser {
273274
}
274275
}
275276

276-
return elements
277+
return elements.toImmutableList()
277278
}
278279

279280
/**
@@ -375,13 +376,13 @@ object MarkdownParser {
375376
/**
376377
* Parse a tips markdown file into list of TipInfo.
377378
*/
378-
fun parseTips(content: String): List<TipInfo> = splitByHeaders(content, "## ").map { (title, sectionContent) ->
379+
fun parseTips(content: String): ImmutableList<TipInfo> = splitByHeaders(content, "## ").map { (title, sectionContent) ->
379380
TipInfo(
380381
id = title.hashCode().toLong(),
381382
title = title,
382383
sections = parseMarkdownContent(sectionContent),
383384
)
384-
}
385+
}.toImmutableList()
385386

386387
/**
387388
* Parse a basics markdown file into BasicInfo.
@@ -405,7 +406,7 @@ object MarkdownParser {
405406
description = groupTitle,
406407
sections = parseMarkdownContent(groupContent),
407408
)
408-
}
409+
}.toImmutableList()
409410

410411
return BasicInfo(title = title, groups = groups)
411412
}
@@ -420,7 +421,7 @@ object MarkdownParser {
420421
content = sectionContent,
421422
elements = parseMarkdownContent(sectionContent),
422423
)
423-
}
424+
}.toImmutableList()
424425

425426
if (sections.isEmpty()) return null
426427

common/src/commonMain/kotlin/com/linuxcommandlibrary/shared/TipSectionElement.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ sealed class TextElement {
1717
* Represents different types of content sections in tips and basics.
1818
*/
1919
sealed class TipSectionElement {
20-
data class Text(val elements: List<TextElement>) : TipSectionElement()
21-
data class Blockquote(val elements: List<TextElement>) : TipSectionElement()
20+
data class Text(val elements: ImmutableList<TextElement>) : TipSectionElement()
21+
data class Blockquote(val elements: ImmutableList<TextElement>) : TipSectionElement()
2222
data class Code(val command: String, val elements: ImmutableList<CommandElement>) : TipSectionElement()
2323
data class Table(
24-
val headers: List<List<TextElement>>,
25-
val rows: List<List<List<TextElement>>>,
24+
val headers: ImmutableList<ImmutableList<TextElement>>,
25+
val rows: ImmutableList<ImmutableList<ImmutableList<TextElement>>>,
2626
) : TipSectionElement()
2727
}

composeApp/src/commonMain/kotlin/com/linuxcommandlibrary/app/ui/screens/basiccategories/BasicCategoriesScreen.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import androidx.compose.ui.input.pointer.PointerIcon
1818
import androidx.compose.ui.input.pointer.pointerHoverIcon
1919
import androidx.compose.ui.unit.dp
2020
import com.linuxcommandlibrary.app.NavEvent
21-
import com.linuxcommandlibrary.app.ui.composables.AppIcon
21+
import com.linuxcommandlibrary.app.data.BasicCategory
2222
import com.linuxcommandlibrary.app.ui.composables.debouncedClickable
2323
import com.linuxcommandlibrary.app.ui.composables.getIconId
2424
import com.linuxcommandlibrary.app.ui.composables.rememberIconPainter
25-
import com.linuxcommandlibrary.app.ui.theme.LocalCustomColors
25+
import kotlinx.collections.immutable.ImmutableList
2626

2727
@Composable
2828
fun BasicCategoriesScreen(
@@ -31,6 +31,17 @@ fun BasicCategoriesScreen(
3131
) {
3232
val basicCategories by viewModel.basicCategories.collectAsState()
3333

34+
BasicCategoriesContent(
35+
basicCategories = basicCategories,
36+
onNavigate = onNavigate,
37+
)
38+
}
39+
40+
@Composable
41+
private fun BasicCategoriesContent(
42+
basicCategories: ImmutableList<BasicCategory>,
43+
onNavigate: (NavEvent) -> Unit,
44+
) {
3445
LazyVerticalGrid(
3546
modifier = Modifier
3647
.background(MaterialTheme.colorScheme.background)

composeApp/src/commonMain/kotlin/com/linuxcommandlibrary/app/ui/screens/basicgroups/BasicEditorScreen.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,29 @@ import com.linuxcommandlibrary.app.NavEvent
2121
import com.linuxcommandlibrary.app.ui.composables.SectionTitle
2222
import com.linuxcommandlibrary.app.ui.composables.TipSectionContent
2323
import com.linuxcommandlibrary.shared.BasicGroup
24+
import kotlinx.collections.immutable.ImmutableList
2425

2526
@Composable
2627
fun BasicEditorScreen(
2728
viewModel: BasicEditorViewModel,
28-
onNavigate: (NavEvent) -> Unit = {},
29+
onNavigate: (NavEvent) -> Unit,
2930
) {
3031
val groups by viewModel.groups.collectAsState()
3132
val showTitles = viewModel.showTitles
3233

34+
BasicEditorContent(
35+
groups = groups,
36+
showTitles = showTitles,
37+
onNavigate = onNavigate,
38+
)
39+
}
40+
41+
@Composable
42+
fun BasicEditorContent(
43+
groups: ImmutableList<BasicGroup>,
44+
showTitles: Boolean,
45+
onNavigate: (NavEvent) -> Unit,
46+
) {
3347
LazyVerticalStaggeredGrid(
3448
modifier = Modifier
3549
.background(MaterialTheme.colorScheme.background)
@@ -48,7 +62,11 @@ fun BasicEditorScreen(
4862
}
4963

5064
@Composable
51-
private fun EditorGroupCard(group: BasicGroup, onNavigate: (NavEvent) -> Unit, showTitle: Boolean = true) {
65+
private fun EditorGroupCard(
66+
group: BasicGroup,
67+
onNavigate: (NavEvent) -> Unit,
68+
showTitle: Boolean = true,
69+
) {
5270
Card(
5371
elevation = CardDefaults.cardElevation(defaultElevation = 4.dp),
5472
modifier = Modifier

composeApp/src/commonMain/kotlin/com/linuxcommandlibrary/app/ui/screens/basicgroups/BasicGroupsScreen.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import com.linuxcommandlibrary.app.ui.composables.CommandView
2424
import com.linuxcommandlibrary.app.ui.composables.HighlightedText
2525
import com.linuxcommandlibrary.app.ui.composables.getIconId
2626
import com.linuxcommandlibrary.app.ui.composables.rememberIconPainter
27-
import com.linuxcommandlibrary.app.ui.theme.LocalCustomColors
2827
import com.linuxcommandlibrary.shared.getCommandList
2928
import kotlinx.collections.immutable.ImmutableList
3029
import kotlinx.collections.immutable.ImmutableSet
@@ -39,6 +38,19 @@ fun BasicGroupsScreen(
3938
) {
4039
val uiState by viewModel.uiState.collectAsState()
4140

41+
BasicGroupsContent(
42+
uiState = uiState,
43+
toggleCollapse = { viewModel.toggleCollapse(it) },
44+
onNavigate = onNavigate,
45+
)
46+
}
47+
48+
@Composable
49+
fun BasicGroupsContent(
50+
uiState: BasicGroupsUiState,
51+
toggleCollapse: (Long) -> Unit,
52+
onNavigate: (NavEvent) -> Unit,
53+
) {
4254
LazyColumn(
4355
Modifier
4456
.fillMaxSize()
@@ -53,7 +65,7 @@ fun BasicGroupsScreen(
5365
basicGroup = basicGroup,
5466
commands = uiState.commandsByGroupId[basicGroup.id] ?: persistentListOf(),
5567
isExpanded = !(uiState.collapsedMap[basicGroup.id] ?: true),
56-
onToggleCollapse = { viewModel.toggleCollapse(basicGroup.id) },
68+
onToggleCollapse = { toggleCollapse(basicGroup.id) },
5769
onNavigate = onNavigate,
5870
)
5971
}

composeApp/src/commonMain/kotlin/com/linuxcommandlibrary/app/ui/screens/commanddetail/CommandDetailScreen.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import com.linuxcommandlibrary.app.NavEvent
3131
import com.linuxcommandlibrary.app.data.CommandSectionInfo
3232
import com.linuxcommandlibrary.app.ui.composables.TipSectionContent
3333
import com.linuxcommandlibrary.app.ui.composables.rememberDebouncedClick
34-
import com.linuxcommandlibrary.app.ui.theme.LocalCustomColors
3534
import com.linuxcommandlibrary.shared.MarkdownParser
3635
import kotlinx.collections.immutable.ImmutableList
3736

@@ -42,6 +41,19 @@ fun CommandDetailScreen(
4241
) {
4342
val uiState by viewModel.state.collectAsState()
4443

44+
CommandDetailContent(
45+
uiState = uiState,
46+
onNavigate = onNavigate,
47+
onToggleExpanded = { id -> viewModel.onToggleExpanded(id) },
48+
)
49+
}
50+
51+
@Composable
52+
private fun CommandDetailContent(
53+
uiState: CommandDetailUiState,
54+
onNavigate: (NavEvent) -> Unit,
55+
onToggleExpanded: (Long) -> Unit,
56+
) {
4557
LazyColumn(
4658
Modifier
4759
.background(MaterialTheme.colorScheme.background)
@@ -56,7 +68,7 @@ fun CommandDetailScreen(
5668
section = section,
5769
isExpanded = uiState.expandedSectionsMap[section.id] ?: false,
5870
seeAlsoCommands = uiState.seeAlsoCommands,
59-
onToggleExpanded = { id -> viewModel.onToggleExpanded(id) },
71+
onToggleExpanded = onToggleExpanded,
6072
onNavigate = onNavigate,
6173
)
6274
}
@@ -131,7 +143,7 @@ private fun SeeAlsoSectionContent(
131143
}
132144

133145
@Composable
134-
private fun DefaultSectionContent(content: String, onNavigate: (NavEvent) -> Unit = {}) {
146+
private fun DefaultSectionContent(content: String, onNavigate: (NavEvent) -> Unit) {
135147
val parsedSections = remember(content) {
136148
MarkdownParser.parseMarkdownContent(content)
137149
}

composeApp/src/commonMain/kotlin/com/linuxcommandlibrary/app/ui/screens/commandlist/CommandListScreen.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import com.linuxcommandlibrary.app.ui.composables.FastScrollBar
2323
import com.linuxcommandlibrary.app.ui.composables.HighlightedText
2424
import com.linuxcommandlibrary.app.ui.composables.debouncedClickable
2525
import com.linuxcommandlibrary.app.ui.composables.rememberIconPainter
26-
import com.linuxcommandlibrary.app.ui.theme.LocalCustomColors
26+
import kotlinx.collections.immutable.ImmutableList
27+
import kotlinx.collections.immutable.ImmutableSet
2728

2829
@Composable
2930
fun CommandListScreen(
@@ -32,8 +33,21 @@ fun CommandListScreen(
3233
) {
3334
val commands by viewModel.commands.collectAsState()
3435
val bookmarkedNames by viewModel.bookmarkedNames.collectAsState()
35-
val listState = rememberLazyListState()
3636

37+
ComposeListContent(
38+
commands = commands,
39+
bookmarkedNames = bookmarkedNames,
40+
onNavigate = onNavigate,
41+
)
42+
}
43+
44+
@Composable
45+
private fun ComposeListContent(
46+
commands: ImmutableList<CommandInfo>,
47+
bookmarkedNames: ImmutableSet<String>,
48+
onNavigate: (NavEvent) -> Unit,
49+
) {
50+
val listState = rememberLazyListState()
3751
Box(
3852
modifier = Modifier
3953
.background(MaterialTheme.colorScheme.background)

0 commit comments

Comments
 (0)