From 2945f3c27a891799c11135e8d366af5c84566848 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 28 Dec 2022 17:07:35 +0100 Subject: [PATCH 1/2] Add a clear button to the watch history --- .../github/libretube/constants/IntentData.kt | 1 + .../libretube/ui/fragments/PlayerFragment.kt | 10 +++- .../ui/fragments/WatchHistoryFragment.kt | 34 +++++++++--- .../github/libretube/util/NavigationHelper.kt | 22 +++++--- .../res/layout/fragment_watch_history.xml | 55 +++++++++++++++++-- 5 files changed, 99 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/github/libretube/constants/IntentData.kt b/app/src/main/java/com/github/libretube/constants/IntentData.kt index b1f1d6a26..2521915d6 100644 --- a/app/src/main/java/com/github/libretube/constants/IntentData.kt +++ b/app/src/main/java/com/github/libretube/constants/IntentData.kt @@ -9,5 +9,6 @@ object IntentData { const val position = "position" const val fileName = "fileName" const val openQueueOnce = "openQueue" + const val keepQueue = "keepQueue" const val playlistType = "playlistType" } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index e206fa963..6a15b6e89 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -127,11 +127,16 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { private val commentsViewModel: CommentsViewModel by activityViewModels() /** - * video information + * Video information passed by the intent */ private var videoId: String? = null private var playlistId: String? = null private var channelId: String? = null + private var keepQueue: Boolean = false + + /** + * Video information fetched at runtime + */ private var isLive = false private lateinit var streams: Streams @@ -179,6 +184,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { videoId = it.getString(IntentData.videoId)!!.toID() playlistId = it.getString(IntentData.playlistId) channelId = it.getString(IntentData.channelId) + keepQueue = it.getBoolean(IntentData.keepQueue, false) } } @@ -202,7 +208,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { context?.hideKeyboard(view) // clear the playing queue - PlayingQueue.resetToDefaults() + if (!keepQueue) PlayingQueue.resetToDefaults() changeOrientationMode() 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 e30527aff..d0a6e1610 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 @@ -9,14 +9,17 @@ import androidx.fragment.app.activityViewModels import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.github.libretube.R import com.github.libretube.databinding.FragmentWatchHistoryBinding import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.extensions.awaitQuery +import com.github.libretube.extensions.query import com.github.libretube.extensions.toPixel import com.github.libretube.ui.adapters.WatchHistoryAdapter import com.github.libretube.ui.base.BaseFragment import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.util.ProxyHelper +import com.google.android.material.dialog.MaterialAlertDialogBuilder class WatchHistoryFragment : BaseFragment() { private lateinit var binding: FragmentWatchHistoryBinding @@ -41,6 +44,21 @@ class WatchHistoryFragment : BaseFragment() { ) } + binding.clear.setOnClickListener { + MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.clear_history) + .setMessage(R.string.irreversible) + .setPositiveButton(R.string.okay) { _, _ -> + binding.historyScrollView.visibility = View.GONE + binding.historyEmpty.visibility = View.VISIBLE + query { + Database.watchHistoryDao().deleteAll() + } + } + .setNegativeButton(R.string.cancel, null) + .show() + } + val watchHistory = awaitQuery { Database.watchHistoryDao().getAll() } @@ -88,17 +106,17 @@ class WatchHistoryFragment : BaseFragment() { // observe changes watchHistoryAdapter.registerAdapterDataObserver(object : - RecyclerView.AdapterDataObserver() { - override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { - if (watchHistoryAdapter.itemCount == 0) { - binding.watchHistoryRecView.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 - binding.watchHistoryRecView.visibility = View.VISIBLE + binding.historyScrollView.visibility = View.VISIBLE } } diff --git a/app/src/main/java/com/github/libretube/util/NavigationHelper.kt b/app/src/main/java/com/github/libretube/util/NavigationHelper.kt index 313178668..6d55680ea 100644 --- a/app/src/main/java/com/github/libretube/util/NavigationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NavigationHelper.kt @@ -49,23 +49,29 @@ object NavigationHelper { context: Context, videoId: String?, playlistId: String? = null, - channelId: String? = null + channelId: String? = null, + keepQueue: Boolean = false ) { if (videoId == null) return - val bundle = Bundle() - bundle.putString(IntentData.videoId, videoId.toID()) - bundle.putString(IntentData.playlistId, playlistId) - bundle.putString(IntentData.channelId, channelId) + val bundle = Bundle().apply { + putString(IntentData.videoId, videoId.toID()) + putString(IntentData.playlistId, playlistId) + putString(IntentData.channelId, channelId) + putBoolean(IntentData.keepQueue, keepQueue) + } - val frag = PlayerFragment() - frag.arguments = bundle val activity = context as AppCompatActivity activity.supportFragmentManager.beginTransaction() .remove(PlayerFragment()) .commit() activity.supportFragmentManager.beginTransaction() - .replace(R.id.container, frag) + .replace( + R.id.container, + PlayerFragment().apply { + arguments = bundle + } + ) .commitNow() } diff --git a/app/src/main/res/layout/fragment_watch_history.xml b/app/src/main/res/layout/fragment_watch_history.xml index 2cb061f89..e527bb8aa 100644 --- a/app/src/main/res/layout/fragment_watch_history.xml +++ b/app/src/main/res/layout/fragment_watch_history.xml @@ -1,5 +1,6 @@ @@ -26,12 +27,56 @@ android:textStyle="bold" /> - + android:visibility="gone"> + + + + + + + + + + + + + + + + \ No newline at end of file From 1e7b38e209c957827ca29d4382802317a7239d57 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 28 Dec 2022 17:16:00 +0100 Subject: [PATCH 2/2] Add play all from watch history button --- .../ui/fragments/WatchHistoryFragment.kt | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) 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 d0a6e1610..85107ec39 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 @@ -10,6 +10,7 @@ import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R +import com.github.libretube.api.obj.StreamItem import com.github.libretube.databinding.FragmentWatchHistoryBinding import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.extensions.awaitQuery @@ -18,6 +19,8 @@ import com.github.libretube.extensions.toPixel import com.github.libretube.ui.adapters.WatchHistoryAdapter import com.github.libretube.ui.base.BaseFragment import com.github.libretube.ui.models.PlayerViewModel +import com.github.libretube.util.NavigationHelper +import com.github.libretube.util.PlayingQueue import com.github.libretube.util.ProxyHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -44,6 +47,17 @@ class WatchHistoryFragment : BaseFragment() { ) } + val watchHistory = awaitQuery { + Database.watchHistoryDao().getAll() + } + + if (watchHistory.isEmpty()) return + + watchHistory.forEach { + it.thumbnailUrl = ProxyHelper.rewriteUrl(it.thumbnailUrl) + it.uploaderAvatar = ProxyHelper.rewriteUrl(it.uploaderAvatar) + } + binding.clear.setOnClickListener { MaterialAlertDialogBuilder(requireContext()) .setTitle(R.string.clear_history) @@ -59,15 +73,23 @@ class WatchHistoryFragment : BaseFragment() { .show() } - val watchHistory = awaitQuery { - Database.watchHistoryDao().getAll() - } - - if (watchHistory.isEmpty()) return - - watchHistory.forEach { - it.thumbnailUrl = ProxyHelper.rewriteUrl(it.thumbnailUrl) - it.uploaderAvatar = ProxyHelper.rewriteUrl(it.uploaderAvatar) + binding.playAll.setOnClickListener { + PlayingQueue.resetToDefaults() + PlayingQueue.add( + *watchHistory.reversed().map { + StreamItem( + url = "/watch?v=${it.videoId}", + title = it.title, + thumbnail = it.thumbnailUrl, + uploaderName = it.uploader, + uploaderUrl = it.uploaderUrl, + uploaderAvatar = it.uploaderAvatar, + uploadedDate = it.uploadDate, + duration = it.duration + ) + }.toTypedArray() + ) + NavigationHelper.navigateVideo(requireContext(), watchHistory.last().videoId, keepQueue = true) } // reversed order