react-native-sheet2 6.1.18 → 7.12.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +754 -80
  2. package/{Sheet.podspec → Sheet2.podspec} +2 -2
  3. package/android/build.gradle +2 -25
  4. package/android/check.jks +0 -0
  5. package/android/src/main/AndroidManifest.xml +1 -2
  6. package/android/src/main/cpp/react/renderer/components/SheetViewSpec/SheetViewComponentDescriptor.h +12 -0
  7. package/android/src/main/cpp/react/renderer/components/SheetViewSpec/SheetViewShadowNode.cpp +17 -0
  8. package/android/src/main/cpp/react/renderer/components/SheetViewSpec/SheetViewShadowNode.h +36 -0
  9. package/android/src/main/cpp/react/renderer/components/SheetViewSpec/SheetViewState.h +52 -0
  10. package/android/src/main/java/com/behavior/BottomSheetBehavior.java +84 -16
  11. package/android/src/main/java/com/sheet2/AppFittedSheet.kt +431 -0
  12. package/android/src/main/java/com/{sheet → sheet2}/BaseRNView.kt +38 -31
  13. package/android/src/main/java/com/sheet2/CenteredSheetDialog.kt +272 -0
  14. package/android/src/main/java/com/{sheet → sheet2}/CustomBottomSheetDialog.java +32 -4
  15. package/android/src/main/java/com/{sheet → sheet2}/DialogRootViewGroup.kt +54 -12
  16. package/android/src/main/java/com/sheet2/FragmentModalBottomSheet.kt +161 -0
  17. package/android/src/main/java/com/sheet2/InlineFooterPresenter.kt +74 -0
  18. package/android/src/main/java/com/sheet2/InlineSheetPresenter.kt +249 -0
  19. package/android/src/main/java/com/sheet2/PassThroughLayouts.kt +17 -0
  20. package/android/src/main/java/com/{sheet → sheet2}/SafeAreaUtils.kt +4 -5
  21. package/android/src/main/java/com/sheet2/Sheet2Package.kt +44 -0
  22. package/android/src/main/java/com/sheet2/Sheet2View.kt +5 -0
  23. package/android/src/main/java/com/sheet2/Sheet2ViewManager.kt +163 -0
  24. package/android/src/main/java/com/{sheet → sheet2}/SheetDismissEvent.kt +1 -1
  25. package/android/src/main/java/com/{sheet → sheet2}/SheetModule.kt +14 -3
  26. package/android/src/main/java/com/{sheet → sheet2}/SheetPackage.kt +2 -3
  27. package/android/src/main/java/com/sheet2/SheetTreeLog.kt +78 -0
  28. package/android/src/main/java/com/{sheet → sheet2}/utils.kt +1 -2
  29. package/android/src/main/jni/CMakeLists.txt +72 -0
  30. package/android/src/main/jni/SheetViewSpec.h +40 -0
  31. package/ios/FittedSheets/SheetOptions.swift +5 -0
  32. package/ios/FittedSheets/SheetTransition.swift +83 -4
  33. package/ios/FittedSheets/SheetViewController.swift +222 -24
  34. package/ios/HostFittedSheet.swift +440 -51
  35. package/ios/SheetModule.h +5 -4
  36. package/ios/SheetModule.mm +11 -1
  37. package/ios/SheetTreeLog.swift +52 -0
  38. package/ios/SheetView.mm +118 -31
  39. package/ios/SheetViewComponentDescriptor.h +24 -0
  40. package/ios/SheetViewShadowNode.cpp +17 -0
  41. package/ios/SheetViewShadowNode.h +37 -0
  42. package/ios/SheetViewState.h +54 -0
  43. package/package.json +44 -74
  44. package/react-native.config.js +6 -4
  45. package/src/FittedSheet.tsx +131 -71
  46. package/src/GlobalSheetView.tsx +209 -0
  47. package/src/NativeSheet.ts +4 -0
  48. package/src/PublicSheetView.tsx +2 -1
  49. package/src/SheetTreeLog.ts +39 -0
  50. package/src/SheetViewNativeComponent.ts +25 -4
  51. package/src/index.tsx +34 -4
  52. package/src/logger.ts +16 -0
  53. package/android/generated/java/com/facebook/react/viewmanagers/SheetViewManagerDelegate.java +0 -67
  54. package/android/generated/java/com/facebook/react/viewmanagers/SheetViewManagerInterface.java +0 -27
  55. package/android/generated/java/com/sheet/NativeSheetSpec.java +0 -78
  56. package/android/generated/jni/CMakeLists.txt +0 -36
  57. package/android/generated/jni/RNSheetViewSpec-generated.cpp +0 -50
  58. package/android/generated/jni/RNSheetViewSpec.h +0 -31
  59. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/ComponentDescriptors.cpp +0 -22
  60. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/ComponentDescriptors.h +0 -24
  61. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/EventEmitters.cpp +0 -24
  62. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/EventEmitters.h +0 -25
  63. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/Props.cpp +0 -33
  64. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/Props.h +0 -36
  65. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/RNSheetViewSpecJSI-generated.cpp +0 -46
  66. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/RNSheetViewSpecJSI.h +0 -98
  67. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/ShadowNodes.cpp +0 -17
  68. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/ShadowNodes.h +0 -32
  69. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/States.cpp +0 -16
  70. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/States.h +0 -29
  71. package/android/src/main/AndroidManifestNew.xml +0 -2
  72. package/android/src/main/java/com/sheet/AppFittedSheet.kt +0 -214
  73. package/android/src/main/java/com/sheet/FragmentModalBottomSheet.kt +0 -67
  74. package/android/src/main/java/com/sheet/SheetView.kt +0 -5
  75. package/android/src/main/java/com/sheet/SheetViewManager.kt +0 -106
  76. package/ios/generated/RNSheetViewSpec/ComponentDescriptors.cpp +0 -22
  77. package/ios/generated/RNSheetViewSpec/ComponentDescriptors.h +0 -24
  78. package/ios/generated/RNSheetViewSpec/EventEmitters.cpp +0 -24
  79. package/ios/generated/RNSheetViewSpec/EventEmitters.h +0 -25
  80. package/ios/generated/RNSheetViewSpec/Props.cpp +0 -33
  81. package/ios/generated/RNSheetViewSpec/Props.h +0 -36
  82. package/ios/generated/RNSheetViewSpec/RCTComponentViewHelpers.h +0 -44
  83. package/ios/generated/RNSheetViewSpec/RNSheetViewSpec-generated.mm +0 -60
  84. package/ios/generated/RNSheetViewSpec/RNSheetViewSpec.h +0 -100
  85. package/ios/generated/RNSheetViewSpec/ShadowNodes.cpp +0 -17
  86. package/ios/generated/RNSheetViewSpec/ShadowNodes.h +0 -32
  87. package/ios/generated/RNSheetViewSpec/States.cpp +0 -16
  88. package/ios/generated/RNSheetViewSpec/States.h +0 -29
  89. package/ios/generated/RNSheetViewSpecJSI-generated.cpp +0 -46
  90. package/ios/generated/RNSheetViewSpecJSI.h +0 -98
  91. package/lib/module/FittedSheet.js +0 -153
  92. package/lib/module/FittedSheet.js.map +0 -1
  93. package/lib/module/NativeSheet.ts +0 -17
  94. package/lib/module/PublicSheetView.js +0 -48
  95. package/lib/module/PublicSheetView.js.map +0 -1
  96. package/lib/module/SheetViewNativeComponent.ts +0 -31
  97. package/lib/module/index.js +0 -13
  98. package/lib/module/index.js.map +0 -1
  99. package/lib/module/package.json +0 -1
  100. package/lib/typescript/package.json +0 -1
  101. package/lib/typescript/src/FittedSheet.d.ts +0 -51
  102. package/lib/typescript/src/FittedSheet.d.ts.map +0 -1
  103. package/lib/typescript/src/NativeSheet.d.ts +0 -16
  104. package/lib/typescript/src/NativeSheet.d.ts.map +0 -1
  105. package/lib/typescript/src/PublicSheetView.d.ts +0 -12
  106. package/lib/typescript/src/PublicSheetView.d.ts.map +0 -1
  107. package/lib/typescript/src/SheetViewNativeComponent.d.ts +0 -22
  108. package/lib/typescript/src/SheetViewNativeComponent.d.ts.map +0 -1
  109. package/lib/typescript/src/index.d.ts +0 -8
  110. package/lib/typescript/src/index.d.ts.map +0 -1
@@ -0,0 +1,272 @@
1
+ package com.sheet2
2
+
3
+ import android.animation.ValueAnimator
4
+ import android.app.Dialog
5
+ import android.content.Context
6
+ import android.graphics.Rect
7
+ import android.view.Gravity
8
+ import android.view.MotionEvent
9
+ import android.view.View
10
+ import android.view.ViewConfiguration
11
+ import android.view.ViewGroup
12
+ import android.view.Window
13
+ import android.view.WindowManager
14
+ import android.widget.FrameLayout
15
+ import kotlin.math.abs
16
+ import kotlin.math.max
17
+
18
+ /**
19
+ * Presents [contentView] as a floating card in the vertical center of the
20
+ * screen with a full-screen window dim. Dismissed by swiping the card down or
21
+ * (when [dismissable]) tapping the dimmed background.
22
+ *
23
+ * Deliberately does NOT use BottomSheetBehavior — a plain centered Dialog plus a
24
+ * small vertical-drag gesture keeps the shared bottom-sheet path untouched.
25
+ *
26
+ * Parity target (iOS): while dragging the card down the whole-screen dim fades
27
+ * in sync with the card position; releasing past a distance/velocity threshold
28
+ * slides the card fully off the bottom while the dim fades to zero, otherwise
29
+ * the card springs back and the dim restores. Because [contentView] hosts a live
30
+ * React view tree that consumes its own touches, the drag is detected by a
31
+ * custom [DragDismissLayout] that intercepts vertical drags (leaving taps to the
32
+ * React children) and treats taps on the dim area as a dismiss request.
33
+ */
34
+ class CenteredSheetDialog(
35
+ context: Context,
36
+ private val contentView: View,
37
+ private val dismissable: Boolean,
38
+ private val slide: Boolean,
39
+ private val onDismiss: () -> Unit,
40
+ ) {
41
+ private val dialog = Dialog(context)
42
+ val isShowing: Boolean get() = dialog.isShowing
43
+
44
+ private val maxDim = 0.5f
45
+ private var dismissing = false
46
+
47
+ init {
48
+ dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
49
+
50
+ val container = DragDismissLayout(context)
51
+ val lp = FrameLayout.LayoutParams(
52
+ ViewGroup.LayoutParams.WRAP_CONTENT,
53
+ ViewGroup.LayoutParams.WRAP_CONTENT,
54
+ ).apply { gravity = Gravity.CENTER }
55
+ container.addView(contentView, lp)
56
+ container.card = contentView
57
+ dialog.setContentView(container)
58
+
59
+ dialog.window?.apply {
60
+ setBackgroundDrawableResource(android.R.color.transparent)
61
+ setLayout(
62
+ WindowManager.LayoutParams.MATCH_PARENT,
63
+ WindowManager.LayoutParams.MATCH_PARENT,
64
+ )
65
+ setGravity(Gravity.CENTER)
66
+ // Full-screen dim behind the card.
67
+ addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
68
+ setDimAmount(maxDim)
69
+ // Enter/exit window animation: reuse existing fade/slide anim styles.
70
+ attributes = attributes.apply {
71
+ windowAnimations = if (slide) {
72
+ R.style.Custom_Theme_FullScreenDialog_Slide
73
+ } else {
74
+ R.style.Custom_Theme_FullScreenDialog_Fade
75
+ }
76
+ }
77
+ }
78
+
79
+ // Back-button dismissal only; tap-outside is handled by DragDismissLayout
80
+ // because a MATCH_PARENT window never reports a touch as "outside".
81
+ dialog.setCancelable(dismissable)
82
+ dialog.setCanceledOnTouchOutside(false)
83
+ dialog.setOnCancelListener {
84
+ // Back button: suppress the window's own exit animation to avoid the
85
+ // flicker (also dodges the shared fade_out resource being a fade-in).
86
+ dialog.window?.setWindowAnimations(0)
87
+ }
88
+ // Single teardown point: fires exactly once per show/dismiss cycle for EVERY
89
+ // dismissal path (swipe, tap-outside, programmatic hide, back button), so the
90
+ // host view is always detached and JS always receives the dismiss event.
91
+ dialog.setOnDismissListener { onDismiss() }
92
+ }
93
+
94
+ fun show() = dialog.show()
95
+
96
+ fun dismiss() {
97
+ if (!dialog.isShowing || dismissing) return
98
+ // Animate the card off the bottom (parity with a swipe / tap-outside
99
+ // dismiss) instead of letting the window fade out — the window exit
100
+ // animation flickered.
101
+ animateDismiss(contentView, contentView.resources.displayMetrics.heightPixels.toFloat())
102
+ }
103
+
104
+ /**
105
+ * Tears the dialog down after the custom exit animation has finished. Disables
106
+ * the window's own exit animation first: the slide-off + dim fade already did
107
+ * the visual exit, so the window transition would only flash on top of it
108
+ * (the shared fade_out resource is actually a fade-in).
109
+ */
110
+ private fun finishDialog() {
111
+ // onDismiss() is dispatched by setOnDismissListener, so just close the window.
112
+ dialog.window?.setWindowAnimations(0)
113
+ if (dialog.isShowing) dialog.dismiss()
114
+ }
115
+
116
+ private fun setDim(amount: Float) {
117
+ dialog.window?.let { w ->
118
+ w.attributes = w.attributes.apply { dimAmount = amount.coerceIn(0f, maxDim) }
119
+ }
120
+ }
121
+
122
+ private fun animateDismiss(card: View, screenHeight: Float) {
123
+ if (dismissing) return
124
+ dismissing = true
125
+ val startY = card.translationY
126
+ val endY = screenHeight // slide fully off the bottom
127
+ val startDim = dialog.window?.attributes?.dimAmount ?: maxDim
128
+ ValueAnimator.ofFloat(0f, 1f).apply {
129
+ duration = 200
130
+ addUpdateListener { a ->
131
+ val f = a.animatedValue as Float
132
+ card.translationY = startY + (endY - startY) * f
133
+ setDim(startDim * (1f - f))
134
+ }
135
+ addListener(object : android.animation.AnimatorListenerAdapter() {
136
+ override fun onAnimationEnd(animation: android.animation.Animator) {
137
+ finishDialog()
138
+ }
139
+ })
140
+ start()
141
+ }
142
+ }
143
+
144
+ private fun animateSpringBack(card: View) {
145
+ val startY = card.translationY
146
+ val startDim = dialog.window?.attributes?.dimAmount ?: maxDim
147
+ ValueAnimator.ofFloat(0f, 1f).apply {
148
+ duration = 180
149
+ addUpdateListener { a ->
150
+ val f = a.animatedValue as Float
151
+ card.translationY = startY * (1f - f)
152
+ setDim(startDim + (maxDim - startDim) * f)
153
+ }
154
+ start()
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Full-screen container that owns the centered [card]. It intercepts a
160
+ * downward drag started on the card (so the card can follow the finger and be
161
+ * flung away) while letting ordinary taps reach the React children, and treats
162
+ * a tap that lands off the card as a dim-background dismiss.
163
+ */
164
+ private inner class DragDismissLayout(context: Context) : FrameLayout(context) {
165
+ var card: View? = null
166
+
167
+ private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
168
+ private val density = resources.displayMetrics.density
169
+ private val screenHeight = resources.displayMetrics.heightPixels.toFloat()
170
+ private val threshold = density * 96f // ~96dp drag distance to dismiss
171
+ // Distance over which the dim fully fades while dragging — keeps the fade
172
+ // roughly in sync with the card sliding toward the bottom edge.
173
+ private val fadeDistance = max(screenHeight * 0.5f, threshold * 2f)
174
+
175
+ private var downX = 0f
176
+ private var downY = 0f
177
+ private var downInsideCard = false
178
+ private var dragging = false
179
+ private var lastY = 0f
180
+ private var lastT = 0L
181
+ private var velocity = 0f
182
+ private val hitRect = Rect()
183
+
184
+ private fun cardContains(x: Float, y: Float): Boolean {
185
+ val c = card ?: return false
186
+ c.getHitRect(hitRect)
187
+ // getHitRect already accounts for translationY.
188
+ return hitRect.contains(x.toInt(), y.toInt())
189
+ }
190
+
191
+ override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
192
+ when (ev.actionMasked) {
193
+ MotionEvent.ACTION_DOWN -> {
194
+ downX = ev.x
195
+ downY = ev.y
196
+ downInsideCard = cardContains(ev.x, ev.y)
197
+ dragging = false
198
+ lastY = ev.rawY
199
+ lastT = ev.eventTime
200
+ velocity = 0f
201
+ }
202
+ MotionEvent.ACTION_MOVE -> {
203
+ if (dismissing) return true
204
+ val dy = ev.y - downY
205
+ val dx = ev.x - downX
206
+ if (downInsideCard && dy > touchSlop && dy > abs(dx)) {
207
+ dragging = true
208
+ return true // start owning the gesture; children get CANCEL
209
+ }
210
+ }
211
+ }
212
+ return dragging
213
+ }
214
+
215
+ override fun onTouchEvent(ev: MotionEvent): Boolean {
216
+ when (ev.actionMasked) {
217
+ MotionEvent.ACTION_DOWN -> {
218
+ // Reached here for taps on the dim area (no child consumed the down).
219
+ downX = ev.x
220
+ downY = ev.y
221
+ downInsideCard = cardContains(ev.x, ev.y)
222
+ return true
223
+ }
224
+ MotionEvent.ACTION_MOVE -> {
225
+ if (dismissing) return true
226
+ if (dragging) {
227
+ val dt = (ev.eventTime - lastT).coerceAtLeast(1L)
228
+ velocity = (ev.rawY - lastY) / dt * 1000f
229
+ lastY = ev.rawY
230
+ lastT = ev.eventTime
231
+ val dy = ev.y - downY
232
+ val card = card
233
+ if (card != null && dy > 0) {
234
+ card.translationY = dy
235
+ setDim(maxDim * (1f - dy / fadeDistance))
236
+ }
237
+ }
238
+ return true
239
+ }
240
+ MotionEvent.ACTION_UP -> {
241
+ val card = card
242
+ if (dragging && card != null) {
243
+ val dy = ev.y - downY
244
+ val fling = velocity > density * 1200f
245
+ if (dismissable && (dy > threshold || fling)) {
246
+ animateDismiss(card, screenHeight)
247
+ } else {
248
+ animateSpringBack(card)
249
+ }
250
+ } else if (!downInsideCard && dismissable && !dismissing) {
251
+ // Tap on the dimmed background — slide the card off the bottom (parity
252
+ // with iOS) instead of the flickering window exit animation.
253
+ val moved = abs(ev.x - downX) > touchSlop || abs(ev.y - downY) > touchSlop
254
+ val c = card
255
+ if (!moved && c != null) {
256
+ animateDismiss(c, screenHeight)
257
+ }
258
+ }
259
+ dragging = false
260
+ return true
261
+ }
262
+ MotionEvent.ACTION_CANCEL -> {
263
+ val card = card
264
+ if (dragging && card != null) animateSpringBack(card)
265
+ dragging = false
266
+ return true
267
+ }
268
+ }
269
+ return super.onTouchEvent(ev)
270
+ }
271
+ }
272
+ }
@@ -1,7 +1,8 @@
1
- package com.sheet;
1
+ package com.sheet2;
2
2
 
3
3
  import android.annotation.SuppressLint;
4
4
  import android.util.TypedValue;
5
+ import android.view.MotionEvent;
5
6
  import android.view.View;
6
7
  import android.view.ViewGroup;
7
8
  import android.widget.FrameLayout;
@@ -20,6 +21,7 @@ public class CustomBottomSheetDialog extends AppCompatDialog {
20
21
  private FrameLayout container;
21
22
  boolean dismissWithAnimation;
22
23
  boolean cancelable = true;
24
+ public boolean dismissOnOverlayTap = true;
23
25
 
24
26
  protected int startState = BottomSheetBehavior.STATE_EXPANDED;
25
27
 
@@ -190,12 +192,38 @@ public class CustomBottomSheetDialog extends AppCompatDialog {
190
192
  coordinator
191
193
  .findViewById(R.id.touch_outside)
192
194
  .setOnClickListener(
193
- view12 -> {if (cancelable && isShowing()) cancel();});
194
- // Handle accessibility events
195
- bottomSheet.setOnTouchListener((view1, event) -> true);
195
+ view12 -> {if (cancelable && dismissOnOverlayTap && isShowing()) cancel();});
196
+ // The bottom sheet container spans the full width, but the card content can be
197
+ // narrower and horizontally centered (maxWidth / landscape). A blanket
198
+ // `event -> true` here swallows taps that land in the dim strips beside the
199
+ // card so they never reach `touch_outside`, meaning left/right taps couldn't
200
+ // dismiss (only taps above the sheet did). Treat a tap that lands outside the
201
+ // card's on-screen bounds as a tap-outside and cancel; taps on the card are
202
+ // consumed as before.
203
+ final View card = view;
204
+ bottomSheet.setOnTouchListener((view1, event) -> {
205
+ if (event.getActionMasked() == MotionEvent.ACTION_UP
206
+ && cancelable && dismissOnOverlayTap && isShowing() && card != null
207
+ && !isPointInsideCard(card, event.getRawX(), event.getRawY())) {
208
+ cancel();
209
+ }
210
+ return true;
211
+ });
196
212
  return container;
197
213
  }
198
214
 
215
+ /**
216
+ * Whether the raw (screen) touch point falls within the card's on-screen box.
217
+ * Uses getLocationOnScreen so it stays correct regardless of the card's
218
+ * horizontal centering offset (translationX).
219
+ */
220
+ private static boolean isPointInsideCard(@NonNull View card, float rawX, float rawY) {
221
+ int[] loc = new int[2];
222
+ card.getLocationOnScreen(loc);
223
+ return rawX >= loc[0] && rawX <= loc[0] + card.getWidth()
224
+ && rawY >= loc[1] && rawY <= loc[1] + card.getHeight();
225
+ }
226
+
199
227
  private static int getThemeResId(@NonNull android.content.Context context, int themeId) {
200
228
  if (themeId == 0) {
201
229
  // If the provided theme is 0, then retrieve the dialogTheme from our theme
@@ -1,4 +1,4 @@
1
- package com.sheet
1
+ package com.sheet2
2
2
 
3
3
  import android.content.Context
4
4
  import android.content.res.Resources
@@ -6,21 +6,36 @@ import android.graphics.Outline
6
6
  import android.os.Looper
7
7
  import android.view.View
8
8
  import android.view.ViewOutlineProvider
9
- import androidx.appcompat.app.AppCompatActivity
10
- import androidx.core.view.ViewCompat
11
- import com.facebook.react.bridge.ReactContext
12
- import com.facebook.react.uimanager.PixelUtil
13
- import com.facebook.react.uimanager.PixelUtil.pxToDp
14
9
  import kotlin.math.max
15
10
  import kotlin.math.min
16
11
 
17
12
  class DialogRootViewGroup(context: Context) : BaseRNView(context) {
18
13
  private var reactView: View? = null
14
+ var onSheetLayoutChanged: (() -> Unit)? = null
19
15
 
20
16
  var sheetMaxHeightSize = Float.MAX_VALUE
21
17
  var sheetMaxWidthSize = Float.MAX_VALUE
22
18
  var sheetMinHeightSize = Float.MIN_VALUE
23
19
 
20
+ /**
21
+ * When true the card is horizontally centered by its parent container (the
22
+ * centered Dialog's FrameLayout via Gravity.CENTER), so this view must NOT
23
+ * additionally offset itself through [translationX]. Applying both double-
24
+ * shifts the card to the right in landscape (where widthPixels >> card width).
25
+ * The bottom-sheet path keeps translationX centering because there the RN view
26
+ * sits left-aligned inside a full-width container.
27
+ */
28
+ var centeredPresentation: Boolean = false
29
+ set(value) {
30
+ field = value
31
+ if (value) translationX = 0f
32
+ layout()
33
+ }
34
+
35
+ private fun centeringTranslationX(newWidth: Int): Float =
36
+ if (centeredPresentation) 0f
37
+ else ((metrics.displayMetrics.widthPixels - newWidth) / 2).toFloat()
38
+
24
39
  private val metrics: Resources by lazy { Resources.getSystem() }
25
40
 
26
41
  fun setCornerRadius(r: Float) {
@@ -60,47 +75,74 @@ class DialogRootViewGroup(context: Context) : BaseRNView(context) {
60
75
  }
61
76
  }
62
77
 
78
+ private fun notifySheetLayoutChanged() {
79
+ post { onSheetLayoutChanged?.invoke() }
80
+ }
81
+
63
82
  fun setVirtualHeight(h: Float) {
64
83
  if (reactView == null) return
65
84
  this.sheetMaxHeightSize = h
66
85
  if (sheetMaxHeightSize == Float.MAX_VALUE) return
67
86
  val newHeight = allowedHeight
68
87
  val newWidth = allowedWidth
69
- println("😀 DialogRootViewGroup.setVirtualHeight ${newHeight.pxToDp()} :${newWidth.pxToDp()}")
70
88
  ensureLayoutParams()
71
89
  layoutParams?.height = newHeight
72
- translationX = ((metrics.displayMetrics.widthPixels - newWidth) / 2).toFloat()
90
+ translationX = centeringTranslationX(newWidth)
73
91
  layoutParams?.width = newWidth
74
92
  layout()
93
+ notifySheetLayoutChanged()
75
94
  }
76
95
 
77
96
  fun updateMaxWidth(value: Float) {
78
97
  sheetMaxWidthSize = value
79
98
  val newWidth = allowedWidth
80
- translationX = ((metrics.displayMetrics.widthPixels - newWidth) / 2).toFloat()
99
+ translationX = centeringTranslationX(newWidth)
81
100
  layoutParams?.width = newWidth
82
101
  layout()
102
+ notifySheetLayoutChanged()
83
103
  }
84
104
 
85
105
  override fun addView(child: View, index: Int, params: LayoutParams) {
86
- println("😀 DialogRootViewGroup.addView ${child.id}")
87
- if (reactView != null) removeView(reactView)
106
+ // Dropping the previous child here is a removal React Native never hears
107
+ // about: it goes on counting that child, and its next insert lands at an
108
+ // index the group no longer has.
109
+ val replaced = reactView
110
+ SheetTreeLog.log(
111
+ context,
112
+ "host.addView",
113
+ "child=${SheetTreeLog.tag(child)} index=$index children=${SheetTreeLog.childTags(this)}" +
114
+ " replaces=${SheetTreeLog.tag(replaced)}",
115
+ )
116
+ if (replaced != null) removeView(replaced)
88
117
  super.addView(child, index, params)
89
118
  reactView = child
90
119
  setVirtualHeight(sheetMaxHeightSize)
91
120
  }
92
121
 
93
122
  override fun removeView(view: View?) {
123
+ SheetTreeLog.log(
124
+ context,
125
+ "host.removeView",
126
+ "child=${SheetTreeLog.tag(view)} children=${SheetTreeLog.childTags(this)}",
127
+ )
94
128
  if (view == reactView) releaseReactView()
95
129
  super.removeView(view)
96
130
  }
97
131
 
98
132
  override fun removeViewAt(index: Int) {
133
+ SheetTreeLog.log(
134
+ context,
135
+ "host.removeViewAt",
136
+ "index=$index child=${SheetTreeLog.tag(getChildAt(index))}" +
137
+ " children=${SheetTreeLog.childTags(this)}",
138
+ )
99
139
  if (getChildAt(index) === reactView) releaseReactView()
100
140
  super.removeViewAt(index)
101
141
  }
102
142
 
103
- override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {}
143
+ override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
144
+ notifySheetLayoutChanged()
145
+ }
104
146
 
105
147
  private fun releaseReactView() {
106
148
  sheetMaxHeightSize = Float.MAX_VALUE
@@ -0,0 +1,161 @@
1
+ package com.sheet2
2
+
3
+ import android.app.Dialog
4
+ import android.content.DialogInterface
5
+ import android.graphics.Color
6
+ import android.os.Bundle
7
+ import android.view.LayoutInflater
8
+ import android.view.MotionEvent
9
+ import android.view.View
10
+ import android.view.ViewGroup
11
+ import android.view.Window
12
+ import android.widget.FrameLayout
13
+ import com.google.android.material.bottomsheet.BottomSheetDialogFragment
14
+ import java.lang.ref.WeakReference
15
+
16
+ class FragmentModalBottomSheet() : BottomSheetDialogFragment() {
17
+
18
+ private var modalView: ViewGroup? = null
19
+ private var overlayHost: ViewGroup? = null
20
+ private var overlayView: View? = null
21
+ private var dismissable: Boolean = true
22
+ private var dismissOnOverlayTap: Boolean = true
23
+ private var overlayOpacity: Float = -1F
24
+ private var isSystemUILight: Boolean = false
25
+ private var onDismiss: ((dismissAll: Boolean) -> Unit)? = null
26
+ var dismissAll = false
27
+
28
+ constructor(
29
+ modalView: ViewGroup,
30
+ dismissable: Boolean,
31
+ dismissOnOverlayTap: Boolean,
32
+ overlayOpacity: Float,
33
+ isSystemUILight: Boolean,
34
+ onDismiss: (dismissAll: Boolean) -> Unit
35
+ ) : this() {
36
+ this.modalView = modalView
37
+ this.dismissable = dismissable
38
+ this.dismissOnOverlayTap = dismissOnOverlayTap
39
+ this.overlayOpacity = overlayOpacity
40
+ this.isSystemUILight = isSystemUILight
41
+ this.onDismiss = onDismiss
42
+ }
43
+
44
+ companion object {
45
+ var presentedWindow: WeakReference<Window>? = null
46
+ }
47
+
48
+ override fun onCreateView(
49
+ inflater: LayoutInflater,
50
+ container: ViewGroup?,
51
+ savedInstanceState: Bundle?
52
+ ): View? {
53
+ // If modalView is null (fragment restored by system), dismiss immediately
54
+ if (modalView == null) {
55
+ dismissAllowingStateLoss()
56
+ return null
57
+ }
58
+ return modalView
59
+ }
60
+
61
+ override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
62
+ this.isCancelable = dismissable
63
+ val dialog = CustomBottomSheetDialog(requireContext(), R.style.AppBottomSheetDialog)
64
+ dialog.setSheetBackgroundColor(Color.TRANSPARENT)
65
+ dialog.dismissOnOverlayTap = dismissOnOverlayTap
66
+ if (overlayOpacity >= 0) dialog.window?.setDimAmount(overlayOpacity)
67
+ dialog.window?.let {
68
+ presentedWindow = WeakReference(it)
69
+ it.setStatusBarStyle(isSystemUILight)
70
+ if (isSystemUILight) {
71
+ it.setSystemUIColor(Color.WHITE)
72
+ } else {
73
+ it.setSystemUIColor(Color.BLACK)
74
+ }
75
+ }
76
+ return dialog
77
+ }
78
+
79
+ override fun onStart() {
80
+ super.onStart()
81
+ attachOverlayView()
82
+ }
83
+
84
+ fun setOverlayView(view: View?) {
85
+ if (overlayView == view) return
86
+ detachOverlayView()
87
+ overlayView = view
88
+ attachOverlayView()
89
+ }
90
+
91
+ private fun attachOverlayView() {
92
+ val view = overlayView ?: return
93
+ val decorView = dialog?.window?.decorView as? ViewGroup ?: return
94
+ detachOverlayHost()
95
+ // Same as the inline presenter: the overlay is taken out of whatever parent
96
+ // React Native gave it and put into the dialog's own host.
97
+ val previousParent = view.parent as? ViewGroup
98
+ SheetTreeLog.log(
99
+ view.context,
100
+ "dialog.attachOverlay",
101
+ "overlay=${SheetTreeLog.tag(view)} from=${SheetTreeLog.tag(previousParent)}" +
102
+ " fromChildren=${SheetTreeLog.childTags(previousParent)}",
103
+ )
104
+ previousParent?.removeView(view)
105
+
106
+ val host = DialogPassThroughFrameLayout(decorView.context).apply {
107
+ layoutParams = ViewGroup.LayoutParams(
108
+ ViewGroup.LayoutParams.MATCH_PARENT,
109
+ ViewGroup.LayoutParams.MATCH_PARENT
110
+ )
111
+ isClickable = false
112
+ isFocusable = false
113
+ }
114
+
115
+ view.layoutParams = FrameLayout.LayoutParams(
116
+ FrameLayout.LayoutParams.MATCH_PARENT,
117
+ FrameLayout.LayoutParams.MATCH_PARENT
118
+ )
119
+ host.addView(view)
120
+ decorView.addView(host)
121
+ host.bringToFront()
122
+ overlayHost = host
123
+ }
124
+
125
+ private fun detachOverlayHost() {
126
+ (overlayHost?.parent as? ViewGroup)?.removeView(overlayHost)
127
+ overlayHost = null
128
+ }
129
+
130
+ private fun detachOverlayView() {
131
+ detachOverlayHost()
132
+ (overlayView?.parent as? ViewGroup)?.removeView(overlayView)
133
+ }
134
+
135
+ fun setNewNestedScrollView(view: View) {
136
+ (dialog as CustomBottomSheetDialog).setNewNestedScrollView(view)
137
+ }
138
+
139
+ fun collapse() {
140
+ (dialog as CustomBottomSheetDialog).collapse()
141
+ }
142
+
143
+ fun expand() {
144
+ (dialog as CustomBottomSheetDialog).expand()
145
+ }
146
+
147
+ override fun onDismiss(dialog: DialogInterface) {
148
+ super.onDismiss(dialog)
149
+ detachOverlayView()
150
+ overlayView = null
151
+ presentedWindow?.clear()
152
+ presentedWindow = null
153
+ onDismiss?.invoke(dismissAll)
154
+ }
155
+ }
156
+
157
+ private class DialogPassThroughFrameLayout(context: android.content.Context) : FrameLayout(context) {
158
+ override fun dispatchTouchEvent(ev: MotionEvent): Boolean = false
159
+ override fun onInterceptTouchEvent(ev: MotionEvent): Boolean = false
160
+ override fun onTouchEvent(event: MotionEvent): Boolean = false
161
+ }
@@ -0,0 +1,74 @@
1
+ package com.sheet2
2
+
3
+ import android.view.Gravity
4
+ import android.view.View
5
+ import android.view.ViewGroup
6
+ import android.view.ViewParent
7
+ import android.widget.FrameLayout
8
+
9
+ internal class InlineFooterPresenter(
10
+ private val anchor: AppFittedSheet,
11
+ private val hostView: View,
12
+ ) {
13
+
14
+ private var overlay: FrameLayout? = null
15
+
16
+ val isShown: Boolean get() = overlay != null
17
+
18
+ fun show() {
19
+ if (isShown) return
20
+ val root = findInlineRoot(anchor) ?: return
21
+
22
+ val overlayRoot = PassThroughFrameLayout(anchor.context).apply {
23
+ layoutParams = ViewGroup.LayoutParams(
24
+ ViewGroup.LayoutParams.MATCH_PARENT,
25
+ ViewGroup.LayoutParams.MATCH_PARENT,
26
+ )
27
+ isClickable = false
28
+ isFocusable = false
29
+ }
30
+
31
+ (hostView.parent as? ViewGroup)?.removeView(hostView)
32
+ val size = hostView.layoutParams
33
+ hostView.layoutParams = FrameLayout.LayoutParams(
34
+ size?.width ?: FrameLayout.LayoutParams.MATCH_PARENT,
35
+ size?.height ?: 0,
36
+ Gravity.BOTTOM,
37
+ )
38
+ overlayRoot.addView(hostView)
39
+
40
+ root.addView(overlayRoot)
41
+ overlay = overlayRoot
42
+ footers.add(overlayRoot)
43
+ hostView.post { anchor.pushContentOriginOffset() }
44
+ }
45
+
46
+ fun dismiss() {
47
+ val layout = overlay ?: return
48
+ footers.remove(layout)
49
+ overlay = null
50
+ (layout.parent as? ViewGroup)?.removeView(layout)
51
+ (hostView.parent as? ViewGroup)?.removeView(hostView)
52
+ }
53
+
54
+ companion object {
55
+ private val footers = mutableListOf<View>()
56
+
57
+ fun bringFootersToFront(root: ViewGroup) {
58
+ footers.filter { it.parent === root }.forEach { it.bringToFront() }
59
+ }
60
+ }
61
+ }
62
+
63
+ internal fun findInlineRoot(anchor: View): ViewGroup? {
64
+ var current: ViewParent? = anchor.parent
65
+ var lastGroup: ViewGroup? = null
66
+ while (current != null) {
67
+ if (current is ViewGroup) lastGroup = current
68
+ if (current.javaClass.name == "com.swmansion.rnscreens.Screen") {
69
+ return (current.parent as? ViewGroup) ?: (current as? ViewGroup)
70
+ }
71
+ current = current.parent
72
+ }
73
+ return lastGroup
74
+ }