Skip to content

Instantly share code, notes, and snippets.

@nisaefendioglu
Created April 27, 2023 14:54
Show Gist options
  • Save nisaefendioglu/81daf52f287ac0c99eac64ba346cffa4 to your computer and use it in GitHub Desktop.
Save nisaefendioglu/81daf52f287ac0c99eac64ba346cffa4 to your computer and use it in GitHub Desktop.
class MyPagingSource(private val apiService: ApiService) : PagingSource<Int, Data>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Data> {
try {
val pageNumber = params.key ?: 1
val pageSize = params.loadSize
val response = apiService.getData(pageNumber, pageSize)
val data = response.body() ?: return LoadResult.Error(Exception("Response is null"))
val prevKey = if (pageNumber == 1) null else pageNumber - 1
val nextKey = if (data.isEmpty()) null else pageNumber + 1
return LoadResult.Page(data, prevKey, nextKey)
} catch (exception: Exception) {
return LoadResult.Error(exception)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment