Skip to content

Commit 9ceb258

Browse files
committed
refactor: cleanup and simplify SponsorBlock code
1 parent 50f8e2d commit 9ceb258

4 files changed

Lines changed: 34 additions & 43 deletions

File tree

app/src/main/java/com/github/libretube/enums/SbSkipOptions.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.github.libretube.enums
22

33
enum class SbSkipOptions {
44
OFF,
5-
VISIBLE,
65
MANUAL,
76
AUTOMATIC,
87
AUTOMATIC_ONCE

app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import com.github.libretube.db.obj.WatchPosition
4141
import com.github.libretube.enums.PlayerEvent
4242
import com.github.libretube.enums.SbSkipOptions
4343
import com.github.libretube.extensions.seekBy
44-
import com.github.libretube.extensions.toastFromMainThread
4544
import com.github.libretube.extensions.togglePlayPauseState
4645
import com.github.libretube.extensions.updateParameters
4746
import com.github.libretube.obj.VideoStats
@@ -50,7 +49,6 @@ import kotlinx.coroutines.CoroutineScope
5049
import kotlinx.coroutines.Dispatchers
5150
import kotlinx.coroutines.launch
5251
import java.util.Locale
53-
import kotlin.math.absoluteValue
5452
import kotlin.math.max
5553
import kotlin.math.roundToInt
5654

@@ -199,7 +197,7 @@ object PlayerHelper {
199197
true
200198
)
201199

202-
private val sponsorBlockNotifications: Boolean
200+
val sponsorBlockNotifications: Boolean
203201
get() = PreferenceHelper.getBoolean(
204202
"sb_notifications_key",
205203
true
@@ -586,44 +584,29 @@ object PlayerHelper {
586584

587585
/**
588586
* Check for SponsorBlock segments matching the current player position
589-
* @param context A main dispatcher context
587+
* Please make sure to set [Segment#skipped] to true after seeking to the segment end
588+
*
590589
* @param segments List of the SponsorBlock segments
591590
* @return If segment found and should skip manually, the end position of the segment in ms, otherwise null
592591
*/
593592
fun Player.checkForSegments(
594-
context: Context,
595593
segments: List<Segment>,
596594
sponsorBlockConfig: MutableMap<String, SbSkipOptions>,
597-
skipAutomaticallyIfEnabled: Boolean
598-
): Segment? {
595+
): Pair<Segment, SbSkipOptions>? {
599596
for (segment in segments.filter { it.category != SPONSOR_HIGHLIGHT_CATEGORY }) {
600597
val (start, end) = segment.segmentStartAndEnd
601598
val (segmentStart, segmentEnd) = (start * 1000f).toLong() to (end * 1000f).toLong()
602599

603600
// avoid seeking to the same segment multiple times, e.g. when the SB segment is at the end of the video
604-
if ((duration - currentPosition).absoluteValue < 500) continue
601+
if (segmentEnd - currentPosition in 0..1000) continue
605602
if (currentPosition !in segmentStart until segmentEnd) continue
606603

607604
val key = sponsorBlockConfig[segment.category]
605+
if (key == SbSkipOptions.AUTOMATIC_ONCE && segment.skipped) continue
608606

609-
if (!skipAutomaticallyIfEnabled || key == SbSkipOptions.MANUAL ||
610-
(key == SbSkipOptions.AUTOMATIC_ONCE && segment.skipped)
611-
) {
612-
return segment
613-
} else if (key == SbSkipOptions.AUTOMATIC ||
614-
(key == SbSkipOptions.AUTOMATIC_ONCE && !segment.skipped)
615-
) {
616-
if (sponsorBlockNotifications) {
617-
runCatching {
618-
context.toastFromMainThread(R.string.segment_skipped)
619-
}
620-
}
621-
seekTo(segmentEnd)
622-
segment.skipped = true
623-
} else {
624-
return null
625-
}
607+
return segment to (key ?: SbSkipOptions.AUTOMATIC)
626608
}
609+
627610
return null
628611
}
629612

app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.github.libretube.constants.IntentData
2222
import com.github.libretube.constants.PreferenceKeys
2323
import com.github.libretube.db.DatabaseHelper
2424
import com.github.libretube.enums.PlayerCommand
25+
import com.github.libretube.enums.SbSkipOptions
2526
import com.github.libretube.extensions.parcelable
2627
import com.github.libretube.extensions.setMetadata
2728
import com.github.libretube.extensions.toastFromMainDispatcher
@@ -219,7 +220,7 @@ open class OnlinePlayerService : AbstractPlayerService() {
219220
sponsorBlockSegments = MediaServiceRepository.instance.getSegments(
220221
videoId,
221222
sponsorBlockConfig.keys.toList(),
222-
listOf("skip","mute","full","poi","chapter")
223+
listOf("skip", "mute", "full", "poi", "chapter")
223224
).segments
224225

225226
withContext(Dispatchers.Main) {
@@ -241,12 +242,17 @@ open class OnlinePlayerService : AbstractPlayerService() {
241242
private fun checkForSegments() {
242243
handler.postDelayed(this::checkForSegments, 100)
243244

244-
exoPlayer?.checkForSegments(
245-
this,
245+
val (currentSegment, sbSkipOption) = exoPlayer?.checkForSegments(
246246
sponsorBlockSegments,
247-
sponsorBlockConfig,
248-
skipAutomaticallyIfEnabled = sponsorBlockAutoSkip
249-
)
247+
sponsorBlockConfig
248+
) ?: return
249+
250+
if (sbSkipOption in arrayOf(SbSkipOptions.AUTOMATIC, SbSkipOptions.AUTOMATIC_ONCE)) {
251+
exoPlayer?.seekTo(currentSegment.segmentStartAndEnd.second.toLong() * 1000)
252+
currentSegment.skipped = true
253+
254+
if (PlayerHelper.sponsorBlockNotifications) toastFromMainThread(R.string.segment_skipped)
255+
}
250256
}
251257

252258
override fun runPlayerCommand(args: Bundle) {

app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import com.github.libretube.databinding.FragmentPlayerBinding
6363
import com.github.libretube.db.DatabaseHolder
6464
import com.github.libretube.enums.PlayerCommand
6565
import com.github.libretube.enums.PlayerEvent
66+
import com.github.libretube.enums.SbSkipOptions
6667
import com.github.libretube.enums.ShareObjectType
6768
import com.github.libretube.extensions.formatShort
6869
import com.github.libretube.extensions.parcelable
@@ -504,7 +505,10 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
504505
BackgroundHelper.startMediaService(
505506
requireContext(),
506507
OnlinePlayerService::class.java,
507-
if (startNewSession) bundleOf(IntentData.playerData to playerData, IntentData.audioOnly to false) else Bundle.EMPTY,
508+
if (startNewSession) bundleOf(
509+
IntentData.playerData to playerData,
510+
IntentData.audioOnly to false
511+
) else Bundle.EMPTY,
508512
) {
509513
if (_binding == null) {
510514
playerController.sendCustomCommand(
@@ -1004,25 +1008,24 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
10041008
if (viewModel.segments.value.isNullOrEmpty()) return
10051009

10061010
playerController.checkForSegments(
1007-
requireContext(),
10081011
viewModel.segments.value.orEmpty(),
1009-
viewModel.sponsorBlockConfig,
1010-
// skipping is done by player service
1011-
skipAutomaticallyIfEnabled = false
1012-
)
1013-
?.let { segment ->
1014-
if (commonPlayerViewModel.isMiniPlayerVisible.value == true) return@let
1012+
viewModel.sponsorBlockConfig
1013+
)?.let { (segment, sbSkipOption) ->
1014+
if (commonPlayerViewModel.isMiniPlayerVisible.value == true) return@let
10151015

1016+
if (sbSkipOption in arrayOf(SbSkipOptions.AUTOMATIC_ONCE, SbSkipOptions.MANUAL)) {
10161017
binding.sbSkipBtn.isVisible = true
10171018
binding.sbSkipBtn.setOnClickListener {
10181019
playerController.seekTo((segment.segmentStartAndEnd.second * 1000f).toLong())
10191020
segment.skipped = true
10201021
}
1021-
return
10221022
}
1023+
return
1024+
}
10231025

1024-
if (!playerController.isInSegment(viewModel.segments.value.orEmpty())) binding.sbSkipBtn.isGone =
1025-
true
1026+
if (!playerController.isInSegment(viewModel.segments.value.orEmpty())) {
1027+
binding.sbSkipBtn.isGone = true
1028+
}
10261029
}
10271030

10281031
private fun setPlayerDefaults() {

0 commit comments

Comments
 (0)