Skip to content

Instantly share code, notes, and snippets.

@emrekose26
Created November 3, 2020 10:47
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 emrekose26/ef54c3a9f606ac3a48a30a5e6f756f7f to your computer and use it in GitHub Desktop.
Save emrekose26/ef54c3a9f606ac3a48a30a5e6f756f7f to your computer and use it in GitHub Desktop.
class PopularMoviesPagingSource @Inject constructor(
val movieApiService: MovieApiService
) : PagingSource<Int, Movie>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Movie> {
val position = params.key ?: STARTING_INDEX
return try {
val movies = movieApiService.getPopularMovies(position)
LoadResult.Page(
data = movies.results,
prevKey = if (position == STARTING_INDEX) null else position - 1,
nextKey = if (movies.results.isEmpty()) null else position + 1
)
} catch (e: Exception) {
LoadResult.Error(e)
}
}
companion object {
const val STARTING_INDEX = 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment