From aa2fd8384dba7ee18e315a1d61e914d2fb0528fd Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 25 Sep 2024 13:59:15 +0200 Subject: [PATCH] fix: no chapters shown if video highlight is the only chapter --- .../libretube/ui/fragments/PlayerFragment.kt | 23 +++++++++++-------- .../libretube/ui/views/CustomExoPlayerView.kt | 9 +++++--- 2 files changed, 19 insertions(+), 13 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 8e522a52d..83d732dd4 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 @@ -17,6 +17,7 @@ import android.os.Bundle import android.os.Handler import android.os.Looper import android.os.PowerManager +import android.util.Log import android.view.KeyEvent import android.view.LayoutInflater import android.view.PixelCopy @@ -1103,6 +1104,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // init the chapters recyclerview chaptersViewModel.chaptersLiveData.value = streams.chapters + chaptersViewModel.chaptersLiveData.observe(viewLifecycleOwner) { + binding.player.setCurrentChapterName() + } if (PlayerHelper.relatedStreamsEnabled) { val relatedLayoutManager = binding.relatedRecView.layoutManager as LinearLayoutManager @@ -1197,10 +1201,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { chaptersViewModel.chaptersLiveData.postValue( chaptersViewModel.chapters.plus(highlightChapter).sortedBy { it.start } ) - - withContext(Dispatchers.Main) { - binding.player.setCurrentChapterName() - } } private fun getSubtitleConfigs(): List = streams.subtitles.map { @@ -1478,12 +1478,15 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { listener = null ) } else { - baseBottomSheet.setSimpleItems(audioLanguages, preselectedItem = audioLanguagesAndRoleFlags.firstOrNull { - val format = viewModel.player.audioFormat - format?.language == it.first && format?.roleFlags == it.second - }?.let { - PlayerHelper.getAudioTrackNameFromFormat(context, it) - },) { index -> + baseBottomSheet.setSimpleItems( + audioLanguages, + preselectedItem = audioLanguagesAndRoleFlags.firstOrNull { + val format = viewModel.player.audioFormat + format?.language == it.first && format?.roleFlags == it.second + }?.let { + PlayerHelper.getAudioTrackNameFromFormat(context, it) + }, + ) { index -> val selectedAudioFormat = audioLanguagesAndRoleFlags[index] viewModel.trackSelector.updateParameters { setPreferredAudioLanguage(selectedAudioFormat.first) diff --git a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt index a43e9bfcf..b14b5f74b 100644 --- a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt @@ -250,7 +250,11 @@ abstract class CustomExoPlayerView( } } - // set the name of the video chapter in the exoPlayerView + /** + * Set the name of the video chapter in the [CustomExoPlayerView] + * @param forceUpdate Update the current chapter name no matter if the seek bar is scrubbed + * @param enqueueNew set a timeout to automatically repeat this function again in 100ms + */ fun setCurrentChapterName(forceUpdate: Boolean = false, enqueueNew: Boolean = true) { val player = player ?: return val chapters = chaptersViewModel.chapters @@ -267,8 +271,7 @@ abstract class CustomExoPlayerView( if (scrubbingTimeBar && !forceUpdate) return val currentIndex = PlayerHelper.getCurrentChapterIndex(player.currentPosition, chapters) - val newChapterName = currentIndex?.let { chapters[it].title.trim() } - ?: context.getString(R.string.no_chapter) + val newChapterName = currentIndex?.let { chapters[it].title.trim() }.orEmpty() chaptersViewModel.currentChapterIndex.updateIfChanged(currentIndex ?: return) // change the chapter name textView text to the chapterName