Skip to content

Commit 212bbc2

Browse files
authored
Merge branch 'next' into @akwasniewski/add-missing-types
2 parents 4b3451d + 215968f commit 212bbc2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1004
-382
lines changed

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,26 @@ open class GestureHandler {
3131
var tag = 0
3232
var view: View? = null
3333
private set
34+
35+
// Host detector view is a reference to a Native Detector designated to handle events from a
36+
// Logic Detector to which the gesture is assigned.
37+
var hostDetectorView: RNGestureHandlerDetectorView? = null
38+
3439
val viewForEvents: RNGestureHandlerDetectorView
3540
get() {
36-
assert(actionType == ACTION_TYPE_NATIVE_DETECTOR) {
41+
assert(usesNativeOrLogicDetector(actionType)) {
3742
"[react-native-gesture-handler] `viewForEvents` can only be used with NativeDetector."
3843
}
3944

40-
val detector = if (this is NativeViewGestureHandler) this.view?.parent else view
45+
val detector = if (actionType ==
46+
ACTION_TYPE_LOGIC_DETECTOR
47+
) {
48+
this.hostDetectorView
49+
} else if (this is NativeViewGestureHandler) {
50+
this.view?.parent
51+
} else {
52+
view
53+
}
4154

4255
if (detector !is RNGestureHandlerDetectorView) {
4356
throw Error(
@@ -1005,6 +1018,7 @@ open class GestureHandler {
10051018
const val ACTION_TYPE_JS_FUNCTION_OLD_API = 3
10061019
const val ACTION_TYPE_JS_FUNCTION_NEW_API = 4
10071020
const val ACTION_TYPE_NATIVE_DETECTOR = 5
1021+
const val ACTION_TYPE_LOGIC_DETECTOR = 6
10081022
const val POINTER_TYPE_TOUCH = 0
10091023
const val POINTER_TYPE_STYLUS = 1
10101024
const val POINTER_TYPE_MOUSE = 2
@@ -1039,6 +1053,9 @@ open class GestureHandler {
10391053
}
10401054
return null
10411055
}
1056+
1057+
fun usesNativeOrLogicDetector(actionType: Int): Boolean =
1058+
actionType == ACTION_TYPE_NATIVE_DETECTOR || actionType == ACTION_TYPE_LOGIC_DETECTOR
10421059
}
10431060

10441061
private data class PointerData(

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerDetectorView.kt

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
1515
private var handlersToAttach: List<Int>? = null
1616
private var nativeHandlers: MutableSet<Int> = mutableSetOf()
1717
private var attachedHandlers: MutableSet<Int> = mutableSetOf()
18+
private var attachedLogicHandlers: MutableMap<Int, MutableSet<Int>> = mutableMapOf()
1819
private var moduleId: Int = -1
1920

21+
data class LogicChildren(val handlerTags: List<Int>, val viewTag: Int)
22+
2023
fun setHandlerTags(handlerTags: ReadableArray?) {
2124
val newHandlers = handlerTags?.toArrayList()?.map { (it as Double).toInt() } ?: emptyList()
2225
if (moduleId == -1) {
@@ -37,6 +40,35 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
3740
handlersToAttach = null
3841
}
3942

43+
fun setLogicChildren(newLogicChildren: ReadableArray?) {
44+
val logicChildrenToDetach = attachedLogicHandlers.keys.toMutableSet()
45+
46+
val mappedChildren = newLogicChildren?.mapLogicChildren().orEmpty()
47+
48+
for (child in mappedChildren) {
49+
if (!attachedLogicHandlers.containsKey(child.viewTag)) {
50+
attachedLogicHandlers[child.viewTag] = mutableSetOf()
51+
}
52+
53+
logicChildrenToDetach.remove(child.viewTag)
54+
55+
attachHandlers(
56+
child.handlerTags,
57+
child.viewTag,
58+
GestureHandler.ACTION_TYPE_LOGIC_DETECTOR,
59+
attachedLogicHandlers[child.viewTag]!!,
60+
)
61+
}
62+
63+
val registry = RNGestureHandlerModule.registries[moduleId]
64+
?: throw Exception("Tried to access a non-existent registry")
65+
66+
for (tag in logicChildrenToDetach) {
67+
registry.detachHandler(tag)
68+
attachedLogicHandlers.remove(tag)
69+
}
70+
}
71+
4072
private fun shouldAttachGestureToChildView(tag: Int): Boolean {
4173
val registry = RNGestureHandlerModule.registries[moduleId]
4274
?: throw Exception("Tried to access a non-existent registry")
@@ -57,39 +89,40 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
5789
super.removeViewAt(index)
5890
}
5991

60-
private fun attachHandlers(newHandlers: List<Int>) {
92+
private fun attachHandlers(
93+
newHandlers: List<Int>,
94+
viewTag: Int = this.id,
95+
actionType: Int = GestureHandler.ACTION_TYPE_NATIVE_DETECTOR,
96+
attachedHandlers: MutableSet<Int> = this.attachedHandlers,
97+
) {
6198
val registry = RNGestureHandlerModule.registries[moduleId]
6299
?: throw Exception("Tried to access a non-existent registry")
63100

64-
val changes = mutableMapOf<Int, GestureHandlerMutation>()
65-
66-
for (tag in attachedHandlers) {
67-
changes[tag] = GestureHandlerMutation.Detach
68-
}
101+
val handlersToDetach = attachedHandlers.toMutableSet()
69102

70103
for (tag in newHandlers) {
71-
changes[tag] = if (changes.containsKey(tag)) GestureHandlerMutation.Keep else GestureHandlerMutation.Attach
72-
}
73-
74-
for (entry in changes) {
75-
val tag = entry.key
76-
77-
if (entry.value == GestureHandlerMutation.Attach) {
104+
handlersToDetach.remove(tag)
105+
if (!attachedHandlers.contains(tag)) {
78106
if (shouldAttachGestureToChildView(tag)) {
79107
// It might happen that `attachHandlers` will be called before children are added into view hierarchy. In that case we cannot
80108
// attach `NativeViewGestureHandlers` here and we have to do it in `addView` method.
81109
nativeHandlers.add(tag)
82110
} else {
83-
registry.attachHandlerToView(tag, this.id, GestureHandler.ACTION_TYPE_NATIVE_DETECTOR)
111+
registry.attachHandlerToView(tag, viewTag, actionType)
112+
if (actionType == GestureHandler.ACTION_TYPE_LOGIC_DETECTOR) {
113+
registry.getHandler(tag)?.hostDetectorView = this
114+
}
84115
attachedHandlers.add(tag)
85116
}
86-
} else if (entry.value == GestureHandlerMutation.Detach) {
87-
registry.detachHandler(tag)
88-
nativeHandlers.remove(tag)
89-
attachedHandlers.remove(tag)
90117
}
91118
}
92119

120+
for (tag in handlersToDetach) {
121+
registry.detachHandler(tag)
122+
nativeHandlers.remove(tag)
123+
attachedHandlers.remove(tag)
124+
}
125+
93126
val child = getChildAt(0)
94127

95128
// This covers the case where `NativeViewGestureHandlers` are attached after child views were created.
@@ -132,13 +165,22 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
132165
registry.detachHandler(tag)
133166
attachedHandlers.remove(tag)
134167
}
135-
}
136168

137-
companion object {
138-
private enum class GestureHandlerMutation {
139-
Attach,
140-
Detach,
141-
Keep,
169+
for (child in attachedLogicHandlers) {
170+
for (tag in child.value) {
171+
registry.detachHandler(tag)
172+
}
173+
child.value.clear()
142174
}
143175
}
176+
177+
private fun ReadableArray.mapLogicChildren(): List<LogicChildren> = List(size()) { i ->
178+
val child = getMap(i) ?: return@List null
179+
val handlerTags = child.getArray("handlerTags")?.toIntList().orEmpty()
180+
val viewTag = child.getInt("viewTag")
181+
182+
LogicChildren(handlerTags, viewTag)
183+
}.filterNotNull()
184+
185+
private fun ReadableArray.toIntList(): List<Int> = List(size()) { getInt(it) }
144186
}

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerDetectorViewManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class RNGestureHandlerDetectorViewManager :
3737
view.setModuleId(value)
3838
}
3939

40+
override fun setLogicChildren(view: RNGestureHandlerDetectorView, value: ReadableArray?) {
41+
view.setLogicChildren(value)
42+
}
43+
4044
override fun onDropViewInstance(view: RNGestureHandlerDetectorView) {
4145
view.onViewDrop()
4246
super.onDropViewInstance(view)

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/events/RNGestureHandlerEvent.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class RNGestureHandlerEvent private constructor() : Event<RNGestureHandlerEvent>
2626
dataBuilder: GestureHandlerEventDataBuilder<T>,
2727
eventHandlerType: EventHandlerType,
2828
) {
29-
val view = if (handler.actionType == GestureHandler.ACTION_TYPE_NATIVE_DETECTOR) {
30-
handler.viewForEvents!!
29+
val view = if (GestureHandler.usesNativeOrLogicDetector(handler.actionType)) {
30+
handler.viewForEvents
3131
} else {
3232
handler.view!!
3333
}
@@ -45,7 +45,7 @@ class RNGestureHandlerEvent private constructor() : Event<RNGestureHandlerEvent>
4545
EVENTS_POOL.release(this)
4646
}
4747

48-
override fun getEventName() = if (actionType == GestureHandler.ACTION_TYPE_NATIVE_DETECTOR) {
48+
override fun getEventName() = if (GestureHandler.usesNativeOrLogicDetector(actionType)) {
4949
if (eventHandlerType == EventHandlerType.ForAnimated) {
5050
NATIVE_DETECTOR_ANIMATED_EVENT_NAME
5151
} else if (eventHandlerType == EventHandlerType.ForReanimated) {
@@ -60,11 +60,11 @@ class RNGestureHandlerEvent private constructor() : Event<RNGestureHandlerEvent>
6060
}
6161

6262
// Unfortunately getCoalescingKey is not considered when sending event to C++, therefore we have to disable coalescing in v3
63-
override fun canCoalesce() = actionType != GestureHandler.ACTION_TYPE_NATIVE_DETECTOR
63+
override fun canCoalesce() = !GestureHandler.usesNativeOrLogicDetector(actionType)
6464

6565
override fun getCoalescingKey() = coalescingKey
6666

67-
override fun getEventData(): WritableMap = if (actionType == GestureHandler.ACTION_TYPE_NATIVE_DETECTOR) {
67+
override fun getEventData(): WritableMap = if (GestureHandler.usesNativeOrLogicDetector(actionType)) {
6868
createNativeEventData(dataBuilder!!)
6969
} else {
7070
createEventData(dataBuilder!!)

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/events/RNGestureHandlerEventDispatcher.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
7171
RNGestureHandlerEvent.createEventData(handlerFactory.createEventBuilder(handler))
7272
sendEventForDeviceEvent(RNGestureHandlerEvent.EVENT_NAME, data)
7373
}
74-
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR -> {
74+
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR, GestureHandler.ACTION_TYPE_LOGIC_DETECTOR -> {
7575
val eventHandlerType = if (handler.dispatchesAnimatedEvents) {
7676
EventHandlerType.ForAnimated
7777
} else if (handler.dispatchesReanimatedEvents) {
@@ -87,7 +87,7 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
8787
eventHandlerType,
8888
)
8989

90-
handler.viewForEvents!!.dispatchEvent(event)
90+
handler.viewForEvents.dispatchEvent(event)
9191
}
9292
}
9393
}
@@ -136,7 +136,7 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
136136
sendEventForDeviceEvent(RNGestureHandlerStateChangeEvent.EVENT_NAME, data)
137137
}
138138

139-
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR -> {
139+
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR, GestureHandler.ACTION_TYPE_LOGIC_DETECTOR -> {
140140
val eventHandlerType = if (handler.dispatchesReanimatedEvents) {
141141
EventHandlerType.ForReanimated
142142
} else {
@@ -152,7 +152,7 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
152152
eventHandlerType,
153153
)
154154

155-
handler.viewForEvents!!.dispatchEvent(event)
155+
handler.viewForEvents.dispatchEvent(event)
156156
}
157157
}
158158
}
@@ -188,15 +188,15 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
188188
val data = RNGestureHandlerTouchEvent.createEventData(handler)
189189
sendEventForDeviceEvent(RNGestureHandlerEvent.EVENT_NAME, data)
190190
}
191-
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR -> {
191+
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR, GestureHandler.ACTION_TYPE_LOGIC_DETECTOR -> {
192192
val eventHandlerType = if (handler.dispatchesReanimatedEvents) {
193193
EventHandlerType.ForReanimated
194194
} else {
195195
EventHandlerType.ForJS
196196
}
197197
val event = RNGestureHandlerTouchEvent.obtain(handler, handler.actionType, eventHandlerType)
198198

199-
handler.viewForEvents!!.dispatchEvent(event)
199+
handler.viewForEvents.dispatchEvent(event)
200200
}
201201
}
202202
}

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/events/RNGestureHandlerStateChangeEvent.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class RNGestureHandlerStateChangeEvent private constructor() : Event<RNGestureHa
2929
dataBuilder: GestureHandlerEventDataBuilder<T>,
3030
eventHandlerType: EventHandlerType,
3131
) {
32-
val view = if (handler.actionType == GestureHandler.ACTION_TYPE_NATIVE_DETECTOR) {
33-
handler.viewForEvents!!
32+
val view = if (GestureHandler.usesNativeOrLogicDetector(handler.actionType)) {
33+
handler.viewForEvents
3434
} else {
3535
handler.view!!
3636
}
@@ -51,7 +51,7 @@ class RNGestureHandlerStateChangeEvent private constructor() : Event<RNGestureHa
5151
EVENTS_POOL.release(this)
5252
}
5353

54-
override fun getEventName() = if (actionType == GestureHandler.ACTION_TYPE_NATIVE_DETECTOR) {
54+
override fun getEventName() = if (GestureHandler.usesNativeOrLogicDetector(actionType)) {
5555
if (eventHandlerType == EventHandlerType.ForReanimated) REANIMATED_EVENT_NAME else EVENT_NAME
5656
} else {
5757
EVENT_NAME
@@ -63,7 +63,7 @@ class RNGestureHandlerStateChangeEvent private constructor() : Event<RNGestureHa
6363
// TODO: coalescing
6464
override fun getCoalescingKey(): Short = 0
6565

66-
override fun getEventData(): WritableMap = if (actionType == GestureHandler.ACTION_TYPE_NATIVE_DETECTOR) {
66+
override fun getEventData(): WritableMap = if (GestureHandler.usesNativeOrLogicDetector(actionType)) {
6767
createNativeEventData(dataBuilder!!, newState, oldState)
6868
} else {
6969
createEventData(dataBuilder!!, newState, oldState)

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/events/RNGestureHandlerTouchEvent.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class RNGestureHandlerTouchEvent private constructor() : Event<RNGestureHandlerT
1414
private lateinit var eventHandlerType: EventHandlerType
1515

1616
private fun <T : GestureHandler> init(handler: T, actionType: Int, eventHandlerType: EventHandlerType) {
17-
val view = if (handler.actionType == GestureHandler.ACTION_TYPE_NATIVE_DETECTOR) {
18-
handler.viewForEvents!!
17+
val view = if (GestureHandler.usesNativeOrLogicDetector(handler.actionType)) {
18+
handler.viewForEvents
1919
} else {
2020
handler.view!!
2121
}
@@ -33,13 +33,13 @@ class RNGestureHandlerTouchEvent private constructor() : Event<RNGestureHandlerT
3333
EVENTS_POOL.release(this)
3434
}
3535

36-
override fun getEventName() = if (actionType == GestureHandler.ACTION_TYPE_NATIVE_DETECTOR) {
36+
override fun getEventName() = if (GestureHandler.usesNativeOrLogicDetector(actionType)) {
3737
if (eventHandlerType == EventHandlerType.ForReanimated) REANIMATED_EVENT_NAME else NATIVE_EVENT_NAME
3838
} else {
3939
EVENT_NAME
4040
}
4141

42-
override fun canCoalesce() = true
42+
override fun canCoalesce() = !GestureHandler.usesNativeOrLogicDetector(actionType)
4343

4444
override fun getCoalescingKey() = coalescingKey
4545
override fun getEventData(): WritableMap? = extraData

packages/react-native-gesture-handler/apple/RNGestureHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
@property (nonatomic) BOOL manualActivation;
8282
@property (nonatomic) BOOL dispatchesAnimatedEvents;
8383
@property (nonatomic) BOOL dispatchesReanimatedEvents;
84+
@property (nonatomic, nonnull, assign) NSNumber *hostDetectorTag;
8485

8586
- (BOOL)isViewParagraphComponent:(nullable RNGHUIView *)view;
8687
- (nonnull RNGHUIView *)chooseViewForInteraction:(nonnull UIGestureRecognizer *)recognizer;

packages/react-native-gesture-handler/apple/RNGestureHandlerActionType.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ typedef NS_ENUM(NSInteger, RNGestureHandlerActionType) {
88
RNGestureHandlerActionTypeJSFunctionNewAPI, // JS function or Animated.event with useNativeDriver: false using new
99
// RNGH API
1010
RNGestureHandlerActionTypeNativeDetector,
11+
RNGestureHandlerActionTypeLogicDetector,
1112
};

0 commit comments

Comments
 (0)