Merge pull request #6482 from Bnyro/master

feat: highlight current selection in player options sheets (audio lan…
This commit is contained in:
Bnyro 2024-09-10 16:21:38 +02:00 committed by GitHub
commit d8f826ec6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 56 additions and 29 deletions

View File

@ -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)})"
}
}

View File

@ -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 = {}

View File

@ -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)

View File

@ -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<BottomSheetItem>
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<String>, listener: (suspend (index: Int) -> Unit)?) =
fun setSimpleItems(
titles: List<String>,
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()

View File

@ -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
}