Skip to content

Commit

Permalink
feat: allow interacting with player while viewing chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Aug 20, 2023
1 parent c310a4f commit b577d96
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
binding.commentsToggle.setOnClickListener {
// set the max height to not cover the currently playing video
commentsViewModel.handleLink = this::handleLink
commentsViewModel.maxHeight = binding.root.height - binding.player.height
updateMaxSheetHeight()
commentsViewModel.videoId = videoId
CommentsSheet().show(childFragmentManager)
}
Expand Down Expand Up @@ -493,6 +493,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
)
}

private fun updateMaxSheetHeight() {
viewModel.maxSheetHeightPx = binding.root.height - binding.player.height
}

private fun playOnBackground() {
BackgroundHelper.stopBackgroundPlay(requireContext())
BackgroundHelper.playOnBackground(
Expand Down Expand Up @@ -771,6 +775,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {

// enable the chapters dialog in the player
playerBinding.chapterLL.setOnClickListener {
updateMaxSheetHeight()
ChaptersBottomSheet(chapters, exoPlayer)
.show(childFragmentManager)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class CommentsViewModel : ViewModel() {
var videoId: String? = null
private var nextPage: String? = null
private var isLoading = false
var maxHeight = 0
var currentCommentsPosition = 0
var commentsSheetDismiss: (() -> Unit)? = null
var handleLink: ((url: String) -> Unit)? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class PlayerViewModel : ViewModel() {
val isFullscreen = MutableLiveData<Boolean>().apply {
value = false
}

var maxSheetHeightPx = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.media3.exoplayer.ExoPlayer
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.api.obj.ChapterSegment
import com.github.libretube.databinding.BottomSheetBinding
import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.ui.adapters.ChaptersAdapter
import com.github.libretube.ui.models.PlayerViewModel

class ChaptersBottomSheet(
private val chapters: List<ChapterSegment>,
private val exoPlayer: ExoPlayer
) : ExpandedBottomSheet() {
) : UndimmedBottomSheet() {
private var _binding: BottomSheetBinding? = null
private val binding get() = _binding!!

private val playerViewModel: PlayerViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -32,6 +36,8 @@ class ChaptersBottomSheet(
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.optionsRecycler.layoutManager = LinearLayoutManager(context)
val adapter = ChaptersAdapter(chapters, exoPlayer)
binding.optionsRecycler.adapter = adapter
Expand All @@ -52,6 +58,12 @@ class ChaptersBottomSheet(
updatePosition.run()
}

override fun getSheetMaxHeightPx() = playerViewModel.maxSheetHeightPx

override fun getDragHandle() = binding.dragHandle

override fun getBottomSheet() = binding.standardBottomSheet

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import com.github.libretube.R
import com.github.libretube.databinding.CommentsSheetBinding
import com.github.libretube.ui.fragments.CommentsMainFragment
import com.github.libretube.ui.models.CommentsViewModel
import com.github.libretube.ui.models.PlayerViewModel

class CommentsSheet : UndimmedBottomSheet() {
lateinit var binding: CommentsSheetBinding
private val playerViewModel: PlayerViewModel by activityViewModels()
private val commentsViewModel: CommentsViewModel by activityViewModels()

override fun onCreateView(
Expand All @@ -40,7 +42,7 @@ class CommentsSheet : UndimmedBottomSheet() {
// limit the recyclerview height to not cover the video
binding.standardBottomSheet.layoutParams =
binding.commentFragContainer.layoutParams.apply {
height = commentsViewModel.maxHeight
height = playerViewModel.maxSheetHeightPx
}
}
})
Expand Down Expand Up @@ -70,6 +72,12 @@ class CommentsSheet : UndimmedBottomSheet() {
}
}

override fun getSheetMaxHeightPx() = playerViewModel.maxSheetHeightPx

override fun getDragHandle() = binding.dragHandle

override fun getBottomSheet() = binding.standardBottomSheet

fun updateFragmentInfo(showBackButton: Boolean, title: String) {
binding.btnBack.isVisible = showBackButton
binding.commentsTitle.text = title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,31 @@ import android.app.Dialog
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.ViewTreeObserver
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.core.view.updateLayoutParams

/**
* A bottom sheet that allows touches on its top/background
*/
open class UndimmedBottomSheet : ExpandedBottomSheet() {
abstract class UndimmedBottomSheet : ExpandedBottomSheet() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

getDragHandle().viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
getDragHandle().viewTreeObserver.removeOnGlobalLayoutListener(this)

// limit the recyclerview height to not cover the video
getBottomSheet().updateLayoutParams {
height = getSheetMaxHeightPx()
}
}
})
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)

Expand Down Expand Up @@ -46,4 +65,8 @@ open class UndimmedBottomSheet : ExpandedBottomSheet() {

return dialog
}

abstract fun getSheetMaxHeightPx(): Int
abstract fun getDragHandle(): View
abstract fun getBottomSheet(): FrameLayout
}

0 comments on commit b577d96

Please sign in to comment.