-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e80f6af
commit 2b69a6f
Showing
13 changed files
with
129 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...c/main/java/com/karumi/jetpack/superheroes/data/repository/SuperHeroesBoundaryCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.karumi.jetpack.superheroes.data.repository | ||
|
||
import androidx.lifecycle.Observer | ||
import androidx.paging.PagedList | ||
import com.karumi.jetpack.superheroes.domain.model.SuperHero | ||
|
||
class SuperHeroesBoundaryCallback( | ||
private val local: LocalSuperHeroDataSource, | ||
private val remote: RemoteSuperHeroDataSource | ||
) : PagedList.BoundaryCallback<SuperHero>() { | ||
|
||
private var pageIndexAboutToLoad = 0 | ||
|
||
override fun onZeroItemsLoaded() { | ||
pageIndexAboutToLoad = 0 | ||
loadNextPage() | ||
} | ||
|
||
override fun onItemAtEndLoaded(itemAtEnd: SuperHero) { | ||
loadNextPage() | ||
} | ||
|
||
private fun loadNextPage() { | ||
val remoteSuperHeroesPage = | ||
remote.getSuperHeroesPage(pageIndexAboutToLoad, SuperHeroRepository.PAGE_SIZE) | ||
|
||
remoteSuperHeroesPage.observeForever(object : Observer<List<SuperHero>> { | ||
override fun onChanged(superHeroes: List<SuperHero>) { | ||
local.saveAll(superHeroes) | ||
remoteSuperHeroesPage.removeObserver(this) | ||
pageIndexAboutToLoad++ | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
app/src/main/java/com/karumi/jetpack/superheroes/domain/usecase/GetSuperHeroes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package com.karumi.jetpack.superheroes.domain.usecase | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.paging.PagedList | ||
import com.karumi.jetpack.superheroes.data.repository.SuperHeroRepository | ||
import com.karumi.jetpack.superheroes.domain.model.SuperHero | ||
|
||
class GetSuperHeroes(private val superHeroesRepository: SuperHeroRepository) { | ||
operator fun invoke(): LiveData<List<SuperHero>> = superHeroesRepository.getAllSuperHeroes() | ||
operator fun invoke(): LiveData<PagedList<SuperHero>> = | ||
superHeroesRepository.getAllSuperHeroes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters