player view cleanup

This commit is contained in:
Bnyro 2022-10-29 15:33:06 +02:00
parent ae3f48fc45
commit c8c2ab02e2
7 changed files with 102 additions and 87 deletions

View File

@ -1,7 +1,7 @@
package com.github.libretube.models.interfaces package com.github.libretube.models.interfaces
interface PlayerOptionsInterface { interface OnlinePlayerOptions {
fun onCaptionClicked() fun onCaptionsClicked()
fun onQualityClicked() fun onQualityClicked()
} }

View File

@ -0,0 +1,11 @@
package com.github.libretube.models.interfaces
interface PlayerOptions {
fun onAutoplayClicked()
fun onPlaybackSpeedClicked()
fun onResizeModeClicked()
fun onRepeatModeClicked()
}

View File

@ -3,5 +3,6 @@ package com.github.libretube.obj
data class BottomSheetItem( data class BottomSheetItem(
val title: String, val title: String,
val drawable: Int? = null, val drawable: Int? = null,
val currentValue: String? = null val currentValue: String? = null,
val onClick: () -> Unit = {}
) )

View File

@ -34,6 +34,7 @@ class BottomSheetAdapter(
} }
root.setOnClickListener { root.setOnClickListener {
item.onClick.invoke()
listener.invoke(position) listener.invoke(position)
} }
} }

View File

@ -56,7 +56,7 @@ import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem import com.github.libretube.extensions.toStreamItem
import com.github.libretube.models.PlayerViewModel import com.github.libretube.models.PlayerViewModel
import com.github.libretube.models.interfaces.PlayerOptionsInterface import com.github.libretube.models.interfaces.OnlinePlayerOptions
import com.github.libretube.services.BackgroundMode import com.github.libretube.services.BackgroundMode
import com.github.libretube.services.DownloadService import com.github.libretube.services.DownloadService
import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.activities.MainActivity
@ -105,7 +105,7 @@ import java.io.IOException
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.abs import kotlin.math.abs
class PlayerFragment : BaseFragment(), PlayerOptionsInterface { class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
lateinit var binding: FragmentPlayerBinding lateinit var binding: FragmentPlayerBinding
private lateinit var playerBinding: ExoStyledPlayerControlViewBinding private lateinit var playerBinding: ExoStyledPlayerControlViewBinding
@ -1319,7 +1319,7 @@ class PlayerFragment : BaseFragment(), PlayerOptionsInterface {
} }
} }
override fun onCaptionClicked() { override fun onCaptionsClicked() {
if (!this@PlayerFragment::streams.isInitialized || if (!this@PlayerFragment::streams.isInitialized ||
streams.subtitles == null || streams.subtitles == null ||
streams.subtitles!!.isEmpty() streams.subtitles!!.isEmpty()

View File

@ -32,18 +32,18 @@ open class BottomSheet : BottomSheetDialogFragment() {
binding.optionsRecycler.adapter = BottomSheetAdapter(items, listener) binding.optionsRecycler.adapter = BottomSheetAdapter(items, listener)
} }
fun setItems(items: List<BottomSheetItem>, listener: (index: Int) -> Unit) = apply { fun setItems(items: List<BottomSheetItem>, listener: ((index: Int) -> Unit)?) = apply {
this.items = items this.items = items
this.listener = { index -> this.listener = { index ->
listener.invoke(index) listener?.invoke(index)
dialog?.dismiss() dialog?.dismiss()
} }
} }
fun setSimpleItems(titles: List<String>, listener: (index: Int) -> Unit) = apply { fun setSimpleItems(titles: List<String>, listener: ((index: Int) -> Unit)?) = apply {
this.items = titles.map { BottomSheetItem(it) } this.items = titles.map { BottomSheetItem(it) }
this.listener = { index -> this.listener = { index ->
listener.invoke(index) listener?.invoke(index)
dialog?.dismiss() dialog?.dismiss()
} }
} }

View File

@ -14,7 +14,8 @@ import com.github.libretube.databinding.DoubleTapOverlayBinding
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
import com.github.libretube.extensions.toDp import com.github.libretube.extensions.toDp
import com.github.libretube.models.interfaces.DoubleTapInterface import com.github.libretube.models.interfaces.DoubleTapInterface
import com.github.libretube.models.interfaces.PlayerOptionsInterface import com.github.libretube.models.interfaces.OnlinePlayerOptions
import com.github.libretube.models.interfaces.PlayerOptions
import com.github.libretube.obj.BottomSheetItem import com.github.libretube.obj.BottomSheetItem
import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.sheets.PlaybackSpeedSheet import com.github.libretube.ui.sheets.PlaybackSpeedSheet
@ -30,7 +31,7 @@ import com.google.android.exoplayer2.util.RepeatModeUtil
internal class CustomExoPlayerView( internal class CustomExoPlayerView(
context: Context, context: Context,
attributeSet: AttributeSet? = null attributeSet: AttributeSet? = null
) : StyledPlayerView(context, attributeSet) { ) : StyledPlayerView(context, attributeSet), PlayerOptions {
val binding: ExoStyledPlayerControlViewBinding = ExoStyledPlayerControlViewBinding.bind(this) val binding: ExoStyledPlayerControlViewBinding = ExoStyledPlayerControlViewBinding.bind(this)
private var doubleTapOverlayBinding: DoubleTapOverlayBinding? = null private var doubleTapOverlayBinding: DoubleTapOverlayBinding? = null
@ -38,7 +39,7 @@ internal class CustomExoPlayerView(
* Objects from the parent fragment * Objects from the parent fragment
*/ */
private var doubleTapListener: DoubleTapInterface? = null private var doubleTapListener: DoubleTapInterface? = null
private var playerOptionsInterface: PlayerOptionsInterface? = null private var playerOptionsInterface: OnlinePlayerOptions? = null
private lateinit var childFragmentManager: FragmentManager private lateinit var childFragmentManager: FragmentManager
private var trackSelector: TrackSelector? = null private var trackSelector: TrackSelector? = null
@ -72,7 +73,7 @@ internal class CustomExoPlayerView(
fun initialize( fun initialize(
childFragmentManager: FragmentManager, childFragmentManager: FragmentManager,
playerViewInterface: PlayerOptionsInterface?, playerViewInterface: OnlinePlayerOptions?,
doubleTapOverlayBinding: DoubleTapOverlayBinding, doubleTapOverlayBinding: DoubleTapOverlayBinding,
trackSelector: TrackSelector? trackSelector: TrackSelector?
) { ) {
@ -136,7 +137,6 @@ internal class CustomExoPlayerView(
private fun initializeAdvancedOptions(context: Context) { private fun initializeAdvancedOptions(context: Context) {
binding.toggleOptions.setOnClickListener { binding.toggleOptions.setOnClickListener {
val bottomSheetFragment = BottomSheet().apply {
val items = mutableListOf( val items = mutableListOf(
BottomSheetItem( BottomSheetItem(
context.getString(R.string.player_autoplay), context.getString(R.string.player_autoplay),
@ -146,7 +146,9 @@ internal class CustomExoPlayerView(
} else { } else {
context.getString(R.string.disabled) context.getString(R.string.disabled)
} }
), ) {
onAutoplayClicked()
},
BottomSheetItem( BottomSheetItem(
context.getString(R.string.repeat_mode), context.getString(R.string.repeat_mode),
R.drawable.ic_repeat, R.drawable.ic_repeat,
@ -155,7 +157,9 @@ internal class CustomExoPlayerView(
} else { } else {
context.getString(R.string.repeat_mode_current) context.getString(R.string.repeat_mode_current)
} }
), ) {
onRepeatModeClicked()
},
BottomSheetItem( BottomSheetItem(
context.getString(R.string.player_resize_mode), context.getString(R.string.player_resize_mode),
R.drawable.ic_aspect_ratio, R.drawable.ic_aspect_ratio,
@ -164,7 +168,9 @@ internal class CustomExoPlayerView(
AspectRatioFrameLayout.RESIZE_MODE_FILL -> context.getString(R.string.resize_mode_fill) AspectRatioFrameLayout.RESIZE_MODE_FILL -> context.getString(R.string.resize_mode_fill)
else -> context.getString(R.string.resize_mode_zoom) else -> context.getString(R.string.resize_mode_zoom)
} }
), ) {
onResizeModeClicked()
},
BottomSheetItem( BottomSheetItem(
context.getString(R.string.playback_speed), context.getString(R.string.playback_speed),
R.drawable.ic_speed, R.drawable.ic_speed,
@ -173,7 +179,9 @@ internal class CustomExoPlayerView(
.toString() .toString()
.replace(".0", "") .replace(".0", "")
}x" }x"
) ) {
onPlaybackSpeedClicked()
}
) )
if (playerOptionsInterface != null) { if (playerOptionsInterface != null) {
@ -182,7 +190,9 @@ internal class CustomExoPlayerView(
context.getString(R.string.quality), context.getString(R.string.quality),
R.drawable.ic_hd, R.drawable.ic_hd,
"${player?.videoSize?.height}p" "${player?.videoSize?.height}p"
) ) {
playerOptionsInterface?.onQualityClicked()
}
) )
items.add( items.add(
BottomSheetItem( BottomSheetItem(
@ -193,21 +203,13 @@ internal class CustomExoPlayerView(
} else { } else {
context.getString(R.string.none) context.getString(R.string.none)
} }
) ) {
playerOptionsInterface?.onCaptionsClicked()
}
) )
} }
setItems(items) { index -> val bottomSheetFragment = BottomSheet().setItems(items, null)
when (index) {
0 -> onAutoplayClicked()
1 -> onRepeatModeClicked()
2 -> onResizeModeClicked()
3 -> onPlaybackSpeedClicked()
4 -> playerOptionsInterface?.onQualityClicked()
5 -> playerOptionsInterface?.onCaptionClicked()
}
}
}
bottomSheetFragment.show(childFragmentManager, null) bottomSheetFragment.show(childFragmentManager, null)
} }
} }
@ -307,7 +309,7 @@ internal class CustomExoPlayerView(
} }
} }
private fun onAutoplayClicked() { override fun onAutoplayClicked() {
// autoplay options dialog // autoplay options dialog
BottomSheet() BottomSheet()
.setSimpleItems( .setSimpleItems(
@ -324,11 +326,11 @@ internal class CustomExoPlayerView(
.show(childFragmentManager) .show(childFragmentManager)
} }
private fun onPlaybackSpeedClicked() { override fun onPlaybackSpeedClicked() {
player?.let { PlaybackSpeedSheet(it).show(childFragmentManager) } player?.let { PlaybackSpeedSheet(it).show(childFragmentManager) }
} }
private fun onResizeModeClicked() { override fun onResizeModeClicked() {
// switching between original aspect ratio (black bars) and zoomed to fill device screen // switching between original aspect ratio (black bars) and zoomed to fill device screen
val aspectRatioModeNames = context.resources?.getStringArray(R.array.resizeMode) val aspectRatioModeNames = context.resources?.getStringArray(R.array.resizeMode)
?.toList().orEmpty() ?.toList().orEmpty()
@ -346,7 +348,7 @@ internal class CustomExoPlayerView(
.show(childFragmentManager) .show(childFragmentManager)
} }
private fun onRepeatModeClicked() { override fun onRepeatModeClicked() {
val repeatModeNames = listOf( val repeatModeNames = listOf(
context.getString(R.string.repeat_mode_none), context.getString(R.string.repeat_mode_none),
context.getString(R.string.repeat_mode_current) context.getString(R.string.repeat_mode_current)