Supports close video on MiniPlayer swipe down

This commit is contained in:
RafaRamos 2023-12-26 16:31:40 +01:00
parent bd2b0eb163
commit 84dbabe23e
3 changed files with 62 additions and 10 deletions

View File

@ -0,0 +1,23 @@
package com.github.libretube.ui.extensions
import android.view.View
/**
* This function animates a view in a downward direction by a specified amount.
*
* @param duration The duration of the animation in milliseconds.
* @param dy The distance to move the view along the Y-axis.
* @param onEnd An optional lambda function that is invoked when the animation ends.
*/
fun View.animateDown(
duration: Long,
dy: Float,
onEnd: () -> Unit = { }
) {
this
.animate()
.withEndAction { onEnd.invoke() }
.y(dy)
.setDuration(duration)
.start()
}

View File

@ -97,6 +97,7 @@ import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.dialogs.AddToPlaylistDialog
import com.github.libretube.ui.dialogs.DownloadDialog
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.extensions.animateDown
import com.github.libretube.ui.extensions.setupSubscriptionButton
import com.github.libretube.ui.interfaces.OnlinePlayerOptions
import com.github.libretube.ui.listeners.SeekbarPreviewListener
@ -461,12 +462,18 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
})
binding.playerMotionLayout.addSwipeUpListener {
if (this::streams.isInitialized && PlayerHelper.fullscreenGesturesEnabled) {
binding.player.hideController()
setFullscreen()
binding.playerMotionLayout
.addSwipeUpListener {
if (this::streams.isInitialized && PlayerHelper.fullscreenGesturesEnabled) {
binding.player.hideController()
setFullscreen()
}
}
.addSwipeDownListener {
if (viewModel.isMiniPlayerVisible.value == true) {
closeMiniPlayer()
}
}
}
binding.playerMotionLayout.progress = 1F
binding.playerMotionLayout.transitionToStart()
@ -477,6 +484,16 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
}
private fun closeMiniPlayer() {
binding
.playerMotionLayout
.animateDown(
duration = 300L,
dy = 500F,
onEnd = ::killPlayerFragment,
)
}
private fun onManualPlayerClose() {
PlayingQueue.clear()
BackgroundHelper.stopBackgroundPlay(requireContext())

View File

@ -21,6 +21,7 @@ class SingleViewTouchableMotionLayout(context: Context, attributeSet: AttributeS
private var touchStarted = false
private val transitionListenerList = mutableListOf<TransitionListener?>()
private val swipeUpListener = mutableListOf<() -> Unit>()
private val swipeDownListener = mutableListOf<() -> Unit>()
init {
addTransitionListener(object : TransitionAdapter() {
@ -63,18 +64,29 @@ class SingleViewTouchableMotionLayout(context: Context, attributeSet: AttributeS
distanceX: Float,
distanceY: Float
): Boolean {
if (progress != 0F || distanceY < 30F) return false
swipeUpListener.forEach {
it.invoke()
if (distanceY < -15F) {
swipeDownListener.forEach { it.invoke() }
return true
}
return true
if (progress == 0F && distanceY > 30F) {
swipeUpListener.forEach { it.invoke() }
return true
}
return false
}
}
fun addSwipeUpListener(listener: () -> Unit) {
fun addSwipeUpListener(listener: () -> Unit) = apply {
swipeUpListener.add(listener)
}
fun addSwipeDownListener(listener: () -> Unit) = apply {
swipeDownListener.add(listener)
}
private val gestureDetector = GestureDetector(context, Listener())
@SuppressLint("ClickableViewAccessibility")