Skip to content

Commit 31a5e05

Browse files
committed
Tried isolating the ViewModel/UI
1 parent 372e7e0 commit 31a5e05

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/detail/DetailContent.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.developersbreach.kotlindictionarymultiplatform.ui.screens.detail
33
import androidx.compose.foundation.layout.fillMaxSize
44
import androidx.compose.foundation.layout.padding
55
import androidx.compose.foundation.lazy.LazyColumn
6+
import androidx.compose.foundation.lazy.items
67
import androidx.compose.foundation.lazy.rememberLazyListState
78
import androidx.compose.runtime.Composable
89
import androidx.compose.runtime.rememberCoroutineScope
@@ -31,24 +32,26 @@ fun DetailContent(
3132
) {
3233
val listState = rememberLazyListState()
3334
val coroutineScope = rememberCoroutineScope()
34-
35-
val tocItems = buildList {
36-
var index = 1
37-
add(stringResource(Res.string.introduction_bullet) to index++)
38-
if (topic.syntax.signature.isNotBlank()) add(stringResource(Res.string.syntax_bullet) to index++)
39-
topic.sections.forEach { section ->
40-
section.heading?.let { add(stringResource(Res.string.bullet_item, it) to index++) }
41-
}
42-
if (topic.pitfalls.isNotEmpty()) add(stringResource(Res.string.pitfalls_bullet) to index++)
43-
if (topic.relatedTopics.isNotEmpty()) add(stringResource(Res.string.related_topics_bullet) to index)
44-
}
45-
35+
val tocItems = list(topic)
4636
LazyColumn(
4737
state = listState,
4838
modifier = modifier
4939
.fillMaxSize()
5040
.padding(horizontal = 16.dp),
5141
) {
42+
items(
43+
tocItems
44+
){
45+
46+
when(it){
47+
DetailViewModel.ItemTableOfContent.Introduction(intro = topic.intro) -> IntroductionSection(it.)
48+
DetailViewModel.ItemTableOfContent.Pitfall -> SyntaxSection(it.)
49+
DetailViewModel.ItemTableOfContent.RelatedTopic -> TODO()
50+
is DetailViewModel.ItemTableOfContent.Section -> TODO()
51+
DetailViewModel.ItemTableOfContent.Syntax -> TODO()
52+
}
53+
}
54+
5255
// Table of Contents
5356
item {
5457
TableOfContents(

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/detail/DetailViewModel.kt

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import androidx.lifecycle.viewModelScope
66
import androidx.navigation.toRoute
77
import com.developersbreach.kotlindictionarymultiplatform.Log
88
import com.developersbreach.kotlindictionarymultiplatform.data.detail.model.KotlinTopicDetails
9-
import kotlinx.coroutines.flow.MutableStateFlow
10-
import kotlinx.coroutines.flow.StateFlow
11-
import kotlinx.coroutines.launch
129
import com.developersbreach.kotlindictionarymultiplatform.data.detail.repository.DetailRepository
1310
import com.developersbreach.kotlindictionarymultiplatform.ui.components.UiState
1411
import com.developersbreach.kotlindictionarymultiplatform.ui.navigation.AppDestinations
12+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.detail.DetailViewModel.ItemTableOfContent
13+
import kotlinx.coroutines.flow.MutableStateFlow
14+
import kotlinx.coroutines.flow.StateFlow
15+
import kotlinx.coroutines.launch
1516
import kotlinx.io.IOException
1617

1718
class DetailViewModel(
@@ -40,4 +41,38 @@ class DetailViewModel(
4041
ifRight = { UiState.Success(it) },
4142
)
4243
}
44+
45+
46+
47+
// data class ItemTableOfContent(
48+
// val index : Int,
49+
// val isVisible : Boolean,
50+
// )
51+
52+
sealed class ItemTableOfContent{
53+
// abstract val index : Int
54+
data class Introduction(val intro : String):ItemTableOfContent()
55+
data object Syntax: ItemTableOfContent()
56+
data class Section (
57+
val index : Int
58+
):ItemTableOfContent()
59+
data object Pitfall: ItemTableOfContent()
60+
data object RelatedTopic: ItemTableOfContent()
61+
}
62+
}
63+
64+
fun list(topic: KotlinTopicDetails): List<ItemTableOfContent> {
65+
val tocItems = buildList {
66+
//add(stringResource(Res.string.introduction_bullet) to index++)
67+
add(ItemTableOfContent.Introduction(intro = topic.intro))
68+
if (topic.syntax.signature.isNotBlank()) add(ItemTableOfContent.Syntax)
69+
topic.sections.forEachIndexed {index, section ->
70+
section.heading?.let {
71+
add(ItemTableOfContent.Section(index))
72+
}
73+
}
74+
if (topic.pitfalls.isNotEmpty()) add(ItemTableOfContent.Pitfall)
75+
if (topic.relatedTopics.isNotEmpty()) add(ItemTableOfContent.RelatedTopic)
76+
}
77+
return tocItems
4378
}

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/detail/components/IntroductionSection.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import androidx.compose.material3.Text
77
import androidx.compose.runtime.Composable
88
import androidx.compose.ui.Modifier
99
import androidx.compose.ui.unit.dp
10-
import com.developersbreach.kotlindictionarymultiplatform.data.detail.model.KotlinTopicDetails
1110
import kotlindictionarymultiplatform.composeapp.generated.resources.Res
1211
import kotlindictionarymultiplatform.composeapp.generated.resources.introduction
1312
import org.jetbrains.compose.resources.stringResource
1413

1514
@Composable
16-
fun IntroductionSection(topic: KotlinTopicDetails) {
15+
fun IntroductionSection(intro : String) {
1716
Text(stringResource(Res.string.introduction), style = MaterialTheme.typography.headlineLarge, color = MaterialTheme.colorScheme.onPrimary)
1817
Spacer(Modifier.height(4.dp))
19-
Text(text = topic.intro, style = MaterialTheme.typography.bodyMedium)
18+
Text(text = intro, style = MaterialTheme.typography.bodyMedium)
2019
Spacer(Modifier.height(16.dp))
2120
}

0 commit comments

Comments
 (0)