Skip to content

Instantly share code, notes, and snippets.

@VictorAlbertos
Last active November 18, 2020 15:34
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 VictorAlbertos/dabdc66dff3746a5ae89c5f80a3e1acb to your computer and use it in GitHub Desktop.
Save VictorAlbertos/dabdc66dff3746a5ae89c5f80a3e1acb to your computer and use it in GitHub Desktop.
class SamplePagingSource(
private val sampleRepository: SampleRepository
) : PagingSource<Int, SampleEntity>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, SampleEntity> {
return try {
val data = sampleRepository.getNextPage(lastSeenId = params.key ?: 1)
.map { SampleEntity(it, "Page number: $it") }
LoadResult.Page(
data = data,
prevKey = null,
nextKey = if (data.isNotEmpty()) {
data.last().id + 1
} else {
// return null when page is empty to denote the end of the pagination
null
}
)
} catch (e: Exception) {
LoadResult.Error(e)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment