Skip to content

Commit 032c840

Browse files
authored
Close overlapping panels on back press
1 parent a3a7b70 commit 032c840

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragmentPhone.kt

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ import com.lagradost.cloudstream3.utils.AppContextUtils.isCastApiAvailable
6868
import com.lagradost.cloudstream3.utils.AppContextUtils.loadCache
6969
import com.lagradost.cloudstream3.utils.AppContextUtils.openBrowser
7070
import com.lagradost.cloudstream3.utils.AppContextUtils.updateHasTrailers
71+
import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.attachBackPressedCallback
72+
import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.detachBackPressedCallback
73+
import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.disableBackPressedCallback
74+
import com.lagradost.cloudstream3.utils.BackPressedCallbackHelper.enableBackPressedCallback
7175
import com.lagradost.cloudstream3.utils.BatteryOptimizationChecker.openBatteryOptimizationSettings
7276
import com.lagradost.cloudstream3.utils.ExtractorLink
7377
import com.lagradost.cloudstream3.utils.ImageLoader.loadImage
@@ -116,16 +120,13 @@ open class ResultFragmentPhone : FullScreenPlayer() {
116120
container: ViewGroup?,
117121
savedInstanceState: Bundle?
118122
): View? {
119-
viewModel =
120-
ViewModelProvider(this)[ResultViewModel2::class.java]
121-
syncModel =
122-
ViewModelProvider(this)[SyncViewModel::class.java]
123+
viewModel = ViewModelProvider(this)[ResultViewModel2::class.java]
124+
syncModel = ViewModelProvider(this)[SyncViewModel::class.java]
123125
updateUIEvent += ::updateUI
124126

125127
val root = super.onCreateView(inflater, container, savedInstanceState) ?: return null
126128
FragmentResultSwipeBinding.bind(root).let { bind ->
127-
resultBinding =
128-
bind.fragmentResult//FragmentResultBinding.bind(binding.root.findViewById(R.id.fragment_result))
129+
resultBinding = bind.fragmentResult
129130
recommendationBinding = bind.resultRecommendations
130131
syncBinding = bind.resultSync
131132
binding = bind
@@ -136,7 +137,6 @@ open class ResultFragmentPhone : FullScreenPlayer() {
136137

137138
override fun onCreate(savedInstanceState: Bundle?) {
138139
super.onCreate(savedInstanceState)
139-
140140
PanelsChildGestureRegionObserver.Provider.get().apply {
141141
resultBinding?.resultCastItems?.let { register(it) }
142142
}
@@ -248,6 +248,7 @@ open class ResultFragmentPhone : FullScreenPlayer() {
248248
resultBinding = null
249249
syncBinding = null
250250
recommendationBinding = null
251+
activity?.detachBackPressedCallback("ResultFragmentPhone")
251252
super.onDestroyView()
252253
}
253254

@@ -473,16 +474,25 @@ open class ResultFragmentPhone : FullScreenPlayer() {
473474
activity?.popCurrentPage()
474475
}
475476

477+
activity?.attachBackPressedCallback("ResultFragmentPhone") {
478+
if (resultOverlappingPanels.getSelectedPanel().ordinal == 1) {
479+
// If we don't disable we end up in a loop with default behavior calling
480+
// this callback as well, so we disable it, run default behavior,
481+
// then re-enable this callback so it can be used for next back press.
482+
activity?.disableBackPressedCallback("ResultFragmentPhone")
483+
activity?.onBackPressedDispatcher?.onBackPressed()
484+
activity?.enableBackPressedCallback("ResultFragmentPhone")
485+
} else resultOverlappingPanels.closePanels()
486+
}
487+
476488
resultMiniSync.setRecycledViewPool(ImageAdapter.sharedPool)
477489
resultMiniSync.adapter = ImageAdapter(
478490
nextFocusDown = R.id.result_sync_set_score,
479491
clickCallback = { action ->
480492
if (action == IMAGE_CLICK || action == IMAGE_LONG_CLICK) {
481-
if (binding?.resultOverlappingPanels?.getSelectedPanel()?.ordinal == 1) {
482-
binding?.resultOverlappingPanels?.openStartPanel()
483-
} else {
484-
binding?.resultOverlappingPanels?.closePanels()
485-
}
493+
if (resultOverlappingPanels.getSelectedPanel().ordinal == 1) {
494+
resultOverlappingPanels.openStartPanel()
495+
} else resultOverlappingPanels.closePanels()
486496
}
487497
})
488498
resultSubscribe.setOnClickListener {

0 commit comments

Comments
 (0)