Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added onBack callback to handle back gesture when inside video's comment's replies #6600

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions app/src/main/java/com/github/libretube/ui/sheets/CommentsSheet.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.github.libretube.ui.sheets

import android.app.Dialog
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.window.OnBackInvokedDispatcher
import androidx.activity.OnBackPressedCallback
import androidx.annotation.RequiresApi
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.commit
Expand All @@ -22,6 +30,7 @@ class CommentsSheet : UndimmedBottomSheet() {
private val commonPlayerViewModel: CommonPlayerViewModel by activityViewModels()
private val commentsViewModel: CommentsViewModel by activityViewModels()


override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -36,12 +45,18 @@ class CommentsSheet : UndimmedBottomSheet() {

val binding = binding

childFragmentManager.setFragmentResultListener(DISMISS_SHEET_REQUEST_KEY, viewLifecycleOwner) { _, _ ->
childFragmentManager.setFragmentResultListener(
DISMISS_SHEET_REQUEST_KEY,
viewLifecycleOwner
) { _, _ ->
dismiss()
}

// forward requests to open links to the parent fragment
childFragmentManager.setFragmentResultListener(HANDLE_LINK_REQUEST_KEY, viewLifecycleOwner) { key, result ->
childFragmentManager.setFragmentResultListener(
HANDLE_LINK_REQUEST_KEY,
viewLifecycleOwner
) { key, result ->
parentFragment?.setFragmentResult(key, result)
}

Expand All @@ -65,6 +80,35 @@ class CommentsSheet : UndimmedBottomSheet() {
else -> dismiss()
}
}

requireActivity().apply {
when {
Build.VERSION.SDK_INT >= 33 -> {
onBackInvokedDispatcher.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT
) {
exitOnBackPressed()
}
}

else -> {
onBackPressedDispatcher.addCallback(
this@CommentsSheet,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
exitOnBackPressed()
}
})
}
}
}
}

private fun exitOnBackPressed() {
if (childFragmentManager.backStackEntryCount > 0)
childFragmentManager.popBackStack()
else
dismiss()
}

override fun onDestroyView() {
Expand Down
Loading