diff --git a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt index 9ddae1a58..0733e50f8 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt @@ -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() diff --git a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt index 6a89f83c9..5088a4841 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -88,7 +88,21 @@ object PlayingQueue { fun move(from: Int, to: Int) = queue.move(from, to) - private fun addToQueueAsync(streams: List, 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, + 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) } } }