mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-01-06 01:20:29 +05:30
Merge pull request #6482 from Bnyro/master
feat: highlight current selection in player options sheets (audio lan…
This commit is contained in:
commit
d8f826ec6b
@ -1,5 +1,7 @@
|
|||||||
package com.github.libretube.api.obj
|
package com.github.libretube.api.obj
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.github.libretube.R
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@ -9,4 +11,10 @@ data class Subtitle(
|
|||||||
val name: String? = null,
|
val name: String? = null,
|
||||||
val code: String? = null,
|
val code: String? = null,
|
||||||
val autoGenerated: Boolean? = null
|
val autoGenerated: Boolean? = null
|
||||||
)
|
) {
|
||||||
|
fun getDisplayName(context: Context) = if (autoGenerated != true) {
|
||||||
|
name!!
|
||||||
|
} else {
|
||||||
|
"$name (${context.getString(R.string.auto_generated)})"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.github.libretube.obj
|
package com.github.libretube.obj
|
||||||
|
|
||||||
data class BottomSheetItem(
|
data class BottomSheetItem(
|
||||||
val title: String,
|
var title: String,
|
||||||
val drawable: Int? = null,
|
val drawable: Int? = null,
|
||||||
val getCurrent: () -> String? = { null },
|
val getCurrent: () -> String? = { null },
|
||||||
val onClick: () -> Unit = {}
|
val onClick: () -> Unit = {}
|
||||||
|
@ -82,7 +82,6 @@ import com.github.libretube.helpers.IntentHelper
|
|||||||
import com.github.libretube.helpers.NavBarHelper
|
import com.github.libretube.helpers.NavBarHelper
|
||||||
import com.github.libretube.helpers.NavigationHelper
|
import com.github.libretube.helpers.NavigationHelper
|
||||||
import com.github.libretube.helpers.PlayerHelper
|
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.checkForSegments
|
||||||
import com.github.libretube.helpers.PlayerHelper.getVideoStats
|
import com.github.libretube.helpers.PlayerHelper.getVideoStats
|
||||||
import com.github.libretube.helpers.PlayerHelper.isInSegment
|
import com.github.libretube.helpers.PlayerHelper.isInSegment
|
||||||
@ -1404,13 +1403,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
|
|
||||||
BaseBottomSheet()
|
BaseBottomSheet()
|
||||||
.setSimpleItems(
|
.setSimpleItems(
|
||||||
subtitles.map {
|
subtitles.map { it.getDisplayName(requireContext()) },
|
||||||
if (it.autoGenerated != true) {
|
preselectedItem = subtitles.firstOrNull { it == viewModel.currentSubtitle }
|
||||||
it.name!!
|
?.getDisplayName(requireContext()) ?: getString(R.string.none)
|
||||||
} else {
|
|
||||||
"${it.name} (${getString(R.string.auto_generated)})"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
) { index ->
|
) { index ->
|
||||||
val subtitle = subtitles.getOrNull(index) ?: return@setSimpleItems
|
val subtitle = subtitles.getOrNull(index) ?: return@setSimpleItems
|
||||||
updateCurrentSubtitle(subtitle)
|
updateCurrentSubtitle(subtitle)
|
||||||
@ -1435,9 +1430,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
// Dialog for quality selection
|
// Dialog for quality selection
|
||||||
BaseBottomSheet()
|
BaseBottomSheet()
|
||||||
.setSimpleItems(
|
.setSimpleItems(
|
||||||
resolutions.map {
|
resolutions.map(VideoResolution::name),
|
||||||
if (currentQuality == it.resolution) "${it.name} ✓" else it.name
|
preselectedItem = resolutions.firstOrNull { it.resolution == currentQuality }?.name
|
||||||
}
|
|
||||||
) { which ->
|
) { which ->
|
||||||
val newResolution = resolutions[which].resolution
|
val newResolution = resolutions[which].resolution
|
||||||
setPlayerResolution(newResolution, true)
|
setPlayerResolution(newResolution, true)
|
||||||
@ -1463,16 +1457,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
}
|
}
|
||||||
val baseBottomSheet = BaseBottomSheet()
|
val baseBottomSheet = BaseBottomSheet()
|
||||||
|
|
||||||
if (audioLanguagesAndRoleFlags.isEmpty()) {
|
if (audioLanguagesAndRoleFlags.isEmpty() || (audioLanguagesAndRoleFlags.size == 1 &&
|
||||||
baseBottomSheet.setSimpleItems(
|
audioLanguagesAndRoleFlags[0].first == null &&
|
||||||
listOf(getString(R.string.unknown_or_no_audio)),
|
!PlayerHelper.haveAudioTrackRoleFlagSet(
|
||||||
null
|
audioLanguagesAndRoleFlags[0].second
|
||||||
)
|
))
|
||||||
} else if (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
|
// 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
|
// 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)
|
// Consider it as the default audio track (or unknown)
|
||||||
baseBottomSheet.setSimpleItems(
|
baseBottomSheet.setSimpleItems(
|
||||||
listOf(getString(R.string.default_or_unknown_audio_track)),
|
listOf(getString(R.string.default_or_unknown_audio_track)),
|
||||||
null
|
preselectedItem = getString(R.string.default_or_unknown_audio_track),
|
||||||
|
listener = null
|
||||||
)
|
)
|
||||||
} else {
|
} 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]
|
val selectedAudioFormat = audioLanguagesAndRoleFlags[index]
|
||||||
viewModel.trackSelector.updateParameters {
|
viewModel.trackSelector.updateParameters {
|
||||||
setPreferredAudioLanguage(selectedAudioFormat.first)
|
setPreferredAudioLanguage(selectedAudioFormat.first)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.github.libretube.ui.sheets
|
package com.github.libretube.ui.sheets
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
@ -20,6 +21,7 @@ open class BaseBottomSheet : ExpandedBottomSheet() {
|
|||||||
private val binding get() = _binding!!
|
private val binding get() = _binding!!
|
||||||
|
|
||||||
private var title: String? = null
|
private var title: String? = null
|
||||||
|
private var preselectedItem: String? = null
|
||||||
private lateinit var items: List<BottomSheetItem>
|
private lateinit var items: List<BottomSheetItem>
|
||||||
private lateinit var listener: (index: Int) -> Unit
|
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.layoutManager = LinearLayoutManager(requireContext())
|
||||||
binding.optionsRecycler.adapter = BottomSheetAdapter(items, listener)
|
binding.optionsRecycler.adapter = BottomSheetAdapter(items, listener)
|
||||||
}
|
}
|
||||||
@ -72,8 +81,14 @@ open class BaseBottomSheet : ExpandedBottomSheet() {
|
|||||||
this.title = title
|
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)
|
setItems(titles.map { BottomSheetItem(it) }, listener)
|
||||||
|
this.preselectedItem = preselectedItem
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val titleTextSize = 7f.dpToPx().toFloat()
|
private val titleTextSize = 7f.dpToPx().toFloat()
|
||||||
|
@ -267,7 +267,8 @@ abstract class CustomExoPlayerView(
|
|||||||
if (scrubbingTimeBar && !forceUpdate) return
|
if (scrubbingTimeBar && !forceUpdate) return
|
||||||
|
|
||||||
val currentIndex = PlayerHelper.getCurrentChapterIndex(player.currentPosition, chapters)
|
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)
|
chaptersViewModel.currentChapterIndex.updateIfChanged(currentIndex ?: return)
|
||||||
|
|
||||||
// change the chapter name textView text to the chapterName
|
// change the chapter name textView text to the chapterName
|
||||||
@ -601,7 +602,10 @@ abstract class CustomExoPlayerView(
|
|||||||
)
|
)
|
||||||
|
|
||||||
BaseBottomSheet()
|
BaseBottomSheet()
|
||||||
.setSimpleItems(aspectRatioModeNames) { index ->
|
.setSimpleItems(
|
||||||
|
aspectRatioModeNames,
|
||||||
|
preselectedItem = aspectRatioModeNames[aspectRatioModes.indexOf(resizeMode)]
|
||||||
|
) { index ->
|
||||||
resizeMode = aspectRatioModes[index]
|
resizeMode = aspectRatioModes[index]
|
||||||
}
|
}
|
||||||
.show(supportFragmentManager)
|
.show(supportFragmentManager)
|
||||||
@ -611,7 +615,12 @@ abstract class CustomExoPlayerView(
|
|||||||
// repeat mode options dialog
|
// repeat mode options dialog
|
||||||
BaseBottomSheet()
|
BaseBottomSheet()
|
||||||
.setSimpleItems(
|
.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 ->
|
) { index ->
|
||||||
PlayingQueue.repeatMode = PlayerHelper.repeatModes[index].first
|
PlayingQueue.repeatMode = PlayerHelper.repeatModes[index].first
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user