Skip to content

Commit 110d333

Browse files
committed
Always assign hostDetectorView
1 parent e84f3cf commit 110d333

7 files changed

Lines changed: 15 additions & 40 deletions

File tree

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import com.facebook.react.bridge.ReadableType
1616
import com.facebook.react.bridge.UiThreadUtil
1717
import com.facebook.react.bridge.WritableArray
1818
import com.facebook.react.uimanager.PixelUtil
19-
import com.facebook.react.views.swiperefresh.ReactSwipeRefreshLayout
2019
import com.swmansion.gesturehandler.BuildConfig
2120
import com.swmansion.gesturehandler.RNSVGHitTester
2221
import com.swmansion.gesturehandler.react.RNGestureHandlerDetectorView
@@ -37,36 +36,6 @@ open class GestureHandler {
3736
// Virtual Detector to which the gesture is assigned.
3837
var hostDetectorView: RNGestureHandlerDetectorView? = null
3938

40-
val viewForEvents: RNGestureHandlerDetectorView
41-
get() {
42-
assert(usesNativeOrVirtualDetector(actionType)) {
43-
"[react-native-gesture-handler] `viewForEvents` can only be used with NativeDetector."
44-
}
45-
46-
val detector = if (actionType ==
47-
ACTION_TYPE_VIRTUAL_DETECTOR
48-
) {
49-
this.hostDetectorView
50-
} else if (this is NativeViewGestureHandler) {
51-
val parent = this.view?.parent
52-
53-
if (parent is ReactSwipeRefreshLayout) {
54-
parent.parent
55-
} else {
56-
parent
57-
}
58-
} else {
59-
view
60-
}
61-
62-
if (detector !is RNGestureHandlerDetectorView) {
63-
throw Error(
64-
"[react-native-gesture-handler] Expected RNGestureHandlerDetectorView to be the target for the event.",
65-
)
66-
}
67-
68-
return detector
69-
}
7039
var state = STATE_UNDETERMINED
7140
private set
7241
var x = 0f

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
9696
// attach `NativeViewGestureHandlers` here and we have to do it in `addView` method.
9797
nativeHandlers.add(tag)
9898
} else {
99-
registry.attachHandlerToView(tag, viewTag, actionType)
99+
registry.attachHandlerToView(tag, viewTag, actionType, this)
100100
if (actionType == GestureHandler.ACTION_TYPE_VIRTUAL_DETECTOR) {
101101
registry.getHandler(tag)?.hostDetectorView = this
102102
}
@@ -161,7 +161,7 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
161161
}
162162

163163
for (tag in nativeHandlers) {
164-
registry.attachHandlerToView(tag, id, GestureHandler.ACTION_TYPE_NATIVE_DETECTOR)
164+
registry.attachHandlerToView(tag, id, GestureHandler.ACTION_TYPE_NATIVE_DETECTOR, this)
165165

166166
attachedHandlers.add(tag)
167167
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ class RNGestureHandlerRegistry : GestureHandlerRegistry {
2121
fun getHandler(handlerTag: Int): GestureHandler? = handlers[handlerTag]
2222

2323
@Synchronized
24-
fun attachHandlerToView(handlerTag: Int, viewTag: Int, actionType: Int): Boolean {
24+
fun attachHandlerToView(
25+
handlerTag: Int,
26+
viewTag: Int,
27+
actionType: Int,
28+
hostDetectorView: RNGestureHandlerDetectorView? = null,
29+
): Boolean {
2530
val handler = handlers[handlerTag]
2631
return handler?.let {
2732
detachHandlerInternal(handler)
2833
handler.actionType = actionType
34+
handler.hostDetectorView = hostDetectorView
2935
registerHandlerForViewWithTag(viewTag, handler)
3036
true
3137
} ?: false

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RNGestureHandlerEvent private constructor() : Event<RNGestureHandlerEvent>
2121
eventHandlerType: EventHandlerType,
2222
) {
2323
val view = if (GestureHandler.usesNativeOrVirtualDetector(handler.actionType)) {
24-
handler.viewForEvents
24+
handler.hostDetectorView!!
2525
} else {
2626
handler.view!!
2727
}

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
@@ -87,7 +87,7 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
8787
eventHandlerType,
8888
)
8989

90-
handler.viewForEvents.dispatchEvent(event)
90+
handler.hostDetectorView?.dispatchEvent(event)
9191
}
9292
}
9393
}
@@ -152,7 +152,7 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
152152
eventHandlerType,
153153
)
154154

155-
handler.viewForEvents.dispatchEvent(event)
155+
handler.hostDetectorView?.dispatchEvent(event)
156156
}
157157
}
158158
}
@@ -196,7 +196,7 @@ class RNGestureHandlerEventDispatcher(private val reactApplicationContext: React
196196
}
197197
val event = RNGestureHandlerTouchEvent.obtain(handler, handler.actionType, eventHandlerType)
198198

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RNGestureHandlerStateChangeEvent private constructor() : Event<RNGestureHa
2424
eventHandlerType: EventHandlerType,
2525
) {
2626
val view = if (GestureHandler.usesNativeOrVirtualDetector(handler.actionType)) {
27-
handler.viewForEvents
27+
handler.hostDetectorView!!
2828
} else {
2929
handler.view!!
3030
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RNGestureHandlerTouchEvent private constructor() : Event<RNGestureHandlerT
1515

1616
private fun <T : GestureHandler> init(handler: T, actionType: Int, eventHandlerType: EventHandlerType) {
1717
val view = if (GestureHandler.usesNativeOrVirtualDetector(handler.actionType)) {
18-
handler.viewForEvents
18+
handler.hostDetectorView!!
1919
} else {
2020
handler.view!!
2121
}

0 commit comments

Comments
 (0)