Merge pull request #3632 from xz-dev/master

Half expand comment dialog when roll player to avoid comments dialog hide the player
This commit is contained in:
Bnyro 2023-04-27 08:36:15 +02:00 committed by GitHub
commit 32d9abc5a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 23 deletions

View File

@ -110,10 +110,6 @@ import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.upstream.DefaultDataSource
import com.google.android.exoplayer2.util.MimeTypes
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import kotlin.math.abs
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -122,6 +118,10 @@ import kotlinx.coroutines.withContext
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import retrofit2.HttpException
import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import kotlin.math.abs
class PlayerFragment : Fragment(), OnlinePlayerOptions {
private var _binding: FragmentPlayerBinding? = null
@ -185,8 +185,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
private val handler = Handler(Looper.getMainLooper())
private val mainActivity get() = activity as MainActivity
private val windowInsetsControllerCompat get() = WindowCompat
.getInsetsController(mainActivity.window, mainActivity.window.decorView)
private val windowInsetsControllerCompat
get() = WindowCompat
.getInsetsController(mainActivity.window, mainActivity.window.decorView)
private var scrubbingTimeBar = false
@ -200,18 +201,23 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
PlayerEvent.Play -> {
exoPlayer.play()
}
PlayerEvent.Pause -> {
exoPlayer.pause()
}
PlayerEvent.Forward -> {
exoPlayer.seekTo(exoPlayer.currentPosition + PlayerHelper.seekIncrement)
}
PlayerEvent.Rewind -> {
exoPlayer.seekTo(exoPlayer.currentPosition - PlayerHelper.seekIncrement)
}
PlayerEvent.Next -> {
playNextVideo()
}
PlayerEvent.Background -> {
playOnBackground()
// wait some time in order for the service to get started properly
@ -219,6 +225,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
activity?.finish()
}
}
else -> {
}
}
@ -298,6 +305,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
mainMotionLayout.progress = abs(progress)
binding.player.hideController()
binding.player.useController = false
commentsViewModel.setCommentSheetExpand(false)
eId = endId
sId = startId
}
@ -310,6 +318,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// disable captions
updateCaptionsLanguage(null)
binding.player.useController = false
commentsViewModel.setCommentSheetExpand(null)
mainMotionLayout.progress = 1F
(activity as MainActivity).requestOrientationChange()
} else if (currentId == sId) {
@ -317,6 +326,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// re-enable captions
updateCaptionsLanguage(captionLanguage)
binding.player.useController = true
commentsViewModel.setCommentSheetExpand(true)
mainMotionLayout.progress = 0F
changeOrientationMode()
}
@ -360,6 +370,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
!exoPlayer.isPlaying && exoPlayer.playbackState == Player.STATE_ENDED -> {
exoPlayer.seekTo(0)
}
!exoPlayer.isPlaying -> exoPlayer.play()
else -> exoPlayer.pause()
}
@ -489,6 +500,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
binding.mainContainer.isClickable = true
binding.linLayout.visibility = View.GONE
commentsViewModel.setCommentSheetExpand(null)
playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen_exit)
playerBinding.exoTitle.visibility = View.VISIBLE
@ -643,13 +655,14 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
if (segments.isEmpty()) return
exoPlayer.checkForSegments(requireContext(), segments, PlayerHelper.skipSegmentsManually)?.let { segmentEnd ->
binding.sbSkipBtn.visibility = View.VISIBLE
binding.sbSkipBtn.setOnClickListener {
exoPlayer.seekTo(segmentEnd)
exoPlayer.checkForSegments(requireContext(), segments, PlayerHelper.skipSegmentsManually)
?.let { segmentEnd ->
binding.sbSkipBtn.visibility = View.VISIBLE
binding.sbSkipBtn.setOnClickListener {
exoPlayer.seekTo(segmentEnd)
}
return
}
return
}
if (PlayerHelper.skipSegmentsManually) binding.sbSkipBtn.visibility = View.GONE
}
@ -1294,7 +1307,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
when {
// LBRY HLS
PreferenceHelper.getBoolean(PreferenceKeys.LBRY_HLS, false) && streams.videoStreams.any {
PreferenceHelper.getBoolean(
PreferenceKeys.LBRY_HLS,
false
) && streams.videoStreams.any {
it.quality.orEmpty().contains("LBRY HLS")
} -> {
val lbryHlsUrl = streams.videoStreams.first {
@ -1303,7 +1319,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
setMediaSource(lbryHlsUrl.toUri(), MimeTypes.APPLICATION_M3U8)
}
// DASH
!PreferenceHelper.getBoolean(PreferenceKeys.USE_HLS_OVER_DASH, false) && streams.videoStreams.isNotEmpty() -> {
!PreferenceHelper.getBoolean(
PreferenceKeys.USE_HLS_OVER_DASH,
false
) && streams.videoStreams.isNotEmpty() -> {
// only use the dash manifest generated by YT if either it's a livestream or no other source is available
val uri = streams.dash?.let { ProxyHelper.unwrapIfEnabled(it) }?.toUri().takeIf {
streams.livestream || streams.videoStreams.isEmpty()

View File

@ -16,6 +16,14 @@ class CommentsViewModel : ViewModel() {
val commentsPage = MutableLiveData<CommentsPage?>()
val commentSheetExpand = MutableLiveData<Boolean?>()
fun setCommentSheetExpand(value: Boolean?) {
if (commentSheetExpand.value != value) {
commentSheetExpand.value = value
}
}
private var nextPage: String? = null
var videoId: String? = null
@ -64,5 +72,6 @@ class CommentsViewModel : ViewModel() {
nextPage = null
commentsPage.value = null
videoId = null
setCommentSheetExpand(null)
}
}

View File

@ -65,6 +65,15 @@ class CommentsSheet : ExpandedBottomSheet() {
.runOnCommit(this@CommentsSheet::onFragmentChanged)
.commit()
}
commentsViewModel.setCommentSheetExpand(true)
commentsViewModel.commentSheetExpand.observe(viewLifecycleOwner) {
when (it) {
true -> expand()
false -> expand(true)
else -> dismiss()
}
}
}
private fun onFragmentChanged() {
@ -74,6 +83,7 @@ class CommentsSheet : ExpandedBottomSheet() {
binding.btnBack.visibility = View.VISIBLE
binding.commentsTitle.text = getString(R.string.replies)
}
else -> {
binding.btnBack.visibility = View.GONE
binding.commentsTitle.text = getString(R.string.comments)

View File

@ -3,28 +3,22 @@ package com.github.libretube.ui.sheets
import android.app.Dialog
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import androidx.fragment.app.FragmentManager
import com.google.android.material.R
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetBehavior.PEEK_HEIGHT_AUTO
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
open class ExpandedBottomSheet : BottomSheetDialogFragment() {
private val bottomSheet: FrameLayout? get() = dialog?.findViewById(R.id.design_bottom_sheet)
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) return dialog
dialog.setOnShowListener {
(it as BottomSheetDialog).let { d ->
(d.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?)?.let { fl ->
BottomSheetBehavior.from(fl).state =
BottomSheetBehavior.STATE_EXPANDED
}
}
}
dialog.setOnShowListener { expand() }
return dialog
}
@ -36,4 +30,17 @@ open class ExpandedBottomSheet : BottomSheetDialogFragment() {
// ensure that the sheet doesn't hide parts of the video
dialog?.dismiss()
}
fun expand(collapse: Boolean = false) {
bottomSheet?.let { fl ->
val bottomSheetInfoBehavior = BottomSheetBehavior.from(fl)
if (collapse) {
bottomSheetInfoBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
bottomSheetInfoBehavior.setPeekHeight(0, true)
} else {
bottomSheetInfoBehavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetInfoBehavior.setPeekHeight(PEEK_HEIGHT_AUTO, true)
}
}
}
}