Created
April 28, 2023 12:03
-
-
Save nisaefendioglu/2b9677152e23ebff696d3ab4c26baf82 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 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