diff --git a/app/src/main/java/com/rob729/newsfeed/model/state/UiStatus.kt b/app/src/main/java/com/rob729/newsfeed/model/state/UiStatus.kt index e4c0d2c..ea01a51 100644 --- a/app/src/main/java/com/rob729/newsfeed/model/state/UiStatus.kt +++ b/app/src/main/java/com/rob729/newsfeed/model/state/UiStatus.kt @@ -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() diff --git a/app/src/main/java/com/rob729/newsfeed/model/ui/PaginationData.kt b/app/src/main/java/com/rob729/newsfeed/model/ui/PaginationData.kt new file mode 100644 index 0000000..ec4c131 --- /dev/null +++ b/app/src/main/java/com/rob729/newsfeed/model/ui/PaginationData.kt @@ -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 +} diff --git a/app/src/main/java/com/rob729/newsfeed/ui/screen/HomeScreen.kt b/app/src/main/java/com/rob729/newsfeed/ui/screen/HomeScreen.kt index af6eb42..4278471 100644 --- a/app/src/main/java/com/rob729/newsfeed/ui/screen/HomeScreen.kt +++ b/app/src/main/java/com/rob729/newsfeed/ui/screen/HomeScreen.kt @@ -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 ) { @@ -258,7 +258,7 @@ private fun DisplayNewsFeed( }) } - if (newsState.uiStatus.showPaginationLoader) { + if (newsState.uiStatus.paginationData.showPaginationLoader) { item { PaginationLoader() } } } diff --git a/app/src/main/java/com/rob729/newsfeed/vm/HomeViewModel.kt b/app/src/main/java/com/rob729/newsfeed/vm/HomeViewModel.kt index 1cfee54..63b365c 100644 --- a/app/src/main/java/com/rob729/newsfeed/vm/HomeViewModel.kt +++ b/app/src/main/java/com/rob729/newsfeed/vm/HomeViewModel.kt @@ -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 @@ -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 } @@ -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 } @@ -163,7 +178,7 @@ class HomeViewModel( ), newsDbEntity.totalResultCount ), - showPaginationLoader = false + paginationData = PaginationData(showPaginationLoader = false, hasPaginationEnded = false) ), ) }