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

Commit

Permalink
[ios, macos] Added completion handlers to animated MGLMapView methods
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed May 29, 2019
1 parent 0a7919e commit 4fc539c
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 134 deletions.
11 changes: 6 additions & 5 deletions platform/darwin/src/MGLGeometry_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ NS_INLINE BOOL MGLLocationCoordinate2DIsValid(CLLocationCoordinate2D coordinate)
}

#if TARGET_OS_IPHONE
NS_INLINE mbgl::EdgeInsets MGLEdgeInsetsFromNSEdgeInsets(UIEdgeInsets insets) {
return { insets.top, insets.left, insets.bottom, insets.right };
}
#define MGLEdgeInsets UIEdgeInsets
#define MGLEdgeInsetsZero UIEdgeInsetsZero
#else
NS_INLINE mbgl::EdgeInsets MGLEdgeInsetsFromNSEdgeInsets(NSEdgeInsets insets) {
#define MGLEdgeInsets NSEdgeInsets
#define MGLEdgeInsetsZero NSEdgeInsetsZero
#endif
NS_INLINE mbgl::EdgeInsets MGLEdgeInsetsFromNSEdgeInsets(MGLEdgeInsets insets) {
return { insets.top, insets.left, insets.bottom, insets.right };
}
#endif

/** Returns MGLRadianCoordinate2D, converted from CLLocationCoordinate2D. */
NS_INLINE MGLRadianCoordinate2D MGLRadianCoordinateFromLocationCoordinate(CLLocationCoordinate2D locationCoordinate) {
Expand Down
3 changes: 1 addition & 2 deletions platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#import "NSExpression+MGLPrivateAdditions.h"

#import "MGLTypes.h"
#import "MGLGeometry_Private.h"
#if TARGET_OS_IPHONE
#import "UIColor+MGLAdditions.h"
#define MGLEdgeInsets UIEdgeInsets
#else
#import "NSColor+MGLAdditions.h"
#define MGLEdgeInsets NSEdgeInsets
#endif
#import "NSPredicate+MGLAdditions.h"
#import "NSValue+MGLStyleAttributeAdditions.h"
Expand Down
7 changes: 1 addition & 6 deletions platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#import "NSValue+MGLStyleAttributeAdditions.h"
#import "MGLLight.h"
#import "MGLLoggingConfiguration_Private.h"
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#define MGLEdgeInsets UIEdgeInsets
#else
#define MGLEdgeInsets NSEdgeInsets
#endif
#import "MGLGeometry_Private.h"

@implementation NSValue (MGLStyleAttributeAdditions)

Expand Down
111 changes: 111 additions & 0 deletions platform/darwin/test/MGLMapViewTests.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#import <Mapbox/Mapbox.h>
#import <XCTest/XCTest.h>
#import <TargetConditionals.h>

#if TARGET_OS_IPHONE
#define MGLEdgeInsetsZero UIEdgeInsetsZero
#else
#define MGLEdgeInsetsZero NSEdgeInsetsZero
#endif

static MGLMapView *mapView;

Expand Down Expand Up @@ -41,4 +48,108 @@ - (void)testCoordinateBoundsConversion {
XCTAssertTrue(CGRectIntersectsRect(spanningBoundsRect, rightAntimeridianBoundsRect), @"Something");
}

#if TARGET_OS_IPHONE
- (void)testUserTrackingModeCompletion {
__block BOOL completed = NO;
[mapView setUserTrackingMode:MGLUserTrackingModeNone animated:NO completionHandler:^{
completed = YES;
}];
XCTAssertTrue(completed, @"Completion block should get called synchronously when the mode is unchanged.");

completed = NO;
[mapView setUserTrackingMode:MGLUserTrackingModeNone animated:YES completionHandler:^{
completed = YES;
}];
XCTAssertTrue(completed, @"Completion block should get called synchronously when the mode is unchanged.");

completed = NO;
[mapView setUserTrackingMode:MGLUserTrackingModeFollow animated:NO completionHandler:^{
completed = YES;
}];
XCTAssertTrue(completed, @"Completion block should get called synchronously when there’s no location.");

completed = NO;
[mapView setUserTrackingMode:MGLUserTrackingModeFollowWithHeading animated:YES completionHandler:^{
completed = YES;
}];
XCTAssertTrue(completed, @"Completion block should get called synchronously when there’s no location.");
}

- (void)testTargetCoordinateCompletion {
__block BOOL completed = NO;
[mapView setTargetCoordinate:kCLLocationCoordinate2DInvalid animated:NO completionHandler:^{
completed = YES;
}];
XCTAssertTrue(completed, @"Completion block should get called synchronously when the target coordinate is unchanged.");

completed = NO;
[mapView setTargetCoordinate:kCLLocationCoordinate2DInvalid animated:YES completionHandler:^{
completed = YES;
}];
XCTAssertTrue(completed, @"Completion block should get called synchronously when the target coordinate is unchanged.");

completed = NO;
[mapView setUserTrackingMode:MGLUserTrackingModeFollow animated:NO completionHandler:nil];
[mapView setTargetCoordinate:CLLocationCoordinate2DMake(39.128106, -84.516293) animated:YES completionHandler:^{
completed = YES;
}];
XCTAssertTrue(completed, @"Completion block should get called synchronously when not tracking user course.");

completed = NO;
[mapView setUserTrackingMode:MGLUserTrackingModeFollowWithCourse animated:NO completionHandler:nil];
[mapView setTargetCoordinate:CLLocationCoordinate2DMake(39.224407, -84.394957) animated:YES completionHandler:^{
completed = YES;
}];
XCTAssertTrue(completed, @"Completion block should get called synchronously when there’s no location.");
}
#endif

- (void)testVisibleCoordinatesCompletion {
XCTestExpectation *expectation = [self expectationWithDescription:@"Completion block should get called when not animated"];
MGLCoordinateBounds unitBounds = MGLCoordinateBoundsMake(CLLocationCoordinate2DMake(0, 0), CLLocationCoordinate2DMake(1, 1));
[mapView setVisibleCoordinateBounds:unitBounds edgePadding:MGLEdgeInsetsZero animated:NO completionHandler:^{
[expectation fulfill];
}];
[self waitForExpectations:@[expectation] timeout:1];

#if TARGET_OS_IPHONE
expectation = [self expectationWithDescription:@"Completion block should get called when animated"];
CLLocationCoordinate2D antiunitCoordinates[] = {
CLLocationCoordinate2DMake(0, 0),
CLLocationCoordinate2DMake(-1, -1),
};
[mapView setVisibleCoordinates:antiunitCoordinates
count:sizeof(antiunitCoordinates) / sizeof(antiunitCoordinates[0])
edgePadding:UIEdgeInsetsZero
direction:0
duration:0
animationTimingFunction:nil
completionHandler:^{
[expectation fulfill];
}];
[self waitForExpectations:@[expectation] timeout:1];
#endif
}

- (void)testShowAnnotationsCompletion {
__block BOOL completed = NO;
[mapView showAnnotations:@[] edgePadding:MGLEdgeInsetsZero animated:NO completionHandler:^{
completed = YES;
}];
XCTAssertTrue(completed, @"Completion block should get called synchronously when there are no annotations to show.");

XCTestExpectation *expectation = [self expectationWithDescription:@"Completion block should get called when not animated"];
MGLPointAnnotation *annotation = [[MGLPointAnnotation alloc] init];
[mapView showAnnotations:@[annotation] edgePadding:MGLEdgeInsetsZero animated:NO completionHandler:^{
[expectation fulfill];
}];
[self waitForExpectations:@[expectation] timeout:1];

expectation = [self expectationWithDescription:@"Completion block should get called when animated."];
[mapView showAnnotations:@[annotation] edgePadding:MGLEdgeInsetsZero animated:YES completionHandler:^{
[expectation fulfill];
}];
[self waitForExpectations:@[expectation] timeout:1];
}

@end
10 changes: 10 additions & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT

* Implemented asymmetric center of perspective: fixed an issue that caused the focal point to be always based on the view's horizontal center when setting [MGLMapView contentInset](https://docs.mapbox.com/ios/api/maps/5.0.0/Classes/MGLMapView.html#/c:objc(cs)MGLMapView(py)contentInset). ([#14664](https://github.com/mapbox/mapbox-gl-native/pull/14664))

### Other changes

* Added variants of several animated `MGLMapView` methods that accept completion handlers ([#14381](https://github.com/mapbox/mapbox-gl-native/pull/14381)):
* `-[MGLMapView setVisibleCoordinateBounds:edgePadding:animated:completionHandler:]`
* `-[MGLMapView setContentInset:animated:completionHandler:]`
* `-[MGLMapView setUserTrackingMode:animated:completionHandler:]`
* `-[MGLMapView setTargetCoordinate:animated:completionHandler:]`
* `-[MGLMapView showAnnotations:edgePadding:animated:completionHandler:]`
* `-[MGLMapView selectAnnotation:animated:completionHandler:]`

## 5.0.0 - May 22, 2019

This release improves how monthly active users are counted. By upgrading to this release, you are opting into the changes outlined in [this blog post](https://www.mapbox.com/52219) and [#14421](https://github.com/mapbox/mapbox-gl-native/pull/14421).
Expand Down
Loading

0 comments on commit 4fc539c

Please sign in to comment.