I'm building an android application in which the user can choose a playlist from youtube and after choosing it I will need to load all the videos in that playlist into a single list.
I found several solutions online but they were using javascript and the ones that were written in rxjava the paginate was using int numbers rather then strings (I won't be able to use range).
For now I'm able to load the first 50 videos, I tried several approaches (one of which was a recursion but apparently I wrote it bad and I ended up using all the api requests in one load).
Here is my code:
fun getVidoesFromPlayList(playListId: String,pageToken: String): Single<MutableList<YTVideo>> {
return youTubeService.getPlayListResponse(playListId, pageToken)
.flatMap { response ->
Observable.just(response.items)
.flatMapIterable { data -> data }
.flatMap { item ->
youTubeService.getVideoResponse(item.snippet!!.resourceId!!.videoId).toObservable()
.subscribeOn(Schedulers.io())
.map { videoResponse ->
YTVideo(
item.snippet.position,
item.snippet.resourceId!!.videoId,
item.snippet.thumbnails!!.medium!!.url,
item.snippet.title,
videoResponse.items!![0].contentDetails!!.duration,
videoResponse.items[0].snippet!!.channelTitle
)
}
}
.repeatUntil { response.nextPageToken!=null }
.toList()
}
}
1) How can I call to getVidoesFromPlayList with a different nextpagetoken while caching the current videos? 2) Is there a way to do it more effecently without using recursion?
I will appreciate any help or guidance, thank you!