Skip to content

Commit 23b3835

Browse files
committed
Fix PagingSource for comments
The previous implementation was skipping the first page of comments
1 parent 412e1d6 commit 23b3835

3 files changed

Lines changed: 41 additions & 28 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.schabi.newpipe.paging
2+
3+
import androidx.paging.PagingSource
4+
import androidx.paging.PagingState
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.withContext
7+
import org.schabi.newpipe.extractor.NewPipe
8+
import org.schabi.newpipe.extractor.Page
9+
import org.schabi.newpipe.extractor.comments.CommentsInfo
10+
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
11+
12+
class CommentRepliesSource(
13+
private val commentInfo: CommentsInfoItem,
14+
) : PagingSource<Page, CommentsInfoItem>() {
15+
private val service = NewPipe.getService(commentInfo.serviceId)
16+
17+
override suspend fun load(params: LoadParams<Page>): LoadResult<Page, CommentsInfoItem> {
18+
// params.key is null the first time load() is called, and we need to return the first page
19+
val repliesPage = params.key ?: commentInfo.replies
20+
val info = withContext(Dispatchers.IO) {
21+
CommentsInfo.getMoreItems(service, commentInfo.url, repliesPage)
22+
}
23+
return LoadResult.Page(info.items, null, info.nextPage)
24+
}
25+
26+
override fun getRefreshKey(state: PagingState<Page, CommentsInfoItem>) = null
27+
}

app/src/main/java/org/schabi/newpipe/paging/CommentsSource.kt

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,20 @@ import org.schabi.newpipe.extractor.Page
99
import org.schabi.newpipe.extractor.comments.CommentsInfo
1010
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
1111
import org.schabi.newpipe.ui.components.video.comment.CommentInfo
12-
import org.schabi.newpipe.util.NO_SERVICE_ID
1312

14-
class CommentsSource(
15-
serviceId: Int,
16-
private val url: String,
17-
private val repliesPage: Page?,
18-
private val commentInfo: CommentInfo? = null,
19-
) : PagingSource<Page, CommentsInfoItem>() {
20-
constructor(commentInfo: CommentInfo) : this(
21-
commentInfo.serviceId, commentInfo.url, commentInfo.nextPage, commentInfo
22-
)
23-
24-
init {
25-
require(serviceId != NO_SERVICE_ID) { "serviceId is NO_SERVICE_ID" }
26-
}
27-
private val service = NewPipe.getService(serviceId)
13+
class CommentsSource(private val commentInfo: CommentInfo) : PagingSource<Page, CommentsInfoItem>() {
14+
private val service = NewPipe.getService(commentInfo.serviceId)
2815

2916
override suspend fun load(params: LoadParams<Page>): LoadResult<Page, CommentsInfoItem> {
30-
// repliesPage is non-null only when used to load the comment replies
31-
val nextKey = params.key ?: repliesPage
32-
33-
return withContext(Dispatchers.IO) {
34-
nextKey?.let {
35-
val info = CommentsInfo.getMoreItems(service, url, it)
36-
LoadResult.Page(info.items, null, info.nextPage)
37-
} ?: run {
38-
val info = commentInfo ?: CommentInfo(CommentsInfo.getInfo(service, url))
39-
LoadResult.Page(info.comments, null, info.nextPage)
17+
// params.key is null the first time the load() function is called, so we need to return the
18+
// first batch of already-loaded comments
19+
if (params.key == null) {
20+
return LoadResult.Page(commentInfo.comments, null, commentInfo.nextPage)
21+
} else {
22+
val info = withContext(Dispatchers.IO) {
23+
CommentsInfo.getMoreItems(service, commentInfo.url, params.key)
4024
}
25+
return LoadResult.Page(info.items, null, info.nextPage)
4126
}
4227
}
4328

app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesDialog.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import my.nanihadesuka.compose.ScrollbarSettings
3232
import org.schabi.newpipe.R
3333
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
3434
import org.schabi.newpipe.extractor.stream.Description
35-
import org.schabi.newpipe.paging.CommentsSource
35+
import org.schabi.newpipe.paging.CommentRepliesSource
3636
import org.schabi.newpipe.ui.components.common.LoadingIndicator
3737
import org.schabi.newpipe.ui.components.common.NoItemsMessage
3838
import org.schabi.newpipe.ui.theme.AppTheme
@@ -46,8 +46,9 @@ fun CommentRepliesDialog(
4646
val coroutineScope = rememberCoroutineScope()
4747
val commentsFlow = remember {
4848
Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) {
49-
CommentsSource(parentComment.serviceId, parentComment.url, parentComment.replies)
50-
}.flow
49+
CommentRepliesSource(parentComment)
50+
}
51+
.flow
5152
.cachedIn(coroutineScope)
5253
}
5354

0 commit comments

Comments
 (0)