From 1efe09d4926790b335aead442f912559f722dbde Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 3 Nov 2023 14:30:44 +0100 Subject: [PATCH] fix: multiple seekbar listeners interfering with each other --- .../libretube/ui/fragments/PlayerFragment.kt | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) 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 2a82b808f..5beb51fc1 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 @@ -183,11 +183,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { private var sponsorBlockConfig = PlayerHelper.getSponsorBlockCategories() private val handler = Handler(Looper.getMainLooper()) - private val mainActivity get() = activity as MainActivity - private val windowInsetsControllerCompat - get() = WindowCompat - .getInsetsController(mainActivity.window, mainActivity.window.decorView) + private var seekBarPreviewListener: SeekbarPreviewListener? = null private var scrubbingTimeBar = false private var chaptersBottomSheet: ChaptersBottomSheet? = null @@ -197,6 +194,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { */ private var playerLayoutOrientation = Int.MIN_VALUE + private val mainActivity get() = activity as MainActivity + private val windowInsetsControllerCompat + get() = WindowCompat + .getInsetsController(mainActivity.window, mainActivity.window.decorView) + private val fullscreenDialog by lazy { object : Dialog(requireContext(), android.R.style.Theme_Black_NoTitleBar_Fullscreen) { override fun onBackPressed() { @@ -758,7 +760,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { playerBinding.exoProgress.setPlayer(exoPlayer) initializePlayerView() - setupSeekbarPreview() exoPlayer.playWhenReady = PlayerHelper.playAutomatically exoPlayer.prepare() @@ -1020,6 +1021,13 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { playerBinding.skipNext.setOnClickListener { playNextVideo() } + + // seekbar preview setup + playerBinding.seekbarPreview.isGone = true + seekBarPreviewListener?.let { playerBinding.exoProgress.removeListener(it) } + seekBarPreviewListener = createSeekbarPreviewListener().also { + playerBinding.exoProgress.addListener(it) + } } private fun showAutoPlayCountdown() { @@ -1531,22 +1539,19 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { } .build() - private fun setupSeekbarPreview() { - playerBinding.seekbarPreview.isGone = true - playerBinding.exoProgress.addListener( - SeekbarPreviewListener( - OnlineTimeFrameReceiver(requireContext(), streams.previewFrames), - playerBinding, - streams.duration * 1000, - onScrub = { - setCurrentChapterName(forceUpdate = true, enqueueNew = false) - scrubbingTimeBar = true - }, - onScrubEnd = { - scrubbingTimeBar = false - setCurrentChapterName(forceUpdate = true, enqueueNew = false) - } - ) + private fun createSeekbarPreviewListener(): SeekbarPreviewListener { + return SeekbarPreviewListener( + OnlineTimeFrameReceiver(requireContext(), streams.previewFrames), + playerBinding, + streams.duration * 1000, + onScrub = { + setCurrentChapterName(forceUpdate = true, enqueueNew = false) + scrubbingTimeBar = true + }, + onScrubEnd = { + scrubbingTimeBar = false + setCurrentChapterName(forceUpdate = true, enqueueNew = false) + } ) }