Skip to content

Commit 8e1b040

Browse files
Rework/simplify topics screen implementation, remove redundant resources
1 parent f11a52d commit 8e1b040

File tree

16 files changed

+92
-250
lines changed

16 files changed

+92
-250
lines changed

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,17 @@ 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
8+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.ItemTopic
99

10-
fun sampleCodeSnippet(): String {
10+
internal fun sampleCodeSnippet(): String {
1111
return """
1212
fun greet(name: String): String {
1313
return "Hello, Sam!"
1414
}
1515
""".trimIndent()
1616
}
1717

18-
fun topic(): String {
19-
return "Smart Casts"
20-
}
21-
22-
fun subtitle(): String {
23-
return "Automatic casting of immutable values"
24-
}
25-
26-
fun fakeTopicDetails(): KotlinTopicDetails {
18+
internal fun fakeTopicDetails(): KotlinTopicDetails {
2719
return KotlinTopicDetails(
2820
topicId = "smart-cast",
2921
topicName = "Smart Cast",
@@ -59,7 +51,7 @@ fun fakeTopicDetails(): KotlinTopicDetails {
5951
)
6052
}
6153

62-
fun sampleTopicList(): List<Topic> {
54+
private fun sampleTopicList(): List<Topic> {
6355
return listOf(
6456
Topic("Smart Casts"),
6557
Topic("Null Safety"),
@@ -69,9 +61,9 @@ fun sampleTopicList(): List<Topic> {
6961
)
7062
}
7163

72-
fun sampleTopicUiList(): List<TopicUi> {
64+
internal fun sampleTopicUiList(): List<ItemTopic> {
7365
return sampleTopicList().map { topic ->
74-
TopicUi(
66+
ItemTopic(
7567
name = topic.name,
7668
initial = topic.name.firstOrNull()?.uppercase() ?: "",
7769
isBookmarked = true,

composeApp/src/androidMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/previews/detail/CodeExampleBoxPreview.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.developersbreach.kotlindictionarymultiplatform.ui.theme.KotlinDiction
1111

1212
@PreviewLightDark
1313
@Composable
14-
fun CodeExampleBoxPreview() {
14+
private fun CodeExampleBoxPreview() {
1515
KotlinDictionaryTheme {
1616
CodeExampleBox(
1717
code = sampleCodeSnippet(),

composeApp/src/androidMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/previews/detail/DetailScreenPreview.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.developersbreach.kotlindictionarymultiplatform.ui.theme.KotlinDiction
99

1010
@PreviewLightDark
1111
@Composable
12-
fun DetailScreenPreview() {
12+
private fun DetailScreenPreview() {
1313
KotlinDictionaryTheme {
1414
DetailScreenUI(
1515
detailUiState = fakeTopicDetails().toDetailUi(),

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,22 @@
11
package com.developersbreach.kotlindictionarymultiplatform.previews.topic
22

33
import androidx.compose.runtime.Composable
4-
import androidx.compose.runtime.collectAsState
5-
import androidx.compose.runtime.getValue
64
import androidx.compose.ui.tooling.preview.PreviewLightDark
7-
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
8-
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.TopicUi
9-
import com.developersbreach.kotlindictionarymultiplatform.previews.sampleTopicList
10-
import com.developersbreach.kotlindictionarymultiplatform.ui.components.UiState
11-
import com.developersbreach.kotlindictionarymultiplatform.ui.components.UiStateHandler
5+
import com.developersbreach.kotlindictionarymultiplatform.previews.sampleTopicUiList
126
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.TopicScreenUI
137
import com.developersbreach.kotlindictionarymultiplatform.ui.theme.KotlinDictionaryTheme
14-
import kotlinx.coroutines.flow.MutableStateFlow
15-
import kotlinx.coroutines.flow.StateFlow
16-
17-
// Fake ViewModel for previews
18-
class FakeTopicViewModel : TopicViewModelFakeBase() {
19-
override val topics = MutableStateFlow<UiState<List<Topic>>>(UiState.Success(sampleTopicList()))
20-
override val searchQuery = MutableStateFlow("")
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-
)
30-
override val bookmarkedStates = MutableStateFlow(List(sampleTopicList().size) { true })
31-
32-
override fun updateSearchQuery(
33-
newQuery: String,
34-
) {}
35-
36-
override fun toggleBookmark(
37-
index: Int,
38-
) {}
39-
}
40-
41-
// Base class to avoid needing Android ViewModel
42-
abstract class TopicViewModelFakeBase {
43-
abstract val topics: StateFlow<UiState<List<Topic>>>
44-
abstract val searchQuery: StateFlow<String>
45-
abstract val filteredTopics: StateFlow<List<TopicUi>>
46-
abstract val bookmarkedStates: StateFlow<List<Boolean>>
47-
48-
abstract fun updateSearchQuery(
49-
newQuery: String,
50-
)
51-
52-
abstract fun toggleBookmark(
53-
index: Int,
54-
)
55-
}
56-
57-
@Composable
58-
fun TopicScreenContent(
59-
viewModel: TopicViewModelFakeBase,
60-
onTopicClick: (String) -> Unit = {},
61-
) {
62-
val topicState by viewModel.topics.collectAsState()
63-
val filteredTopics by viewModel.filteredTopics.collectAsState()
64-
val searchQuery by viewModel.searchQuery.collectAsState()
65-
val bookmarkedStates by viewModel.bookmarkedStates.collectAsState()
66-
67-
UiStateHandler(uiState = topicState) {
68-
TopicScreenUI(
69-
topics = filteredTopics,
70-
bookmarkedStates = bookmarkedStates,
71-
searchQuery = searchQuery,
72-
onQueryChange = viewModel::updateSearchQuery,
73-
onBookmarkClick = viewModel::toggleBookmark,
74-
onTopicClick = onTopicClick,
75-
)
76-
}
77-
}
788

799
@PreviewLightDark
8010
@Composable
81-
fun TopicScreenPreview() {
11+
private fun TopicScreenPreview() {
8212
KotlinDictionaryTheme {
83-
TopicScreenContent(viewModel = FakeTopicViewModel())
13+
TopicScreenUI(
14+
topics = sampleTopicUiList(),
15+
bookmarkedStates = listOf(),
16+
searchQuery = "Search",
17+
onQueryChange = { },
18+
onBookmarkClick = { },
19+
onTopicClick = { },
20+
)
8421
}
8522
}

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

Lines changed: 0 additions & 14 deletions
This file was deleted.

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/data/topic/model/TopicUi.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import androidx.lifecycle.SavedStateHandle
44
import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
66
import androidx.navigation.toRoute
7-
import com.developersbreach.kotlindictionarymultiplatform.Log
87
import com.developersbreach.kotlindictionarymultiplatform.data.detail.repository.DetailRepository
98
import com.developersbreach.kotlindictionarymultiplatform.ui.components.UiState
109
import com.developersbreach.kotlindictionarymultiplatform.ui.navigation.AppDestinations
1110
import kotlinx.coroutines.flow.MutableStateFlow
1211
import kotlinx.coroutines.flow.StateFlow
1312
import kotlinx.coroutines.launch
14-
import kotlinx.io.IOException
1513

1614
class DetailViewModel(
17-
private val savedStateHandle: SavedStateHandle,
15+
savedStateHandle: SavedStateHandle,
1816
private val repository: DetailRepository,
1917
) : ViewModel() {
2018

@@ -25,11 +23,7 @@ class DetailViewModel(
2523

2624
init {
2725
viewModelScope.launch {
28-
try {
29-
fetchTopic()
30-
} catch (e: IOException) {
31-
Log.e("DetailViewModel", "Error fetching topic: ${e.message}", e)
32-
}
26+
fetchTopic()
3327
}
3428
}
3529

0 commit comments

Comments
 (0)