Skip to content

Commit

Permalink
fix: some fixes to prevent multiple attach
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Oct 20, 2020
1 parent 6443cdc commit 5bcdb57
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
19 changes: 17 additions & 2 deletions src/gesturehandler.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export abstract class Handler<T extends com.swmansion.gesturehandler.GestureHand
}

tag: number = 0;
attachedView: View;
setTag(tag: number) {
this.tag = tag;
if (this.native) {
Expand All @@ -245,10 +246,22 @@ export abstract class Handler<T extends com.swmansion.gesturehandler.GestureHand
}

attachToView(view: View) {
if (view === this.attachedView) {
return;
}
if (this.attachedView) {
this.detachFromView(this.attachedView);
}
this.attachedView = view;
console.log('attachToView', view);
this.manager.get().attachGestureHandler(this, view);
}
detachFromView(view: View) {
this.manager.get().detachGestureHandler(this, view);
detachFromView(view?: View) {
if (view && view !== this.attachedView) {
return;
}
this.manager.get().detachGestureHandler(this, this.attachedView);
this.attachedView = null;
}
}

Expand Down Expand Up @@ -457,6 +470,7 @@ export class Manager extends ManagerBase {
}
attachGestureHandlerToView(handler: Handler<any, any>, view: View) {
const nHandler = handler.getNative();
console.log('attachGestureHandlerToView', nHandler, view);
if (nHandler) {
const page = view.page as PageGestureExtended;
if (page) {
Expand All @@ -483,6 +497,7 @@ export class Manager extends ManagerBase {

viewListeners = new Map<View, Map<number, { init: () => void; dispose: () => void }>>();
attachGestureHandler(handler: Handler<any, any>, view: View) {
console.log('attachGestureHandler', view, view.nativeView);
if (view.nativeView) {
this.attachGestureHandlerToView(handler, view);
}
Expand Down
4 changes: 2 additions & 2 deletions src/gesturehandler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export abstract class Handler<T, U extends HandlerOptions> extends BaseNative<T,
shouldCancelWhenOutside: boolean;
setTag(tag: number);
getTag(): number;
getView(): any;
getView(): View;
cancel();
attachToView(view: View);
detachFromView(view: View);
detachFromView(view?: View);
}
export interface TapGestureHandlerOptions extends HandlerOptions {
numberOfTaps?: number;
Expand Down
18 changes: 15 additions & 3 deletions src/gesturehandler.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,28 @@ export class Handler<T extends GestureHandler, U extends HandlerOptions> extends
createNative() {
return null;
}
attachedView: View;
attachToView(view: View) {
if (view === this.attachedView) {
return;
}
if (this.attachedView) {
this.detachFromView(this.attachedView);
}
this.attachedView = view;
const tag = this.native.tag;
this.delegate = HandlerDelegate.initWithOwner(new WeakRef(this));
this.native.delegate = this.delegate;
this.manager.get().attachGestureHandler(tag, view);
}
detachFromView(view: View) {
detachFromView(view?: View) {
if (view && view !== this.attachedView) {
return;
}
const tag = this.native.tag;
this.delegate = this.native.delegate = null;
this.manager.get().detachGestureHandler(tag, view);
this.manager.get().detachGestureHandler(tag, this.attachedView);
this.attachedView = null;
}
getTag() {
return this.native.tag;
Expand All @@ -187,7 +199,7 @@ export class Handler<T extends GestureHandler, U extends HandlerOptions> extends
// cant change tag on ios
}
getView() {
return null;
return this.attachedView;
}
cancel() {
this.native.reset();
Expand Down

0 comments on commit 5bcdb57

Please sign in to comment.