From 7834552f28e31fdc1f3515c8c3ac2e27f45fb2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Fri, 19 Aug 2016 13:56:46 -0700 Subject: [PATCH] [ios, macos] Key-value compliance for MGLStyle Replaced -[MGLMapView style] with a property. Keep the MGLStyle object around for the lifetime of the style. Bracket changes to layers in will/did change calls. --- platform/darwin/src/MGLStyle.mm | 21 +++++++++++++++++++-- platform/darwin/src/MGLStyle_Private.h | 5 ++++- platform/ios/src/MGLMapView.h | 15 +++++++++++---- platform/ios/src/MGLMapView.mm | 13 ++++++------- platform/macos/src/MGLMapView.h | 15 +++++++++++---- platform/macos/src/MGLMapView.mm | 12 +++++------- 6 files changed, 56 insertions(+), 25 deletions(-) diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index 4805d223737..a92c1140c2b 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -32,8 +32,10 @@ #include #include -@interface MGLStyle() -@property (nonatomic, weak) MGLMapView *mapView; +@interface MGLStyle () + +@property (nonatomic, readwrite, weak) MGLMapView *mapView; + @end @implementation MGLStyle @@ -90,6 +92,15 @@ + (NSURL *)emeraldStyleURL { return MGLStyleURL_emerald; } +#pragma mark - + +- (instancetype)initWithMapView:(MGLMapView *)mapView { + if (self = [super init]) { + _mapView = mapView; + } + return self; +} + #pragma mark Sources - (mbgl::style::Source *)mbglSourceWithIdentifier:(NSString *)identifier @@ -252,19 +263,25 @@ - (Class)classFromLayer:(mbgl::style::Layer *)layer - (void)removeLayer:(id )styleLayer { + [self willChangeValueForKey:@"layers"]; self.mapView.mbglMap->removeLayer(styleLayer.layer->getID()); + [self didChangeValueForKey:@"layers"]; } - (void)addLayer:(id )styleLayer { + [self willChangeValueForKey:@"layers"]; self.mapView.mbglMap->addLayer(std::unique_ptr(styleLayer.layer)); + [self didChangeValueForKey:@"layers"]; } - (void)insertLayer:(id )styleLayer belowLayer:(id )belowLayer { + [self willChangeValueForKey:@"layers"]; const mbgl::optional belowLayerId{[belowLayer layerIdentifier].UTF8String}; self.mapView.mbglMap->addLayer(std::unique_ptr(styleLayer.layer), belowLayerId); + [self didChangeValueForKey:@"layers"]; } #pragma mark Style classes diff --git a/platform/darwin/src/MGLStyle_Private.h b/platform/darwin/src/MGLStyle_Private.h index 46291b37b9b..b7c2fa4cdb3 100644 --- a/platform/darwin/src/MGLStyle_Private.h +++ b/platform/darwin/src/MGLStyle_Private.h @@ -6,7 +6,10 @@ #include @interface MGLStyle (Private) -@property (nonatomic, weak) MGLMapView *mapView; + +- (instancetype)initWithMapView:(MGLMapView *)mapView; + +@property (nonatomic, readonly, weak) MGLMapView *mapView; - (void)setStyleClasses:(NS_ARRAY_OF(NSString *) *)appliedClasses transitionDuration:(NSTimeInterval)transitionDuration; diff --git a/platform/ios/src/MGLMapView.h b/platform/ios/src/MGLMapView.h index db273ebcb65..c0bc0a0f0f8 100644 --- a/platform/ios/src/MGLMapView.h +++ b/platform/ios/src/MGLMapView.h @@ -136,6 +136,14 @@ IB_DESIGNABLE #pragma mark Configuring the Map’s Appearance +/** + The style currently displayed in the receiver. + + Unlike the `styleURL` property, this property is set to an object that allows + you to manipulate every aspect of the style locally. + */ +@property (nonatomic, readonly) MGLStyle *style; + /** URLs of the styles bundled with the library. @@ -153,6 +161,9 @@ IB_DESIGNABLE If you set this property to `nil`, the receiver will use the default style and this property will automatically be set to that style’s URL. + + If you want to modify the current style without replacing it outright, or if + you want to introspect individual style attributes, use the `style` property. */ @property (nonatomic, null_resettable) NSURL *styleURL; @@ -1068,10 +1079,6 @@ IB_DESIGNABLE */ - (void)removeOverlays:(NS_ARRAY_OF(id ) *)overlays; -#pragma mark - Runtime styling API - -- (MGLStyle *)style; - #pragma mark Accessing the Underlying Map Data /** diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index d588f797ccd..f880d85cdec 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -223,6 +223,9 @@ @interface MGLMapView () setStyleURL([[styleURL absoluteString] UTF8String]); + self.style = [[MGLStyle alloc] initWithMapView:self]; } - (IBAction)reloadStyle:(__unused id)sender { @@ -598,13 +602,6 @@ - (UIImage *)compassImage return image; } -- (MGLStyle *)style -{ - MGLStyle *style = [[MGLStyle alloc] init]; - style.mapView = self; - return style; -} - - (void)reachabilityChanged:(NSNotification *)notification { MGLReachability *reachability = [notification object]; @@ -4480,6 +4477,7 @@ - (void)notifyMapChange:(mbgl::MapChange)change } case mbgl::MapChangeWillStartLoadingMap: { + [self.style willChangeValueForKey:@"layers"]; if ([self.delegate respondsToSelector:@selector(mapViewWillStartLoadingMap:)]) { [self.delegate mapViewWillStartLoadingMap:self]; @@ -4488,6 +4486,7 @@ - (void)notifyMapChange:(mbgl::MapChange)change } case mbgl::MapChangeDidFinishLoadingMap: { + [self.style didChangeValueForKey:@"layers"]; if ([self.delegate respondsToSelector:@selector(mapViewDidFinishLoadingMap:)]) { [self.delegate mapViewDidFinishLoadingMap:self]; diff --git a/platform/macos/src/MGLMapView.h b/platform/macos/src/MGLMapView.h index 37c13802b1f..07d351d8284 100644 --- a/platform/macos/src/MGLMapView.h +++ b/platform/macos/src/MGLMapView.h @@ -121,6 +121,14 @@ IB_DESIGNABLE #pragma mark Configuring the Map’s Appearance +/** + The style currently displayed in the receiver. + + Unlike the `styleURL` property, this property is set to an object that allows + you to manipulate every aspect of the style locally. + */ +@property (nonatomic, readonly) MGLStyle *style; + /** URL of the style currently displayed in the receiver. @@ -130,6 +138,9 @@ IB_DESIGNABLE If you set this property to `nil`, the receiver will use the default style and this property will automatically be set to that style’s URL. + + If you want to modify the current style without replacing it outright, or if + you want to introspect individual style attributes, use the `style` property. */ @property (nonatomic, null_resettable) NSURL *styleURL; @@ -938,10 +949,6 @@ IB_DESIGNABLE */ @property (nonatomic) MGLMapDebugMaskOptions debugMask; -#pragma mark Runtime styling API - -- (MGLStyle *)style; - @end NS_ASSUME_NONNULL_END diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index 7b7ab0247b0..5995f943b3e 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -137,6 +137,8 @@ @interface MGLMapView () @property (nonatomic, readwrite) NSImageView *logoView; @property (nonatomic, readwrite) NSView *attributionView; +@property (nonatomic, readwrite) MGLStyle *style; + /// Mapping from reusable identifiers to annotation images. @property (nonatomic) NS_MUTABLE_DICTIONARY_OF(NSString *, MGLAnnotationImage *) *annotationImagesByIdentifier; /// Currently shown popover representing the selected annotation. @@ -542,6 +544,7 @@ - (void)setStyleURL:(nullable NSURL *)styleURL { styleURL = styleURL.mgl_URLByStandardizingScheme; _mbglMap->setStyleURL(styleURL.absoluteString.UTF8String); + self.style = [[MGLStyle alloc] initWithMapView:self]; } - (IBAction)reloadStyle:(__unused id)sender { @@ -793,6 +796,7 @@ - (void)notifyMapChange:(mbgl::MapChange)change { } case mbgl::MapChangeWillStartLoadingMap: { + [self.style willChangeValueForKey:@"layers"]; if ([self.delegate respondsToSelector:@selector(mapViewWillStartLoadingMap:)]) { [self.delegate mapViewWillStartLoadingMap:self]; } @@ -800,6 +804,7 @@ - (void)notifyMapChange:(mbgl::MapChange)change { } case mbgl::MapChangeDidFinishLoadingMap: { + [self.style didChangeValueForKey:@"layers"]; if ([self.delegate respondsToSelector:@selector(mapViewDidFinishLoadingMap:)]) { [self.delegate mapViewDidFinishLoadingMap:self]; } @@ -2117,13 +2122,6 @@ - (void)updateAnnotationCallouts { #pragma mark - Runtime styling -- (MGLStyle *)style -{ - MGLStyle *style = [[MGLStyle alloc] init]; - style.mapView = self; - return style; -} - - (mbgl::Map *)mbglMap { return _mbglMap;