Created
September 5, 2023 14:08
-
-
Save jonrysimbolon/4456a5949343fc71fda3e97f81b5226b 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
package com.jonrysimbolon.base.adapter | |
import androidx.paging.PagingSource | |
import androidx.paging.PagingState | |
/* | |
class BasePagingSource( | |
private val remote: ApiService, | |
private val idCategory: String, | |
) : PagingSource<Int, MovieModel>() { | |
override fun getRefreshKey(state: PagingState<Int, MovieModel>): Int? { | |
return state.anchorPosition?.let { anchorPosition -> | |
val anchorPage = state.closestPageToPosition(anchorPosition) | |
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1) | |
} | |
} | |
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MovieModel> { | |
return try { | |
val page = params.key ?: INITIAL_PAGE_INDEX | |
val responseData = remote.getAllMovies(page, idCategory).results | |
LoadResult.Page( | |
data = responseData, | |
prevKey = if (page == 1) null else page - 1, | |
nextKey = if (responseData.isEmpty()) null else page + 1 | |
) | |
} catch (exception: Exception) { | |
return LoadResult.Error(exception) | |
} | |
} | |
private companion object { | |
const val INITIAL_PAGE_INDEX = 1 | |
} | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment