-
-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathEdgeToEdgeReactViewGroup.kt
More file actions
249 lines (211 loc) · 7.6 KB
/
EdgeToEdgeReactViewGroup.kt
File metadata and controls
249 lines (211 loc) · 7.6 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package com.reactnativekeyboardcontroller.views
import android.annotation.SuppressLint
import android.content.res.Configuration
import android.os.Handler
import android.os.Looper
import android.widget.FrameLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsAnimationCompat
import androidx.core.view.WindowInsetsCompat
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.views.view.ReactViewGroup
import com.reactnativekeyboardcontroller.extensions.content
import com.reactnativekeyboardcontroller.extensions.removeSelf
import com.reactnativekeyboardcontroller.extensions.requestApplyInsetsWhenAttached
import com.reactnativekeyboardcontroller.extensions.rootView
import com.reactnativekeyboardcontroller.listeners.KeyboardAnimationCallback
import com.reactnativekeyboardcontroller.listeners.KeyboardAnimationCallbackConfig
import com.reactnativekeyboardcontroller.log.Logger
import com.reactnativekeyboardcontroller.modal.ModalAttachedWatcher
private val TAG = EdgeToEdgeReactViewGroup::class.qualifiedName
@Suppress("detekt:TooManyFunctions")
@SuppressLint("ViewConstructor")
class EdgeToEdgeReactViewGroup(
private val reactContext: ThemedReactContext,
) : ReactViewGroup(reactContext) {
// props
private var isStatusBarTranslucent = false
private var isNavigationBarTranslucent = false
private var isPreservingEdgeToEdge = false
private var active = false
private var isEdgeToEdge = false
// internal class members
private var eventView: ReactViewGroup? = null
private var wasMounted = false
private var callback: KeyboardAnimationCallback? = null
private val config =
KeyboardAnimationCallbackConfig(
persistentInsetTypes = WindowInsetsCompat.Type.systemBars(),
deferredInsetTypes = WindowInsetsCompat.Type.ime(),
dispatchMode = WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE,
hasTranslucentNavigationBar = isNavigationBarTranslucent,
)
// managers/watchers
private val modalAttachedWatcher = ModalAttachedWatcher(this, reactContext, config, ::getKeyboardCallback)
init {
tag = VIEW_TAG
}
// region View life cycles
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (!wasMounted) {
// skip logic with callback re-creation if it was first render/mount
wasMounted = true
return
}
this.setupKeyboardCallbacks()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
this.removeKeyboardCallbacks()
}
override fun onConfigurationChanged(newConfig: Configuration?) {
this.reApplyWindowInsets()
}
// endregion
// region State manager helpers
private fun setupWindowInsets() {
val rootView = reactContext.rootView
if (rootView != null) {
ViewCompat.setOnApplyWindowInsetsListener(rootView) { v, insets ->
val content = reactContext.content
val params =
FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT,
)
val shouldApplyZeroPaddingTop = !active || this.isStatusBarTranslucent
val shouldApplyZeroPaddingBottom = !active || this.isNavigationBarTranslucent
val navBarInsets = insets.getInsets(WindowInsetsCompat.Type.navigationBars())
val systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
params.setMargins(
navBarInsets.left,
if (shouldApplyZeroPaddingTop) {
0
} else {
systemBarInsets.top
},
navBarInsets.right,
if (shouldApplyZeroPaddingBottom) {
0
} else {
navBarInsets.bottom
},
)
content?.layoutParams = params
val defaultInsets = ViewCompat.onApplyWindowInsets(v, insets)
defaultInsets.replaceSystemWindowInsets(
defaultInsets.systemWindowInsetLeft,
if (this.isStatusBarTranslucent) 0 else defaultInsets.systemWindowInsetTop,
defaultInsets.systemWindowInsetRight,
defaultInsets.systemWindowInsetBottom,
)
}
}
}
fun removeWindowInsetsListener() {
val rootView = reactContext.rootView
if (rootView != null) {
ViewCompat.setOnApplyWindowInsetsListener(rootView, null)
}
}
fun setEdgeToEdge() {
val nextValue = active || isPreservingEdgeToEdge
if (isEdgeToEdge != nextValue) {
isEdgeToEdge = nextValue
reactContext.currentActivity?.let {
WindowCompat.setDecorFitsSystemWindows(
it.window,
!isEdgeToEdge,
)
}
}
}
private fun setupKeyboardCallbacks() {
val activity = reactContext.currentActivity
if (activity != null) {
eventView = ReactViewGroup(context)
val root = reactContext.content
root?.addView(eventView)
callback =
KeyboardAnimationCallback(
view = this,
eventPropagationView = this,
context = reactContext,
config = config,
)
eventView?.let {
ViewCompat.setWindowInsetsAnimationCallback(it, callback)
ViewCompat.setOnApplyWindowInsetsListener(it, callback)
it.requestApplyInsetsWhenAttached()
}
} else {
Logger.w(TAG, "Can not setup keyboard animation listener, since `currentActivity` is null")
}
}
private fun removeKeyboardCallbacks() {
callback?.destroy()
// capture view into closure, because if `onDetachedFromWindow` and `onAttachedToWindow`
// dispatched synchronously after each other (open application on Fabric), then `.post`
// will destroy just newly created view (if we have a reference via `this`)
// and we'll have a memory leak or zombie-view
val view = eventView
// we need to remove view asynchronously from `onDetachedFromWindow` method
// otherwise we may face NPE when app is getting opened via universal link
// see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/242
// for more details
Handler(Looper.getMainLooper()).post { view.removeSelf() }
}
private fun reApplyWindowInsets() {
this.setupWindowInsets()
this.requestApplyInsetsWhenAttached()
}
// endregion
// region State managers
private fun enable() {
this.setupWindowInsets()
this.setupKeyboardCallbacks()
modalAttachedWatcher.enable()
}
private fun disable() {
this.setupWindowInsets()
this.removeKeyboardCallbacks()
modalAttachedWatcher.disable()
}
// endregion
// region Helpers
private fun getKeyboardCallback(): KeyboardAnimationCallback? = this.callback
// endregion
// region Props setters
fun setStatusBarTranslucent(isStatusBarTranslucent: Boolean) {
this.isStatusBarTranslucent = isStatusBarTranslucent
}
fun setNavigationBarTranslucent(isNavigationBarTranslucent: Boolean) {
this.isNavigationBarTranslucent = isNavigationBarTranslucent
this.config.hasTranslucentNavigationBar = isNavigationBarTranslucent
}
fun setPreserveEdgeToEdge(isPreservingEdgeToEdge: Boolean) {
this.isPreservingEdgeToEdge = isPreservingEdgeToEdge
}
fun setActive(active: Boolean) {
this.active = active
if (active) {
this.enable()
} else {
this.disable()
}
}
// endregion
// region external methods
fun forceStatusBarTranslucent(isStatusBarTranslucent: Boolean) {
if (active && this.isStatusBarTranslucent != isStatusBarTranslucent) {
this.isStatusBarTranslucent = isStatusBarTranslucent
this.reApplyWindowInsets()
}
}
// endregion
companion object {
val VIEW_TAG = EdgeToEdgeReactViewGroup::class.simpleName
}
}