Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
convenience function for performing callbacks async on main thread
Browse files Browse the repository at this point in the history
also migrate instances of `void (^)(void)` to `dispatch_block_t`
  • Loading branch information
incanus committed Sep 6, 2016
1 parent f5e67ed commit a20fefb
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,7 @@ - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(
[self setCenterCoordinate:centerCoordinate zoomLevel:zoomLevel direction:direction animated:animated completionHandler:NULL];
}

- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(double)zoomLevel direction:(CLLocationDirection)direction animated:(BOOL)animated completionHandler:(nullable void (^)(void))completion
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(double)zoomLevel direction:(CLLocationDirection)direction animated:(BOOL)animated completionHandler:(nullable dispatch_block_t)completion
{
if ( ! [self viewportWouldChangeWithCenterCoordinate:centerCoordinate
zoomLevel:zoomLevel
Expand All @@ -2218,7 +2218,7 @@ - (void)_setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate animated:(
[self _setCenterCoordinate:centerCoordinate edgePadding:self.contentInset zoomLevel:self.zoomLevel direction:self.direction duration:animated ? MGLAnimationDuration : 0 animationTimingFunction:nil completionHandler:NULL];
}

- (void)_setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate edgePadding:(UIEdgeInsets)insets zoomLevel:(double)zoomLevel direction:(CLLocationDirection)direction duration:(NSTimeInterval)duration animationTimingFunction:(nullable CAMediaTimingFunction *)function completionHandler:(nullable void (^)(void))completion
- (void)_setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate edgePadding:(UIEdgeInsets)insets zoomLevel:(double)zoomLevel direction:(CLLocationDirection)direction duration:(NSTimeInterval)duration animationTimingFunction:(nullable CAMediaTimingFunction *)function completionHandler:(nullable dispatch_block_t)completion
{
if ( ! [self viewportWouldChangeWithCenterCoordinate:centerCoordinate
zoomLevel:zoomLevel
Expand Down Expand Up @@ -2247,9 +2247,7 @@ - (void)_setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate edgePaddin
// Must run asynchronously after the transition is completely over.
// Otherwise, a call to -setCenterCoordinate: within the completion
// handler would reenter the completion handler’s caller.
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
MGLPerformAsyncOnMainThread(completion);
};
}
_mbglMap->easeTo(cameraOptions, animationOptions);
Expand Down Expand Up @@ -2399,22 +2397,22 @@ - (void)setVisibleCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUIn
[self setVisibleCoordinates:coordinates count:count edgePadding:insets direction:direction duration:duration animationTimingFunction:function completionHandler:NULL];
}

- (void)setVisibleCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count edgePadding:(UIEdgeInsets)insets direction:(CLLocationDirection)direction duration:(NSTimeInterval)duration animationTimingFunction:(nullable CAMediaTimingFunction *)function completionHandler:(nullable void (^)(void))completion
- (void)setVisibleCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count edgePadding:(UIEdgeInsets)insets direction:(CLLocationDirection)direction duration:(NSTimeInterval)duration animationTimingFunction:(nullable CAMediaTimingFunction *)function completionHandler:(nullable dispatch_block_t)completion
{
if ( ! [self viewportWouldChangeWithVisibleCoordinates:coordinates
count:count
edgePadding:insets
direction:direction]) return completion();
direction:direction]) return MGLPerformAsyncOnMainThread(completion);

self.userTrackingMode = MGLUserTrackingModeNone;
[self _setVisibleCoordinates:coordinates count:count edgePadding:insets direction:direction duration:duration animationTimingFunction:function completionHandler:completion];
}

- (void)_setVisibleCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count edgePadding:(UIEdgeInsets)insets direction:(CLLocationDirection)direction duration:(NSTimeInterval)duration animationTimingFunction:(nullable CAMediaTimingFunction *)function completionHandler:(nullable void (^)(void))completion
- (void)_setVisibleCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count edgePadding:(UIEdgeInsets)insets direction:(CLLocationDirection)direction duration:(NSTimeInterval)duration animationTimingFunction:(nullable CAMediaTimingFunction *)function completionHandler:(nullable dispatch_block_t)completion
{
const mbgl::CameraOptions cameraOptions = [self cameraOptionsForVisibleCoordinates:coordinates count:count edgePadding:insets direction:direction];

if ( ! [self viewportWouldChangeWithCamera:[self cameraForCameraOptions:cameraOptions]]) return completion();
if ( ! [self viewportWouldChangeWithCamera:[self cameraForCameraOptions:cameraOptions]]) return MGLPerformAsyncOnMainThread(completion);

_mbglMap->cancelTransitions();

Expand All @@ -2428,9 +2426,7 @@ - (void)_setVisibleCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUI
if (completion)
{
animationOptions.transitionFinishFn = [completion]() {
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
MGLPerformAsyncOnMainThread(completion);
};
}

Expand Down Expand Up @@ -2535,7 +2531,7 @@ - (void)setCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration a
[self setCamera:camera withDuration:duration animationTimingFunction:function completionHandler:NULL];
}

- (void)setCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration animationTimingFunction:(nullable CAMediaTimingFunction *)function completionHandler:(nullable void (^)(void))completion
- (void)setCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration animationTimingFunction:(nullable CAMediaTimingFunction *)function completionHandler:(nullable dispatch_block_t)completion
{
if ( ! [self viewportWouldChangeWithCamera:camera]) return completion();

Expand All @@ -2553,9 +2549,7 @@ - (void)setCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration a
if (completion)
{
animationOptions.transitionFinishFn = [completion]() {
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
MGLPerformAsyncOnMainThread(completion);
};
}

Expand All @@ -2564,21 +2558,21 @@ - (void)setCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration a
[self didChangeValueForKey:@"camera"];
}

- (void)flyToCamera:(MGLMapCamera *)camera completionHandler:(nullable void (^)(void))completion
- (void)flyToCamera:(MGLMapCamera *)camera completionHandler:(nullable dispatch_block_t)completion
{
if ( ! [self viewportWouldChangeWithCamera:camera]) return completion();

[self flyToCamera:camera withDuration:-1 completionHandler:completion];
}

- (void)flyToCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration completionHandler:(nullable void (^)(void))completion
- (void)flyToCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration completionHandler:(nullable dispatch_block_t)completion
{
if ( ! [self viewportWouldChangeWithCamera:camera]) return completion();

[self flyToCamera:camera withDuration:duration peakAltitude:-1 completionHandler:completion];
}

- (void)flyToCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration peakAltitude:(CLLocationDistance)peakAltitude completionHandler:(nullable void (^)(void))completion
- (void)flyToCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration peakAltitude:(CLLocationDistance)peakAltitude completionHandler:(nullable dispatch_block_t)completion
{
if ( ! [self viewportWouldChangeWithCamera:camera]) return completion();

Expand All @@ -2587,7 +2581,7 @@ - (void)flyToCamera:(MGLMapCamera *)camera withDuration:(NSTimeInterval)duration
[self _flyToCamera:camera edgePadding:self.contentInset withDuration:duration peakAltitude:peakAltitude completionHandler:completion];
}

- (void)_flyToCamera:(MGLMapCamera *)camera edgePadding:(UIEdgeInsets)insets withDuration:(NSTimeInterval)duration peakAltitude:(CLLocationDistance)peakAltitude completionHandler:(nullable void (^)(void))completion
- (void)_flyToCamera:(MGLMapCamera *)camera edgePadding:(UIEdgeInsets)insets withDuration:(NSTimeInterval)duration peakAltitude:(CLLocationDistance)peakAltitude completionHandler:(nullable dispatch_block_t)completion
{
const mbgl::CameraOptions cameraOptions = [self cameraOptionsForAnimatingToCamera:camera edgePadding:insets];

Expand All @@ -2610,9 +2604,7 @@ - (void)_flyToCamera:(MGLMapCamera *)camera edgePadding:(UIEdgeInsets)insets wit
if (completion)
{
animationOptions.transitionFinishFn = [completion]() {
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
MGLPerformAsyncOnMainThread(completion);
};
}

Expand Down Expand Up @@ -4270,7 +4262,7 @@ - (void)didUpdateLocationSignificantlyAnimated:(BOOL)animated
- (void)didUpdateLocationWithTargetAnimated:(BOOL)animated
{
BOOL firstUpdate = self.userTrackingState == MGLUserTrackingStatePossible;
void (^completion)(void);
dispatch_block_t completion;
if (animated && firstUpdate)
{
self.userTrackingState = MGLUserTrackingStateBegan;
Expand Down Expand Up @@ -4490,7 +4482,12 @@ - (void)updateHeadingForDeviceOrientation

#pragma mark - Utility -

- (void)animateWithDelay:(NSTimeInterval)delay animations:(void (^)(void))animations
void MGLPerformAsyncOnMainThread(dispatch_block_t completion)
{
dispatch_async(dispatch_get_main_queue(), completion);
}

- (void)animateWithDelay:(NSTimeInterval)delay animations:(dispatch_block_t)animations
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), animations);
}
Expand Down Expand Up @@ -5263,7 +5260,7 @@ void MGLFinishCustomStyleLayer(void *context)

@implementation MGLMapView (MGLCustomStyleLayerAdditions)

- (void)insertCustomStyleLayerWithIdentifier:(NSString *)identifier preparationHandler:(void (^)())preparation drawingHandler:(MGLCustomStyleLayerDrawingHandler)drawing completionHandler:(void (^)())completion belowStyleLayerWithIdentifier:(nullable NSString *)otherIdentifier
- (void)insertCustomStyleLayerWithIdentifier:(NSString *)identifier preparationHandler:(void (^)())preparation drawingHandler:(MGLCustomStyleLayerDrawingHandler)drawing completionHandler:(dispatch_block_t)completion belowStyleLayerWithIdentifier:(nullable NSString *)otherIdentifier
{
NSAssert(identifier, @"Style layer needs an identifier");
MGLCustomStyleLayerHandlers *context = new MGLCustomStyleLayerHandlers(preparation, drawing, completion);
Expand Down

0 comments on commit a20fefb

Please sign in to comment.