Created
November 3, 2020 10:47
-
-
Save emrekose26/ef54c3a9f606ac3a48a30a5e6f756f7f 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 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