Last active
November 18, 2020 15:34
-
-
Save VictorAlbertos/dabdc66dff3746a5ae89c5f80a3e1acb to your computer and use it in GitHub Desktop.
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 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