0

In fact, I am trying to migrate from paging 2 to paging 3. Already I have successfully implemented PageKeyedDataSource of Paging 2 with RxpagingSource in my codebase. But when I tried to implement ItemKeyedDataSource of Paging 2 to Paging library 3, I got confused to implement that. I also tried to code. but got stuck. Here is my code.

JobSliderRestApi.kt

@GET("job/list/slides")
fun getDetailOfSelectedJob(
    @Query("current_job") currentJodId: Int?,
    @Query("limit") jobLimit: Int?,
    @Query("search_in") fetchType: String?
): Single<Response<JobViewResponse>>

JobViewResponse.kt

data class JobViewResponse(
    @SerializedName("data") val data: ArrayList<JobDetail>?
) : BaseResponse()

JodSliderDataSource.kt

class JodSliderDataSource @Inject constructor(
    private val jobSliderRestApi: JobSliderRestApi,
    private val currentJobId: Int
): RxPagingSource<Int, JobDetail>() {

    override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, JobDetail>> {
        
        return jobSliderRestApi.getDetailOfSelectedJob(currentJobId, 20, "next").toSingle()
            .subscribeOn(Schedulers.io())
            .map { jobResponse -> jobResponse.data }
            .map { jobData -> toLoadResult(jobData, position) } // Don't know what to do
            .onErrorReturn { LoadResult.Error(it) }
    }

    //Don't know what to do
    private fun toLoadResult(data: JobDetail, position: Int): LoadResult<Int, JobDetail> {
        /**val prevKey = if (position == 1) null else position-1
        val nextKey = if (data.hasMore) position+1 else null

        return LoadResult.Page(data.jobs, prevKey, nextKey)*/
    }

    @ExperimentalPagingApi
    override fun getRefreshKey(state: PagingState<Int, JobDetail>): Int? {
        return super.getRefreshKey(state)
    }
}

in fact, previously I used 'currentJodId' to differentiate list in Paging 2. but in paging 3, I did not get any clue t fix these

Aminul Haque Aome
  • 2,261
  • 21
  • 34

1 Answers1

0

After a lot of searching I solved the problem. Here is the updated JodSliderDataSource class

class JodSliderDataSource @Inject constructor(
    private val jobSliderRestApi: JobSliderRestApi
): RxPagingSource<Int, JobDetail>() {

    override val keyReuseSupported = true

    @ExperimentalPagingApi
    override fun getRefreshKey(state: PagingState<Int, JobDetail>): Int? {
        return state.anchorPosition?.let {
            state.closestItemToPosition(it)?.jobId
        }
    }

    override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, JobDetail>> {
        return jobSliderRestApi.getDetailOfSelectedJob(42673, 2, "next").toSingle()
            .subscribeOn(Schedulers.io())
            .map { jobResponse -> toLoadResult(jobResponse.data) }
            .onErrorReturn { LoadResult.Error(it) }
    }

    private fun toLoadResult(data: ArrayList<JobDetail>): LoadResult<Int, JobDetail> {
        return LoadResult.Page(data = data, prevKey = null, nextKey = data.lastOrNull()?.jobId)
    }
}
Aminul Haque Aome
  • 2,261
  • 21
  • 34