From 0b9c20d57324aec0f1a0f34577c4caeaf3920b12 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Fri, 15 Dec 2023 15:12:49 +0100 Subject: [PATCH 1/2] Fix buttons getting stuck --- .../react/RNGestureHandlerButtonViewManager.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt b/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt index fabd819233..599517d305 100644 --- a/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt +++ b/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt @@ -172,15 +172,15 @@ class RNGestureHandlerButtonViewManager : ViewGroupManager(), R * [com.swmansion.gesturehandler.NativeViewGestureHandler.onHandle] */ @SuppressLint("ClickableViewAccessibility") override fun onTouchEvent(event: MotionEvent): Boolean { + val eventTime = event.eventTime + val action = event.action + if (event.action == MotionEvent.ACTION_CANCEL) { tryFreeingResponder() - return super.onTouchEvent(event) } - val eventTime = event.eventTime - val action = event.action // always true when lastEventTime or lastAction have default value (-1) - if (lastEventTime != eventTime || lastAction != action) { + if (lastEventTime != eventTime || lastAction != action || event.action == MotionEvent.ACTION_CANCEL) { lastEventTime = eventTime lastAction = action return super.onTouchEvent(event) From 92dc61210c9ae3092eb0b003e6fde041eca29b0f Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Tue, 14 May 2024 12:36:37 +0200 Subject: [PATCH 2/2] Fix buttons, for real this time --- .../core/NativeViewGestureHandler.kt | 24 ++++++++++++------- .../RNGestureHandlerButtonViewManager.kt | 8 +++++-- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/android/src/main/java/com/swmansion/gesturehandler/core/NativeViewGestureHandler.kt b/android/src/main/java/com/swmansion/gesturehandler/core/NativeViewGestureHandler.kt index 64f230a2c8..411e1b10cf 100644 --- a/android/src/main/java/com/swmansion/gesturehandler/core/NativeViewGestureHandler.kt +++ b/android/src/main/java/com/swmansion/gesturehandler/core/NativeViewGestureHandler.kt @@ -83,11 +83,21 @@ class NativeViewGestureHandler : GestureHandler() { override fun onHandle(event: MotionEvent, sourceEvent: MotionEvent) { val view = view!! if (event.actionMasked == MotionEvent.ACTION_UP) { - view.onTouchEvent(event) - if ((state == STATE_UNDETERMINED || state == STATE_BEGAN) && view.isPressed) { - activate() + if (state == STATE_UNDETERMINED && !hook.canBegin(event)) { + cancel() + } else { + view.onTouchEvent(event) + if ((state == STATE_UNDETERMINED || state == STATE_BEGAN) && view.isPressed) { + activate() + } + + if (state == STATE_UNDETERMINED) { + cancel() + } else { + end() + } } - end() + hook.afterGestureEnd(event) } else if (state == STATE_UNDETERMINED || state == STATE_BEGAN) { when { @@ -104,10 +114,8 @@ class NativeViewGestureHandler : GestureHandler() { hook.handleEventBeforeActivation(event) } state != STATE_BEGAN -> { - if (hook.canBegin()) { + if (hook.canBegin(event)) { begin() - } else { - cancel() } } } @@ -144,7 +152,7 @@ class NativeViewGestureHandler : GestureHandler() { * @return Boolean value signalling whether the handler can transition to the BEGAN state. If false * the gesture will be cancelled. */ - fun canBegin() = true + fun canBegin(event: MotionEvent) = true /** * Called after the gesture transitions to the END state. diff --git a/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt b/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt index fc4356098b..e47f115bfb 100644 --- a/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt +++ b/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt @@ -278,7 +278,7 @@ class RNGestureHandlerButtonViewManager : ViewGroupManager(), R } // always true when lastEventTime or lastAction have default value (-1) - if (lastEventTime != eventTime || lastAction != action || event.action == MotionEvent.ACTION_CANCEL) { + if (lastEventTime != eventTime || lastAction != action || action == MotionEvent.ACTION_CANCEL) { lastEventTime = eventTime lastAction = action return super.onTouchEvent(event) @@ -384,7 +384,11 @@ class RNGestureHandlerButtonViewManager : ViewGroupManager(), R } } - override fun canBegin(): Boolean { + override fun canBegin(event: MotionEvent): Boolean { + if (event.action == MotionEvent.ACTION_CANCEL || event.action == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_POINTER_UP) { + return false + } + val isResponder = tryGrabbingResponder() if (isResponder) { isTouched = true