Skip to content

Conversation

@jlkim909
Copy link
Contributor

🎯 Goal

This PR fixes an issue where additional network requests were being made on the Home screen even after reaching the last page of the Pokémon API.
Unnecessary fetches are now prevented once the end of the list is reached.

🛠 Implementation details

  • Added an onLastPageReached: () -> Unit callback to HomeRepository.fetchPokemonList.
  • When the next field in the Pokémon API response is null, indicating the last page, onLastPageReached() is called.
  • In HomeViewModel, this callback sets an internal isLastPageReached flag to true.
  • fetchNextPokemonList() checks this flag to determine whether to initiate another fetch.
  • This ensures that no further network calls are made after the final page, preventing redundant data loading.

✍️ Explain examples

// HomeRepositoryImpl.kt
if (data.next == null) {
    onLastPageReached() // Last page reached
}

// HomeViewModel.kt
fun fetchNextPokemonList() {
    if (uiState.value != HomeUiState.Loading && !isLastPageReached) {
        pokemonFetchingIndex.value++
    }
}

Screenshot

@jlkim909 jlkim909 requested a review from skydoves as a code owner May 23, 2025 18:18
Copy link
Owner

@skydoves skydoves left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never thought about the last page detection 😄 This makes sense and thank you for your contribution! Just small comments.

@skydoves skydoves self-requested a review May 27, 2025 13:08
@skydoves skydoves merged commit b48bf7a into skydoves:main May 27, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants