From 11e7b3ac01c85a5cba1980f52c9b9a7f53ec4859 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 20 Nov 2022 17:09:07 +0100 Subject: [PATCH] add support for the queue --- .../github/libretube/api/PlaylistsHelper.kt | 18 +++++++++++------- .../com/github/libretube/enums/PlaylistType.kt | 13 ++++++++++++- .../libretube/services/BackgroundMode.kt | 7 ++++++- .../libretube/ui/fragments/HomeFragment.kt | 2 +- .../libretube/ui/fragments/LibraryFragment.kt | 2 +- .../libretube/ui/fragments/PlaylistFragment.kt | 2 +- .../ui/sheets/PlaylistOptionsBottomSheet.kt | 2 +- .../com/github/libretube/util/PlayingQueue.kt | 9 ++++++--- 8 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt index 7c876a4ff..9dfd2560b 100644 --- a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt +++ b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt @@ -19,6 +19,8 @@ import retrofit2.HttpException import java.io.IOException object PlaylistsHelper { + private val pipedPlaylistRegex = "[\\da-fA-F]{8}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{12}".toRegex() + val token get() = PreferenceHelper.getToken() private fun loggedIn() = token != "" @@ -46,7 +48,7 @@ object PlaylistsHelper { suspend fun getPlaylist(playlistType: PlaylistType, playlistId: String): Playlist { // load locally stored playlists with the auth api return when (playlistType) { - PlaylistType.OWNED -> RetrofitInstance.authApi.getPlaylist(playlistId) + PlaylistType.PRIVATE -> RetrofitInstance.authApi.getPlaylist(playlistId) PlaylistType.PUBLIC -> RetrofitInstance.api.getPlaylist(playlistId) PlaylistType.LOCAL -> { val relation = awaitQuery { @@ -172,11 +174,13 @@ object PlaylistsHelper { ) } - fun getType(): PlaylistType { - return if (PreferenceHelper.getToken() != "") { - PlaylistType.PUBLIC - } else { - PlaylistType.LOCAL - } + fun getPrivateType(): PlaylistType { + return if (loggedIn()) PlaylistType.PRIVATE else PlaylistType.LOCAL + } + + fun getPrivateType(playlistId: String): PlaylistType { + if (playlistId.all { it.isDigit() }) return PlaylistType.LOCAL + if (playlistId.matches(pipedPlaylistRegex)) return PlaylistType.PRIVATE + return PlaylistType.PUBLIC } } diff --git a/app/src/main/java/com/github/libretube/enums/PlaylistType.kt b/app/src/main/java/com/github/libretube/enums/PlaylistType.kt index f42672538..852a1cda9 100644 --- a/app/src/main/java/com/github/libretube/enums/PlaylistType.kt +++ b/app/src/main/java/com/github/libretube/enums/PlaylistType.kt @@ -1,7 +1,18 @@ package com.github.libretube.enums enum class PlaylistType { + /** + * Local playlist + */ LOCAL, - OWNED, + + /** + * Piped playlist + */ + PRIVATE, + + /** + * YouTube playlist + */ PUBLIC } diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index 2030401cc..96a9f2fbc 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -22,10 +22,12 @@ import com.github.libretube.constants.PLAYER_NOTIFICATION_ID import com.github.libretube.constants.PreferenceKeys import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.obj.WatchPosition +import com.github.libretube.enums.PlaylistType import com.github.libretube.extensions.awaitQuery import com.github.libretube.extensions.query import com.github.libretube.extensions.toID import com.github.libretube.extensions.toStreamItem +import com.github.libretube.ui.extensions.serializable import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.PlayerHelper import com.github.libretube.util.PlayingQueue @@ -53,6 +55,7 @@ class BackgroundMode : Service() { *PlaylistId for autoplay */ private var playlistId: String? = null + private var playlistType: PlaylistType? = null /** * The response that gets when called the Api. @@ -162,7 +165,9 @@ class BackgroundMode : Service() { // add the playlist video to the queue if (playlistId != null && PlayingQueue.isEmpty()) { streams?.toStreamItem(videoId) - ?.let { PlayingQueue.insertPlaylist(playlistId!!, it) } + ?.let { + PlayingQueue.insertPlaylist(playlistId!!, it) + } } else { streams?.toStreamItem(videoId)?.let { PlayingQueue.updateCurrent(it) } streams?.relatedStreams?.toTypedArray()?.let { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt index ebe5afde1..a6d97fec4 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt @@ -107,7 +107,7 @@ class HomeFragment : BaseFragment() { runOnUiThread { makeVisible(binding.playlistsRV, binding.playlistsTV) binding.playlistsRV.layoutManager = LinearLayoutManager(context) - binding.playlistsRV.adapter = PlaylistsAdapter(playlists.toMutableList(), PlaylistsHelper.getType()) + binding.playlistsRV.adapter = PlaylistsAdapter(playlists.toMutableList(), PlaylistsHelper.getPrivateType()) binding.playlistsRV.adapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt index d34f2c6b9..99051b126 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt @@ -117,7 +117,7 @@ class LibraryFragment : BaseFragment() { val playlistsAdapter = PlaylistsAdapter( playlists.toMutableList(), - PlaylistsHelper.getType() + PlaylistsHelper.getPrivateType() ) // listen for playlists to become deleted diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt index 46b0dbbc0..168926a41 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt @@ -220,7 +220,7 @@ class PlaylistFragment : BaseFragment() { lifecycleScope.launchWhenCreated { val response = try { // load locally stored playlists with the auth api - if (playlistType == PlaylistType.OWNED) { + if (playlistType == PlaylistType.PRIVATE) { RetrofitInstance.authApi.getPlaylistNextPage( playlistId!!, nextPage!! 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 80e6d01d9..3568eafbc 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 @@ -51,7 +51,7 @@ class PlaylistOptionsBottomSheet( context?.getString(R.string.playOnBackground) -> { runBlocking { val playlist = - if (playlistType == PlaylistType.OWNED) { + if (playlistType == PlaylistType.PRIVATE) { RetrofitInstance.authApi.getPlaylist(playlistId) } else { RetrofitInstance.api.getPlaylist(playlistId) 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 f8e62cf4c..7f2cfdaf1 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -1,6 +1,7 @@ package com.github.libretube.util import android.util.Log +import com.github.libretube.api.PlaylistsHelper import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.obj.StreamItem import com.github.libretube.extensions.move @@ -106,14 +107,16 @@ object PlayingQueue { fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) { CoroutineScope(Dispatchers.IO).launch { try { - val response = RetrofitInstance.authApi.getPlaylist(playlistId) + val playlistType = PlaylistsHelper.getPrivateType(playlistId) + val playlist = PlaylistsHelper.getPlaylist(playlistType, playlistId) add( - *response.relatedStreams + *playlist.relatedStreams .orEmpty() .toTypedArray() ) updateCurrent(newCurrentStream) - fetchMoreFromPlaylist(playlistId, response.nextpage) + if (playlist.nextpage == null) return@launch + fetchMoreFromPlaylist(playlistId, playlist.nextpage) } catch (e: Exception) { e.printStackTrace() }