feat: option to add whole playlist to current queue

This commit is contained in:
Bnyro 2023-08-09 12:34:24 +02:00
parent b42bcf66c7
commit e037da2e0c
2 changed files with 27 additions and 7 deletions

View File

@ -15,6 +15,7 @@ import com.github.libretube.ui.dialogs.DeletePlaylistDialog
import com.github.libretube.ui.dialogs.PlaylistDescriptionDialog
import com.github.libretube.ui.dialogs.RenamePlaylistDialog
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.util.PlayingQueue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
@ -31,7 +32,8 @@ class PlaylistOptionsBottomSheet(
override fun onCreate(savedInstanceState: Bundle?) {
// options for the dialog
val optionsList = mutableListOf(
getString(R.string.playOnBackground)
getString(R.string.playOnBackground),
getString(R.string.add_to_queue)
)
val isBookmarked = runBlocking(Dispatchers.IO) {
@ -67,6 +69,9 @@ class PlaylistOptionsBottomSheet(
)
}
}
getString(R.string.add_to_queue) -> {
PlayingQueue.insertPlaylist(playlistId, null)
}
// Clone the playlist to the users Piped account
getString(R.string.clonePlaylist) -> {
val context = requireContext()

View File

@ -88,7 +88,21 @@ object PlayingQueue {
fun move(from: Int, to: Int) = queue.move(from, to)
private fun addToQueueAsync(streams: List<StreamItem>, currentStreamItem: StreamItem? = null) {
/**
* Adds a list of videos to the current queue while updating the position of the current stream
* @param isMainList: whether the videos are part of the list, that initially has been used to
* start the queue, either from a channel or playlist. If it's false, the current stream won't
* be touched, since it's an independent list.
*/
private fun addToQueueAsync(
streams: List<StreamItem>,
currentStreamItem: StreamItem? = null,
isMainList: Boolean = true
) {
if (!isMainList) {
add(*streams.toTypedArray())
return
}
val currentStream = currentStreamItem ?: this.currentStream
// if the stream already got added to the queue earlier, although it's not yet
// been found in the playlist, remove it and re-add it later
@ -109,25 +123,26 @@ object PlayingQueue {
}
}
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?) {
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) {
var playlistNextPage = nextPage
scope.launch(Dispatchers.IO) {
while (playlistNextPage != null) {
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage!!).run {
addToQueueAsync(relatedStreams)
addToQueueAsync(relatedStreams, isMainList = isMainList)
playlistNextPage = this.nextpage
}
}
}
}
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) {
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem?) {
scope.launch(Dispatchers.IO) {
runCatching {
val playlist = PlaylistsHelper.getPlaylist(playlistId)
addToQueueAsync(playlist.relatedStreams, newCurrentStream)
val isMainList = newCurrentStream != null
addToQueueAsync(playlist.relatedStreams, newCurrentStream, isMainList)
if (playlist.nextpage == null) return@launch
fetchMoreFromPlaylist(playlistId, playlist.nextpage)
fetchMoreFromPlaylist(playlistId, playlist.nextpage, isMainList)
}
}
}