From b6056b8610f08683370b074f285bf9c1e135d4f9 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 28 Dec 2022 17:43:27 +0100 Subject: [PATCH] Add shuffle button to owned playlists --- .../github/libretube/api/PlaylistsHelper.kt | 1 + .../ui/dialogs/CreatePlaylistDialog.kt | 19 +++++-- .../ui/fragments/PlaylistFragment.kt | 53 ++++++++++++------- .../ui/fragments/WatchHistoryFragment.kt | 20 ++++--- app/src/main/res/values/strings.xml | 1 + 5 files changed, 63 insertions(+), 31 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 add8856f9..ea6b314ac 100644 --- a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt +++ b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt @@ -105,6 +105,7 @@ object PlaylistsHelper { appContext.toastFromMainThread(R.string.server_error) return null } + Log.e("created pl", response.playlistId.toString()) if (response.playlistId != null) { appContext.toastFromMainThread(R.string.playlistCreated) return response.playlistId!! diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt index ac1ec1a6a..9b8c98027 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt @@ -4,12 +4,15 @@ import android.app.Dialog import android.os.Bundle import android.widget.Toast import androidx.fragment.app.DialogFragment -import androidx.lifecycle.lifecycleScope import com.github.libretube.R import com.github.libretube.api.PlaylistsHelper import com.github.libretube.databinding.DialogCreatePlaylistBinding import com.github.libretube.util.TextUtils import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class CreatePlaylistDialog( private val onSuccess: () -> Unit = {} @@ -26,6 +29,7 @@ class CreatePlaylistDialog( return@setOnClickListener } PlaylistsHelper.clonePlaylist(requireContext().applicationContext, playlistUrl) + dismiss() } binding.cancelButton.setOnClickListener { @@ -37,11 +41,16 @@ class CreatePlaylistDialog( binding.createNewPlaylist.setOnClickListener(null) val listName = binding.playlistName.text.toString() if (listName != "") { - lifecycleScope.launchWhenCreated { - PlaylistsHelper.createPlaylist(listName, requireContext().applicationContext) - onSuccess.invoke() - dismiss() + CoroutineScope(Dispatchers.IO).launch { + val playlistId = PlaylistsHelper.createPlaylist( + listName, + requireContext().applicationContext + ) + withContext(Dispatchers.Main) { + if (playlistId != null) onSuccess.invoke() + } } + dismiss() } else { Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show() } 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 0bdf9194b..0f98dab94 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 @@ -33,6 +33,7 @@ import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet import com.github.libretube.util.ImageHelper import com.github.libretube.util.NavigationHelper +import com.github.libretube.util.PlayingQueue import com.github.libretube.util.TextUtils import java.io.IOException import retrofit2.HttpException @@ -146,27 +147,43 @@ class PlaylistFragment : BaseFragment() { ) } - if (playlistType != PlaylistType.PUBLIC) binding.bookmark.visibility = View.GONE - - binding.bookmark.setOnClickListener { - isBookmarked = !isBookmarked - updateBookmarkRes() - query { - if (!isBookmarked) { - DatabaseHolder.Database.playlistBookmarkDao().deleteById(playlistId!!) - } else { - DatabaseHolder.Database.playlistBookmarkDao().insertAll( - PlaylistBookmark( - playlistId = playlistId!!, - playlistName = response.name, - thumbnailUrl = response.thumbnailUrl, - uploader = response.uploader, - uploaderAvatar = response.uploaderAvatar, - uploaderUrl = response.uploaderUrl + if (playlistType == PlaylistType.PUBLIC) { + binding.bookmark.setOnClickListener { + isBookmarked = !isBookmarked + updateBookmarkRes() + query { + if (!isBookmarked) { + DatabaseHolder.Database.playlistBookmarkDao() + .deleteById(playlistId!!) + } else { + DatabaseHolder.Database.playlistBookmarkDao().insertAll( + PlaylistBookmark( + playlistId = playlistId!!, + playlistName = response.name, + thumbnailUrl = response.thumbnailUrl, + uploader = response.uploader, + uploaderAvatar = response.uploaderAvatar, + uploaderUrl = response.uploaderUrl + ) ) - ) + } } } + } else { + // private playlist, means shuffle is possible because all videos are received at once + binding.bookmark.setIconResource(R.drawable.ic_shuffle) + binding.bookmark.text = getString(R.string.shuffle) + binding.bookmark.setOnClickListener { + val queue = playlistFeed.shuffled() + PlayingQueue.resetToDefaults() + PlayingQueue.add(*queue.toTypedArray()) + NavigationHelper.navigateVideo( + requireContext(), + queue.first().url?.toID(), + playlistId = playlistId, + keepQueue = true + ) + } } playlistAdapter = PlaylistAdapter( diff --git a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt index 85107ec39..8a3976bb3 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt @@ -89,7 +89,11 @@ class WatchHistoryFragment : BaseFragment() { ) }.toTypedArray() ) - NavigationHelper.navigateVideo(requireContext(), watchHistory.last().videoId, keepQueue = true) + NavigationHelper.navigateVideo( + requireContext(), + watchHistory.last().videoId, + keepQueue = true + ) } // reversed order @@ -128,14 +132,14 @@ class WatchHistoryFragment : BaseFragment() { // observe changes watchHistoryAdapter.registerAdapterDataObserver(object : - RecyclerView.AdapterDataObserver() { - override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { - if (watchHistoryAdapter.itemCount == 0) { - binding.historyScrollView.visibility = View.GONE - binding.historyEmpty.visibility = View.VISIBLE - } + RecyclerView.AdapterDataObserver() { + override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { + if (watchHistoryAdapter.itemCount == 0) { + binding.historyScrollView.visibility = View.GONE + binding.historyEmpty.visibility = View.VISIBLE } - }) + } + }) binding.watchHistoryRecView.adapter = watchHistoryAdapter binding.historyEmpty.visibility = View.GONE diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3711b9453..5d58c7255 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -422,6 +422,7 @@ Don\'t show videos being watched more than 90% in the subscriptions tab. Playlist URL Pause on quit + Shuffle Download Service