Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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