Created
April 27, 2023 14:54
-
-
Save nisaefendioglu/81daf52f287ac0c99eac64ba346cffa4 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