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 423d37b4d..d8fe2ec39 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 @@ -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() } @@ -643,13 +654,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 } diff --git a/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt b/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt index 82faff0b8..245b2bbda 100644 --- a/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt +++ b/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt @@ -16,6 +16,14 @@ class CommentsViewModel : ViewModel() { val commentsPage = MutableLiveData() + val commentSheetExpand = MutableLiveData() + + 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) } } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt index 8f8432475..0a377c0a9 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt @@ -65,6 +65,17 @@ class CommentsSheet : ExpandedBottomSheet() { .runOnCommit(this@CommentsSheet::onFragmentChanged) .commit() } + + commentsViewModel.setCommentSheetExpand(true) + commentsViewModel.commentSheetExpand.observe(viewLifecycleOwner) { + if (it == null) { + dismiss() + } else if (it) { + expand() + } else { + expand(true) + } + } } private fun onFragmentChanged() { @@ -74,6 +85,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) diff --git a/app/src/main/java/com/github/libretube/ui/sheets/ExpandedBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/ExpandedBottomSheet.kt index 533aef983..40259ed73 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/ExpandedBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/ExpandedBottomSheet.kt @@ -3,7 +3,6 @@ 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 @@ -12,19 +11,13 @@ 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(R.id.design_bottom_sheet) as FrameLayout?)?.let { fl -> - BottomSheetBehavior.from(fl).state = - BottomSheetBehavior.STATE_EXPANDED - } - } - } + dialog.setOnShowListener { expand() } return dialog } @@ -36,4 +29,15 @@ open class ExpandedBottomSheet : BottomSheetDialogFragment() { // ensure that the sheet doesn't hide parts of the video dialog?.dismiss() } + + fun expand(half: Boolean = false) { + bottomSheet?.let { fl -> + val bottomSheetInfoBehavior = BottomSheetBehavior.from(fl) + bottomSheetInfoBehavior.state = if (half) { + BottomSheetBehavior.STATE_HALF_EXPANDED + } else { + BottomSheetBehavior.STATE_EXPANDED + } + } + } }