Files
rider-pro-android-app/app/src/main/java/com/aiosman/riderpro/entity/Comment.kt

56 lines
1.7 KiB
Kotlin
Raw Normal View History

2024-08-24 23:11:20 +08:00
package com.aiosman.riderpro.entity
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.aiosman.riderpro.data.CommentRemoteDataSource
import com.aiosman.riderpro.data.NoticePost
import java.io.IOException
import java.util.Date
data class CommentEntity(
val id: Int,
val name: String,
val comment: String,
val date: Date,
val likes: Int,
val replies: List<CommentEntity>,
val postId: Int = 0,
val avatar: String,
val author: Long,
var liked: Boolean,
var unread: Boolean = false,
var post: NoticePost?
)
class CommentPagingSource(
private val remoteDataSource: CommentRemoteDataSource,
private val postId: Int? = null,
private val postUser: Int? = null,
2024-09-07 20:26:15 +08:00
private val selfNotice: Boolean? = null,
private val order: String? = null
2024-08-24 23:11:20 +08:00
) : PagingSource<Int, CommentEntity>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CommentEntity> {
return try {
val currentPage = params.key ?: 1
val comments = remoteDataSource.getComments(
pageNumber = currentPage,
postId = postId,
postUser = postUser,
2024-09-07 20:26:15 +08:00
selfNotice = selfNotice,
order = order
2024-08-24 23:11:20 +08:00
)
LoadResult.Page(
data = comments.list,
prevKey = if (currentPage == 1) null else currentPage - 1,
nextKey = if (comments.list.isEmpty()) null else comments.page + 1
)
} catch (exception: IOException) {
return LoadResult.Error(exception)
}
}
override fun getRefreshKey(state: PagingState<Int, CommentEntity>): Int? {
return state.anchorPosition
}
}