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
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
}
}

View File

@ -1,7 +1,18 @@
package com.github.libretube.enums
enum class PlaylistType {
/**
* Local playlist
*/
LOCAL,
OWNED,
/**
* Piped playlist
*/
PRIVATE,
/**
* YouTube playlist
*/
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.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 {

View File

@ -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) {

View File

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

View File

@ -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!!

View File

@ -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)

View File

@ -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()
}