Skip to content

Commit 6a4eb59

Browse files
committed
chore: refactor to allow interaction with root gesture ahnderl
1 parent d214a20 commit 6a4eb59

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

plugin/platforms/android/java/com/nativescript/gesturehandler/PageLayout.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
import com.swmansion.gesturehandler.GestureHandler;
1616
import com.swmansion.gesturehandler.ViewConfigurationHelper;
1717

18+
import java.util.ArrayList;
19+
1820
public class PageLayout extends org.nativescript.widgets.GridLayout {
19-
public PageLayout(Context context) {
21+
public PageLayout(Context context, int rootGestureTag) {
2022
super(context);
23+
mRootGestureTag = rootGestureTag;
2124
addRow(new ItemSpec(1, GridUnitType.auto));
2225
addRow(new ItemSpec(1, GridUnitType.star));
2326
}
24-
private final static int GESTURE_HANDLER_TAG = -12345;
2527

28+
private int mRootGestureTag;
2629
private GestureHandlerOrchestrator mOrchestrator;
2730
private GestureHandlerRegistryImpl mRegistry;
2831
private ViewConfigurationHelper configurationHelper;
@@ -31,6 +34,7 @@ public PageLayout(Context context) {
3134
private boolean mShouldIntercept = false;
3235
private boolean mPassingTouch = false;
3336
private boolean mDispatchToOrchestra = true;
37+
private boolean mShouldAddRootGesture = true;
3438

3539
public void setShouldIntercept(boolean value) {
3640
if (GestureHandler.debug) {
@@ -42,6 +46,13 @@ public void setShouldIntercept(boolean value) {
4246
public void setPassingTouch(boolean value) {
4347
this.mPassingTouch = value;
4448
}
49+
public void setShouldAddRootGesture(boolean value) {
50+
this.mShouldAddRootGesture = value;
51+
}
52+
53+
public int getRootGestureTag() {
54+
return mRootGestureTag;
55+
}
4556

4657
public void setDispatchToOrchestra(boolean value) {
4758
if (GestureHandler.debug) {
@@ -71,12 +82,21 @@ public void tryCancelAllHandlers() {
7182
if (GestureHandler.debug) {
7283
Log.d("JS", "PageLayout tryCancelAllHandlers ");
7384
}
74-
// In order to cancel handlers we activate handler that is hooked to the root view
75-
if (this.rootGestureHandler != null && this.rootGestureHandler.getState() == com.swmansion.gesturehandler.GestureHandler.STATE_BEGAN) {
76-
// Try activate main JS handler
77-
this.rootGestureHandler.activate();
78-
this.rootGestureHandler.end();
85+
ArrayList<GestureHandler> handlers = this.mRegistry.getAllHandlers();
86+
if (handlers != null) {
87+
for(int i = 0; i < handlers.size(); i++) {
88+
GestureHandler handler = handlers.get(i);
89+
if (handler != this.rootGestureHandler) {
90+
handler.cancel();
91+
}
92+
}
7993
}
94+
// In order to cancel handlers we activate handler that is hooked to the root view
95+
// if (this.rootGestureHandler != null && this.rootGestureHandler.getState() == com.swmansion.gesturehandler.GestureHandler.STATE_BEGAN) {
96+
// // Try activate main JS handler
97+
// this.rootGestureHandler.activate();
98+
// this.rootGestureHandler.end();
99+
// }
80100
}
81101

82102
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
@@ -156,9 +176,9 @@ public View getChildInDrawingOrderAtIndex(ViewGroup parent, int index) {
156176
this.mOrchestrator.setMinimumAlphaForTraversal(0.01f);
157177

158178
this.rootGestureHandler = new RootViewGestureHandler();
159-
this.rootGestureHandler.setTag(GESTURE_HANDLER_TAG);
179+
this.rootGestureHandler.setTag(mRootGestureTag);
160180
this.mRegistry.registerHandler(this.rootGestureHandler);
161-
this.mRegistry.attachHandlerToView(GESTURE_HANDLER_TAG, this);
181+
this.mRegistry.attachHandlerToView(mRootGestureTag, this);
162182
}
163183

164184
public void tearDown() {

plugin/platforms/android/java/com/swmansion/gesturehandler/GestureHandlerRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77

88
public interface GestureHandlerRegistry {
99
ArrayList<GestureHandler> getHandlersForView(View view);
10+
ArrayList<GestureHandler> getAllHandlers();
11+
1012
}

plugin/platforms/android/java/com/swmansion/gesturehandler/GestureHandlerRegistryImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.swmansion.gesturehandler.GestureHandlerRegistry;
88

99
import java.util.ArrayList;
10+
import java.util.List;
1011
import java.util.WeakHashMap;
1112

1213
import androidx.annotation.Nullable;
@@ -27,6 +28,16 @@ public synchronized void registerHandler(GestureHandler handler) {
2728
return mHandlers.get(handlerTag);
2829
}
2930

31+
public synchronized ArrayList<GestureHandler> getAllHandlers() {
32+
if (mHandlers == null)
33+
return null;
34+
ArrayList<GestureHandler> arrayList = new ArrayList<GestureHandler>(mHandlers.size());
35+
for (int i = 0; i < mHandlers.size(); i++) {
36+
arrayList.add(mHandlers.valueAt(i));
37+
}
38+
return arrayList;
39+
}
40+
3041
public synchronized boolean attachHandlerToView(int handlerTag, android.view.View view) {
3142
GestureHandler handler = mHandlers.get(handlerTag);
3243
if (handler != null) {

src/gesturehandler.android.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
applyMixins,
2828
install as installBase,
2929
nativeProperty,
30+
ROOT_GESTURE_HANDLER_TAG,
3031
} from './gesturehandler.common';
3132
import { observe as gestureObserve } from './gestures_override';
3233

@@ -47,6 +48,7 @@ class PageGestureExtended extends Page {
4748
}
4849
let installed = false;
4950
let installedOverrides = false;
51+
5052
export function install(overrideNGestures = false) {
5153
if (!installed) {
5254
installed = true;
@@ -56,7 +58,7 @@ export function install(overrideNGestures = false) {
5658
if (!PageLayout) {
5759
PageLayout = com.nativescript.gesturehandler.PageLayout;
5860
}
59-
const layout = new PageLayout(this._context);
61+
const layout = new PageLayout(this._context, ROOT_GESTURE_HANDLER_TAG);
6062
this.gestureRegistry = layout.registry;
6163
return layout;
6264
};

src/gesturehandler.common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030

3131
export const GestureHandlerStateEvent = 'GestureHandlerStateEvent';
3232
export const GestureHandlerTouchEvent = 'GestureHandlerTouchEvent';
33+
export const ROOT_GESTURE_HANDLER_TAG = -12345;
3334

3435
export enum HandlerType {
3536
PAN = 'pan',

0 commit comments

Comments
 (0)