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

Commit

Permalink
[ios, macos] Key-value compliance for MGLStyle
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
1ec5 committed Aug 20, 2016
1 parent b5d7b42 commit 7834552
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
21 changes: 19 additions & 2 deletions platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
#include <mbgl/style/sources/raster_source.hpp>
#include <mbgl/mbgl.hpp>

@interface MGLStyle()
@property (nonatomic, weak) MGLMapView *mapView;
@interface MGLStyle ()

@property (nonatomic, readwrite, weak) MGLMapView *mapView;

@end

@implementation MGLStyle
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -252,19 +263,25 @@ - (Class)classFromLayer:(mbgl::style::Layer *)layer

- (void)removeLayer:(id <MGLStyleLayer_Private>)styleLayer
{
[self willChangeValueForKey:@"layers"];
self.mapView.mbglMap->removeLayer(styleLayer.layer->getID());
[self didChangeValueForKey:@"layers"];
}

- (void)addLayer:(id <MGLStyleLayer, MGLStyleLayer_Private>)styleLayer
{
[self willChangeValueForKey:@"layers"];
self.mapView.mbglMap->addLayer(std::unique_ptr<mbgl::style::Layer>(styleLayer.layer));
[self didChangeValueForKey:@"layers"];
}

- (void)insertLayer:(id <MGLStyleLayer, MGLStyleLayer_Private>)styleLayer
belowLayer:(id <MGLStyleLayer, MGLStyleLayer_Private>)belowLayer
{
[self willChangeValueForKey:@"layers"];
const mbgl::optional<std::string> belowLayerId{[belowLayer layerIdentifier].UTF8String};
self.mapView.mbglMap->addLayer(std::unique_ptr<mbgl::style::Layer>(styleLayer.layer), belowLayerId);
[self didChangeValueForKey:@"layers"];
}

#pragma mark Style classes
Expand Down
5 changes: 4 additions & 1 deletion platform/darwin/src/MGLStyle_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
#include <mbgl/mbgl.hpp>

@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;

Expand Down
15 changes: 11 additions & 4 deletions platform/ios/src/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -1068,10 +1079,6 @@ IB_DESIGNABLE
*/
- (void)removeOverlays:(NS_ARRAY_OF(id <MGLOverlay>) *)overlays;

#pragma mark - Runtime styling API

- (MGLStyle *)style;

#pragma mark Accessing the Underlying Map Data

/**
Expand Down
13 changes: 6 additions & 7 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ @interface MGLMapView () <UIGestureRecognizerDelegate,
@property (nonatomic, readwrite) UIButton *attributionButton;
@property (nonatomic) NS_MUTABLE_ARRAY_OF(NSLayoutConstraint *) *attributionButtonConstraints;
@property (nonatomic) UIActionSheet *attributionSheet;

@property (nonatomic, readwrite) MGLStyle *style;

@property (nonatomic) UIPanGestureRecognizer *pan;
@property (nonatomic) UIPinchGestureRecognizer *pinch;
@property (nonatomic) UIRotationGestureRecognizer *rotate;
Expand Down Expand Up @@ -347,6 +350,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 {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand Down
15 changes: 11 additions & 4 deletions platform/macos/src/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -938,10 +949,6 @@ IB_DESIGNABLE
*/
@property (nonatomic) MGLMapDebugMaskOptions debugMask;

#pragma mark Runtime styling API

- (MGLStyle *)style;

@end

NS_ASSUME_NONNULL_END
12 changes: 5 additions & 7 deletions platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ @interface MGLMapView () <NSPopoverDelegate, MGLMultiPointDelegate>
@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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -793,13 +796,15 @@ - (void)notifyMapChange:(mbgl::MapChange)change {
}
case mbgl::MapChangeWillStartLoadingMap:
{
[self.style willChangeValueForKey:@"layers"];
if ([self.delegate respondsToSelector:@selector(mapViewWillStartLoadingMap:)]) {
[self.delegate mapViewWillStartLoadingMap:self];
}
break;
}
case mbgl::MapChangeDidFinishLoadingMap:
{
[self.style didChangeValueForKey:@"layers"];
if ([self.delegate respondsToSelector:@selector(mapViewDidFinishLoadingMap:)]) {
[self.delegate mapViewDidFinishLoadingMap:self];
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7834552

Please sign in to comment.