Skip to content

Instantly share code, notes, and snippets.

@1jGabriel
Created January 23, 2021 17:26
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 1jGabriel/08c2dea487950edf720aac87dcaddbde to your computer and use it in GitHub Desktop.
Save 1jGabriel/08c2dea487950edf720aac87dcaddbde to your computer and use it in GitHub Desktop.
PagingSource
class PersonageDataSource(
private val api: RickNMortyApi
) : PagingSource<Int, CharacterUi>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CharacterUi> {
return try {
val result = api.getCharacters(params.key ?: STARTING_PAGE_INDEX).toCharacters()
Page(
data = result,
prevKey = params.key,
nextKey = params.key?.plus(1) ?: STARTING_PAGE_INDEX.plus(1)
)
} catch (e: IOException) {
LoadResult.Error(e)
} catch (e: HttpException) {
LoadResult.Error(e)
}
}
companion object {
private const val STARTING_PAGE_INDEX = 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment