Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,16 @@ open class GestureHandler {
// generated faster than they can be treated by JS thread
eventCoalescingKey = nextEventCoalescingKey++
}

check(hostDetectorView != null || orchestrator != null) {
"Manually handled gesture had not been assigned to any detector"
}

if (orchestrator == null) {
// If the state is set manually, the handler may not have been fully recorded by the orchestrator.
hostDetectorView?.recordHandlerIfNotPresent(this)
}
Comment thread
m-bert marked this conversation as resolved.

orchestrator!!.onHandlerStateChange(this, newState, oldState)
onStateChange(newState, oldState)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ class GestureHandlerOrchestrator(
}
}

private fun recordHandlerIfNotPresent(handler: GestureHandler, view: View) {
fun recordHandlerIfNotPresent(handler: GestureHandler, view: View?) {
if (gestureHandlers.contains(handler)) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.swmansion.gesturehandler.react

import android.content.Context
import android.view.View
import android.view.ViewParent
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerHelper
Expand Down Expand Up @@ -202,7 +203,23 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
child.value.clear()
}
}
fun recordHandlerIfNotPresent(handler: GestureHandler) {
findGestureHandlerRootView()?.recordHandlerIfNotPresent(handler)
}

private fun findGestureHandlerRootView(): RNGestureHandlerRootView? {
var parent: ViewParent? = this.parent
var gestureHandlerRootView: RNGestureHandlerRootView? = null

while (parent != null) {
if (parent is RNGestureHandlerRootView) {
gestureHandlerRootView = parent
}
parent = parent.parent
}

return gestureHandlerRootView
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have this exact method already defined somewhere? Maybe it's worth moving it to some utils, or making it an extension on View?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I moved it to be companion object for the rootView in 8a4ffc9. Let me know what you think.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure whether this.findGestureHandlerRootView()?... wouldn't have been cleaner, but I'll leave the final decision to you. Both work for me.

private fun ReadableArray.mapVirtualChildren(): List<VirtualChildren> = List(size()) { i ->
val child = getMap(i) ?: return@List null
val handlerTags = child.getArray("handlerTags")?.toIntList().orEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView:
orchestrator?.activateNativeHandlersForView(view)
}

fun recordHandlerIfNotPresent(handler: GestureHandler) {
orchestrator?.recordHandlerIfNotPresent(handler, null)
}

companion object {
private const val MIN_ALPHA_FOR_TOUCH = 0.1f
private fun findRootViewTag(viewGroup: ViewGroup): ViewGroup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.common.ReactConstants
import com.facebook.react.uimanager.RootView
import com.facebook.react.views.view.ReactViewGroup
import com.swmansion.gesturehandler.core.GestureHandler

class RNGestureHandlerRootView(context: Context?) : ReactViewGroup(context) {
private var moduleId: Int = -1
Expand Down Expand Up @@ -39,6 +40,10 @@ class RNGestureHandlerRootView(context: Context?) : ReactViewGroup(context) {
rootHelper?.tearDown()
}

fun recordHandlerIfNotPresent(handler: GestureHandler) {
rootHelper?.recordHandlerIfNotPresent(handler)
}

override fun dispatchTouchEvent(event: MotionEvent) = if (rootViewEnabled && rootHelper!!.dispatchTouchEvent(event)) {
true
} else {
Expand Down