Skip to content

Commit 8dd560f

Browse files
authored
Rename logic detector to virtual detector (#3796)
## Description This PR renames `logicDetector` to `virtualDetector`. It also resolves the issue of android setters relying on the order they are executed in. ## Test plan `yarn lint-js`
1 parent 441a011 commit 8dd560f

21 files changed

+156
-139
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ open class GestureHandler {
3333
private set
3434

3535
// 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.
36+
// Virtual Detector to which the gesture is assigned.
3737
var hostDetectorView: RNGestureHandlerDetectorView? = null
3838

3939
val viewForEvents: RNGestureHandlerDetectorView
4040
get() {
41-
assert(usesNativeOrLogicDetector(actionType)) {
41+
assert(usesNativeOrVirtualDetector(actionType)) {
4242
"[react-native-gesture-handler] `viewForEvents` can only be used with NativeDetector."
4343
}
4444

4545
val detector = if (actionType ==
46-
ACTION_TYPE_LOGIC_DETECTOR
46+
ACTION_TYPE_VIRTUAL_DETECTOR
4747
) {
4848
this.hostDetectorView
4949
} else if (this is NativeViewGestureHandler) {
@@ -1020,7 +1020,7 @@ open class GestureHandler {
10201020
const val ACTION_TYPE_JS_FUNCTION_OLD_API = 3
10211021
const val ACTION_TYPE_JS_FUNCTION_NEW_API = 4
10221022
const val ACTION_TYPE_NATIVE_DETECTOR = 5
1023-
const val ACTION_TYPE_LOGIC_DETECTOR = 6
1023+
const val ACTION_TYPE_VIRTUAL_DETECTOR = 6
10241024
const val POINTER_TYPE_TOUCH = 0
10251025
const val POINTER_TYPE_STYLUS = 1
10261026
const val POINTER_TYPE_MOUSE = 2
@@ -1056,8 +1056,8 @@ open class GestureHandler {
10561056
return null
10571057
}
10581058

1059-
fun usesNativeOrLogicDetector(actionType: Int): Boolean =
1060-
actionType == ACTION_TYPE_NATIVE_DETECTOR || actionType == ACTION_TYPE_LOGIC_DETECTOR
1059+
fun usesNativeOrVirtualDetector(actionType: Int): Boolean =
1060+
actionType == ACTION_TYPE_NATIVE_DETECTOR || actionType == ACTION_TYPE_VIRTUAL_DETECTOR
10611061
}
10621062

10631063
private data class PointerData(

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

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
1313
private val reactContext: ThemedReactContext
1414
get() = context as ThemedReactContext
1515
private var handlersToAttach: List<Int>? = null
16+
private var virtualChildrenToAttach: List<VirtualChildren>? = null
1617
private var nativeHandlers: MutableSet<Int> = mutableSetOf()
1718
private var attachedHandlers: MutableSet<Int> = mutableSetOf()
18-
private var attachedLogicHandlers: MutableMap<Int, MutableSet<Int>> = mutableMapOf()
19+
private var attachedVirtualHandlers: MutableMap<Int, MutableSet<Int>> = mutableMapOf()
1920
private var moduleId: Int = -1
2021

21-
data class LogicChildren(val handlerTags: List<Int>, val viewTag: Int)
22+
data class VirtualChildren(val handlerTags: List<Int>, val viewTag: Int)
2223

2324
fun setHandlerTags(handlerTags: ReadableArray?) {
2425
val newHandlers = handlerTags?.toArrayList()?.map { (it as Double).toInt() } ?: emptyList()
@@ -38,35 +39,21 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
3839
this.moduleId = id
3940
this.attachHandlers(handlersToAttach ?: return)
4041
handlersToAttach = null
42+
this.attachVirtualChildren(virtualChildrenToAttach ?: return)
43+
virtualChildrenToAttach = null
4144
}
4245

43-
fun setLogicChildren(newLogicChildren: ReadableArray?) {
44-
val logicChildrenToDetach = attachedLogicHandlers.keys.toMutableSet()
46+
fun setVirtualChildren(newVirtualChildren: ReadableArray?) {
47+
val mappedChildren = newVirtualChildren?.mapVirtualChildren().orEmpty()
4548

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-
)
49+
if (moduleId == -1) {
50+
// It's possible that handlerTags will be set before module id. In that case, store
51+
// the handler ids and attach them after setting module id.
52+
virtualChildrenToAttach = mappedChildren
53+
return
6154
}
6255

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-
}
56+
attachVirtualChildren(mappedChildren)
7057
}
7158

7259
private fun shouldAttachGestureToChildView(tag: Int): Boolean {
@@ -109,7 +96,7 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
10996
nativeHandlers.add(tag)
11097
} else {
11198
registry.attachHandlerToView(tag, viewTag, actionType)
112-
if (actionType == GestureHandler.ACTION_TYPE_LOGIC_DETECTOR) {
99+
if (actionType == GestureHandler.ACTION_TYPE_VIRTUAL_DETECTOR) {
113100
registry.getHandler(tag)?.hostDetectorView = this
114101
}
115102
attachedHandlers.add(tag)
@@ -131,6 +118,32 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
131118
}
132119
}
133120

121+
private fun attachVirtualChildren(virtualChildrenToAttach: List<VirtualChildren>) {
122+
val virtualChildrenToDetach = attachedVirtualHandlers.keys.toMutableSet()
123+
124+
for (child in virtualChildrenToAttach) {
125+
if (!attachedVirtualHandlers.containsKey(child.viewTag)) {
126+
attachedVirtualHandlers[child.viewTag] = mutableSetOf()
127+
}
128+
129+
virtualChildrenToDetach.remove(child.viewTag)
130+
attachHandlers(
131+
child.handlerTags,
132+
child.viewTag,
133+
GestureHandler.ACTION_TYPE_VIRTUAL_DETECTOR,
134+
attachedVirtualHandlers[child.viewTag]!!,
135+
)
136+
}
137+
138+
val registry = RNGestureHandlerModule.registries[moduleId]
139+
?: throw Exception("Tried to access a non-existent registry")
140+
141+
for (tag in virtualChildrenToDetach) {
142+
registry.detachHandler(tag)
143+
attachedVirtualHandlers.remove(tag)
144+
}
145+
}
146+
134147
private fun tryAttachNativeHandlersToChildView(childId: Int) {
135148
val registry = RNGestureHandlerModule.registries[moduleId]
136149
?: throw Exception("Tried to access a non-existent registry")
@@ -166,20 +179,20 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
166179
attachedHandlers.remove(tag)
167180
}
168181

169-
for (child in attachedLogicHandlers) {
182+
for (child in attachedVirtualHandlers) {
170183
for (tag in child.value) {
171184
registry.detachHandler(tag)
172185
}
173186
child.value.clear()
174187
}
175188
}
176189

177-
private fun ReadableArray.mapLogicChildren(): List<LogicChildren> = List(size()) { i ->
190+
private fun ReadableArray.mapVirtualChildren(): List<VirtualChildren> = List(size()) { i ->
178191
val child = getMap(i) ?: return@List null
179192
val handlerTags = child.getArray("handlerTags")?.toIntList().orEmpty()
180193
val viewTag = child.getInt("viewTag")
181194

182-
LogicChildren(handlerTags, viewTag)
195+
VirtualChildren(handlerTags, viewTag)
183196
}.filterNotNull()
184197

185198
private fun ReadableArray.toIntList(): List<Int> = List(size()) { getInt(it) }

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

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

40-
override fun setLogicChildren(view: RNGestureHandlerDetectorView, value: ReadableArray?) {
41-
view.setLogicChildren(value)
40+
override fun setVirtualChildren(view: RNGestureHandlerDetectorView, value: ReadableArray?) {
41+
view.setVirtualChildren(value)
4242
}
4343

4444
override fun onDropViewInstance(view: RNGestureHandlerDetectorView) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class RNGestureHandlerEvent private constructor() : Event<RNGestureHandlerEvent>
2020
dataBuilder: GestureHandlerEventDataBuilder<T>,
2121
eventHandlerType: EventHandlerType,
2222
) {
23-
val view = if (GestureHandler.usesNativeOrLogicDetector(handler.actionType)) {
23+
val view = if (GestureHandler.usesNativeOrVirtualDetector(handler.actionType)) {
2424
handler.viewForEvents
2525
} else {
2626
handler.view!!
@@ -39,7 +39,7 @@ class RNGestureHandlerEvent private constructor() : Event<RNGestureHandlerEvent>
3939
EVENTS_POOL.release(this)
4040
}
4141

42-
override fun getEventName() = if (GestureHandler.usesNativeOrLogicDetector(actionType)) {
42+
override fun getEventName() = if (GestureHandler.usesNativeOrVirtualDetector(actionType)) {
4343
if (eventHandlerType == EventHandlerType.ForAnimated) {
4444
NATIVE_DETECTOR_ANIMATED_EVENT_NAME
4545
} else if (eventHandlerType == EventHandlerType.ForReanimated) {
@@ -54,11 +54,11 @@ class RNGestureHandlerEvent private constructor() : Event<RNGestureHandlerEvent>
5454
}
5555

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

5959
override fun getCoalescingKey() = coalescingKey
6060

61-
override fun getEventData(): WritableMap = if (GestureHandler.usesNativeOrLogicDetector(actionType)) {
61+
override fun getEventData(): WritableMap = if (GestureHandler.usesNativeOrVirtualDetector(actionType)) {
6262
createNativeEventData(dataBuilder!!)
6363
} else {
6464
createEventData(dataBuilder!!)

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

Lines changed: 3 additions & 3 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, GestureHandler.ACTION_TYPE_LOGIC_DETECTOR -> {
74+
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR, GestureHandler.ACTION_TYPE_VIRTUAL_DETECTOR -> {
7575
val eventHandlerType = if (handler.dispatchesAnimatedEvents) {
7676
EventHandlerType.ForAnimated
7777
} else if (handler.dispatchesReanimatedEvents) {
@@ -136,7 +136,7 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
136136
sendEventForDeviceEvent(RNGestureHandlerStateChangeEvent.EVENT_NAME, data)
137137
}
138138

139-
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR, GestureHandler.ACTION_TYPE_LOGIC_DETECTOR -> {
139+
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR, GestureHandler.ACTION_TYPE_VIRTUAL_DETECTOR -> {
140140
val eventHandlerType = if (handler.dispatchesReanimatedEvents) {
141141
EventHandlerType.ForReanimated
142142
} else {
@@ -188,7 +188,7 @@ 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, GestureHandler.ACTION_TYPE_LOGIC_DETECTOR -> {
191+
GestureHandler.ACTION_TYPE_NATIVE_DETECTOR, GestureHandler.ACTION_TYPE_VIRTUAL_DETECTOR -> {
192192
val eventHandlerType = if (handler.dispatchesReanimatedEvents) {
193193
EventHandlerType.ForReanimated
194194
} else {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class RNGestureHandlerStateChangeEvent private constructor() : Event<RNGestureHa
2323
dataBuilder: GestureHandlerEventDataBuilder<T>,
2424
eventHandlerType: EventHandlerType,
2525
) {
26-
val view = if (GestureHandler.usesNativeOrLogicDetector(handler.actionType)) {
26+
val view = if (GestureHandler.usesNativeOrVirtualDetector(handler.actionType)) {
2727
handler.viewForEvents
2828
} else {
2929
handler.view!!
@@ -45,7 +45,7 @@ class RNGestureHandlerStateChangeEvent private constructor() : Event<RNGestureHa
4545
EVENTS_POOL.release(this)
4646
}
4747

48-
override fun getEventName() = if (GestureHandler.usesNativeOrLogicDetector(actionType)) {
48+
override fun getEventName() = if (GestureHandler.usesNativeOrVirtualDetector(actionType)) {
4949
if (eventHandlerType == EventHandlerType.ForReanimated) REANIMATED_EVENT_NAME else EVENT_NAME
5050
} else {
5151
EVENT_NAME
@@ -57,7 +57,7 @@ class RNGestureHandlerStateChangeEvent private constructor() : Event<RNGestureHa
5757
// TODO: coalescing
5858
override fun getCoalescingKey(): Short = 0
5959

60-
override fun getEventData(): WritableMap = if (GestureHandler.usesNativeOrLogicDetector(actionType)) {
60+
override fun getEventData(): WritableMap = if (GestureHandler.usesNativeOrVirtualDetector(actionType)) {
6161
createNativeEventData(dataBuilder!!, newState, oldState)
6262
} else {
6363
createEventData(dataBuilder!!, newState, oldState)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ 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 (GestureHandler.usesNativeOrLogicDetector(handler.actionType)) {
17+
val view = if (GestureHandler.usesNativeOrVirtualDetector(handler.actionType)) {
1818
handler.viewForEvents
1919
} else {
2020
handler.view!!
@@ -33,13 +33,13 @@ class RNGestureHandlerTouchEvent private constructor() : Event<RNGestureHandlerT
3333
EVENTS_POOL.release(this)
3434
}
3535

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

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

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ - (void)handleGesture:(UIGestureRecognizer *)recognizer
294294
// it that recognizer tried to send event, the app would crash because the target of the event
295295
// would be nil.
296296
if (view.reactTag == nil && _actionType != RNGestureHandlerActionTypeNativeDetector &&
297-
_actionType != RNGestureHandlerActionTypeLogicDetector) {
297+
_actionType != RNGestureHandlerActionTypeVirtualDetector) {
298298
return;
299299
}
300300

@@ -314,7 +314,7 @@ - (void)handleGesture:(UIGestureRecognizer *)recognizer inState:(RNGestureHandle
314314
tag = @(recognizer.view.tag);
315315
}
316316

317-
if (_virtualViewTag != nil && _actionType == RNGestureHandlerActionTypeLogicDetector) {
317+
if (_virtualViewTag != nil && _actionType == RNGestureHandlerActionTypeVirtualDetector) {
318318
tag = _virtualViewTag;
319319
}
320320

@@ -673,7 +673,7 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
673673
}
674674

675675
// Logic detector has a virtual view tag set only if the real hierarchy was folded into a single View
676-
if (_actionType == RNGestureHandlerActionTypeLogicDetector && _virtualViewTag != nil) {
676+
if (_actionType == RNGestureHandlerActionTypeVirtualDetector && _virtualViewTag != nil) {
677677
// In this case, logic detector is attached to the DetectorView, which has a single subview representing
678678
// the actual target view in the RN hierarchy
679679
RNGHUIView *view = _recognizer.view.subviews[0];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +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,
11+
RNGestureHandlerActionTypeVirtualDetector,
1212
};

0 commit comments

Comments
 (0)