diff options
author | Julian Rex <julian.rex@gmail.com> | 2018-11-20 10:26:09 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-20 10:26:09 -0500 |
commit | 90f609ca8fe92f89374a7c1438511cde0f72195a (patch) | |
tree | 575483facd4204f0da290a5c37da620107737f18 /platform/darwin/src/MGLSource.mm | |
parent | 9ac444a08d9701dc7b0da41859842a77d7be8e00 (diff) | |
download | qtlocation-mapboxgl-90f609ca8fe92f89374a7c1438511cde0f72195a.tar.gz |
[ios, macos] Added `-(BOOL)[MGLStyle removeSource:error:]` that provides an NSError. (#13399)
Diffstat (limited to 'platform/darwin/src/MGLSource.mm')
-rw-r--r-- | platform/darwin/src/MGLSource.mm | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/platform/darwin/src/MGLSource.mm b/platform/darwin/src/MGLSource.mm index a32e223782..62d3cfa808 100644 --- a/platform/darwin/src/MGLSource.mm +++ b/platform/darwin/src/MGLSource.mm @@ -1,6 +1,7 @@ #import "MGLSource_Private.h" #import "MGLStyle_Private.h" #import "MGLMapView_Private.h" +#import "NSBundle+MGLAdditions.h" #include <mbgl/style/style.hpp> #include <mbgl/map/map.hpp> @@ -57,11 +58,39 @@ _mapView.style.rawStyle->addSource(std::move(_pendingSource)); } -- (void)removeFromMapView:(MGLMapView *)mapView { +- (BOOL)removeFromMapView:(MGLMapView *)mapView error:(NSError * __nullable * __nullable)outError { + BOOL removed = NO; + if (self.rawSource == mapView.style.rawStyle->getSource(self.identifier.UTF8String)) { - _pendingSource = mapView.style.rawStyle->removeSource(self.identifier.UTF8String); - _mapView = nil; + + auto removedSource = mapView.style.rawStyle->removeSource(self.identifier.UTF8String); + + if (removedSource) { + removed = YES; + _pendingSource = std::move(removedSource); + _mapView = nil; + } else if (outError) { + NSString *format = NSLocalizedStringWithDefaultValue(@"REMOVE_SRC_FAIL_IN_USE_FMT", @"Foundation", nil, @"Source '%@' is in use, cannot remove.", @"User-friendly error description"); + NSString *localizedDescription = [NSString stringWithFormat:format, self.identifier]; + + *outError = [NSError errorWithDomain:MGLErrorDomain + code:MGLErrorCodeSourceIsInUseCannotRemove + userInfo:@{ NSLocalizedDescriptionKey : localizedDescription }]; + } } + else if (outError) { + // Consider raising an exception here + NSString *format = NSLocalizedStringWithDefaultValue(@"REMOVE_SRC_FAIL_MISMATCH_FMT", @"Foundation", nil, @"Identifier '%@' does not match source identifier '%s'", @"User-friendly error description"); + NSString *localizedDescription = [NSString stringWithFormat:format, + self.identifier, + self.rawSource ? self.rawSource->getID().c_str() : "(null)"]; + + *outError = [NSError errorWithDomain:MGLErrorDomain + code:MGLErrorCodeSourceIdentifierMismatch + userInfo:@{ NSLocalizedDescriptionKey : localizedDescription }]; + } + + return removed; } - (NSString *)description { |