Repository navigation
Added -(BOOL)[MGLStyle removeSource:error:] that provides an NSError.
#13399
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 @@ - (void)addToMapView:(MGLMapView *)mapView { | |
| _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)"]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By using an Objective-C self.rawSource ? @(self.rawSource->getID().c_str()) : nil |
||
|
|
||
| *outError = [NSError errorWithDomain:MGLErrorDomain | ||
| code:MGLErrorCodeSourceIdentifierMismatch | ||
| userInfo:@{ NSLocalizedDescriptionKey : localizedDescription }]; | ||
| } | ||
|
|
||
| return removed; | ||
| } | ||
|
|
||
| - (NSString *)description { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -318,6 +318,27 @@ MGL_EXPORT | |
| */ | ||
| - (void)removeSource:(MGLSource *)source; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we going to live with both methods or should we deprecate this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can hold off on deprecation for the moment. |
||
|
|
||
| /** | ||
| Removes a source from the current style. | ||
|
|
||
| @note Source identifiers are not guaranteed to exist across styles or different | ||
| versions of the same style. Applications that use this API must first set the | ||
| style URL to an explicitly versioned style using a convenience method like | ||
| `+[MGLStyle outdoorsStyleURLWithVersion:]`, `MGLMapView`’s “Style URL” | ||
| inspectable in Interface Builder, or a manually constructed `NSURL`. This | ||
| approach also avoids source identifer name changes that will occur in the default | ||
| style’s sources over time. | ||
|
|
||
| @param source The source to remove from the current style. | ||
| @param outError Upon return, if an error has occurred, a pointer to an `NSError` | ||
| object describing the error. Pass in `NULL` to ignore any error. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that is makes a difference in practice, but since
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but it's a " pointer-to-pointer-to-ObjC" object thus following the apple's convention on using |
||
|
|
||
| @return `YES` if `source` was removed successfully. If `NO`, `outError` contains | ||
| an `NSError` object describing the problem. | ||
| */ | ||
| - (BOOL)removeSource:(MGLSource *)source error:(NSError * __nullable * __nullable)outError; | ||
|
|
||
|
|
||
| #pragma mark Managing Style Layers | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty specific error to use as the localized description. Note that localized descriptions are meant to be displayed to the end user, at least in theory. Other fields, such as the failure reason, can be used for technical details for the developer. In practice, it’s unlikely this error message would be displayed in an alert, but translators are going to find this string a bit more difficult to comprehend than the usual fare. By the way, I wonder if the Transifex and NSLocalizedString will correctly handle
%s.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I thought I had add a comment about this somewhere: I'm tempted to change this before release so that the description is generic, and we just use a raw (not translated) message as the failure reason.
Do we have a generic error message that can be reused here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, I guess it’s OK for these error messages to contain some technical details.
-[MGLStyle removeSource:error:]is likely to be called as part of some larger task, not directly in response to a user action. (Mapbox Studio Preview and macosapp are the only applications that would likely contain a user-facing Remove Source command.)ADD_FILE_CONTENTS_FAILED_DESCis another localizable error description that wouldn’t be presented to the user, since the user has no awareness about file paths.The documentation about underlying errors implies that some subsystem (in this case, the map SDK) would produce an error with less than user-friendly text. Examples include errors that originate from NSURLSession or NSFileManager. Such errors are sometimes localized, so it’s also OK to keep these strings in the localization files, but we should keep them to a minimum to avoid unnecessary translator effort.
Nonetheless, I’ve opened #13492 to revise these messages.