Last active
August 18, 2021 20:41
-
-
Save rubenquadros/3b3c2e614a177c59e24f42db18710101 to your computer and use it in GitHub Desktop.
Paging source of our Paging 3 library
This file contains hidden or 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
class GamesSource( | |
private val gamesRepository: GamesRepository | |
) : PagingSource<Int, GameResultsEntity>() { | |
override suspend fun load( | |
params: LoadParams<Int> | |
): LoadResult<Int, GameResultsEntity> { | |
val nextPage = params.key ?: 1 | |
val gamesResponse = | |
gamesRepository.getAllGames(nextPage) | |
return if (gamesResponse.data == null) { | |
LoadResult.Error( | |
Exception(gamesResponse.error.toString()) | |
) | |
} else { | |
LoadResult.Page( | |
data = gamesResponse.data.gameEntities, | |
prevKey = | |
if (nextPage == 1) null | |
else nextPage - 1, | |
nextKey = nextPage.plus(1) | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment