add support for the queue

This commit is contained in:
Bnyro 2022-11-20 17:09:07 +01:00
parent 826e054790
commit 11e7b3ac01
8 changed files with 39 additions and 16 deletions

View File

@ -19,6 +19,8 @@ import retrofit2.HttpException
import java.io.IOException import java.io.IOException
object PlaylistsHelper { 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() val token get() = PreferenceHelper.getToken()
private fun loggedIn() = token != "" private fun loggedIn() = token != ""
@ -46,7 +48,7 @@ object PlaylistsHelper {
suspend fun getPlaylist(playlistType: PlaylistType, playlistId: String): Playlist { suspend fun getPlaylist(playlistType: PlaylistType, playlistId: String): Playlist {
// load locally stored playlists with the auth api // load locally stored playlists with the auth api
return when (playlistType) { return when (playlistType) {
PlaylistType.OWNED -> RetrofitInstance.authApi.getPlaylist(playlistId) PlaylistType.PRIVATE -> RetrofitInstance.authApi.getPlaylist(playlistId)
PlaylistType.PUBLIC -> RetrofitInstance.api.getPlaylist(playlistId) PlaylistType.PUBLIC -> RetrofitInstance.api.getPlaylist(playlistId)
PlaylistType.LOCAL -> { PlaylistType.LOCAL -> {
val relation = awaitQuery { val relation = awaitQuery {
@ -172,11 +174,13 @@ object PlaylistsHelper {
) )
} }
fun getType(): PlaylistType { fun getPrivateType(): PlaylistType {
return if (PreferenceHelper.getToken() != "") { return if (loggedIn()) PlaylistType.PRIVATE else PlaylistType.LOCAL
PlaylistType.PUBLIC
} 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
} }
} }

View File

@ -1,7 +1,18 @@
package com.github.libretube.enums package com.github.libretube.enums
enum class PlaylistType { enum class PlaylistType {
/**
* Local playlist
*/
LOCAL, LOCAL,
OWNED,
/**
* Piped playlist
*/
PRIVATE,
/**
* YouTube playlist
*/
PUBLIC PUBLIC
} }

View File

@ -22,10 +22,12 @@ import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.WatchPosition import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.awaitQuery import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem import com.github.libretube.extensions.toStreamItem
import com.github.libretube.ui.extensions.serializable
import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper import com.github.libretube.util.PlayerHelper
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
@ -53,6 +55,7 @@ class BackgroundMode : Service() {
*PlaylistId for autoplay *PlaylistId for autoplay
*/ */
private var playlistId: String? = null private var playlistId: String? = null
private var playlistType: PlaylistType? = null
/** /**
* The response that gets when called the Api. * The response that gets when called the Api.
@ -162,7 +165,9 @@ class BackgroundMode : Service() {
// add the playlist video to the queue // add the playlist video to the queue
if (playlistId != null && PlayingQueue.isEmpty()) { if (playlistId != null && PlayingQueue.isEmpty()) {
streams?.toStreamItem(videoId) streams?.toStreamItem(videoId)
?.let { PlayingQueue.insertPlaylist(playlistId!!, it) } ?.let {
PlayingQueue.insertPlaylist(playlistId!!, it)
}
} else { } else {
streams?.toStreamItem(videoId)?.let { PlayingQueue.updateCurrent(it) } streams?.toStreamItem(videoId)?.let { PlayingQueue.updateCurrent(it) }
streams?.relatedStreams?.toTypedArray()?.let { streams?.relatedStreams?.toTypedArray()?.let {

View File

@ -107,7 +107,7 @@ class HomeFragment : BaseFragment() {
runOnUiThread { runOnUiThread {
makeVisible(binding.playlistsRV, binding.playlistsTV) makeVisible(binding.playlistsRV, binding.playlistsTV)
binding.playlistsRV.layoutManager = LinearLayoutManager(context) 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 : binding.playlistsRV.adapter?.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() { RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {

View File

@ -117,7 +117,7 @@ class LibraryFragment : BaseFragment() {
val playlistsAdapter = PlaylistsAdapter( val playlistsAdapter = PlaylistsAdapter(
playlists.toMutableList(), playlists.toMutableList(),
PlaylistsHelper.getType() PlaylistsHelper.getPrivateType()
) )
// listen for playlists to become deleted // listen for playlists to become deleted

View File

@ -220,7 +220,7 @@ class PlaylistFragment : BaseFragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
// load locally stored playlists with the auth api // load locally stored playlists with the auth api
if (playlistType == PlaylistType.OWNED) { if (playlistType == PlaylistType.PRIVATE) {
RetrofitInstance.authApi.getPlaylistNextPage( RetrofitInstance.authApi.getPlaylistNextPage(
playlistId!!, playlistId!!,
nextPage!! nextPage!!

View File

@ -51,7 +51,7 @@ class PlaylistOptionsBottomSheet(
context?.getString(R.string.playOnBackground) -> { context?.getString(R.string.playOnBackground) -> {
runBlocking { runBlocking {
val playlist = val playlist =
if (playlistType == PlaylistType.OWNED) { if (playlistType == PlaylistType.PRIVATE) {
RetrofitInstance.authApi.getPlaylist(playlistId) RetrofitInstance.authApi.getPlaylist(playlistId)
} else { } else {
RetrofitInstance.api.getPlaylist(playlistId) RetrofitInstance.api.getPlaylist(playlistId)

View File

@ -1,6 +1,7 @@
package com.github.libretube.util package com.github.libretube.util
import android.util.Log import android.util.Log
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.move import com.github.libretube.extensions.move
@ -106,14 +107,16 @@ object PlayingQueue {
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) { fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
try { try {
val response = RetrofitInstance.authApi.getPlaylist(playlistId) val playlistType = PlaylistsHelper.getPrivateType(playlistId)
val playlist = PlaylistsHelper.getPlaylist(playlistType, playlistId)
add( add(
*response.relatedStreams *playlist.relatedStreams
.orEmpty() .orEmpty()
.toTypedArray() .toTypedArray()
) )
updateCurrent(newCurrentStream) updateCurrent(newCurrentStream)
fetchMoreFromPlaylist(playlistId, response.nextpage) if (playlist.nextpage == null) return@launch
fetchMoreFromPlaylist(playlistId, playlist.nextpage)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }