Skip to content

Instantly share code, notes, and snippets.

@rubenquadros
Last active August 18, 2021 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubenquadros/3b3c2e614a177c59e24f42db18710101 to your computer and use it in GitHub Desktop.
Save rubenquadros/3b3c2e614a177c59e24f42db18710101 to your computer and use it in GitHub Desktop.
Paging source of our Paging 3 library
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