mirror of
https://github.com/TeamNewPipe/NewPipe.git
synced 2024-11-22 02:53:09 +01:00
Fix PagingSource for comments
The previous implementation was skipping the first page of comments
This commit is contained in:
parent
412e1d602a
commit
23b3835af0
@ -0,0 +1,27 @@
|
|||||||
|
package org.schabi.newpipe.paging
|
||||||
|
|
||||||
|
import androidx.paging.PagingSource
|
||||||
|
import androidx.paging.PagingState
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import org.schabi.newpipe.extractor.NewPipe
|
||||||
|
import org.schabi.newpipe.extractor.Page
|
||||||
|
import org.schabi.newpipe.extractor.comments.CommentsInfo
|
||||||
|
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
|
||||||
|
|
||||||
|
class CommentRepliesSource(
|
||||||
|
private val commentInfo: CommentsInfoItem,
|
||||||
|
) : PagingSource<Page, CommentsInfoItem>() {
|
||||||
|
private val service = NewPipe.getService(commentInfo.serviceId)
|
||||||
|
|
||||||
|
override suspend fun load(params: LoadParams<Page>): LoadResult<Page, CommentsInfoItem> {
|
||||||
|
// params.key is null the first time load() is called, and we need to return the first page
|
||||||
|
val repliesPage = params.key ?: commentInfo.replies
|
||||||
|
val info = withContext(Dispatchers.IO) {
|
||||||
|
CommentsInfo.getMoreItems(service, commentInfo.url, repliesPage)
|
||||||
|
}
|
||||||
|
return LoadResult.Page(info.items, null, info.nextPage)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getRefreshKey(state: PagingState<Page, CommentsInfoItem>) = null
|
||||||
|
}
|
@ -9,35 +9,20 @@ import org.schabi.newpipe.extractor.Page
|
|||||||
import org.schabi.newpipe.extractor.comments.CommentsInfo
|
import org.schabi.newpipe.extractor.comments.CommentsInfo
|
||||||
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
|
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
|
||||||
import org.schabi.newpipe.ui.components.video.comment.CommentInfo
|
import org.schabi.newpipe.ui.components.video.comment.CommentInfo
|
||||||
import org.schabi.newpipe.util.NO_SERVICE_ID
|
|
||||||
|
|
||||||
class CommentsSource(
|
class CommentsSource(private val commentInfo: CommentInfo) : PagingSource<Page, CommentsInfoItem>() {
|
||||||
serviceId: Int,
|
private val service = NewPipe.getService(commentInfo.serviceId)
|
||||||
private val url: String,
|
|
||||||
private val repliesPage: Page?,
|
|
||||||
private val commentInfo: CommentInfo? = null,
|
|
||||||
) : PagingSource<Page, CommentsInfoItem>() {
|
|
||||||
constructor(commentInfo: CommentInfo) : this(
|
|
||||||
commentInfo.serviceId, commentInfo.url, commentInfo.nextPage, commentInfo
|
|
||||||
)
|
|
||||||
|
|
||||||
init {
|
|
||||||
require(serviceId != NO_SERVICE_ID) { "serviceId is NO_SERVICE_ID" }
|
|
||||||
}
|
|
||||||
private val service = NewPipe.getService(serviceId)
|
|
||||||
|
|
||||||
override suspend fun load(params: LoadParams<Page>): LoadResult<Page, CommentsInfoItem> {
|
override suspend fun load(params: LoadParams<Page>): LoadResult<Page, CommentsInfoItem> {
|
||||||
// repliesPage is non-null only when used to load the comment replies
|
// params.key is null the first time the load() function is called, so we need to return the
|
||||||
val nextKey = params.key ?: repliesPage
|
// first batch of already-loaded comments
|
||||||
|
if (params.key == null) {
|
||||||
return withContext(Dispatchers.IO) {
|
return LoadResult.Page(commentInfo.comments, null, commentInfo.nextPage)
|
||||||
nextKey?.let {
|
} else {
|
||||||
val info = CommentsInfo.getMoreItems(service, url, it)
|
val info = withContext(Dispatchers.IO) {
|
||||||
LoadResult.Page(info.items, null, info.nextPage)
|
CommentsInfo.getMoreItems(service, commentInfo.url, params.key)
|
||||||
} ?: run {
|
|
||||||
val info = commentInfo ?: CommentInfo(CommentsInfo.getInfo(service, url))
|
|
||||||
LoadResult.Page(info.comments, null, info.nextPage)
|
|
||||||
}
|
}
|
||||||
|
return LoadResult.Page(info.items, null, info.nextPage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ import my.nanihadesuka.compose.ScrollbarSettings
|
|||||||
import org.schabi.newpipe.R
|
import org.schabi.newpipe.R
|
||||||
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
|
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
|
||||||
import org.schabi.newpipe.extractor.stream.Description
|
import org.schabi.newpipe.extractor.stream.Description
|
||||||
import org.schabi.newpipe.paging.CommentsSource
|
import org.schabi.newpipe.paging.CommentRepliesSource
|
||||||
import org.schabi.newpipe.ui.components.common.LoadingIndicator
|
import org.schabi.newpipe.ui.components.common.LoadingIndicator
|
||||||
import org.schabi.newpipe.ui.components.common.NoItemsMessage
|
import org.schabi.newpipe.ui.components.common.NoItemsMessage
|
||||||
import org.schabi.newpipe.ui.theme.AppTheme
|
import org.schabi.newpipe.ui.theme.AppTheme
|
||||||
@ -46,8 +46,9 @@ fun CommentRepliesDialog(
|
|||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
val commentsFlow = remember {
|
val commentsFlow = remember {
|
||||||
Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) {
|
Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) {
|
||||||
CommentsSource(parentComment.serviceId, parentComment.url, parentComment.replies)
|
CommentRepliesSource(parentComment)
|
||||||
}.flow
|
}
|
||||||
|
.flow
|
||||||
.cachedIn(coroutineScope)
|
.cachedIn(coroutineScope)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user