KV
06/28/2021, 7:57 AMprivate const val STARTING_PAGE_INDEX = 1
class AbsencePagingDataSource(
private val apiProvider: ApiProvider,
private val absenceDataMapper: AbsenceDataMapper,
private val workspaceId: String
) :
PagingSource<Int, AbsenceData>() {
lateinit var response: ListApiResponse<WorkspaceAbsenceResponse>
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AbsenceData> {
val position = params.key ?: STARTING_PAGE_INDEX
val request = Endpoint.GetWorkspaceAbsence(
workspaceId = workspaceId,
Queries = listOf(
Queries.Paginate(position, 50), -------> SEE URL BELOW
)
)
return try {
apiProvider.api.performRequest<ListApiResponse<WorkspaceAbsenceResponse>>(request).map { listApiResponse ->
response = listApiResponse
}
val endOfPaginationReached = response.data.isEmpty()
LoadResult.Page(
data = absenceDataMapper.mapToDomain(response),
prevKey = if (position == STARTING_PAGE_INDEX) null else position - 1,
nextKey = if (endOfPaginationReached) null else position + 1
)
} catch (exception: IOException) {
LoadResult.Error(exception)
}
}
override fun getRefreshKey(state: PagingState<Int, AbsenceData>): Int? {
return state.anchorPosition?.let { anchorPosition ->
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
}
}
}
URL/request looks like ->
---------------------------
1. url=<https://PATH/api/v3/workspaces/120/absences?page=1&items=50> ----> 200
2. url=<https://PATH/api/v3/workspaces/120/absences?page=2&items=50> ----> 422
3. url=<https://PATH/api/v3/workspaces/120/absences?page=3&items=50> ----> 422
solidogen
06/28/2021, 8:03 AMsolidogen
06/28/2021, 8:04 AMsolidogen
06/28/2021, 8:06 AMKV
06/28/2021, 8:06 AMsolidogen
06/28/2021, 8:07 AMsolidogen
06/28/2021, 8:07 AMKV
06/28/2021, 8:08 AMKV
06/28/2021, 8:08 AMsolidogen
06/28/2021, 8:10 AMsolidogen
06/28/2021, 8:10 AMPager(
config = PagingConfig(
pageSize = ITEMS_PER_PAGE,
initialLoadSize = // overridable, by default 3x pagesize
),
KV
06/28/2021, 8:13 AMKV
06/28/2021, 8:15 AMreturn Pager(config = PagingConfig(pageSize = 50, prefetchDistance = 0),
pagingSourceFactory = { AbsencePagingDataSource(apiProvider, workspaceMapper.absenceDataMapper, workspaceId) }).flow
I am using like this and it will call 1 time api with success only 🙂
But I am not sure if I have multiple data then is this works fine or notsolidogen
06/28/2021, 8:15 AMsolidogen
06/28/2021, 8:16 AMprefetchDistance = 0
is not a good choice, see property documentation how it workssolidogen
06/28/2021, 8:17 AMKV
06/28/2021, 8:17 AMsolidogen
06/28/2021, 8:17 AMKV
06/28/2021, 8:19 AMsolidogen
06/28/2021, 8:20 AMurl=<https://PATH/api/v3/workspaces/120/absences?page=2&items=50> ----> 422
try this in postman/make a retrofit call like this and see if it works. if it doesn’t, this is not paging fault and you can return to whatever worksKV
06/28/2021, 8:24 AMreturn Pager(config = PagingConfig(pageSize = 1, prefetchDistance = 0),
pagingSourceFactory = { AbsencePagingDataSource(apiProvider, workspaceMapper.absenceDataMapper, workspaceId) }).flow
and now if I call the API with
page=1&items=20 it works
but it never call page=2&items=20 (THIS URL NOT TRIGGERED)
But using postman , the URL give me the result of page=2 &items=20KV
06/28/2021, 8:33 AMKV
06/28/2021, 8:34 AMsolidogen
06/28/2021, 9:11 AM