-
Notifications
You must be signed in to change notification settings - Fork 317
/
FullScreenFeature.kt
97 lines (83 loc) · 3.22 KB
/
FullScreenFeature.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.feature.session
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import mozilla.components.browser.state.selector.findTabOrCustomTabOrSelectedTab
import mozilla.components.browser.state.state.SessionState
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.lib.state.ext.flowScoped
import mozilla.components.support.base.feature.LifecycleAwareFeature
import mozilla.components.support.base.feature.UserInteractionHandler
/**
* Feature implementation for handling fullscreen mode (exiting and back button presses).
*/
open class FullScreenFeature(
private val store: BrowserStore,
private val sessionUseCases: SessionUseCases,
private val tabId: String? = null,
private val viewportFitChanged: (Int) -> Unit = {},
private val fullScreenChanged: (Boolean) -> Unit,
) : LifecycleAwareFeature, UserInteractionHandler {
private var scope: CoroutineScope? = null
private var observation: Observation = createDefaultObservation()
/**
* Starts the feature and a observer to listen for fullscreen changes.
*/
override fun start() {
scope = store.flowScoped { flow ->
flow.map { state -> state.findTabOrCustomTabOrSelectedTab(tabId) }
.map { tab -> tab.toObservation() }
.distinctUntilChanged()
.collect { observation -> onChange(observation) }
}
}
override fun stop() {
scope?.cancel()
}
private fun onChange(observation: Observation) {
if (observation.inFullScreen != this.observation.inFullScreen) {
fullScreenChanged(observation.inFullScreen)
}
if (observation.layoutInDisplayCutoutMode != this.observation.layoutInDisplayCutoutMode) {
viewportFitChanged(observation.layoutInDisplayCutoutMode)
}
this.observation = observation
}
/**
* To be called when the back button is pressed, so that only fullscreen mode closes.
*
* @return Returns true if the fullscreen mode was successfully exited; false if no effect was taken.
*/
override fun onBackPressed(): Boolean {
val observation = observation
if (observation.inFullScreen && observation.tabId != null) {
sessionUseCases.exitFullscreen(observation.tabId)
return true
}
return false
}
}
/**
* Simple holder data class to keep a reference to the last values we observed.
*/
private data class Observation(
val tabId: String?,
val inFullScreen: Boolean,
val layoutInDisplayCutoutMode: Int,
)
private fun SessionState?.toObservation(): Observation {
return if (this != null) {
Observation(id, content.fullScreen, content.layoutInDisplayCutoutMode)
} else {
createDefaultObservation()
}
}
private fun createDefaultObservation() = Observation(
tabId = null,
inFullScreen = false,
layoutInDisplayCutoutMode = 0,
)