Created
January 23, 2021 17:26
-
-
Save 1jGabriel/08c2dea487950edf720aac87dcaddbde to your computer and use it in GitHub Desktop.
PagingSource
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
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