From c683475e563686412299e52caa53f03ee2ae864d Mon Sep 17 00:00:00 2001 From: hodak Date: Sat, 24 May 2025 02:40:37 +0900 Subject: [PATCH 1/3] Add callback for last page reached --- .../compose/core/data/repository/home/FakeHomeRepository.kt | 1 + .../compose/core/data/repository/home/HomeRepository.kt | 1 + .../compose/core/data/repository/home/HomeRepositoryImpl.kt | 5 +++++ 3 files changed, 7 insertions(+) diff --git a/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/FakeHomeRepository.kt b/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/FakeHomeRepository.kt index 6340610..0980535 100644 --- a/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/FakeHomeRepository.kt +++ b/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/FakeHomeRepository.kt @@ -25,6 +25,7 @@ class FakeHomeRepository : HomeRepository { page: Int, onStart: () -> Unit, onComplete: () -> Unit, + onLastPageReached: () -> Unit, onError: (String?) -> Unit, ): Flow> = flowOf() } diff --git a/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepository.kt b/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepository.kt index eaa7ff8..402874f 100644 --- a/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepository.kt +++ b/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepository.kt @@ -27,6 +27,7 @@ interface HomeRepository { page: Int, onStart: () -> Unit, onComplete: () -> Unit, + onLastPageReached: () -> Unit, onError: (String?) -> Unit, ): Flow> } diff --git a/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepositoryImpl.kt b/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepositoryImpl.kt index fb7702e..effae23 100644 --- a/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepositoryImpl.kt +++ b/core/data/src/main/kotlin/com/skydoves/pokedex/compose/core/data/repository/home/HomeRepositoryImpl.kt @@ -48,6 +48,7 @@ class HomeRepositoryImpl @Inject constructor( page: Int, onStart: () -> Unit, onComplete: () -> Unit, + onLastPageReached: () -> Unit, onError: (String?) -> Unit, ) = flow { var pokemons = pokemonDao.getPokemonList(page).asDomain() @@ -58,6 +59,10 @@ class HomeRepositoryImpl @Inject constructor( */ val response = pokedexClient.fetchPokemonList(page = page) response.suspendOnSuccess { + if (data.next == null) { + onLastPageReached() + } + pokemons = data.results pokemons.forEach { pokemon -> pokemon.page = page } pokemonDao.insertPokemonList(pokemons.asEntity()) From cefa6d456bb7ab26026ef573ab3e38f6f80fc1f8 Mon Sep 17 00:00:00 2001 From: hodak Date: Sat, 24 May 2025 02:41:04 +0900 Subject: [PATCH 2/3] Add last page reached check to prevent unnecessary fetches --- .../pokedex/compose/feature/home/HomeViewModel.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home/HomeViewModel.kt b/feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home/HomeViewModel.kt index 86ca4ca..d08a15a 100644 --- a/feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home/HomeViewModel.kt +++ b/feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home/HomeViewModel.kt @@ -37,12 +37,19 @@ class HomeViewModel @Inject constructor( internal val uiState: ViewModelStateFlow = viewModelStateFlow(HomeUiState.Loading) + private var isLastPageReached = false private val pokemonFetchingIndex: MutableStateFlow = MutableStateFlow(0) + val pokemonList: StateFlow> = pokemonFetchingIndex.flatMapLatest { page -> homeRepository.fetchPokemonList( page = page, - onStart = { uiState.tryEmit(key, HomeUiState.Loading) }, - onComplete = { uiState.tryEmit(key, HomeUiState.Idle) }, + onStart = { + uiState.tryEmit(key, HomeUiState.Loading) + }, + onComplete = { + uiState.tryEmit(key, HomeUiState.Idle) + }, + onLastPageReached = { isLastPageReached = true }, onError = { uiState.tryEmit(key, HomeUiState.Error(it)) }, ) }.stateIn( @@ -52,7 +59,7 @@ class HomeViewModel @Inject constructor( ) fun fetchNextPokemonList() { - if (uiState.value != HomeUiState.Loading) { + if (uiState.value != HomeUiState.Loading && !isLastPageReached) { pokemonFetchingIndex.value++ } } From f0932b6a19893126293a6ef2b0642310ca2db12f Mon Sep 17 00:00:00 2001 From: hodak Date: Mon, 26 May 2025 20:21:29 +0900 Subject: [PATCH 3/3] Clean up indentation of pokemonList logic in HomeViewModel --- .../pokedex/compose/feature/home/HomeViewModel.kt | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home/HomeViewModel.kt b/feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home/HomeViewModel.kt index d08a15a..a9c44be 100644 --- a/feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home/HomeViewModel.kt +++ b/feature/home/src/main/kotlin/com/skydoves/pokedex/compose/feature/home/HomeViewModel.kt @@ -36,19 +36,14 @@ class HomeViewModel @Inject constructor( ) : BaseViewModel() { internal val uiState: ViewModelStateFlow = viewModelStateFlow(HomeUiState.Loading) - private var isLastPageReached = false - private val pokemonFetchingIndex: MutableStateFlow = MutableStateFlow(0) + private val pokemonFetchingIndex: MutableStateFlow = MutableStateFlow(0) val pokemonList: StateFlow> = pokemonFetchingIndex.flatMapLatest { page -> homeRepository.fetchPokemonList( page = page, - onStart = { - uiState.tryEmit(key, HomeUiState.Loading) - }, - onComplete = { - uiState.tryEmit(key, HomeUiState.Idle) - }, + onStart = { uiState.tryEmit(key, HomeUiState.Loading) }, + onComplete = { uiState.tryEmit(key, HomeUiState.Idle) }, onLastPageReached = { isLastPageReached = true }, onError = { uiState.tryEmit(key, HomeUiState.Error(it)) }, )