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

Commit

Permalink
[ios, macos] handle duplicate layer error
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Dec 7, 2016
1 parent daa39cf commit 57b0ba7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
38 changes: 31 additions & 7 deletions platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,18 @@ - (void)insertObject:(MGLStyleLayer *)styleLayer inLayersAtIndex:(NSUInteger)ind
[NSException raise:NSRangeException
format:@"Cannot insert style layer at out-of-bounds index %lu.", (unsigned long)index];
} else if (index == 0) {
[styleLayer addToMapView:self.mapView];
try {
[styleLayer addToMapView:self.mapView];
} catch (const std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
} else {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(layers.size() - index)];
[styleLayer addToMapView:self.mapView belowLayer:sibling];
try {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(layers.size() - index)];
[styleLayer addToMapView:self.mapView belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
}
}

Expand Down Expand Up @@ -350,7 +358,11 @@ - (void)addLayer:(MGLStyleLayer *)layer
layer];
}
[self willChangeValueForKey:@"layers"];
[layer addToMapView:self.mapView];
try {
[layer addToMapView:self.mapView];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
[self didChangeValueForKey:@"layers"];
}

Expand All @@ -375,7 +387,11 @@ - (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)sibling
sibling];
}
[self willChangeValueForKey:@"layers"];
[layer addToMapView:self.mapView belowLayer:sibling];
try {
[layer addToMapView:self.mapView belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
[self didChangeValueForKey:@"layers"];
}

Expand Down Expand Up @@ -413,10 +429,18 @@ - (void)insertLayer:(MGLStyleLayer *)layer aboveLayer:(MGLStyleLayer *)sibling {
@"Make sure sibling was obtained using -[MGLStyle layerWithIdentifier:].",
sibling];
} else if (index + 1 == layers.size()) {
[layer addToMapView:self.mapView];
try {
[layer addToMapView:self.mapView];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
} else {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index + 1)];
[layer addToMapView:self.mapView belowLayer:sibling];
try {
[layer addToMapView:self.mapView belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
}
[self didChangeValueForKey:@"layers"];
}
Expand Down
27 changes: 27 additions & 0 deletions platform/darwin/test/MGLStyleTests.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#import "MGLMapView.h"
#import "MGLStyle_Private.h"
#if TARGET_OS_IPHONE
#import "MGLMapView+MGLCustomStyleLayerAdditions.h"
#endif

#import "MGLGeoJSONSource.h"
#import "MGLRasterSource.h"
Expand Down Expand Up @@ -165,6 +168,30 @@ - (void)testAddingLayersTwice {
XCTAssertThrowsSpecificNamed([self.style addLayer:symbolLayer], NSException, @"MGLRedundantLayerException");
}

- (void)testAddingLayersWithDuplicateIdentifiers {
//Just some source
MGLVectorSource *source = [[MGLVectorSource alloc] initWithIdentifier:@"my-source" URL:[NSURL URLWithString:@"mapbox://mapbox.mapbox-terrain-v2"]];
[self.mapView.style addSource: source];

//Add initial layer
MGLFillStyleLayer *initial = [[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source];
[self.mapView.style addLayer:initial];

//Try to add the duplicate
XCTAssertThrowsSpecificNamed([self.mapView.style addLayer:[[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source]], NSException, @"MGLRedundantLayerIdentifierException");
XCTAssertThrowsSpecificNamed([self.mapView.style insertLayer:[[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source] belowLayer:initial],NSException, @"MGLRedundantLayerIdentifierException");
XCTAssertThrowsSpecificNamed([self.mapView.style insertLayer:[[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source] aboveLayer:initial], NSException, @"MGLRedundantLayerIdentifierException");
XCTAssertThrowsSpecificNamed([self.mapView.style insertLayer:[[MGLFillStyleLayer alloc] initWithIdentifier:@"my-layer" source:source] atIndex:0], NSException, @"MGLRedundantLayerIdentifierException");

#if TARGET_OS_IPHONE
//Try to insert a duplicate custom layer
MGLCustomStyleLayerDrawingHandler drawingHandler = ^(CGSize size, CLLocationCoordinate2D centerCoordinate, double zoomLevel, CLLocationDirection direction, CGFloat pitch, CGFloat perspectiveSkew) {
};

XCTAssertThrowsSpecific([self.mapView insertCustomStyleLayerWithIdentifier:@"my-layer" preparationHandler:^{} drawingHandler:drawingHandler completionHandler: ^{} belowStyleLayerWithIdentifier:nil], NSException);
#endif
}

- (NSString *)stringWithContentsOfStyleHeader {
NSURL *styleHeaderURL = [[[NSBundle mgl_frameworkBundle].bundleURL
URLByAppendingPathComponent:@"Headers" isDirectory:YES]
Expand Down
10 changes: 7 additions & 3 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5353,9 +5353,13 @@ - (void)insertCustomStyleLayerWithIdentifier:(NSString *)identifier preparationH
{
NSAssert(identifier, @"Style layer needs an identifier");
MGLCustomStyleLayerHandlers *context = new MGLCustomStyleLayerHandlers(preparation, drawing, completion);
_mbglMap->addLayer(std::make_unique<mbgl::style::CustomLayer>(identifier.UTF8String, MGLPrepareCustomStyleLayer,
MGLDrawCustomStyleLayer, MGLFinishCustomStyleLayer, context),
otherIdentifier ? mbgl::optional<std::string>(otherIdentifier.UTF8String) : mbgl::optional<std::string>());
try {
_mbglMap->addLayer(std::make_unique<mbgl::style::CustomLayer>(identifier.UTF8String, MGLPrepareCustomStyleLayer,
MGLDrawCustomStyleLayer, MGLFinishCustomStyleLayer, context),
otherIdentifier ? mbgl::optional<std::string>(otherIdentifier.UTF8String) : mbgl::optional<std::string>());
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
}
}

- (void)removeCustomStyleLayerWithIdentifier:(NSString *)identifier
Expand Down

0 comments on commit 57b0ba7

Please sign in to comment.