Use Handler.postDelayed() extension with tokens.

This commit is contained in:
Isira Seneviratne 2023-01-26 12:42:11 +05:30
parent c5e21b5f18
commit f522f1e1de
2 changed files with 31 additions and 34 deletions

View File

@ -14,6 +14,7 @@ import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.os.postDelayed
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleOwner
import com.github.libretube.R
@ -90,10 +91,6 @@ internal class CustomExoPlayerView(
if (isControllerFullyVisible) hideController() else showController()
}
private val hideControllerRunnable = Runnable {
hideController()
}
fun initialize(
playerViewInterface: OnlinePlayerOptions?,
doubleTapOverlayBinding: DoubleTapOverlayBinding,
@ -199,9 +196,7 @@ internal class CustomExoPlayerView(
}
private fun cancelHideControllerTask() {
runCatching {
handler.removeCallbacks(hideControllerRunnable)
}
handler.removeCallbacksAndMessages(HIDE_CONTROLLER_TOKEN)
}
override fun hideController() {
@ -222,7 +217,9 @@ internal class CustomExoPlayerView(
// remove the previous callback from the queue to prevent a flashing behavior
cancelHideControllerTask()
// automatically hide the controller after 2 seconds
handler.postDelayed(hideControllerRunnable, AUTO_HIDE_CONTROLLER_DELAY)
handler.postDelayed(AUTO_HIDE_CONTROLLER_DELAY, HIDE_CONTROLLER_TOKEN) {
hideController()
}
super.showController()
}
@ -380,8 +377,10 @@ internal class CustomExoPlayerView(
animateSeeking(rewindBTN, rewindIV, rewindTV, true)
// start callback to hide the button
runnableHandler.removeCallbacks(hideRewindButtonRunnable)
runnableHandler.postDelayed(hideRewindButtonRunnable, 700)
runnableHandler.removeCallbacksAndMessages(HIDE_REWIND_BUTTON_TOKEN)
runnableHandler.postDelayed(700, HIDE_REWIND_BUTTON_TOKEN) {
rewindBTN.visibility = View.GONE
}
}
}
@ -393,8 +392,10 @@ internal class CustomExoPlayerView(
animateSeeking(forwardBTN, forwardIV, forwardTV, false)
// start callback to hide the button
runnableHandler.removeCallbacks(hideForwardButtonRunnable)
runnableHandler.postDelayed(hideForwardButtonRunnable, 700)
runnableHandler.removeCallbacksAndMessages(HIDE_FORWARD_BUTTON_TOKEN)
runnableHandler.postDelayed(700, HIDE_FORWARD_BUTTON_TOKEN) {
forwardBTN.visibility = View.GONE
}
}
}
@ -447,17 +448,6 @@ internal class CustomExoPlayerView(
}
}
private val hideForwardButtonRunnable = Runnable {
doubleTapOverlayBinding?.forwardBTN?.apply {
this.visibility = View.GONE
}
}
private val hideRewindButtonRunnable = Runnable {
doubleTapOverlayBinding?.rewindBTN?.apply {
this.visibility = View.GONE
}
}
private fun initializeGestureProgress() {
gestureViewBinding.brightnessProgressBar.let { bar ->
bar.progress =
@ -701,12 +691,18 @@ internal class CustomExoPlayerView(
// when a control is clicked, restart the countdown to hide the controller
if (isControllerFullyVisible) {
cancelHideControllerTask()
handler.postDelayed(hideControllerRunnable, AUTO_HIDE_CONTROLLER_DELAY)
handler.postDelayed(AUTO_HIDE_CONTROLLER_DELAY, HIDE_CONTROLLER_TOKEN) {
hideController()
}
}
return super.onInterceptTouchEvent(ev)
}
companion object {
private const val HIDE_CONTROLLER_TOKEN = "hideController"
private const val HIDE_FORWARD_BUTTON_TOKEN = "hideForwardButton"
private const val HIDE_REWIND_BUTTON_TOKEN = "hideRewindButton"
private const val SUBTITLE_BOTTOM_PADDING_FRACTION = 0.158f
private const val ANIMATION_DURATION = 100L
private const val AUTO_HIDE_CONTROLLER_DELAY = 2000L

View File

@ -11,6 +11,7 @@ import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.view.View
import androidx.activity.viewModels
import androidx.core.os.postDelayed
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.interfaces.PlayerGestureOptions
import com.github.libretube.ui.models.PlayerViewModel
@ -27,7 +28,7 @@ class PlayerGestureController(activity: BaseActivity, private val listener: Play
private val elapsedTime get() = SystemClock.elapsedRealtime()
private val playerViewModel: PlayerViewModel by activity.viewModels()
private val handler: Handler = Handler(Looper.getMainLooper())
private val handler = Handler(Looper.getMainLooper())
private val gestureDetector: GestureDetector
private val scaleGestureDetector: ScaleGestureDetector
@ -110,7 +111,7 @@ class PlayerGestureController(activity: BaseActivity, private val listener: Play
}
if (isEnabled && isSecondClick()) {
handler.removeCallbacks(runnable)
handler.removeCallbacksAndMessages(SINGLE_TAP_TOKEN)
lastDoubleClick = elapsedTime
val eventPositionPercentageX = e.x / width
@ -121,8 +122,12 @@ class PlayerGestureController(activity: BaseActivity, private val listener: Play
}
} else {
if (recentDoubleClick()) return true
handler.removeCallbacks(runnable)
handler.postDelayed(runnable, MAX_TIME_DIFF)
handler.removeCallbacksAndMessages(SINGLE_TAP_TOKEN)
handler.postDelayed(MAX_TIME_DIFF, SINGLE_TAP_TOKEN) {
// If the last event was for scroll or pinch then avoid single tap call
if (!wasClick || isSecondClick()) return@postDelayed
listener.onSingleTap()
}
lastClick = elapsedTime
}
return true
@ -155,12 +160,6 @@ class PlayerGestureController(activity: BaseActivity, private val listener: Play
return true
}
private val runnable = Runnable {
// If the last event was for scroll or pinch then avoid single tap call
if (!wasClick || isSecondClick()) return@Runnable
listener.onSingleTap()
}
private fun isSecondClick(): Boolean {
return elapsedTime - lastClick < MAX_TIME_DIFF
}
@ -171,6 +170,8 @@ class PlayerGestureController(activity: BaseActivity, private val listener: Play
}
companion object {
private const val SINGLE_TAP_TOKEN = "singleTap"
private const val MAX_TIME_DIFF = 400L
private const val MOVEMENT_THRESHOLD = 30
private const val BORDER_THRESHOLD = 90