fix: don't show the highlight as chapter for the whole remaining video

This commit is contained in:
Bnyro 2023-08-24 17:12:27 +02:00
parent c4983aa00d
commit c30def8c16
4 changed files with 29 additions and 8 deletions

View File

@ -10,5 +10,12 @@ data class ChapterSegment(
val image: String = "", val image: String = "",
val start: Long, val start: Long,
// Used only for video highlights // Used only for video highlights
@Transient var drawable: Drawable? = null @Transient var highlightDrawable: Drawable? = null
) ) {
companion object {
/**
* Length to show for a highlight in seconds
*/
const val HIGHLIGHT_LENGTH = 10L
}
}

View File

@ -533,7 +533,16 @@ object PlayerHelper {
*/ */
fun getCurrentChapterIndex(exoPlayer: ExoPlayer, chapters: List<ChapterSegment>): Int? { fun getCurrentChapterIndex(exoPlayer: ExoPlayer, chapters: List<ChapterSegment>): Int? {
val currentPosition = exoPlayer.currentPosition / 1000 val currentPosition = exoPlayer.currentPosition / 1000
return chapters.indexOfLast { currentPosition >= it.start }.takeIf { it >= 0 } return chapters
.filter {
it.highlightDrawable == null ||
// remove the video highlight if it's already longer ago than [ChapterSegment.HIGHLIGHT_LENGTH],
// otherwise the SponsorBlock highlight would be shown from its starting point to the end
(currentPosition - it.start) < ChapterSegment.HIGHLIGHT_LENGTH
}
.sortedBy { it.start }
.indexOfLast { currentPosition >= it.start }
.takeIf { it >= 0 }
} }
fun getPosition(videoId: String, duration: Long?): Long? { fun getPosition(videoId: String, duration: Long?): Long? {

View File

@ -28,16 +28,21 @@ class ChaptersAdapter(
override fun onBindViewHolder(holder: ChaptersViewHolder, position: Int) { override fun onBindViewHolder(holder: ChaptersViewHolder, position: Int) {
val chapter = chapters[position] val chapter = chapters[position]
holder.binding.apply { holder.binding.apply {
if (chapter.drawable != null) { if (chapter.highlightDrawable != null) {
chapterImage.setImageDrawable(chapter.drawable) chapterImage.setImageDrawable(chapter.highlightDrawable)
} else { } else {
ImageHelper.loadImage(chapter.image, chapterImage) ImageHelper.loadImage(chapter.image, chapterImage)
} }
chapterTitle.text = chapter.title chapterTitle.text = chapter.title
timeStamp.text = DateUtils.formatElapsedTime(chapter.start) timeStamp.text = DateUtils.formatElapsedTime(chapter.start)
val chapterEnd = chapters.getOrNull(position + 1)?.start val playerDurationSeconds = exoPlayer.duration / 1000
?: (exoPlayer.duration / 1000) val chapterEnd = if (chapter.highlightDrawable == null) {
chapters.getOrNull(position + 1)?.start ?: playerDurationSeconds
} else {
// the duration for chapters is hardcoded, since it's not provided by the SB API
minOf(chapter.start + ChapterSegment.HIGHLIGHT_LENGTH, playerDurationSeconds)
}
val durationSpan = chapterEnd - chapter.start val durationSpan = chapterEnd - chapter.start
duration.text = root.context.getString( duration.text = root.context.getString(
R.string.duration_span, R.string.duration_span,

View File

@ -1175,7 +1175,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
val highlightChapter = ChapterSegment( val highlightChapter = ChapterSegment(
title = getString(R.string.chapters_videoHighlight), title = getString(R.string.chapters_videoHighlight),
start = highlight.segmentStartAndEnd.first.toLong(), start = highlight.segmentStartAndEnd.first.toLong(),
drawable = frame?.toDrawable(requireContext().resources) highlightDrawable = frame?.toDrawable(requireContext().resources)
) )
chapters.add(highlightChapter) chapters.add(highlightChapter)
chapters.sortBy { it.start } chapters.sortBy { it.start }