Skip to content

Instantly share code, notes, and snippets.

@ekoo
Created June 13, 2020 07:54
Show Gist options
  • Save ekoo/2d3ada1f51e33e6477416e8e21cdea6c to your computer and use it in GitHub Desktop.
Save ekoo/2d3ada1f51e33e6477416e8e21cdea6c to your computer and use it in GitHub Desktop.
class MoviePagingSource(private val service: MovieServices = MovieServiceBuilder.build()) : PagingSource<Int, ResponseModel.MovieModel>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ResponseModel.MovieModel> {
val pagePosition = params.key ?: STARTING_PAGE_INDEX
return try {
val data = service.getMovie(API_KEY, LANGUAGE, pagePosition).movieList //using retrofit
val prevKey = if (pagePosition == STARTING_PAGE_INDEX) null else pagePosition - 1
val nextKey = if (data.isEmpty()) null else pagePosition + 1
LoadResult.Page(data, prevKey, nextKey)
} catch (exception: Exception){
LoadResult.Error(exception)
}
}
companion object{
private const val STARTING_PAGE_INDEX = 1
private const val API_KEY = "......."
private const val LANGUAGE = "en-EN"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment