@onekeyfe/react-native-pager-view 6.7.0-4 → 6.7.0-40

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 (52) hide show
  1. package/README.md +21 -0
  2. package/android/build.gradle +1 -0
  3. package/android/src/androidTest/AndroidManifest.xml +7 -0
  4. package/android/src/androidTest/java/com/reactnativepagerview/CollapsiblePagerTabReservationTest.kt +476 -0
  5. package/android/src/main/java/com/reactnativepagerview/CollapsiblePagerHost.kt +233 -43
  6. package/android/src/main/java/com/reactnativepagerview/CollapsiblePagerNativeHeaders.kt +596 -27
  7. package/android/src/main/java/com/reactnativepagerview/CollapsiblePagerViewManager.kt +40 -0
  8. package/android/src/main/java/com/reactnativepagerview/NativeScrollerScrollAwayInsets.kt +19 -0
  9. package/android/src/main/java/com/reactnativepagerview/NativeScrollerView.kt +50 -6
  10. package/android/src/main/java/com/reactnativepagerview/NestedScrollableHost.kt +8 -2
  11. package/android/src/main/java/com/reactnativepagerview/event/NativeHeaderPressEvent.kt +8 -0
  12. package/android/src/test/java/com/reactnativepagerview/NativeScrollerScrollAwayInsetsTest.kt +40 -0
  13. package/docs/SPEC.md +700 -10
  14. package/ios/RNCCollapsiblePagerViewComponentView.mm +2221 -94
  15. package/ios/RNCNativeScrollerComponentView.h +2 -0
  16. package/ios/RNCNativeScrollerComponentView.mm +42 -0
  17. package/ios/tests/RNCCollapsiblePagerFirstFrameTests.mm +562 -1
  18. package/ios/tests/RNCCollapsiblePagerHitTestTests.mm +1116 -0
  19. package/ios/tests/RNCCollapsiblePagerPressCancellationTests.mm +74 -9
  20. package/ios/tests/RNCCollapsiblePagerRefreshControlTests.mm +50 -0
  21. package/lib/module/CollapsiblePagerView.js +75 -11
  22. package/lib/module/CollapsiblePagerView.web.js +653 -8
  23. package/lib/module/CollapsiblePagerViewNativeComponent.ts +57 -42
  24. package/lib/module/NativeScroller.web.js +3 -0
  25. package/lib/module/NativeScrollerNativeComponent.ts +42 -46
  26. package/lib/module/PagerViewNativeComponent.ts +20 -25
  27. package/lib/module/WebCollapsiblePagerPage.js +7 -2
  28. package/lib/module/index.native.js +9 -0
  29. package/lib/typescript/src/CollapsiblePagerView.d.ts +62 -4
  30. package/lib/typescript/src/CollapsiblePagerView.web.d.ts +56 -2
  31. package/lib/typescript/src/CollapsiblePagerViewNativeComponent.d.ts +54 -38
  32. package/lib/typescript/src/NativeScroller.d.ts +5 -3
  33. package/lib/typescript/src/NativeScroller.web.d.ts +1 -1
  34. package/lib/typescript/src/NativeScrollerNativeComponent.d.ts +47 -48
  35. package/lib/typescript/src/PagerView.d.ts +1 -1
  36. package/lib/typescript/src/PagerView.web.d.ts +1 -1
  37. package/lib/typescript/src/PagerViewNativeComponent.d.ts +20 -21
  38. package/lib/typescript/src/WebCollapsiblePagerPage.d.ts +1 -1
  39. package/lib/typescript/src/index.native.d.ts +13 -0
  40. package/lib/typescript/src/usePagerView.d.ts +2 -2
  41. package/lib/typescript/src/utils.d.ts +2 -2
  42. package/package.json +22 -7
  43. package/react-native-pager-view.podspec +1 -0
  44. package/src/CollapsiblePagerView.tsx +208 -12
  45. package/src/CollapsiblePagerView.web.tsx +587 -7
  46. package/src/CollapsiblePagerViewNativeComponent.ts +57 -42
  47. package/src/NativeScroller.tsx +14 -4
  48. package/src/NativeScroller.web.tsx +15 -1
  49. package/src/NativeScrollerNativeComponent.ts +42 -46
  50. package/src/PagerViewNativeComponent.ts +20 -25
  51. package/src/WebCollapsiblePagerPage.tsx +7 -2
  52. package/src/index.native.tsx +30 -0
@@ -70,6 +70,10 @@ class CollapsiblePagerViewManager : ViewGroupManager<CollapsiblePagerHost>(),
70
70
  }
71
71
  }
72
72
  })
73
+ host.onNativeToolbarPress = { key, rect ->
74
+ dispatch(reactContext, host, NativeHeaderPressEvent(host.id, 0, key,
75
+ NativeHeaderPressEvent.TOOLBAR_EVENT_NAME, rect))
76
+ }
73
77
  host.onNativeTabPress = { position, key ->
74
78
  dispatch(
75
79
  reactContext,
@@ -206,6 +210,9 @@ class CollapsiblePagerViewManager : ViewGroupManager<CollapsiblePagerHost>(),
206
210
  view?.stickyHeaderHeightPx = PixelUtil.toPixelFromDIP(value.toDouble()).roundToInt()
207
211
  }
208
212
 
213
+ @ReactProp(name = "stickyHeaderHeights")
214
+ override fun setStickyHeaderHeights(view: CollapsiblePagerHost?, value: String?) = Unit
215
+
209
216
  // The scroll-edge effect and external overlay are UIKit-only.
210
217
  @ReactProp(name = "topScrollEdgeEffect")
211
218
  override fun setTopScrollEdgeEffect(view: CollapsiblePagerHost?, value: String?) = Unit
@@ -227,6 +234,11 @@ class CollapsiblePagerViewManager : ViewGroupManager<CollapsiblePagerHost>(),
227
234
  view?.retainedPages = value ?: "[]"
228
235
  }
229
236
 
237
+ @ReactProp(name = "nativeTabBarConfig")
238
+ override fun setNativeTabBarConfig(view: CollapsiblePagerHost?, value: String?) {
239
+ view?.updateNativeTabBarConfig(value)
240
+ }
241
+
230
242
  @ReactProp(name = "nativeTabBarItems")
231
243
  override fun setNativeTabBarItems(view: CollapsiblePagerHost?, value: String?) {
232
244
  view?.updateNativeTabBarItems(value)
@@ -239,6 +251,18 @@ class CollapsiblePagerViewManager : ViewGroupManager<CollapsiblePagerHost>(),
239
251
  view.applyNativeHeaderStyle()
240
252
  }
241
253
 
254
+ @ReactProp(name = "nativeTabBarVariant")
255
+ override fun setNativeTabBarVariant(view: CollapsiblePagerHost?, value: String?) {
256
+ view ?: return
257
+ view.nativeTabBarVariant = if (value == "segmented" || value == "text" || value == "pill") value else "underline"
258
+ view.applyNativeHeaderStyle()
259
+ }
260
+
261
+ @ReactProp(name = "nativeTabBarTrailingReservedWidth", defaultDouble = 0.0)
262
+ override fun setNativeTabBarTrailingReservedWidth(view: CollapsiblePagerHost?, value: Double) {
263
+ view?.nativeTabBarTrailingReservedWidth = value
264
+ }
265
+
242
266
  @ReactProp(name = "nativeTabBarContentPaddingHorizontal")
243
267
  override fun setNativeTabBarContentPaddingHorizontal(
244
268
  view: CollapsiblePagerHost?,
@@ -249,6 +273,13 @@ class CollapsiblePagerViewManager : ViewGroupManager<CollapsiblePagerHost>(),
249
273
  view.applyNativeHeaderStyle()
250
274
  }
251
275
 
276
+ @ReactProp(name = "nativeTabBarContentPaddingVertical")
277
+ override fun setNativeTabBarContentPaddingVertical(view: CollapsiblePagerHost?, value: Double) {
278
+ view ?: return
279
+ view.nativeTabBarContentPaddingVertical = if (value.isFinite()) value.coerceAtLeast(0.0) else 0.0
280
+ view.applyNativeHeaderStyle()
281
+ }
282
+
252
283
  @ReactProp(name = "nativeTabBarItemSpacing")
253
284
  override fun setNativeTabBarItemSpacing(view: CollapsiblePagerHost?, value: Double) {
254
285
  view ?: return
@@ -343,6 +374,14 @@ class CollapsiblePagerViewManager : ViewGroupManager<CollapsiblePagerHost>(),
343
374
  view?.scrollToTop(target, animated)
344
375
  }
345
376
 
377
+ override fun scrollHeaderBy(view: CollapsiblePagerHost?, delta: Double) {
378
+ view?.scrollHeaderBy(delta)
379
+ }
380
+
381
+ override fun scrollHeaderToOffset(view: CollapsiblePagerHost?, offset: Double) {
382
+ view?.scrollHeaderToOffset(offset)
383
+ }
384
+
346
385
  override fun setPage(view: CollapsiblePagerHost?, selectedPage: Int) {
347
386
  if (view != null && selectedPage in 0 until view.adapter.itemCount) {
348
387
  view.beginPageTransition(selectedPage)
@@ -377,6 +416,7 @@ class CollapsiblePagerViewManager : ViewGroupManager<CollapsiblePagerHost>(),
377
416
  CollapsibleStateChangedEvent.EVENT_NAME,
378
417
  MapBuilder.of("registrationName", "onCollapsibleStateChanged"),
379
418
  )
419
+ .put(NativeHeaderPressEvent.TOOLBAR_EVENT_NAME, MapBuilder.of("registrationName", "onNativeToolbarPress"))
380
420
  .put(
381
421
  NativeHeaderPressEvent.TAB_EVENT_NAME,
382
422
  MapBuilder.of("registrationName", "onNativeTabPress"),
@@ -39,3 +39,22 @@ internal class NativeRefreshForegroundAlpha(initialAlpha: Float) {
39
39
  return lastApplied
40
40
  }
41
41
  }
42
+
43
+ // Skip ViewPager2 RecyclerView layout when explicitly suppressed or when all React page
44
+ // children have been removed on unmount while rendered ViewHolder views remain attached.
45
+ internal fun shouldSuppressCollapsiblePagerRecyclerLayout(
46
+ isLayoutSuppressed: Boolean,
47
+ adapterItemCount: Int,
48
+ attachedPageCount: Int,
49
+ ): Boolean = isLayoutSuppressed || (adapterItemCount <= 0 && attachedPageCount > 0)
50
+
51
+ internal fun shouldDrainCollapsiblePagerLayout(
52
+ isLayoutRequested: Boolean,
53
+ isLayoutSuppressed: Boolean,
54
+ isComputingLayout: Boolean,
55
+ width: Int,
56
+ height: Int,
57
+ adapterItemCount: Int,
58
+ ): Boolean = adapterItemCount > 0 && isLayoutRequested && !isLayoutSuppressed &&
59
+ !isComputingLayout && width > 0 && height > 0
60
+
@@ -37,26 +37,50 @@ class NativeScrollerView(context: Context) : FrameLayout(context) {
37
37
  setMeasuredDimension(MeasureSpec.getSize(widthSpec), MeasureSpec.getSize(heightSpec))
38
38
  }
39
39
  override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) = Unit
40
+
41
+ override fun startViewTransition(view: View) {
42
+ super.startViewTransition(view)
43
+ if (view === reactChild) transitioningReactChild = view
44
+ }
45
+
46
+ override fun endViewTransition(view: View) {
47
+ super.endViewTransition(view)
48
+ if (transitioningReactChild === view) transitioningReactChild = null
49
+ if (retainedReactChild === view) {
50
+ clearRetainedGeometry()
51
+ if (isAttachedToWindow) scheduleLayout()
52
+ }
53
+ }
40
54
  }
41
55
  private val contentFrame = object : FrameLayout(context) {
42
56
  override fun onMeasure(widthSpec: Int, heightSpec: Int) {
43
57
  setMeasuredDimension(
44
- max(MeasureSpec.getSize(widthSpec), contentWidthPx + insetLeft + insetRight),
45
- contentHeightPx + contentTop + insetBottom,
58
+ max(MeasureSpec.getSize(widthSpec), effectiveContentWidthPx + insetLeft + insetRight),
59
+ effectiveContentHeightPx + contentTop + insetBottom,
46
60
  )
47
61
  }
48
62
 
49
63
  override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
50
64
  // Only the native origin layer moves. Fabric owns its React child's layout/transform.
65
+ val contentWidth = effectiveContentWidthPx
66
+ val contentHeight = effectiveContentHeightPx
51
67
  contentOriginFrame.measure(
52
- MeasureSpec.makeMeasureSpec(contentWidthPx, MeasureSpec.EXACTLY),
53
- MeasureSpec.makeMeasureSpec(contentHeightPx, MeasureSpec.EXACTLY),
68
+ MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY),
69
+ MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY),
54
70
  )
55
- contentOriginFrame.layout(insetLeft, contentTop, insetLeft + contentWidthPx, contentTop + contentHeightPx)
71
+ contentOriginFrame.layout(insetLeft, contentTop, insetLeft + contentWidth, contentTop + contentHeight)
56
72
  }
57
73
  }
58
74
  private val pagerInsets = WeakHashMap<View, Int>()
59
75
  private var reactChild: View? = null
76
+ private var transitioningReactChild: View? = null
77
+ private var retainedReactChild: View? = null
78
+ private var retainedContentWidthPx = 0
79
+ private var retainedContentHeightPx = 0
80
+ private val effectiveContentWidthPx: Int
81
+ get() = if (reactChild == null && retainedReactChild != null) retainedContentWidthPx else contentWidthPx
82
+ private val effectiveContentHeightPx: Int
83
+ get() = if (reactChild == null && retainedReactChild != null) retainedContentHeightPx else contentHeightPx
60
84
  private var fabricState: StateWrapper? = null
61
85
  private var lastOriginX: Int? = null
62
86
  private var lastOriginY: Int? = null
@@ -172,6 +196,8 @@ class NativeScrollerView(context: Context) : FrameLayout(context) {
172
196
  internal fun addReactChild(child: View, index: Int) {
173
197
  require(index == 0 && reactChild == null) { "NativeScroller requires one React content view" }
174
198
  viewport.stopMotion()
199
+ clearRetainedGeometry()
200
+ transitioningReactChild = null
175
201
  reactChild = child
176
202
  contentOriginFrame.addView(child)
177
203
  scheduleLayout()
@@ -180,13 +206,29 @@ class NativeScrollerView(context: Context) : FrameLayout(context) {
180
206
  internal fun removeReactChild() {
181
207
  viewport.stopMotion()
182
208
  pendingRestore.cancel()
183
- reactChild?.let(contentOriginFrame::removeView)
209
+ reactChild?.let { child ->
210
+ clearRetainedGeometry()
211
+ // Fabric removes the child before its disappearing view finishes drawing.
212
+ if (transitioningReactChild === child &&
213
+ contentOriginFrame.width > 0 && contentOriginFrame.height > 0) {
214
+ retainedReactChild = child
215
+ retainedContentWidthPx = contentOriginFrame.width
216
+ retainedContentHeightPx = contentOriginFrame.height
217
+ }
218
+ contentOriginFrame.removeView(child)
219
+ }
184
220
  reactChild = null
185
221
  contentWidthPx = 0
186
222
  contentHeightPx = 0
187
223
  scheduleLayout()
188
224
  }
189
225
 
226
+ private fun clearRetainedGeometry() {
227
+ retainedReactChild = null
228
+ retainedContentWidthPx = 0
229
+ retainedContentHeightPx = 0
230
+ }
231
+
190
232
  internal fun reactChildCount() = if (reactChild == null) 0 else 1
191
233
  internal fun reactChildAt(index: Int): View {
192
234
  require(index == 0)
@@ -381,6 +423,8 @@ class NativeScrollerView(context: Context) : FrameLayout(context) {
381
423
  lastContentViewportHeight = null
382
424
  viewport.stopMotion()
383
425
  viewport.setTag(R.id.onekey_native_scroll_coordinator_offset_listener, null)
426
+ transitioningReactChild = null
427
+ clearRetainedGeometry()
384
428
  super.onDetachedFromWindow()
385
429
  }
386
430
 
@@ -112,6 +112,10 @@ open class NestedScrollableHost : FrameLayout {
112
112
  else -> 0
113
113
  }
114
114
  if (smoothTouchAxis != 0) {
115
+ if (!nestedNativeGestureStarted) {
116
+ NativeGestureUtil.notifyNativeGestureStarted(this, e)
117
+ nestedNativeGestureStarted = true
118
+ }
115
119
  OneKeyLog.debug("CollapsiblePager", "content-direction owner=${if (smoothTouchAxis == 1) "list" else "horizontal"} dx=$dx dy=$dy nested=${nestedHorizontalOwner != null}")
116
120
  }
117
121
  }
@@ -184,8 +188,10 @@ open class NestedScrollableHost : FrameLayout {
184
188
  val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f
185
189
 
186
190
  if (scaledDx > touchSlop || scaledDy > touchSlop) {
187
- NativeGestureUtil.notifyNativeGestureStarted(this, e)
188
- nestedNativeGestureStarted = true
191
+ if (!nestedNativeGestureStarted) {
192
+ NativeGestureUtil.notifyNativeGestureStarted(this, e)
193
+ nestedNativeGestureStarted = true
194
+ }
189
195
 
190
196
  if (orientation == null) return
191
197
  if (isVpHorizontal == (scaledDy > scaledDx)) {
@@ -10,6 +10,7 @@ class NativeHeaderPressEvent(
10
10
  private val position: Int,
11
11
  private val key: String,
12
12
  private val name: String,
13
+ private val anchorRect: DoubleArray? = null,
13
14
  ) : Event<NativeHeaderPressEvent>(viewTag) {
14
15
  override fun getEventName() = name
15
16
 
@@ -22,9 +23,16 @@ class NativeHeaderPressEvent(
22
23
  private fun serializeEventData(): WritableMap = Arguments.createMap().apply {
23
24
  putInt("position", position)
24
25
  putString("key", key)
26
+ anchorRect?.let { rect ->
27
+ putMap("anchorRect", Arguments.createMap().apply {
28
+ putDouble("x", rect[0]); putDouble("y", rect[1])
29
+ putDouble("width", rect[2]); putDouble("height", rect[3])
30
+ })
31
+ }
25
32
  }
26
33
 
27
34
  companion object {
35
+ const val TOOLBAR_EVENT_NAME = "topNativeToolbarPress"
28
36
  const val TAB_EVENT_NAME = "topNativeTabPress"
29
37
  const val SUB_HEADER_EVENT_NAME = "topNativeSubHeaderPress"
30
38
  }
@@ -72,4 +72,44 @@ fun main() {
72
72
  check(nativeRefreshForegroundTop(37 + 230 + 64, 294f) == 37f)
73
73
  check(nativeRefreshForegroundTop(-40 + 294, 294f) == -40f)
74
74
  println("Refresh foreground policy: repeat, owner transfer/release, external alpha, transparent baseline, caller offset, and combined nested inset passed")
75
+
76
+ // Initial mount before any page ViewHolder is attached still measures ViewPager2.
77
+ check(!shouldSuppressCollapsiblePagerRecyclerLayout(isLayoutSuppressed = false, adapterItemCount = 0, attachedPageCount = 0))
78
+ // Unmount removes all adapter pages while rendered ViewHolder views remain attached.
79
+ check(shouldSuppressCollapsiblePagerRecyclerLayout(isLayoutSuppressed = false, adapterItemCount = 0, attachedPageCount = 1))
80
+ check(shouldSuppressCollapsiblePagerRecyclerLayout(isLayoutSuppressed = true, adapterItemCount = 2, attachedPageCount = 1))
81
+ // Re-adding pages resumes ViewPager2 layout.
82
+ check(!shouldSuppressCollapsiblePagerRecyclerLayout(isLayoutSuppressed = false, adapterItemCount = 1, attachedPageCount = 1))
83
+
84
+ check(
85
+ !shouldDrainCollapsiblePagerLayout(
86
+ isLayoutRequested = true,
87
+ isLayoutSuppressed = false,
88
+ isComputingLayout = false,
89
+ width = 1080,
90
+ height = 2400,
91
+ adapterItemCount = 0,
92
+ ),
93
+ )
94
+ check(
95
+ shouldDrainCollapsiblePagerLayout(
96
+ isLayoutRequested = true,
97
+ isLayoutSuppressed = false,
98
+ isComputingLayout = false,
99
+ width = 1080,
100
+ height = 2400,
101
+ adapterItemCount = 2,
102
+ ),
103
+ )
104
+ check(
105
+ !shouldDrainCollapsiblePagerLayout(
106
+ isLayoutRequested = true,
107
+ isLayoutSuppressed = true,
108
+ isComputingLayout = false,
109
+ width = 1080,
110
+ height = 2400,
111
+ adapterItemCount = 2,
112
+ ),
113
+ )
114
+ println("CollapsiblePager unmount layout guard: initial mount, unmount retention, suppression, and re-add passed")
75
115
  }