Skip to content

Commit

Permalink
fix(ios): prevent error while overriding N gestures
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Nov 21, 2023
1 parent a6349e8 commit bb04710
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- (void)updateGestureHandler:(nonnull NSNumber *)handlerTag config:(nonnull NSDictionary *)config;

- (void)dropGestureHandler:(nonnull NSNumber *)handlerTag;
- (void)detachGestureHandler:(nonnull NSNumber *)handlerTag;

//- (void)handleSetJSResponder:(nonnull UIView *)view
// blockNativeResponder:(nonnull NSNumber *)blockNativeResponder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ - (void)dropGestureHandler:(NSNumber *)handlerTag
[_registry dropHandlerWithTag:handlerTag];
}

- (void)detachGestureHandler:(NSNumber *)handlerTag
{
[_registry detachHandlerWithTag:handlerTag];
}
//- (void)handleSetJSResponder:(NSNumber *)viewTag blockNativeResponder:(NSNumber *)blockNativeResponder
//{
// if ([blockNativeResponder boolValue]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
- (void)registerGestureHandler:(nonnull GestureHandler *)gestureHandler;
- (void)attachHandlerWithTag:(nonnull NSNumber *)handlerTag toView:(nonnull UIView *)view;
- (void)dropHandlerWithTag:(nonnull NSNumber *)handlerTag;
- (void)detachHandlerWithTag:(nonnull NSNumber *)handlerTag;
- (void)detachHandlerWithTag:(nonnull NSNumber *)handlerTag;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ - (void)dropHandlerWithTag:(NSNumber *)handlerTag
[_handlers removeObjectForKey:handlerTag];
}

- (void)detachHandlerWithTag:(NSNumber *)handlerTag
{
GestureHandler *handler = _handlers[handlerTag];
[handler unbindFromView];
}
@end
4 changes: 2 additions & 2 deletions src/gesturehandler/gesturehandler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export enum FlingDirection {
DIRECTION_LEFT,
DIRECTION_UP,
DIRECTION_DOWN,
DIRECTION_RIGHT,
DIRECTION_RIGHT
}

export abstract class BaseNative<T, U extends {}> extends Observable {
Expand Down Expand Up @@ -65,7 +65,7 @@ export abstract class Handler<T, U extends HandlerOptions> extends BaseNative<T,
getView(): View;
cancel();
attachToView(view: View);
detachFromView(view?: View);
detachFromView(view?: View, drop?: boolean);
}
export interface TapGestureHandlerOptions extends HandlerOptions {
numberOfTaps?: number;
Expand Down
12 changes: 7 additions & 5 deletions src/gesturehandler/gesturehandler.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ export class Handler<T extends GestureHandler, U extends HandlerOptions> extends
return;
}
if (this.attachedView) {
this.detachFromView(this.attachedView);
this.detachFromView(this.attachedView, false);
}
this.attachedView = view;
this.delegate = HandlerDelegate.initWithOwner(new WeakRef(this));
this.native.delegate = this.delegate;
this.manager.get().attachGestureHandler(this, view);
}
detachFromView(view?: View) {
detachFromView(view?: View, drop = true) {
if ((view && view !== this.attachedView) || !this.attachedView) {
return;
}
Expand All @@ -212,7 +212,7 @@ export class Handler<T extends GestureHandler, U extends HandlerOptions> extends
}
const tag = this.native.tag;
this.delegate = this.native.delegate = null;
this.manager.get().detachGestureHandler(tag, this.attachedView);
this.manager.get().detachGestureHandler(tag, this.attachedView, drop);
this.attachedView = null;
}
getTag() {
Expand Down Expand Up @@ -274,9 +274,11 @@ export class Manager extends ManagerBase {
dispose: onDispose
});
}
detachGestureHandler(handlerTag: number, view: View) {
if (view.nativeView) {
detachGestureHandler(handlerTag: number, view: View, drop = true) {
if (drop) {
this.manager.dropGestureHandler(handlerTag);
} else {
this.manager.detachGestureHandler(handlerTag);
}
if (view) {
const viewListeners = this.viewListeners.get(view);
Expand Down
6 changes: 3 additions & 3 deletions src/gesturehandler/gestures_override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class GesturesObserver {
}

public disconnect() {
this._detach();
this._detach(true);

if (this.target) {
this.target.off('loaded', this._onTargetLoaded);
Expand Down Expand Up @@ -123,12 +123,12 @@ export class GesturesObserver {
}
}

private _detach() {
private _detach(drop = false) {
if (this.gestureHandler) {
// dont detach events. It will be done on dispose
// this.gestureHandler.off(GestureHandlerStateEvent);
// this.gestureHandler.off(GestureHandlerTouchEvent);
this.gestureHandler.detachFromView(this.target);
this.gestureHandler.detachFromView(this.target, drop);
}
this._notifyTouch = false;
this._eventData = {};
Expand Down
1 change: 1 addition & 0 deletions src/gesturehandler/typings/ios.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ declare class GestureHandlerManager extends NSObject {
createGestureHandlerTagConfig(handlerName: string, handlerTag: number, config: NSDictionary<any, any>): GestureHandler;

dropGestureHandler(handlerTag: number): void;
detachGestureHandler(handlerTag: number): void;

updateGestureHandlerConfig(handlerTag: number, config: NSDictionary<any, any>): void;
}
Expand Down

0 comments on commit bb04710

Please sign in to comment.