LibreTube/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt

120 lines
4.1 KiB
Kotlin
Raw Normal View History

2022-08-07 23:31:22 +05:30
package com.github.libretube.util
2022-08-10 19:19:31 +05:30
import com.github.libretube.Globals
2022-08-14 13:25:28 +05:30
import com.github.libretube.api.RetrofitInstance
2022-08-10 19:19:31 +05:30
import com.github.libretube.obj.StreamItem
2022-08-07 23:31:22 +05:30
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class AutoPlayHelper(
2022-08-10 19:19:31 +05:30
private val playlistId: String?
2022-08-07 23:31:22 +05:30
) {
private val playlistStreamIds = mutableListOf<String>()
private var playlistNextPage: String? = null
2022-08-21 13:45:37 +05:30
/**
* get the id of the next video to be played
*/
2022-08-10 19:19:31 +05:30
suspend fun getNextVideoId(
currentVideoId: String,
2022-08-21 13:45:37 +05:30
relatedStreams: List<StreamItem>?
2022-08-10 19:19:31 +05:30
): String? {
2022-08-10 19:53:57 +05:30
return if (Globals.playingQueue.last() != currentVideoId) {
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
2022-08-10 19:23:34 +05:30
Globals.playingQueue[currentVideoIndex + 1]
2022-08-24 21:26:57 +05:30
} else if (playlistId == null) {
getNextTrendingVideoId(
currentVideoId,
relatedStreams
)
} else {
getNextPlaylistVideoId(
currentVideoId
)
}
2022-08-10 19:19:31 +05:30
}
2022-08-21 13:45:37 +05:30
/**
* get the id of the next related video
*/
private fun getNextTrendingVideoId(
videoId: String,
relatedStreams: List<StreamItem>?
): String? {
2022-08-10 19:19:31 +05:30
// don't play a video if it got played before already
2022-08-21 13:45:37 +05:30
if (relatedStreams == null || relatedStreams.isEmpty()) return null
2022-08-10 19:19:31 +05:30
var index = 0
var nextStreamId: String? = null
while (nextStreamId == null ||
(
2022-08-10 19:23:34 +05:30
Globals.playingQueue.contains(nextStreamId) &&
Globals.playingQueue.indexOf(videoId) > Globals.playingQueue.indexOf(
2022-08-10 19:19:31 +05:30
nextStreamId
)
)
) {
nextStreamId = relatedStreams[index].url.toID()
2022-08-24 21:26:57 +05:30
if (index + 1 < relatedStreams.size) {
index += 1
} else {
break
}
2022-08-10 19:19:31 +05:30
}
return nextStreamId
}
2022-08-21 13:45:37 +05:30
/**
* get the videoId of the next video in a playlist
*/
2022-08-10 19:19:31 +05:30
private suspend fun getNextPlaylistVideoId(currentVideoId: String): String? {
2022-08-07 23:31:22 +05:30
// if the playlists contain the video, then save the next video as next stream
if (playlistStreamIds.contains(currentVideoId)) {
val index = playlistStreamIds.indexOf(currentVideoId)
// check whether there's a next video
2022-08-24 21:26:57 +05:30
return if (index + 1 < playlistStreamIds.size) {
playlistStreamIds[index + 1]
} else if (playlistNextPage == null) {
null
} else {
getNextPlaylistVideoId(currentVideoId)
}
2022-08-07 23:31:22 +05:30
} else if (playlistStreamIds.isEmpty() || playlistNextPage != null) {
// fetch the next page of the playlist
return withContext(Dispatchers.IO) {
2022-08-08 14:13:46 +05:30
// fetch the playlists or its nextPage's videos
val playlist =
2022-08-24 21:26:57 +05:30
if (playlistNextPage == null) {
RetrofitInstance.authApi.getPlaylist(playlistId!!)
} else {
RetrofitInstance.authApi.getPlaylistNextPage(
playlistId!!,
playlistNextPage!!
)
}
2022-08-08 14:13:46 +05:30
// save the playlist urls to the list
2022-08-07 23:31:22 +05:30
playlistStreamIds += playlist.relatedStreams!!.map { it.url.toID() }
// save playlistNextPage for usage if video is not contained
playlistNextPage = playlist.nextpage
return@withContext getNextPlaylistVideoId(currentVideoId)
}
}
2022-08-08 14:13:46 +05:30
// return null when no nextPage is found
2022-08-07 23:31:22 +05:30
return null
}
2022-08-10 19:53:57 +05:30
2022-08-21 13:45:37 +05:30
/**
* get the videoId of the next video in the playing queue
*/
2022-08-10 19:53:57 +05:30
fun getNextPlayingQueueVideoId(
currentVideoId: String
): String? {
return if (Globals.playingQueue.last() != currentVideoId) {
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
Globals.playingQueue[currentVideoIndex + 1]
2022-08-24 21:26:57 +05:30
} else {
null
}
2022-08-10 19:53:57 +05:30
}
2022-08-07 23:31:22 +05:30
}