File tree Expand file tree Collapse file tree 4 files changed +10
-1
lines changed
core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home
feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home Expand file tree Collapse file tree 4 files changed +10
-1
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ class FakeHomeRepository : HomeRepository {
2525 page : Int ,
2626 onStart : () -> Unit ,
2727 onComplete : () -> Unit ,
28+ onLastPageReached : () -> Unit ,
2829 onError : (String? ) -> Unit ,
2930 ): Flow <List <Pokemon >> = flowOf()
3031}
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ interface HomeRepository {
2727 page : Int ,
2828 onStart : () -> Unit ,
2929 onComplete : () -> Unit ,
30+ onLastPageReached : () -> Unit ,
3031 onError : (String? ) -> Unit ,
3132 ): Flow <List <Pokemon >>
3233}
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ class HomeRepositoryImpl @Inject constructor(
4848 page : Int ,
4949 onStart : () -> Unit ,
5050 onComplete : () -> Unit ,
51+ onLastPageReached : () -> Unit ,
5152 onError : (String? ) -> Unit ,
5253 ) = flow {
5354 var pokemons = pokemonDao.getPokemonList(page).asDomain()
@@ -58,6 +59,10 @@ class HomeRepositoryImpl @Inject constructor(
5859 */
5960 val response = pokedexClient.fetchPokemonList(page = page)
6061 response.suspendOnSuccess {
62+ if (data.next == null ) {
63+ onLastPageReached()
64+ }
65+
6166 pokemons = data.results
6267 pokemons.forEach { pokemon -> pokemon.page = page }
6368 pokemonDao.insertPokemonList(pokemons.asEntity())
Original file line number Diff line number Diff line change @@ -36,13 +36,15 @@ class HomeViewModel @Inject constructor(
3636) : BaseViewModel() {
3737
3838 internal val uiState: ViewModelStateFlow <HomeUiState > = viewModelStateFlow(HomeUiState .Loading )
39+ private var isLastPageReached = false
3940
4041 private val pokemonFetchingIndex: MutableStateFlow <Int > = MutableStateFlow (0 )
4142 val pokemonList: StateFlow <List <Pokemon >> = pokemonFetchingIndex.flatMapLatest { page ->
4243 homeRepository.fetchPokemonList(
4344 page = page,
4445 onStart = { uiState.tryEmit(key, HomeUiState .Loading ) },
4546 onComplete = { uiState.tryEmit(key, HomeUiState .Idle ) },
47+ onLastPageReached = { isLastPageReached = true },
4648 onError = { uiState.tryEmit(key, HomeUiState .Error (it)) },
4749 )
4850 }.stateIn(
@@ -52,7 +54,7 @@ class HomeViewModel @Inject constructor(
5254 )
5355
5456 fun fetchNextPokemonList () {
55- if (uiState.value != HomeUiState .Loading ) {
57+ if (uiState.value != HomeUiState .Loading && ! isLastPageReached ) {
5658 pokemonFetchingIndex.value++
5759 }
5860 }
You can’t perform that action at this time.
0 commit comments