Created
June 13, 2020 07:54
-
-
Save ekoo/2d3ada1f51e33e6477416e8e21cdea6c 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 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