Merge pull request #5392 from RafaelsRamos/feat/2020

feat: support close video on MiniPlayer swipe down
This commit is contained in:
Bnyro 2023-12-28 18:19:09 +01:00 committed by GitHub
commit c28cbe7f11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.AddToPlaylistDialog
import com.github.libretube.ui.dialogs.DownloadDialog import com.github.libretube.ui.dialogs.DownloadDialog
import com.github.libretube.ui.dialogs.ShareDialog 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.extensions.setupSubscriptionButton
import com.github.libretube.ui.interfaces.OnlinePlayerOptions import com.github.libretube.ui.interfaces.OnlinePlayerOptions
import com.github.libretube.ui.listeners.SeekbarPreviewListener import com.github.libretube.ui.listeners.SeekbarPreviewListener
@ -461,12 +462,18 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
} }
}) })
binding.playerMotionLayout.addSwipeUpListener { binding.playerMotionLayout
.addSwipeUpListener {
if (this::streams.isInitialized && PlayerHelper.fullscreenGesturesEnabled) { if (this::streams.isInitialized && PlayerHelper.fullscreenGesturesEnabled) {
binding.player.hideController() binding.player.hideController()
setFullscreen() setFullscreen()
} }
} }
.addSwipeDownListener {
if (viewModel.isMiniPlayerVisible.value == true) {
closeMiniPlayer()
}
}
binding.playerMotionLayout.progress = 1F binding.playerMotionLayout.progress = 1F
binding.playerMotionLayout.transitionToStart() 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() { private fun onManualPlayerClose() {
PlayingQueue.clear() PlayingQueue.clear()
BackgroundHelper.stopBackgroundPlay(requireContext()) BackgroundHelper.stopBackgroundPlay(requireContext())

View File

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