Skip to content

Commit

Permalink
Fixed pagination end handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rob729 committed Jul 21, 2024
1 parent c984a94 commit ac7f86e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.rob729.newsfeed.model.state

import com.rob729.newsfeed.model.ui.NewsEntityUiData
import com.rob729.newsfeed.model.ui.PaginationData

sealed class UiStatus {
data class Success(val newsEntityUiData: NewsEntityUiData, val showPaginationLoader: Boolean = false) : UiStatus()
data class Success(
val newsEntityUiData: NewsEntityUiData,
val paginationData: PaginationData = PaginationData()
) : UiStatus()

data object Loading : UiStatus()
data object Error : UiStatus()

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/rob729/newsfeed/model/ui/PaginationData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.rob729.newsfeed.model.ui

import java.io.Serializable

data class PaginationData(
val showPaginationLoader: Boolean = false,
val hasPaginationEnded: Boolean = false
) : Serializable {

fun shouldTriggerPagination() = !showPaginationLoader && !hasPaginationEnded
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/rob729/newsfeed/ui/screen/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fun HomeScreen(
LaunchedEffect(listState, newsState.uiStatus) {
snapshotFlow { listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index }.collect { lastVisibleItemIndex ->
(newsState.uiStatus as? UiStatus.Success)?.let { state ->
if (!state.showPaginationLoader
if (state.paginationData.shouldTriggerPagination()
&& lastVisibleItemIndex != null && lastVisibleItemIndex
>= listState.layoutInfo.totalItemsCount - Constants.PAGINATION_TRIGGER_THRESHOLD
) {
Expand Down Expand Up @@ -258,7 +258,7 @@ private fun DisplayNewsFeed(
})
}

if (newsState.uiStatus.showPaginationLoader) {
if (newsState.uiStatus.paginationData.showPaginationLoader) {
item { PaginationLoader() }
}
}
Expand Down
21 changes: 18 additions & 3 deletions app/src/main/java/com/rob729/newsfeed/vm/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.rob729.newsfeed.model.state.home.HomeFeedSideEffect
import com.rob729.newsfeed.model.state.home.HomeFeedState
import com.rob729.newsfeed.model.ui.NewsArticleUiData
import com.rob729.newsfeed.model.ui.NewsEntityUiData
import com.rob729.newsfeed.model.ui.PaginationData
import com.rob729.newsfeed.repository.NewsRepository
import com.rob729.newsfeed.repository.PreferenceRepository
import kotlinx.coroutines.flow.collectLatest
Expand Down Expand Up @@ -123,7 +124,14 @@ class HomeViewModel(
state.copy(uiStatus = UiStatus.Error)
} else {
(state.uiStatus as? UiStatus.Success)?.let { currentState ->
state.copy(uiStatus = currentState.copy(showPaginationLoader = false))
state.copy(
uiStatus = currentState.copy(
paginationData = PaginationData(
showPaginationLoader = false,
hasPaginationEnded = true
)
)
)
} ?: state
}

Expand All @@ -132,7 +140,14 @@ class HomeViewModel(
state.copy(uiStatus = UiStatus.Loading)
} else {
(state.uiStatus as? UiStatus.Success)?.let { currentState ->
state.copy(uiStatus = currentState.copy(showPaginationLoader = true))
state.copy(
uiStatus = currentState.copy(
paginationData = PaginationData(
showPaginationLoader = true,
hasPaginationEnded = false
)
)
)
} ?: state
}

Expand Down Expand Up @@ -163,7 +178,7 @@ class HomeViewModel(
),
newsDbEntity.totalResultCount
),
showPaginationLoader = false
paginationData = PaginationData(showPaginationLoader = false, hasPaginationEnded = false)
),
)
}
Expand Down

0 comments on commit ac7f86e

Please sign in to comment.