Skip to content

Commit

Permalink
Fix PagingSource for comments
Browse files Browse the repository at this point in the history
The previous implementation was skipping the first page of comments
  • Loading branch information
Stypox committed Nov 10, 2024
1 parent 412e1d6 commit 23b3835
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 10 additions & 25 deletions app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<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)
class CommentsSource(private val commentInfo: CommentInfo) : PagingSource<Page, CommentsInfoItem>() {
private val service = NewPipe.getService(commentInfo.serviceId)

override suspend fun load(params: LoadParams<Page>): LoadResult<Page, CommentsInfoItem> {
// 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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down

0 comments on commit 23b3835

Please sign in to comment.