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

47 lines
1.5 KiB
Kotlin
Raw Normal View History

2021-04-22 11:47:18 +02:00
package org.pixeldroid.app.posts.feeds.uncachedFeeds.profile
2021-01-22 16:42:23 +01:00
import androidx.paging.PagingSource
2021-03-14 23:03:36 +01:00
import androidx.paging.PagingState
2021-04-22 11:47:18 +02:00
import org.pixeldroid.app.utils.api.PixelfedAPI
import org.pixeldroid.app.utils.api.objects.Status
2021-01-22 16:42:23 +01:00
import retrofit2.HttpException
import java.io.IOException
class ProfilePagingSource(
private val api: PixelfedAPI,
2022-10-23 16:01:50 +02:00
private val accountId: String,
private val bookmarks: Boolean
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 {
2022-10-23 16:01:50 +02:00
val posts =
if(bookmarks) {
api.bookmarks(
limit = params.loadSize,
max_id = position
)
} else {
api.accountPosts(
account_id = accountId,
max_id = position,
limit = params.loadSize
)
}
2021-01-22 16:42:23 +01:00
val nextKey = posts.lastOrNull()?.id
2021-01-22 16:42:23 +01:00
LoadResult.Page(
data = posts,
prevKey = null,
nextKey = if(nextKey == position) null else nextKey
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
}