Skip to content

Commit f59bb7a

Browse files
committed
Review changes
1 parent 0e36025 commit f59bb7a

1 file changed

Lines changed: 78 additions & 37 deletions

File tree

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

Lines changed: 78 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import com.swmansion.gesturehandler.core.GestureHandler
1010
import com.swmansion.gesturehandler.core.OnTouchEventListener
1111
import com.swmansion.gesturehandler.dispatchEvent
1212

13-
class RNGestureHandlerEventDispatcher(private val reactApplicationContext: ReactApplicationContext) : OnTouchEventListener {
13+
class RNGestureHandlerEventDispatcher(private val reactApplicationContext: ReactApplicationContext) :
14+
OnTouchEventListener {
1415
private val reanimatedEventDispatcher = ReanimatedEventDispatcher()
1516

1617
override fun <T : GestureHandler<T>> onHandlerUpdate(handler: T, event: MotionEvent) {
@@ -28,37 +29,49 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
2829
private fun <T : GestureHandler<T>> dispatchHandlerUpdateEvent(handler: T) {
2930
// triggers onUpdate and onChange callbacks on the JS side
3031

31-
if (handler.tag < 0) {
32-
// root containers use negative tags, we don't need to dispatch events for them to the JS
32+
// root containers use negative tags, we don't need to dispatch events for them to the JS
33+
if (handler.tag < 0 || handler.state != GestureHandler.STATE_ACTIVE) {
3334
return
3435
}
35-
if (handler.state == GestureHandler.STATE_ACTIVE) {
36-
val handlerFactory = RNGestureHandlerFactoryUtil.findFactoryForHandler(handler) ?: return
3736

38-
if (handler.actionType == GestureHandler.ACTION_TYPE_REANIMATED_WORKLET) {
37+
val handlerFactory = RNGestureHandlerFactoryUtil.findFactoryForHandler(handler) ?: return
38+
when (handler.actionType) {
39+
GestureHandler.ACTION_TYPE_REANIMATED_WORKLET -> {
3940
// Reanimated worklet
40-
val event = RNGestureHandlerEvent.obtain(handler, handlerFactory.createEventBuilder(handler))
41+
val event = RNGestureHandlerEvent.obtain(
42+
handler,
43+
handlerFactory.createEventBuilder(handler),
44+
)
4145
sendEventForReanimated(event)
42-
} else if (handler.actionType == GestureHandler.ACTION_TYPE_NATIVE_ANIMATED_EVENT) {
46+
}
47+
GestureHandler.ACTION_TYPE_NATIVE_ANIMATED_EVENT -> {
4348
// Animated with useNativeDriver: true
4449
val event = RNGestureHandlerEvent.obtain(
4550
handler,
4651
handlerFactory.createEventBuilder(handler),
47-
true
52+
true,
4853
)
4954
sendEventForNativeAnimatedEvent(event)
50-
} else if (handler.actionType == GestureHandler.ACTION_TYPE_JS_FUNCTION_OLD_API) {
55+
}
56+
GestureHandler.ACTION_TYPE_JS_FUNCTION_OLD_API -> {
5157
// JS function, Animated.event with useNativeDriver: false using old API
5258
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
53-
val data = RNGestureHandlerEvent.createEventData(handlerFactory.createEventBuilder(handler))
59+
val data = RNGestureHandlerEvent.createEventData(
60+
handlerFactory.createEventBuilder(handler),
61+
)
5462
sendEventForDeviceEvent(RNGestureHandlerEvent.EVENT_NAME, data)
5563
} else {
56-
val event = RNGestureHandlerEvent.obtain(handler, handlerFactory.createEventBuilder(handler))
64+
val event = RNGestureHandlerEvent.obtain(
65+
handler,
66+
handlerFactory.createEventBuilder(handler),
67+
)
5768
sendEventForDirectEvent(event)
5869
}
59-
} else if (handler.actionType == GestureHandler.ACTION_TYPE_JS_FUNCTION_NEW_API) {
70+
}
71+
GestureHandler.ACTION_TYPE_JS_FUNCTION_NEW_API -> {
6072
// JS function, Animated.event with useNativeDriver: false using new API
61-
val data = RNGestureHandlerEvent.createEventData(handlerFactory.createEventBuilder(handler))
73+
val data =
74+
RNGestureHandlerEvent.createEventData(handlerFactory.createEventBuilder(handler))
6275
sendEventForDeviceEvent(RNGestureHandlerEvent.EVENT_NAME, data)
6376
}
6477
}
@@ -73,25 +86,45 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
7386
}
7487
val handlerFactory = RNGestureHandlerFactoryUtil.findFactoryForHandler(handler) ?: return
7588

76-
if (handler.actionType == GestureHandler.ACTION_TYPE_REANIMATED_WORKLET) {
77-
// Reanimated worklet
78-
val event = RNGestureHandlerStateChangeEvent.obtain(handler, newState, oldState, handlerFactory.createEventBuilder(handler))
79-
sendEventForReanimated(event)
80-
} else if (handler.actionType == GestureHandler.ACTION_TYPE_NATIVE_ANIMATED_EVENT ||
81-
handler.actionType == GestureHandler.ACTION_TYPE_JS_FUNCTION_OLD_API
82-
) {
83-
// JS function or Animated.event with useNativeDriver: false with old API
84-
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
85-
val data = RNGestureHandlerStateChangeEvent.createEventData(handlerFactory.createEventBuilder(handler), newState, oldState)
89+
when (handler.actionType) {
90+
GestureHandler.ACTION_TYPE_REANIMATED_WORKLET -> {
91+
// Reanimated worklet
92+
val event = RNGestureHandlerStateChangeEvent.obtain(
93+
handler,
94+
newState,
95+
oldState,
96+
handlerFactory.createEventBuilder(handler),
97+
)
98+
sendEventForReanimated(event)
99+
}
100+
GestureHandler.ACTION_TYPE_NATIVE_ANIMATED_EVENT, GestureHandler.ACTION_TYPE_JS_FUNCTION_OLD_API -> {
101+
// JS function or Animated.event with useNativeDriver: false with old API
102+
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
103+
val data = RNGestureHandlerStateChangeEvent.createEventData(
104+
handlerFactory.createEventBuilder(handler),
105+
newState,
106+
oldState,
107+
)
108+
sendEventForDeviceEvent(RNGestureHandlerStateChangeEvent.EVENT_NAME, data)
109+
} else {
110+
val event = RNGestureHandlerStateChangeEvent.obtain(
111+
handler,
112+
newState,
113+
oldState,
114+
handlerFactory.createEventBuilder(handler),
115+
)
116+
sendEventForDirectEvent(event)
117+
}
118+
}
119+
GestureHandler.ACTION_TYPE_JS_FUNCTION_NEW_API -> {
120+
// JS function or Animated.event with useNativeDriver: false with new API
121+
val data = RNGestureHandlerStateChangeEvent.createEventData(
122+
handlerFactory.createEventBuilder(handler),
123+
newState,
124+
oldState,
125+
)
86126
sendEventForDeviceEvent(RNGestureHandlerStateChangeEvent.EVENT_NAME, data)
87-
} else {
88-
val event = RNGestureHandlerStateChangeEvent.obtain(handler, newState, oldState, handlerFactory.createEventBuilder(handler))
89-
sendEventForDirectEvent(event)
90127
}
91-
} else if (handler.actionType == GestureHandler.ACTION_TYPE_JS_FUNCTION_NEW_API) {
92-
// JS function or Animated.event with useNativeDriver: false with new API
93-
val data = RNGestureHandlerStateChangeEvent.createEventData(handlerFactory.createEventBuilder(handler), newState, oldState)
94-
sendEventForDeviceEvent(RNGestureHandlerStateChangeEvent.EVENT_NAME, data)
95128
}
96129
}
97130

@@ -102,22 +135,30 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
102135
// root containers use negative tags, we don't need to dispatch events for them to the JS
103136
return
104137
}
105-
if (handler.state == GestureHandler.STATE_BEGAN || handler.state == GestureHandler.STATE_ACTIVE ||
106-
handler.state == GestureHandler.STATE_UNDETERMINED || handler.view != null
138+
139+
if (handler.state != GestureHandler.STATE_BEGAN &&
140+
handler.state != GestureHandler.STATE_ACTIVE &&
141+
handler.state != GestureHandler.STATE_UNDETERMINED &&
142+
handler.view == null
107143
) {
108-
if (handler.actionType == GestureHandler.ACTION_TYPE_REANIMATED_WORKLET) {
144+
return
145+
}
146+
147+
when (handler.actionType) {
148+
GestureHandler.ACTION_TYPE_REANIMATED_WORKLET -> {
109149
// Reanimated worklet
110150
val event = RNGestureHandlerTouchEvent.obtain(handler)
111151
sendEventForReanimated(event)
112-
} else if (handler.actionType == GestureHandler.ACTION_TYPE_JS_FUNCTION_NEW_API) {
152+
}
153+
GestureHandler.ACTION_TYPE_JS_FUNCTION_NEW_API -> {
113154
// JS function, Animated.event with useNativeDriver: false with new API
114155
val data = RNGestureHandlerTouchEvent.createEventData(handler)
115156
sendEventForDeviceEvent(RNGestureHandlerEvent.EVENT_NAME, data)
116157
}
117158
}
118159
}
119160

120-
private fun <T : Event<T>>sendEventForReanimated(event: T) {
161+
private fun <T : Event<T>> sendEventForReanimated(event: T) {
121162
// Delivers the event to Reanimated.
122163
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
123164
// Send event directly to Reanimated
@@ -136,7 +177,7 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
136177
reactApplicationContext.dispatchEvent(event)
137178
}
138179

139-
private fun <T : Event<T>>sendEventForDirectEvent(event: T) {
180+
private fun <T : Event<T>> sendEventForDirectEvent(event: T) {
140181
// Delivers the event to JS as a direct event. This method is called only on Paper.
141182
reactApplicationContext.dispatchEvent(event)
142183
}

0 commit comments

Comments
 (0)