@@ -9,35 +9,20 @@ import org.schabi.newpipe.extractor.Page
99import org.schabi.newpipe.extractor.comments.CommentsInfo
1010import org.schabi.newpipe.extractor.comments.CommentsInfoItem
1111import 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
0 commit comments