Skip to content

Commit b48bf7a

Browse files
authored
Add last page detection to stop unnecessary API calls (#37)
* Add callback for last page reached * Add last page reached check to prevent unnecessary fetches * Clean up indentation of pokemonList logic in HomeViewModel
1 parent 84a1c2d commit b48bf7a

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/FakeHomeRepository.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
}

core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepository.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
}

core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepositoryImpl.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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())

feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home/HomeViewModel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)