[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 willChange and didChange calls. The built-in point annotation layer is added after the style is finished loading but before the map is finished loading. Cause a second wave of change notifications to go out, about both sources and layers. Issue change notifications for style layers when shape annotations are added or removed.
This commit is contained in:
Minh Nguyễn
2016-08-19 13:56:46 -07:00
committed by Minh Nguyễn
parent 850b70ff91
commit 9fac640bf3
6 changed files with 108 additions and 33 deletions

View File

@@ -236,6 +236,9 @@ public:
@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;
@@ -284,6 +287,8 @@ public:
std::vector<MGLAnnotationTag> _annotationsNearbyLastTap;
CGPoint _initialImplicitCalloutViewOffset;
NSDate *_userLocationAnimationCompletionDate;
/// True if a willChange notification has been issued for shape annotation layers and a didChange notification is pending.
BOOL _isChangingAnnotationLayers;
BOOL _isWaitingForRedundantReachableNotification;
BOOL _isTargetingInterfaceBuilder;
@@ -355,14 +360,17 @@ public:
- (void)setStyleURL:(nullable NSURL *)styleURL
{
if (_isTargetingInterfaceBuilder) return;
if ( ! styleURL)
{
styleURL = [MGLStyle streetsStyleURLWithVersion:MGLStyleDefaultVersion];
}
styleURL = styleURL.mgl_URLByStandardizingScheme;
[self willChangeValueForKey:@"style"];
_style = [[MGLStyle alloc] initWithMapView:self];
_mbglMap->setStyleURL([[styleURL absoluteString] UTF8String]);
[self didChangeValueForKey:@"style"];
}
- (IBAction)reloadStyle:(__unused id)sender {
@@ -620,13 +628,6 @@ public:
return image;
}
- (MGLStyle *)style
{
MGLStyle *style = [[MGLStyle alloc] init];
style.mapView = self;
return style;
}
- (void)reachabilityChanged:(NSNotification *)notification
{
MGLReachability *reachability = [notification object];
@@ -2895,6 +2896,7 @@ public:
continue;
}
_isChangingAnnotationLayers = YES;
MGLAnnotationTag annotationTag = _mbglMap->addAnnotation([multiPoint annotationObjectWithDelegate:self]);
MGLAnnotationContext context;
context.annotation = annotation;
@@ -2991,6 +2993,10 @@ public:
[self updateAnnotationContainerViewWithAnnotationViews:newAnnotationViews];
[self didChangeValueForKey:@"annotations"];
if (_isChangingAnnotationLayers)
{
[self.style willChangeValueForKey:@"layers"];
}
if ([self.delegate respondsToSelector:@selector(mapView:didAddAnnotationViews:)])
{
@@ -3213,11 +3219,16 @@ public:
[(NSObject *)annotation removeObserver:self forKeyPath:@"coordinates" context:(void *)(NSUInteger)annotationTag];
}
_isChangingAnnotationLayers = YES;
_mbglMap->removeAnnotation(annotationTag);
}
[self didChangeValueForKey:@"annotations"];
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
if (_isChangingAnnotationLayers)
{
[self.style willChangeValueForKey:@"layers"];
}
}
- (void)addOverlay:(id <MGLOverlay>)overlay
@@ -4559,6 +4570,10 @@ public:
}
case mbgl::MapChangeDidFinishLoadingMap:
{
[self.style willChangeValueForKey:@"sources"];
[self.style didChangeValueForKey:@"sources"];
[self.style willChangeValueForKey:@"layers"];
[self.style didChangeValueForKey:@"layers"];
if ([self.delegate respondsToSelector:@selector(mapViewDidFinishLoadingMap:)])
{
[self.delegate mapViewDidFinishLoadingMap:self];
@@ -4602,6 +4617,11 @@ public:
case mbgl::MapChangeDidFinishRenderingFrame:
case mbgl::MapChangeDidFinishRenderingFrameFullyRendered:
{
if (_isChangingAnnotationLayers)
{
_isChangingAnnotationLayers = NO;
[self.style didChangeValueForKey:@"layers"];
}
[self updateAnnotationViews];
if ([self.delegate respondsToSelector:@selector(mapViewDidFinishRenderingFrame:fullyRendered:)])
{
@@ -4611,6 +4631,11 @@ public:
}
case mbgl::MapChangeDidFinishLoadingStyle:
{
[self.style willChangeValueForKey:@"name"];
[self.style willChangeValueForKey:@"sources"];
[self.style didChangeValueForKey:@"sources"];
[self.style willChangeValueForKey:@"layers"];
[self.style didChangeValueForKey:@"layers"];
if ([self.delegate respondsToSelector:@selector(mapView:didFinishLoadingStyle:)])
{
[self.delegate mapView:self didFinishLoadingStyle:self.style];