diff --git a/app/src/main/java/com/github/libretube/api/obj/Subtitle.kt b/app/src/main/java/com/github/libretube/api/obj/Subtitle.kt index 91b6760cd..8e1b349f2 100644 --- a/app/src/main/java/com/github/libretube/api/obj/Subtitle.kt +++ b/app/src/main/java/com/github/libretube/api/obj/Subtitle.kt @@ -1,5 +1,7 @@ package com.github.libretube.api.obj +import android.content.Context +import com.github.libretube.R import kotlinx.serialization.Serializable @Serializable @@ -9,4 +11,10 @@ data class Subtitle( val name: String? = null, val code: String? = null, val autoGenerated: Boolean? = null -) +) { + fun getDisplayName(context: Context) = if (autoGenerated != true) { + name!! + } else { + "$name (${context.getString(R.string.auto_generated)})" + } +} diff --git a/app/src/main/java/com/github/libretube/obj/BottomSheetItem.kt b/app/src/main/java/com/github/libretube/obj/BottomSheetItem.kt index 73195412a..9db697838 100644 --- a/app/src/main/java/com/github/libretube/obj/BottomSheetItem.kt +++ b/app/src/main/java/com/github/libretube/obj/BottomSheetItem.kt @@ -1,7 +1,7 @@ package com.github.libretube.obj data class BottomSheetItem( - val title: String, + var title: String, val drawable: Int? = null, val getCurrent: () -> String? = { null }, val onClick: () -> Unit = {} 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 68505caa0..f64e16208 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 @@ -82,7 +82,6 @@ import com.github.libretube.helpers.IntentHelper import com.github.libretube.helpers.NavBarHelper import com.github.libretube.helpers.NavigationHelper import com.github.libretube.helpers.PlayerHelper -import com.github.libretube.helpers.PlayerHelper.autoPlayEnabled import com.github.libretube.helpers.PlayerHelper.checkForSegments import com.github.libretube.helpers.PlayerHelper.getVideoStats import com.github.libretube.helpers.PlayerHelper.isInSegment @@ -1404,13 +1403,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { BaseBottomSheet() .setSimpleItems( - subtitles.map { - if (it.autoGenerated != true) { - it.name!! - } else { - "${it.name} (${getString(R.string.auto_generated)})" - } - } + subtitles.map { it.getDisplayName(requireContext()) }, + preselectedItem = subtitles.firstOrNull { it == viewModel.currentSubtitle } + ?.getDisplayName(requireContext()) ?: getString(R.string.none) ) { index -> val subtitle = subtitles.getOrNull(index) ?: return@setSimpleItems updateCurrentSubtitle(subtitle) @@ -1435,9 +1430,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // Dialog for quality selection BaseBottomSheet() .setSimpleItems( - resolutions.map { - if (currentQuality == it.resolution) "${it.name} ✓" else it.name - } + resolutions.map(VideoResolution::name), + preselectedItem = resolutions.firstOrNull { it.resolution == currentQuality }?.name ) { which -> val newResolution = resolutions[which].resolution setPlayerResolution(newResolution, true) @@ -1463,16 +1457,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { } val baseBottomSheet = BaseBottomSheet() - if (audioLanguagesAndRoleFlags.isEmpty()) { - baseBottomSheet.setSimpleItems( - listOf(getString(R.string.unknown_or_no_audio)), - null - ) - } else if (audioLanguagesAndRoleFlags.size == 1 && - audioLanguagesAndRoleFlags[0].first == null && - !PlayerHelper.haveAudioTrackRoleFlagSet( - audioLanguagesAndRoleFlags[0].second - ) + if (audioLanguagesAndRoleFlags.isEmpty() || (audioLanguagesAndRoleFlags.size == 1 && + audioLanguagesAndRoleFlags[0].first == null && + !PlayerHelper.haveAudioTrackRoleFlagSet( + audioLanguagesAndRoleFlags[0].second + )) ) { // Regardless of audio format or quality, if there is only one audio stream which has // no language and no role flags, it should mean that there is only a single audio @@ -1480,10 +1469,16 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // Consider it as the default audio track (or unknown) baseBottomSheet.setSimpleItems( listOf(getString(R.string.default_or_unknown_audio_track)), - null + preselectedItem = getString(R.string.default_or_unknown_audio_track), + listener = null ) } else { - baseBottomSheet.setSimpleItems(audioLanguages) { 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/sheets/BaseBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/BaseBottomSheet.kt index 236b07711..3c6a24455 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/BaseBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/BaseBottomSheet.kt @@ -1,6 +1,7 @@ package com.github.libretube.ui.sheets import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -20,6 +21,7 @@ open class BaseBottomSheet : ExpandedBottomSheet() { private val binding get() = _binding!! private var title: String? = null + private var preselectedItem: String? = null private lateinit var items: List private lateinit var listener: (index: Int) -> Unit @@ -46,6 +48,13 @@ open class BaseBottomSheet : ExpandedBottomSheet() { } } + // set the selected item + for (item in items) { + Log.e(item.title, preselectedItem.toString()) + } + for (item in items.filter { it.title == preselectedItem }) { + item.title = "${item.title} ✓" + } binding.optionsRecycler.layoutManager = LinearLayoutManager(requireContext()) binding.optionsRecycler.adapter = BottomSheetAdapter(items, listener) } @@ -72,8 +81,14 @@ open class BaseBottomSheet : ExpandedBottomSheet() { this.title = title } - fun setSimpleItems(titles: List, listener: (suspend (index: Int) -> Unit)?) = + fun setSimpleItems( + titles: List, + preselectedItem: String? = null, + listener: (suspend (index: Int) -> Unit)? + ) = apply { setItems(titles.map { BottomSheetItem(it) }, listener) + this.preselectedItem = preselectedItem + } companion object { private val titleTextSize = 7f.dpToPx().toFloat() 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 93386bb76..a43e9bfcf 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 @@ -267,7 +267,8 @@ 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() } + ?: context.getString(R.string.no_chapter) chaptersViewModel.currentChapterIndex.updateIfChanged(currentIndex ?: return) // change the chapter name textView text to the chapterName @@ -601,7 +602,10 @@ abstract class CustomExoPlayerView( ) BaseBottomSheet() - .setSimpleItems(aspectRatioModeNames) { index -> + .setSimpleItems( + aspectRatioModeNames, + preselectedItem = aspectRatioModeNames[aspectRatioModes.indexOf(resizeMode)] + ) { index -> resizeMode = aspectRatioModes[index] } .show(supportFragmentManager) @@ -611,7 +615,12 @@ abstract class CustomExoPlayerView( // repeat mode options dialog BaseBottomSheet() .setSimpleItems( - PlayerHelper.repeatModes.map { context.getString(it.second) } + PlayerHelper.repeatModes.map { context.getString(it.second) }, + preselectedItem = PlayerHelper.repeatModes + .firstOrNull { it.first == PlayingQueue.repeatMode } + ?.second?.let { + context.getString(it) + } ) { index -> PlayingQueue.repeatMode = PlayerHelper.repeatModes[index].first }