Skip to content

Commit 6a86cbe

Browse files
committed
- Introduce TopicUi data class to represent topic data for the UI layer.
- Update `TopicViewModel` to emit `TopicUi` objects, including initial and bookmarked status. - Modify `TopicCard`, `TopicList`, and `TopicScreenUI` to consume `TopicUi`. - Update previews to use `TopicUi` and sample data.
1 parent 4c56955 commit 6a86cbe

File tree

9 files changed

+58
-21
lines changed

9 files changed

+58
-21
lines changed

composeApp/src/androidMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/previews/PreviewUtils.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.developersbreach.kotlindictionarymultiplatform.data.detail.model.Kotl
55
import com.developersbreach.kotlindictionarymultiplatform.data.detail.model.Section
66
import com.developersbreach.kotlindictionarymultiplatform.data.detail.model.Syntax
77
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
8+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.TopicUi
89

910
fun sampleCodeSnippet(): String = """
1011
fun greet(name: String): String {
@@ -52,4 +53,13 @@ fun sampleTopicList(): List<Topic> = listOf(
5253
Topic("Coroutines"),
5354
Topic("Lambdas"),
5455
Topic("Sealed Classes"),
55-
)
56+
)
57+
58+
fun sampleTopicUiList(): List<TopicUi> =
59+
sampleTopicList().map { topic ->
60+
TopicUi(
61+
name = topic.name,
62+
initial = topic.name.firstOrNull()?.uppercase() ?: "",
63+
isBookmarked = true,
64+
)
65+
}

composeApp/src/androidMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/previews/topic/TopicCardPreview.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.developersbreach.kotlindictionarymultiplatform.previews.topic
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.ui.tooling.preview.PreviewLightDark
5+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.TopicUi
56
import com.developersbreach.kotlindictionarymultiplatform.previews.subtitle
67
import com.developersbreach.kotlindictionarymultiplatform.previews.topic
78
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.TopicCard
@@ -17,6 +18,11 @@ fun TopicCardPreview() {
1718
isBookmarked = false,
1819
onBookmarkClick = {},
1920
onCardClick = {},
21+
topicUi = TopicUi(
22+
name = "Emerson Vega",
23+
initial = "senectus",
24+
isBookmarked = false,
25+
),
2026
)
2127
}
2228
}

composeApp/src/androidMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/previews/topic/TopicListPreview.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.developersbreach.kotlindictionarymultiplatform.previews.topic
33
import androidx.compose.runtime.Composable
44
import androidx.compose.ui.tooling.preview.PreviewLightDark
55
import com.developersbreach.kotlindictionarymultiplatform.previews.sampleTopicList
6+
import com.developersbreach.kotlindictionarymultiplatform.previews.sampleTopicUiList
67
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.TopicList
78
import com.developersbreach.kotlindictionarymultiplatform.ui.theme.KotlinDictionaryTheme
89

@@ -11,7 +12,7 @@ import com.developersbreach.kotlindictionarymultiplatform.ui.theme.KotlinDiction
1112
fun TopicListPreview() {
1213
KotlinDictionaryTheme {
1314
TopicList(
14-
topics = sampleTopicList(),
15+
topics = sampleTopicUiList(),
1516
bookmarkedStates = List(sampleTopicList().size) { true },
1617
onBookmarkClick = {},
1718
onTopicClick = {},

composeApp/src/androidMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/previews/topic/TopicScreenPreview.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.compose.runtime.collectAsState
55
import androidx.compose.runtime.getValue
66
import androidx.compose.ui.tooling.preview.PreviewLightDark
77
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
8+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.TopicUi
89
import com.developersbreach.kotlindictionarymultiplatform.previews.sampleTopicList
910
import com.developersbreach.kotlindictionarymultiplatform.ui.components.UiState
1011
import com.developersbreach.kotlindictionarymultiplatform.ui.components.UiStateHandler
@@ -17,7 +18,15 @@ import kotlinx.coroutines.flow.StateFlow
1718
class FakeTopicViewModel : TopicViewModelFakeBase() {
1819
override val topics = MutableStateFlow<UiState<List<Topic>>>(UiState.Success(sampleTopicList()))
1920
override val searchQuery = MutableStateFlow("")
20-
override val filteredTopics = MutableStateFlow(sampleTopicList())
21+
override val filteredTopics = MutableStateFlow(
22+
sampleTopicList().map { topic ->
23+
TopicUi(
24+
name = topic.name,
25+
initial = topic.name.firstOrNull()?.uppercase() ?: "",
26+
isBookmarked = true,
27+
)
28+
},
29+
)
2130
override val bookmarkedStates = MutableStateFlow(List(sampleTopicList().size) { true })
2231

2332
override fun updateSearchQuery(newQuery: String) {}
@@ -29,7 +38,7 @@ class FakeTopicViewModel : TopicViewModelFakeBase() {
2938
abstract class TopicViewModelFakeBase {
3039
abstract val topics: StateFlow<UiState<List<Topic>>>
3140
abstract val searchQuery: StateFlow<String>
32-
abstract val filteredTopics: StateFlow<List<Topic>>
41+
abstract val filteredTopics: StateFlow<List<TopicUi>>
3342
abstract val bookmarkedStates: StateFlow<List<Boolean>>
3443

3544
abstract fun updateSearchQuery(newQuery: String)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.developersbreach.kotlindictionarymultiplatform.data.topic.model
2+
3+
data class TopicUi(
4+
val name: String,
5+
val initial: String,
6+
val isBookmarked: Boolean,
7+
)

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/topic/TopicCard.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ import androidx.compose.ui.Modifier
2727
import androidx.compose.ui.draw.shadow
2828
import androidx.compose.ui.text.style.TextOverflow
2929
import androidx.compose.ui.unit.dp
30+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.TopicUi
3031
import kotlindictionarymultiplatform.composeapp.generated.resources.Res
3132
import kotlindictionarymultiplatform.composeapp.generated.resources.add_bookmark
3233
import kotlindictionarymultiplatform.composeapp.generated.resources.remove_bookmark
3334
import org.jetbrains.compose.resources.stringResource
3435

3536
@Composable
3637
fun TopicCard(
38+
topicUi: TopicUi?,
3739
topic: String,
3840
subtitle: String,
3941
isBookmarked: Boolean,
@@ -61,12 +63,9 @@ fun TopicCard(
6163
.background(MaterialTheme.colorScheme.primary, CircleShape),
6264
contentAlignment = Alignment.Center,
6365
) {
64-
Text(
65-
text = topic.firstOrNull()?.uppercase() ?: "?",
66-
style = MaterialTheme.typography.bodyLarge.copy(
67-
color = MaterialTheme.colorScheme.onPrimary,
68-
),
69-
)
66+
if (topicUi != null) {
67+
Text(text = topicUi.initial)
68+
}
7069
}
7170

7271
Spacer(modifier = Modifier.width(12.dp))

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/topic/TopicList.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import androidx.compose.foundation.lazy.itemsIndexed
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.topic.model.Topic
10+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.TopicUi
1111
import kotlindictionarymultiplatform.composeapp.generated.resources.Res
1212
import kotlindictionarymultiplatform.composeapp.generated.resources.description_subtitle
1313
import org.jetbrains.compose.resources.stringResource
1414

1515
@Composable
1616
fun TopicList(
17-
topics: List<Topic>,
17+
topics: List<TopicUi>,
1818
bookmarkedStates: List<Boolean>,
1919
onBookmarkClick: (Int) -> Unit,
2020
onTopicClick: (String) -> Unit,
@@ -27,6 +27,7 @@ fun TopicList(
2727
val isBookmarked = bookmarkedStates.getOrNull(index) ?: true
2828
TopicCard(
2929
topic = topic.name,
30+
topicUi = topic,
3031
subtitle = stringResource(Res.string.description_subtitle),
3132
isBookmarked = isBookmarked,
3233
onBookmarkClick = { onBookmarkClick(index) },

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/topic/TopicScreenUI.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import androidx.compose.material3.Scaffold
99
import androidx.compose.runtime.Composable
1010
import androidx.compose.ui.Modifier
1111
import androidx.compose.ui.unit.dp
12-
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
12+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.TopicUi
1313

1414
@Composable
1515
fun TopicScreenUI(
16-
topics: List<Topic>,
16+
topics: List<TopicUi>,
1717
bookmarkedStates: List<Boolean>,
1818
searchQuery: String,
1919
onQueryChange: (String) -> Unit,

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/topic/TopicViewModel.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
55
import com.developersbreach.kotlindictionarymultiplatform.Log
66
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
7+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.TopicUi
78
import com.developersbreach.kotlindictionarymultiplatform.data.topic.repository.TopicRepository
89
import com.developersbreach.kotlindictionarymultiplatform.ui.components.UiState
910
import kotlinx.coroutines.flow.MutableStateFlow
@@ -28,15 +29,18 @@ class TopicViewModel(
2829
private val _bookmarkedStates = MutableStateFlow<List<Boolean>>(emptyList())
2930
val bookmarkedStates: StateFlow<List<Boolean>> = _bookmarkedStates
3031

31-
val filteredTopics: StateFlow<List<Topic>> = combine(_topics, _searchQuery) { uiState, query ->
32+
val filteredTopics: StateFlow<List<TopicUi>> = combine(_topics, _searchQuery, _bookmarkedStates) { uiState, query, bookmarks ->
3233
val allTopics = (uiState as? UiState.Success)?.data ?: return@combine emptyList()
33-
if (query.isBlank()) {
34-
allTopics
35-
} else {
36-
allTopics.filter {
37-
it.name.contains(query, ignoreCase = true)
34+
35+
allTopics
36+
.withIndex()
37+
.map { (index, topic) ->
38+
TopicUi(
39+
name = topic.name,
40+
initial = topic.name.first().uppercase(),
41+
isBookmarked = bookmarks.getOrNull(index) ?: false,
42+
)
3843
}
39-
}
4044
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
4145

4246
init {

0 commit comments

Comments
 (0)