-
-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathScreen.kt
More file actions
628 lines (558 loc) · 22.4 KB
/
Screen.kt
File metadata and controls
628 lines (558 loc) · 22.4 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
package com.swmansion.rnscreens
import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.graphics.Paint
import android.os.Parcelable
import android.util.Log
import android.util.SparseArray
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.webkit.WebView
import android.widget.ImageView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.children
import androidx.fragment.app.Fragment
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.facebook.react.bridge.GuardedRunnable
import com.facebook.react.bridge.ReactContext
import com.facebook.react.uimanager.PixelUtil
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.UIManagerModule
import com.facebook.react.uimanager.events.EventDispatcher
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import com.swmansion.rnscreens.bottomsheet.isSheetFitToContents
import com.swmansion.rnscreens.bottomsheet.useSingleDetent
import com.swmansion.rnscreens.bottomsheet.usesFormSheetPresentation
import com.swmansion.rnscreens.events.HeaderHeightChangeEvent
import com.swmansion.rnscreens.events.SheetDetentChangedEvent
import com.swmansion.rnscreens.ext.parentAsViewGroup
@SuppressLint("ViewConstructor") // Only we construct this view, it is never inflated.
class Screen(
val reactContext: ThemedReactContext,
) : FabricEnabledViewGroup(reactContext),
ScreenContentWrapper.OnLayoutCallback {
val fragment: Fragment?
get() = fragmentWrapper?.fragment
val sheetBehavior: BottomSheetBehavior<Screen>?
get() = (layoutParams as? CoordinatorLayout.LayoutParams)?.behavior as? BottomSheetBehavior<Screen>
val reactEventDispatcher: EventDispatcher?
get() = UIManagerHelper.getEventDispatcherForReactTag(reactContext, id)
var fragmentWrapper: ScreenFragmentWrapper? = null
var container: ScreenContainer? = null
var activityState: ActivityState? = null
private set
private var isTransitioning = false
var stackPresentation = StackPresentation.PUSH
var replaceAnimation = ReplaceAnimation.POP
var stackAnimation = StackAnimation.DEFAULT
var isGestureEnabled = true
var screenOrientation: Int? = null
private set
var isStatusBarAnimated: Boolean? = null
var isBeingRemoved = false
// Props for controlling modal presentation
var isSheetGrabberVisible: Boolean = false
// corner radius must be updated after all props prop updates from a single transaction
// have been applied, because it depends on the presentation type.
private var shouldUpdateSheetCornerRadius = false
var sheetCornerRadius: Float = 0F
set(value) {
if (field != value) {
field = value
shouldUpdateSheetCornerRadius = true
}
}
var sheetExpandsWhenScrolledToEdge: Boolean = true
// We want to make sure here that at least one value is present in this array all the time.
// TODO: Model this with custom data structure to guarantee that this invariant is not violated.
var sheetDetents = mutableListOf(1.0)
var sheetLargestUndimmedDetentIndex: Int = -1
var sheetInitialDetentIndex: Int = 0
var sheetClosesOnTouchOutside = true
var sheetElevation: Float = 24F
/**
* When using form sheet presentation we want to delay enter transition **on Paper** in order
* to wait for initial layout from React, otherwise the animator-based animation will look
* glitchy. *This is not needed on Fabric*.
*/
var shouldTriggerPostponedTransitionAfterLayout = false
var footer: ScreenFooter? = null
set(value) {
if (value == null && field != null) {
sheetBehavior?.let { field!!.unregisterWithSheetBehavior(it) }
} else if (value != null) {
sheetBehavior?.let { value.registerWithSheetBehavior(it) }
}
field = value
}
private val isNativeStackScreen: Boolean
get() = container is ScreenStack
init {
// we set layout params as WindowManager.LayoutParams to workaround the issue with TextInputs
// not displaying modal menus (e.g., copy/paste or selection). The missing menus are due to the
// fact that TextView implementation is expected to be attached to window when layout happens.
// Then, at the moment of layout it checks whether window type is in a reasonable range to tell
// whether it should enable selection controls (see Editor.java#prepareCursorControllers).
// With screens, however, the text input component can be laid out before it is attached, in
// that case TextView tries to get window type property from the oldest existing parent, which
// in this case is a Screen class, as it is the root of the screen that is about to be attached.
// Setting params this way is not the most elegant way to solve this problem but workarounds it
// for the time being
layoutParams = WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION)
}
/**
* ScreenContentWrapper notifies us here on it's layout. It is essential for implementing
* `fitToContents` for formSheets, as this is first entry point where we can acquire
* height of our content.
*/
override fun onContentWrapperLayout(
changed: Boolean,
left: Int,
top: Int,
right: Int,
bottom: Int,
) {
val height = bottom - top
Log.i("HT", "Screen [$id] onContentWrapperLayout - $height")
if (usesFormSheetPresentation()) {
if (isSheetFitToContents()) {
sheetBehavior?.useSingleDetent(height)
}
if (!BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// On old architecture we delay enter transition in order to wait for initial frame.
Log.i("HT", "Screen [$id] onContentWrapperLayout - request transition trigger")
shouldTriggerPostponedTransitionAfterLayout = true
val parent = parentAsViewGroup()
if (parent != null && !parent.isInLayout) {
// There are reported cases (irreproducible) when Screen is not laid out after
// maxHeight is set on behaviour.
Log.i("HT", "Screen [$id] onContentWrapperLayout - request parent layout")
parent.requestLayout()
}
}
}
}
fun registerLayoutCallbackForWrapper(wrapper: ScreenContentWrapper) {
wrapper.delegate = this
}
override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>) {
// do nothing, react native will keep the view hierarchy so no need to serialize/deserialize
// view's states. The side effect of restoring is that TextInput components would trigger
// set-text events which may confuse text input handling.
}
override fun dispatchRestoreInstanceState(container: SparseArray<Parcelable>) {
// ignore restoring instance state too as we are not saving anything anyways.
}
override fun onLayout(
changed: Boolean,
l: Int,
t: Int,
r: Int,
b: Int,
) {
Log.i("HT", "Screen [$id] received layout ${b - t}")
// In case of form sheet we get layout notification a bit later, in `onBottomSheetBehaviorDidLayout`
// after the attached behaviour laid out this view.
if (changed && isNativeStackScreen && !usesFormSheetPresentation()) {
val width = r - l
val height = b - t
dispatchShadowStateUpdate(width, height, t)
// FormSheet has no header in current model.
notifyHeaderHeightChange(t)
}
}
internal fun onBottomSheetBehaviorDidLayout(coordinatorLayoutDidChange: Boolean) {
if (!usesFormSheetPresentation() || !isNativeStackScreen) {
return
}
if (coordinatorLayoutDidChange) {
dispatchShadowStateUpdate(width, height, top)
}
footer?.onParentLayout(coordinatorLayoutDidChange, left, top, right, bottom, container!!.height)
Log.i("HT", "Screen [$id] behavior layout")
if (!BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// When using form sheet presentation we want to delay enter transition **on Paper** in order
// to wait for initial layout from React, otherwise the animator-based animation will look
// glitchy. *This seems to not be needed on Fabric*.
triggerPostponedEnterTransitionIfNeeded()
}
}
private fun triggerPostponedEnterTransitionIfNeeded() {
if (shouldTriggerPostponedTransitionAfterLayout) {
shouldTriggerPostponedTransitionAfterLayout = false
// This will trigger enter transition only if one was requested by ScreenStack
Log.i("HT", "Screen [$id] triggering postponed transition")
fragment?.startPostponedEnterTransition()
}
}
private fun updateScreenSizePaper(
width: Int,
height: Int,
) {
reactContext.runOnNativeModulesQueueThread(
object : GuardedRunnable(reactContext.exceptionHandler) {
override fun runGuarded() {
reactContext
.getNativeModule(UIManagerModule::class.java)
?.updateNodeSize(id, width, height)
}
},
)
}
/**
* @param offsetY ignored on old architecture
*/
private fun dispatchShadowStateUpdate(
width: Int,
height: Int,
offsetY: Int,
) {
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
updateScreenSizeFabric(width, height, offsetY)
} else {
updateScreenSizePaper(width, height)
}
}
val headerConfig: ScreenStackHeaderConfig?
get() = children.find { it is ScreenStackHeaderConfig } as? ScreenStackHeaderConfig
val contentWrapper: ScreenContentWrapper?
get() = children.find { it is ScreenContentWrapper } as? ScreenContentWrapper
/**
* While transitioning this property allows to optimize rendering behavior on Android and provide
* a correct blending options for the animated screen. It is turned on automatically by the
* container when transitioning is detected and turned off immediately after
*/
fun setTransitioning(transitioning: Boolean) {
if (isTransitioning == transitioning) {
return
}
isTransitioning = transitioning
val isWebViewInScreen = hasWebView(this)
if (isWebViewInScreen && layerType != LAYER_TYPE_HARDWARE) {
return
}
super.setLayerType(
if (transitioning && !isWebViewInScreen) LAYER_TYPE_HARDWARE else LAYER_TYPE_NONE,
null,
)
}
/**
* Whether this screen allows to see the content underneath it.
*/
fun isTranslucent(): Boolean =
when (stackPresentation) {
StackPresentation.TRANSPARENT_MODAL,
StackPresentation.FORM_SHEET,
-> true
else -> false
}
private fun hasWebView(viewGroup: ViewGroup): Boolean {
for (i in 0 until viewGroup.childCount) {
val child = viewGroup.getChildAt(i)
if (child is WebView) {
return true
} else if (child is ViewGroup) {
if (hasWebView(child)) {
return true
}
}
}
return false
}
override fun setLayerType(
layerType: Int,
paint: Paint?,
) {
// ignore - layer type is controlled by `transitioning` prop
}
fun setActivityState(activityState: ActivityState) {
if (activityState == this.activityState) {
return
}
if (container is ScreenStack && this.activityState != null && activityState < this.activityState!!) {
throw IllegalStateException("[RNScreens] activityState can only progress in NativeStack")
}
this.activityState = activityState
Log.i("HT", "Screen [$id] activityState change to $activityState")
container?.onChildUpdate()
}
fun setScreenOrientation(screenOrientation: String?) {
if (screenOrientation == null) {
this.screenOrientation = null
return
}
ScreenWindowTraits.applyDidSetOrientation()
this.screenOrientation =
when (screenOrientation) {
"all" -> ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
"portrait" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
"portrait_up" -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
"portrait_down" -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
"landscape" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
"landscape_left" -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
"landscape_right" -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
else -> ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
fragmentWrapper?.let { ScreenWindowTraits.setOrientation(this, it.tryGetActivity()) }
}
// Accepts one of 4 accessibility flags
// developer.android.com/reference/android/view/View#attr_android:importantForAccessibility
fun changeAccessibilityMode(mode: Int) {
this.importantForAccessibility = mode
this.headerConfig?.toolbar?.importantForAccessibility = mode
}
var statusBarStyle: String? = null
set(statusBarStyle) {
if (statusBarStyle != null) {
ScreenWindowTraits.applyDidSetStatusBarAppearance()
}
field = statusBarStyle
fragmentWrapper?.let {
ScreenWindowTraits.setStyle(
this,
it.tryGetActivity(),
it.tryGetContext(),
)
}
}
var isStatusBarHidden: Boolean? = null
set(statusBarHidden) {
if (statusBarHidden != null) {
ScreenWindowTraits.applyDidSetStatusBarAppearance()
}
field = statusBarHidden
fragmentWrapper?.let { ScreenWindowTraits.setHidden(this, it.tryGetActivity()) }
}
var isStatusBarTranslucent: Boolean? = null
set(statusBarTranslucent) {
if (statusBarTranslucent != null) {
ScreenWindowTraits.applyDidSetStatusBarAppearance()
}
field = statusBarTranslucent
fragmentWrapper?.let {
ScreenWindowTraits.setTranslucent(
this,
it.tryGetActivity(),
it.tryGetContext(),
)
}
}
var statusBarColor: Int? = null
set(statusBarColor) {
if (statusBarColor != null) {
ScreenWindowTraits.applyDidSetStatusBarAppearance()
}
field = statusBarColor
fragmentWrapper?.let {
ScreenWindowTraits.setColor(
this,
it.tryGetActivity(),
it.tryGetContext(),
)
}
}
var navigationBarColor: Int? = null
set(navigationBarColor) {
if (navigationBarColor != null) {
ScreenWindowTraits.applyDidSetNavigationBarAppearance()
}
field = navigationBarColor
fragmentWrapper?.let {
ScreenWindowTraits.setNavigationBarColor(
this,
it.tryGetActivity(),
)
}
}
var isNavigationBarTranslucent: Boolean? = null
set(navigationBarTranslucent) {
if (navigationBarTranslucent != null) {
ScreenWindowTraits.applyDidSetNavigationBarAppearance()
}
field = navigationBarTranslucent
fragmentWrapper?.let {
ScreenWindowTraits.setNavigationBarTranslucent(
this,
it.tryGetActivity(),
)
}
}
var isNavigationBarHidden: Boolean? = null
set(navigationBarHidden) {
if (navigationBarHidden != null) {
ScreenWindowTraits.applyDidSetNavigationBarAppearance()
}
field = navigationBarHidden
fragmentWrapper?.let {
ScreenWindowTraits.setNavigationBarHidden(
this,
it.tryGetActivity(),
)
}
}
var nativeBackButtonDismissalEnabled: Boolean = true
fun startRemovalTransition() {
if (!isBeingRemoved) {
isBeingRemoved = true
startTransitionRecursive(this)
}
}
fun endRemovalTransition() {
if (!isBeingRemoved) {
return
}
isBeingRemoved = false
endTransitionRecursive(this)
}
private fun endTransitionRecursive(parent: ViewGroup) {
parent.children.forEach { childView ->
parent.endViewTransition(childView)
if (childView is ScreenStackHeaderConfig) {
endTransitionRecursive(childView.toolbar)
}
if (childView is ViewGroup) {
endTransitionRecursive(childView)
}
}
}
private fun startTransitionRecursive(parent: ViewGroup?) {
parent?.let {
for (i in 0 until it.childCount) {
val child = it.getChildAt(i)
if (parent is SwipeRefreshLayout && child is ImageView) {
// SwipeRefreshLayout class which has CircleImageView as a child,
// does not handle `startViewTransition` properly.
// It has a custom `getChildDrawingOrder` method which returns
// wrong index if we called `startViewTransition` on the views on new arch.
// We add a simple View to bump the number of children to make it work.
// TODO: find a better way to handle this scenario
it.addView(View(context), i)
} else {
child?.let { view -> it.startViewTransition(view) }
}
if (child is ScreenStackHeaderConfig) {
// we want to start transition on children of the toolbar too,
// which is not a child of ScreenStackHeaderConfig
startTransitionRecursive(child.toolbar)
}
if (child is ViewGroup) {
startTransitionRecursive(child)
}
}
}
}
// We do not want to perform any action, therefore do not need to override the associated method.
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean =
if (usesFormSheetPresentation()) {
// If we're a form sheet we want to consume the gestures to prevent
// DimmingView's callback from triggering when clicking on the sheet itself.
true
} else {
super.onTouchEvent(event)
}
private fun notifyHeaderHeightChange(headerHeight: Int) {
val screenContext = context as ReactContext
val surfaceId = UIManagerHelper.getSurfaceId(screenContext)
UIManagerHelper
.getEventDispatcherForReactTag(screenContext, id)
?.dispatchEvent(HeaderHeightChangeEvent(surfaceId, id, headerHeight))
}
internal fun onSheetDetentChanged(
detentIndex: Int,
isStable: Boolean,
) {
dispatchSheetDetentChanged(detentIndex, isStable)
// There is no need to update shadow state for transient sheet states -
// we are unsure of the exact sheet position anyway.
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED && isStable) {
updateScreenSizeFabric(width, height, top)
}
}
private fun dispatchSheetDetentChanged(
detentIndex: Int,
isStable: Boolean,
) {
val surfaceId = UIManagerHelper.getSurfaceId(reactContext)
reactEventDispatcher?.dispatchEvent(
SheetDetentChangedEvent(
surfaceId,
id,
detentIndex,
isStable,
),
)
}
internal fun onFinalizePropsUpdate() {
if (shouldUpdateSheetCornerRadius) {
shouldUpdateSheetCornerRadius = false
onSheetCornerRadiusChange()
}
}
internal fun onSheetCornerRadiusChange() {
if (stackPresentation !== StackPresentation.FORM_SHEET || background == null) {
return
}
(background as? MaterialShapeDrawable?)?.let {
val resolvedCornerRadius = PixelUtil.toDIPFromPixel(sheetCornerRadius)
it.shapeAppearanceModel =
ShapeAppearanceModel
.Builder()
.apply {
setTopLeftCorner(CornerFamily.ROUNDED, resolvedCornerRadius)
setTopRightCorner(CornerFamily.ROUNDED, resolvedCornerRadius)
}.build()
}
}
enum class StackPresentation {
PUSH,
MODAL,
TRANSPARENT_MODAL,
FORM_SHEET,
}
enum class StackAnimation {
DEFAULT,
NONE,
FADE,
SLIDE_FROM_BOTTOM,
SLIDE_FROM_RIGHT,
SLIDE_FROM_LEFT,
FADE_FROM_BOTTOM,
IOS_FROM_RIGHT,
IOS_FROM_LEFT,
}
enum class ReplaceAnimation {
PUSH,
POP,
}
enum class ActivityState {
INACTIVE,
TRANSITIONING_OR_BELOW_TOP,
ON_TOP,
}
enum class WindowTraits {
ORIENTATION,
COLOR,
STYLE,
TRANSLUCENT,
HIDDEN,
ANIMATED,
NAVIGATION_BAR_COLOR,
NAVIGATION_BAR_TRANSLUCENT,
NAVIGATION_BAR_HIDDEN,
}
companion object {
const val TAG = "Screen"
/**
* This value describes value in sheet detents array that will be treated as `fitToContents` option.
*/
const val SHEET_FIT_TO_CONTENTS = -1.0
}
}
internal fun View.asScreen() = this as Screen