From 23b3835af015d76c97985fbcd6326216d45156fb Mon Sep 17 00:00:00 2001 From: Stypox Date: Mon, 11 Nov 2024 00:16:32 +0100 Subject: [PATCH] Fix PagingSource for comments The previous implementation was skipping the first page of comments --- .../newpipe/paging/CommentRepliesSource.kt | 27 ++++++++++++++ .../schabi/newpipe/paging/CommentsSource.kt | 35 ++++++------------- .../video/comment/CommentRepliesDialog.kt | 7 ++-- 3 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/paging/CommentRepliesSource.kt diff --git a/app/src/main/java/org/schabi/newpipe/paging/CommentRepliesSource.kt b/app/src/main/java/org/schabi/newpipe/paging/CommentRepliesSource.kt new file mode 100644 index 000000000..efa189db9 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/paging/CommentRepliesSource.kt @@ -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() { + private val service = NewPipe.getService(commentInfo.serviceId) + + override suspend fun load(params: LoadParams): LoadResult { + // 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) = null +} diff --git a/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt b/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt index d92589732..669485e66 100644 --- a/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt +++ b/app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt @@ -9,35 +9,20 @@ import org.schabi.newpipe.extractor.Page import org.schabi.newpipe.extractor.comments.CommentsInfo import org.schabi.newpipe.extractor.comments.CommentsInfoItem import org.schabi.newpipe.ui.components.video.comment.CommentInfo -import org.schabi.newpipe.util.NO_SERVICE_ID -class CommentsSource( - serviceId: Int, - private val url: String, - private val repliesPage: Page?, - private val commentInfo: CommentInfo? = null, -) : PagingSource() { - 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) +class CommentsSource(private val commentInfo: CommentInfo) : PagingSource() { + private val service = NewPipe.getService(commentInfo.serviceId) override suspend fun load(params: LoadParams): LoadResult { - // repliesPage is non-null only when used to load the comment replies - val nextKey = params.key ?: repliesPage - - return withContext(Dispatchers.IO) { - nextKey?.let { - val info = CommentsInfo.getMoreItems(service, url, it) - LoadResult.Page(info.items, null, info.nextPage) - } ?: run { - val info = commentInfo ?: CommentInfo(CommentsInfo.getInfo(service, url)) - LoadResult.Page(info.comments, null, info.nextPage) + // params.key is null the first time the load() function is called, so we need to return the + // first batch of already-loaded comments + if (params.key == null) { + return LoadResult.Page(commentInfo.comments, null, commentInfo.nextPage) + } else { + val info = withContext(Dispatchers.IO) { + CommentsInfo.getMoreItems(service, commentInfo.url, params.key) } + return LoadResult.Page(info.items, null, info.nextPage) } } diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesDialog.kt b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesDialog.kt index 5e8ce1c4a..80d5a86ee 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesDialog.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesDialog.kt @@ -32,7 +32,7 @@ import my.nanihadesuka.compose.ScrollbarSettings import org.schabi.newpipe.R import org.schabi.newpipe.extractor.comments.CommentsInfoItem 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.NoItemsMessage import org.schabi.newpipe.ui.theme.AppTheme @@ -46,8 +46,9 @@ fun CommentRepliesDialog( val coroutineScope = rememberCoroutineScope() val commentsFlow = remember { Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) { - CommentsSource(parentComment.serviceId, parentComment.url, parentComment.replies) - }.flow + CommentRepliesSource(parentComment) + } + .flow .cachedIn(coroutineScope) }