Skip to content

Commit 1ffffb2

Browse files
committed
- Rename TopicUi to Topic for better clarity.
- Move `TopicPagingSource` to the data layer. - Update `TopicViewModel`, `TopicList`, `TopicCard`, and `TopicScreen` to use the new `Topic` data class. - Adjust `TopicRepository` to return `Topic` objects. - Enhance `TopicPagingSource`'s `getRefreshKey` logic. - Update `PreviewUtils` to use the renamed `Topic` class. - Remove unused Android Paging dependency.
1 parent 18b2b85 commit 1ffffb2

File tree

10 files changed

+22
-22
lines changed

10 files changed

+22
-22
lines changed

composeApp/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ kotlin {
8888
implementation(libs.generativeai)
8989
implementation(compose.uiTooling)
9090
implementation(libs.ktor.client.okhttp)
91-
implementation("androidx.paging:paging-compose:3.3.0-alpha02")
9291
}
9392
commonMain.dependencies {
9493
implementation(compose.runtime)

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.developersbreach.kotlindictionarymultiplatform.data.detail.model.Kotl
66
import com.developersbreach.kotlindictionarymultiplatform.data.detail.model.Section
77
import com.developersbreach.kotlindictionarymultiplatform.data.detail.model.Syntax
88
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
9-
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.TopicUi
109
import kotlinx.coroutines.flow.Flow
1110
import kotlinx.coroutines.flow.flowOf
1211

@@ -79,16 +78,16 @@ private fun sampleTopicList(): List<Topic> {
7978
)
8079
}
8180

82-
internal fun sampleTopicUiList(): List<TopicUi> {
81+
internal fun sampleTopicUiList(): List<com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.Topic> {
8382
return sampleTopicList().map { topic ->
84-
TopicUi(
83+
com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.Topic(
8584
name = topic.name ?: "",
8685
initial = topic.name?.firstOrNull()?.uppercase() ?: "",
8786
description = topic.description ?: "",
8887
)
8988
}
9089
}
9190

92-
internal fun samplePagingData(): Flow<PagingData<TopicUi>> {
91+
internal fun samplePagingData(): Flow<PagingData<com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.Topic>> {
9392
return flowOf(PagingData.from(sampleTopicUiList()))
9493
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic
1+
package com.developersbreach.kotlindictionarymultiplatform.data.topic.repository
22

33
import app.cash.paging.PagingSource
44
import app.cash.paging.PagingState
5-
import com.developersbreach.kotlindictionarymultiplatform.data.topic.repository.TopicRepository
5+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.Topic
66

77
class TopicPagingSource(
88
private val repository: TopicRepository,
99
private val query: String,
10-
) : PagingSource<Int, TopicUi>() {
10+
) : PagingSource<Int, Topic>() {
1111

1212
override suspend fun load(
1313
params: LoadParams<Int>,
14-
): LoadResult<Int, TopicUi> {
14+
): LoadResult<Int, Topic> {
1515
val page = params.key ?: 1
1616
val pageSize = params.loadSize
1717
return try {
@@ -27,8 +27,11 @@ class TopicPagingSource(
2727
}
2828

2929
override fun getRefreshKey(
30-
state: PagingState<Int, TopicUi>,
31-
): Int {
32-
return 1
30+
state: PagingState<Int, Topic>,
31+
): Int? {
32+
return state.anchorPosition?.let { anchorPosition ->
33+
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
34+
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
35+
}
3336
}
3437
}

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/data/topic/repository/TopicRepository.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
66
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.Topic
77
import com.developersbreach.kotlindictionarymultiplatform.data.topic.model.toTopic
88
import com.developersbreach.kotlindictionarymultiplatform.core.network.topicSource.FirestoreConstants
9-
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.TopicUi
109
import io.ktor.client.HttpClient
1110
import io.ktor.client.call.body
1211
import io.ktor.client.request.get
@@ -25,13 +24,13 @@ class TopicRepository(
2524
page: Int,
2625
pageSize: Int,
2726
query: String,
28-
): List<TopicUi> {
27+
): List<com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.Topic> {
2928
val allTopics = getTopics().getOrElse { emptyList() }
3029
val filteredTopics = allTopics
3130
.filter { it.name?.contains(query, ignoreCase = true) == true }
3231
.sortedBy { it.name?.lowercase() ?: "" }
3332
.map { topic ->
34-
TopicUi(
33+
com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic.Topic(
3534
name = topic.name ?: "",
3635
initial = topic.name?.firstOrNull()?.uppercase() ?: "",
3736
description = topic.description ?: "",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.developersbreach.kotlindictionarymultiplatform.ui.screens.topic
22

3-
data class TopicUi(
3+
data class Topic(
44
val name: String,
55
val initial: String,
66
val description: String,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import com.developersbreach.designsystem.components.KdText
2525

2626
@Composable
2727
fun TopicCard(
28-
topicUI: TopicUi,
28+
topicUI: Topic,
2929
topic: String,
3030
description: String,
3131
onCardClick: () -> Unit,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import app.cash.paging.compose.LazyPagingItems
1010

1111
@Composable
1212
fun TopicList(
13-
topics: LazyPagingItems<TopicUi>,
13+
topics: LazyPagingItems<Topic>,
1414
onTopicClick: (String) -> Unit,
1515
) {
1616
LazyColumn(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fun TopicScreen(
99
viewModel: TopicViewModel,
1010
onTopicClick: (String) -> Unit,
1111
) {
12-
val pagingItems = viewModel.pagingDataFlow.collectAsLazyPagingItems()
12+
val pagingItems = viewModel.topics.collectAsLazyPagingItems()
1313
val searchQuery = viewModel.searchQuery.collectAsState().value
1414

1515
TopicScreenUI(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import androidx.compose.runtime.Composable
88
import androidx.compose.ui.Modifier
99
import androidx.compose.ui.unit.dp
1010
import com.developersbreach.designsystem.components.KdScaffold
11-
1211
import app.cash.paging.compose.LazyPagingItems
1312

1413
@Composable
1514
fun TopicScreenUI(
16-
topics: LazyPagingItems<TopicUi>,
15+
topics: LazyPagingItems<Topic>,
1716
searchQuery: String,
1817
onQueryChange: (String) -> Unit,
1918
onTopicClick: (String) -> Unit,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
55
import app.cash.paging.Pager
66
import app.cash.paging.PagingConfig
77
import app.cash.paging.PagingData
8+
import com.developersbreach.kotlindictionarymultiplatform.data.topic.repository.TopicPagingSource
89
import com.developersbreach.kotlindictionarymultiplatform.data.topic.repository.TopicRepository
910
import kotlinx.coroutines.ExperimentalCoroutinesApi
1011
import kotlinx.coroutines.flow.Flow
@@ -23,7 +24,7 @@ class TopicViewModel(
2324
val searchQuery: StateFlow<String> = _searchQuery.asStateFlow()
2425

2526
@OptIn(ExperimentalCoroutinesApi::class)
26-
val pagingDataFlow: Flow<PagingData<TopicUi>> = searchQuery
27+
val topics: Flow<PagingData<Topic>> = searchQuery
2728
.flatMapLatest { query ->
2829
Pager(
2930
config = PagingConfig(pageSize = 8),

0 commit comments

Comments
 (0)