allow clicking queue items

This commit is contained in:
Bnyro 2022-11-06 11:50:04 +01:00
parent d18e0dfc1f
commit 632f4d6df4
5 changed files with 45 additions and 7 deletions

View File

@ -24,6 +24,7 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHelper import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.extensions.awaitQuery import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem import com.github.libretube.extensions.toStreamItem
import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper import com.github.libretube.util.PlayerHelper
@ -124,6 +125,10 @@ class BackgroundMode : Service() {
// play the audio in the background // play the audio in the background
loadAudio(videoId, position) loadAudio(videoId, position)
PlayingQueue.setOnQueueTapListener { streamItem ->
streamItem.url?.toID()?.let { playNextVideo(it) }
}
updateWatchPosition() updateWatchPosition()
} catch (e: Exception) { } catch (e: Exception) {
onDestroy() onDestroy()
@ -264,8 +269,8 @@ class BackgroundMode : Service() {
/** /**
* Plays the first related video to the current (used when the playback of the current video ended) * Plays the first related video to the current (used when the playback of the current video ended)
*/ */
private fun playNextVideo() { private fun playNextVideo(nextId: String? = null) {
val nextVideo = PlayingQueue.getNext() val nextVideo = nextId ?: PlayingQueue.getNext()
// play new video on background // play new video on background
if (nextVideo != null) { if (nextVideo != null) {

View File

@ -13,7 +13,6 @@ import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.ThemeHelper import com.github.libretube.util.ThemeHelper
class PlayingQueueAdapter : RecyclerView.Adapter<PlayingQueueViewHolder>() { class PlayingQueueAdapter : RecyclerView.Adapter<PlayingQueueViewHolder>() {
private val currentIndex = PlayingQueue.currentIndex()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayingQueueViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayingQueueViewHolder {
val binding = QueueRowBinding.inflate( val binding = QueueRowBinding.inflate(
@ -37,6 +36,7 @@ class PlayingQueueAdapter : RecyclerView.Adapter<PlayingQueueViewHolder>() {
videoInfo.text = streamItem.uploaderName + "" + videoInfo.text = streamItem.uploaderName + "" +
DateUtils.formatElapsedTime(streamItem.duration ?: 0) DateUtils.formatElapsedTime(streamItem.duration ?: 0)
val currentIndex = PlayingQueue.currentIndex()
root.setBackgroundColor( root.setBackgroundColor(
if (currentIndex == position) { if (currentIndex == position) {
ThemeHelper.getThemeColor(root.context, android.R.attr.colorControlHighlight) ThemeHelper.getThemeColor(root.context, android.R.attr.colorControlHighlight)
@ -44,6 +44,13 @@ class PlayingQueueAdapter : RecyclerView.Adapter<PlayingQueueViewHolder>() {
Color.TRANSPARENT Color.TRANSPARENT
} }
) )
root.setOnClickListener {
val oldIndex = PlayingQueue.currentIndex()
PlayingQueue.onQueueItemSelected(position)
notifyItemChanged(oldIndex)
notifyItemChanged(position)
}
} }
} }
} }

View File

@ -503,6 +503,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
try { try {
// clear the playing queue // clear the playing queue
PlayingQueue.clear() PlayingQueue.clear()
PlayingQueue.removeOnQueueTapListener()
saveWatchPosition() saveWatchPosition()
nowPlayingNotification.destroySelfAndPlayer() nowPlayingNotification.destroySelfAndPlayer()
@ -602,6 +603,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
PlayingQueue.updateCurrent(streams.toStreamItem(videoId!!)) PlayingQueue.updateCurrent(streams.toStreamItem(videoId!!))
} }
PlayingQueue.setOnQueueTapListener { streamItem ->
streamItem.url?.toID()?.let { playNextVideo(it) }
}
runOnUiThread { runOnUiThread {
// hide the button to skip SponsorBlock segments manually // hide the button to skip SponsorBlock segments manually
binding.sbSkipBtn.visibility = View.GONE binding.sbSkipBtn.visibility = View.GONE
@ -702,8 +707,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
// used for autoplay and skipping to next video // used for autoplay and skipping to next video
private fun playNextVideo() { private fun playNextVideo(nextId: String? = null) {
val nextVideoId = PlayingQueue.getNext() val nextVideoId = nextId ?: PlayingQueue.getNext()
// by making sure that the next and the current video aren't the same // by making sure that the next and the current video aren't the same
saveWatchPosition() saveWatchPosition()
@ -987,7 +992,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
binding.chaptersRecView.adapter = ChaptersAdapter(chapters, exoPlayer) binding.chaptersRecView.adapter = ChaptersAdapter(chapters, exoPlayer)
// enable the chapters dialog in the player // enable the chapters dialog in the player
val titles = chapters.map { "${it.title} (${it.start?.let { DateUtils.formatElapsedTime(it) }})" } val titles = chapters.map { chapter ->
"${chapter.title} (${chapter.start?.let { DateUtils.formatElapsedTime(it) }})"
}
playerBinding.chapterLL.setOnClickListener { playerBinding.chapterLL.setOnClickListener {
if (viewModel.isFullscreen.value!!) { if (viewModel.isFullscreen.value!!) {
MaterialAlertDialogBuilder(requireContext()) MaterialAlertDialogBuilder(requireContext())

View File

@ -1,5 +1,6 @@
package com.github.libretube.util package com.github.libretube.util
import android.util.Log
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
@ -11,6 +12,7 @@ import kotlinx.coroutines.launch
object PlayingQueue { object PlayingQueue {
private val queue = mutableListOf<StreamItem>() private val queue = mutableListOf<StreamItem>()
private var currentStream: StreamItem? = null private var currentStream: StreamItem? = null
private var onQueueTapListener: (StreamItem) -> Unit = {}
fun add(vararg streamItem: StreamItem) { fun add(vararg streamItem: StreamItem) {
streamItem.forEach { streamItem.forEach {
@ -116,4 +118,22 @@ object PlayingQueue {
} }
} }
} }
fun onQueueItemSelected(index: Int) {
try {
val streamItem = queue[index]
updateCurrent(streamItem)
onQueueTapListener.invoke(streamItem)
} catch (e: Exception) {
Log.e("Queue on tap", "lifecycle already ended")
}
}
fun setOnQueueTapListener(listener: (StreamItem) -> Unit) {
onQueueTapListener = listener
}
fun removeOnQueueTapListener() {
onQueueTapListener = {}
}
} }

View File

@ -51,7 +51,6 @@
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginHorizontal="10dp" android:layout_marginHorizontal="10dp"
android:layout_weight="1" android:layout_weight="1"
android:rotation="180"
android:src="@drawable/ic_arrow_down" /> android:src="@drawable/ic_arrow_down" />
<ImageView <ImageView