Skip to content

Commit b5fc0c5

Browse files
authored
Merge pull request #14 from DevelopersBreach/shreyas/topic-card-icon-update
Replace search icon with initial letter and apply topic sorting
2 parents 5da6ce2 + 2d6ee9f commit b5fc0c5

File tree

9 files changed

+60
-21
lines changed

9 files changed

+60
-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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import androidx.compose.foundation.shape.CircleShape
1515
import androidx.compose.foundation.shape.RoundedCornerShape
1616
import androidx.compose.material.icons.Icons
1717
import androidx.compose.material.icons.filled.Bookmark
18-
import androidx.compose.material.icons.filled.Search
1918
import androidx.compose.material.icons.outlined.BookmarkBorder
2019
import androidx.compose.material3.Icon
2120
import androidx.compose.material3.IconButton
@@ -28,14 +27,15 @@ import androidx.compose.ui.Modifier
2827
import androidx.compose.ui.draw.shadow
2928
import androidx.compose.ui.text.style.TextOverflow
3029
import androidx.compose.ui.unit.dp
30+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.TopicUi
3131
import kotlindictionarymultiplatform.composeapp.generated.resources.Res
3232
import kotlindictionarymultiplatform.composeapp.generated.resources.add_bookmark
33-
import kotlindictionarymultiplatform.composeapp.generated.resources.icon
3433
import kotlindictionarymultiplatform.composeapp.generated.resources.remove_bookmark
3534
import org.jetbrains.compose.resources.stringResource
3635

3736
@Composable
3837
fun TopicCard(
38+
topicUi: TopicUi,
3939
topic: String,
4040
subtitle: String,
4141
isBookmarked: Boolean,
@@ -63,7 +63,7 @@ fun TopicCard(
6363
.background(MaterialTheme.colorScheme.primary, CircleShape),
6464
contentAlignment = Alignment.Center,
6565
) {
66-
Icon(Icons.Filled.Search, contentDescription = stringResource(Res.string.icon))
66+
Text(text = topicUi.initial)
6767
}
6868

6969
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: 15 additions & 10 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 {
@@ -53,9 +57,10 @@ class TopicViewModel(
5357
val result = repository.getTopics()
5458
_topics.value = result.fold(
5559
ifLeft = { UiState.Error(it) },
56-
ifRight = {
57-
_bookmarkedStates.value = List(it.size) { true }
58-
UiState.Success(it)
60+
ifRight = { list ->
61+
val sortedList = list.sortedBy { it.name.lowercase() }
62+
_bookmarkedStates.value = List(sortedList.size) { true }
63+
UiState.Success(sortedList)
5964
},
6065
)
6166
}

0 commit comments

Comments
 (0)