PixelDroid-App-Android/app/src/main/java/com/h/pixeldroid/posts/feeds/uncachedFeeds/profile/ProfilePagingSource.kt

36 lines
1.1 KiB
Kotlin
Raw Normal View History

2021-01-22 16:42:23 +01:00
package com.h.pixeldroid.posts.feeds.uncachedFeeds.profile
import androidx.paging.PagingSource
2021-03-14 23:03:36 +01:00
import androidx.paging.PagingState
2021-01-22 16:42:23 +01:00
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.api.objects.Status
import retrofit2.HttpException
import java.io.IOException
class ProfilePagingSource(
private val api: PixelfedAPI,
private val accountId: String
2021-01-22 19:42:56 +01:00
) : PagingSource<String, Status>() {
override suspend fun load(params: LoadParams<String>): LoadResult<String, Status> {
2021-01-22 16:42:23 +01:00
val position = params.key
return try {
val posts = api.accountPosts(
account_id = accountId,
max_id = position,
limit = params.loadSize
2021-01-22 19:25:57 +01:00
)
2021-01-22 16:42:23 +01:00
LoadResult.Page(
data = posts,
prevKey = null,
2021-01-22 19:42:56 +01:00
nextKey = posts.lastOrNull()?.id
2021-01-22 16:42:23 +01:00
)
} catch (exception: HttpException) {
LoadResult.Error(exception)
2021-03-26 17:51:42 +01:00
} catch (exception: IOException) {
LoadResult.Error(exception)
2021-01-22 16:42:23 +01:00
}
}
2021-03-14 23:03:36 +01:00
2021-03-19 17:28:13 +01:00
override fun getRefreshKey(state: PagingState<String, Status>): String? = null
2021-01-22 16:42:23 +01:00
}