Skip to content

Commit

Permalink
Weak refs to gesture targets (react-native-maps#2818)
Browse files Browse the repository at this point in the history
  • Loading branch information
ukrbublik authored and christopherdro committed Apr 17, 2019
1 parent 90e4a6e commit 55a2879
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/ios/AirGoogleMaps/AIRGoogleMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ @interface AIRGoogleMap ()

- (id)eventFromCoordinate:(CLLocationCoordinate2D)coordinate;

@property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSDictionary*> *origGestureRecognizersMeta;

@end

@implementation AIRGoogleMap
Expand All @@ -62,7 +64,6 @@ @implementation AIRGoogleMap
BOOL _initialCameraSetOnLoad;
BOOL _didCallOnMapReady;
BOOL _didMoveToWindow;
NSMutableDictionary<NSNumber *, NSDictionary*> *_origGestureRecognizersMeta;
BOOL _zoomTapEnabled;
}

Expand Down Expand Up @@ -91,7 +92,7 @@ - (instancetype)init
options:NSKeyValueObservingOptionNew
context:NULL];

_origGestureRecognizersMeta = [[NSMutableDictionary alloc] init];
self.origGestureRecognizersMeta = [[NSMutableDictionary alloc] init];
}
return self;
}
Expand Down Expand Up @@ -629,7 +630,7 @@ -(void)overrideGestureRecognizersForView:(UIView*)view {
NSArray* grs = view.gestureRecognizers;
for (UIGestureRecognizer* gestureRecognizer in grs) {
NSNumber* grHash = [NSNumber numberWithUnsignedInteger:gestureRecognizer.hash];
if([_origGestureRecognizersMeta objectForKey:grHash] != nil)
if([self.origGestureRecognizersMeta objectForKey:grHash] != nil)
continue; //already patched

//get original handlers
Expand All @@ -640,7 +641,10 @@ -(void)overrideGestureRecognizersForView:(UIView*)view {
NSObject* target = [trg valueForKey:@"target"];
SEL action = [self getActionForTarget:trg];
isZoomTapGesture = [NSStringFromSelector(action) isEqualToString:@"handleZoomTapGesture:"];
[origTargetsActions addObject:@{@"target": target, @"action": NSStringFromSelector(action)}];
[origTargetsActions addObject:@{
@"target": [NSValue valueWithNonretainedObject:target],
@"action": NSStringFromSelector(action)
}];
}
if (isZoomTapGesture && self.zoomTapEnabled == NO) {
[view removeGestureRecognizer:gestureRecognizer];
Expand All @@ -649,15 +653,16 @@ -(void)overrideGestureRecognizersForView:(UIView*)view {

//replace with extendedMapGestureHandler
for (NSDictionary* origTargetAction in origTargetsActions) {
NSObject* target = [origTargetAction objectForKey:@"target"];
NSValue* targetValue = [origTargetAction objectForKey:@"target"];
NSObject* target = [targetValue nonretainedObjectValue];
NSString* actionString = [origTargetAction objectForKey:@"action"];
SEL action = NSSelectorFromString(actionString);
[gestureRecognizer removeTarget:target action:action];
}
[gestureRecognizer addTarget:self action:@selector(extendedMapGestureHandler:)];

[_origGestureRecognizersMeta setObject:@{@"targets": origTargetsActions}
forKey:grHash];
[self.origGestureRecognizersMeta setObject:@{@"targets": origTargetsActions}
forKey:grHash];
}
}

Expand Down Expand Up @@ -735,10 +740,11 @@ - (id)extendedMapGestureHandler:(UIGestureRecognizer*)gestureRecognizer {
}

if (performOriginalActions) {
NSDictionary* origMeta = [_origGestureRecognizersMeta objectForKey:grHash];
NSDictionary* origMeta = [self.origGestureRecognizersMeta objectForKey:grHash];
NSDictionary* origTargets = [origMeta objectForKey:@"targets"];
for (NSDictionary* origTarget in origTargets) {
NSObject* target = [origTarget objectForKey:@"target"];
NSValue* targetValue = [origTarget objectForKey:@"target"];
NSObject* target = [targetValue nonretainedObjectValue];
NSString* actionString = [origTarget objectForKey:@"action"];
SEL action = NSSelectorFromString(actionString);
#pragma clang diagnostic push
Expand Down

0 comments on commit 55a2879

Please sign in to comment.