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

Commit ef71597

Browse files
authored
[ios, macos] Simplify MGLSource and subclasses (#7377)
* [ios, macos] Audited source headers for nullability * [macos] Made MGLTileSet public * [ios, macos] Replaced MGLTileSet with MGLTileSource MGLRasterSource and MGLVectorSource now share a common abstract superclass, MGLTileSource. MGLTileSet has been removed. MGLTileSource is modeled after mbgl::style::RasterSource and mbgl::style::VectorSource. It has initializers that incorporate the parameters of MGLTileSet’s initializers, but it lacks getters for everything but the attribution string. MGLTileSet’s properties have been converted into options that can be passed into MGLTileSource’s initializers in a dictionary. Properly implement rawSource as a covariant property so that it doesn’t end up getting autosynthesized as a shadow ivar. This prevents concrete subclasses of MGLSource from setting _rawSource directly but getting a different value out of self.rawSource. Marked -[MGLSource init] as unavailable and ensured that concrete subclasses of MGLSource have the right set of initializers marked as designated initializers. Documentation comments for each concrete source class identify the corresponding source type in the style specification. Clarified the purpose of MGLTileSetScheme, now known as MGLTileCoordinateSystem. * [ios, macos] Clarified tile size interpretation Sticking to a default value of 256 for mapbox: URLs, but other URLs get the standard 512 value. * [ios, macos] rawSource is always set * [ios, macos] Cleaned up MGLShapeSource initialization rawSource is never nil, so there’s no need for a -commonInit method. Extracted -geoJSONOptions from MGLShapeSource into a standalone function for easier testing. * [ios, macos] Synchronized headers in project Realphabetized headers in groups. Added headers missing from one project or the other. * [ios, macos] Added MGLShape methods to (de)serialize GeoJSON data Added a class initializer and instance method to MGLShape that deserialize and serialize the shape as GeoJSON data, respectively. The new initializer handles parsing errors gracefully. Removed methods specific to GeoJSON data from MGLShapeSource, in an effort to reduce parallel state. Developers are now expected to go through the new MGLShape initializer to get an MGLShape representation. Alternatively, a local file URL can be passed into the other MGLShapeSource initializer. * [ios, macos] Typo in assertion message * [ios, macos] Simplified GeoJSON serialization Every MGLShape now knows its most specific mbgl::GeoJSON representation. * [ios, macos] Reremoved MGLFeaturePrivate mbgl::GeoJSON, which is a variant, allows a single GeoJSON representation method to traffic in whatever type is needed for a particular shape class. This change removes some hidden private protocols, which are a bug waiting to happen. * [ios, macos] Fixed covariant rawLayer property Properly implement rawLayer as a covariant property so that it doesn’t end up getting autosynthesized as a shadow ivar. This prevents concrete subclasses of MGLStyleLayer from setting _rawLayer directly but getting a different value out of self.rawLayer. * [ios, macos] Use MGLAttributionInfo for source attribution Made MGLAttributionInfo public. Replaced MGLTileSource’s attribution property with an attributionInfos property set to an array of MGLAttributionInfo objects. Added an MGLTileSourceOption for specifying an array of MGLAttributionInfo objects instead of an HTML string (either is acceptable when creating an MGLTileSource). * [ios, macos] Corrected method references in documentation
1 parent b221b6c commit ef71597

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1443
-1067
lines changed

platform/darwin/src/MGLAttributionInfo.h

+29-25
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,49 @@ NS_ASSUME_NONNULL_BEGIN
1313
@interface MGLAttributionInfo : NSObject
1414

1515
/**
16-
Parses and returns the attribution infos contained in the given HTML source
17-
code string.
16+
Returns an initialized attribution info object with the given title and URL.
1817
19-
@param htmlString The HTML source code to parse.
20-
@param fontSize The default text size in points.
21-
@param linkColor The default link color.
18+
@param title The attribution statement’s title.
19+
@param URL A URL to more information about the entity named in the attribution.
20+
@return An initialized attribution info object.
2221
*/
23-
+ (NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfosFromHTMLString:(NSString *)htmlString fontSize:(CGFloat)fontSize linkColor:(nullable MGLColor *)linkColor;
24-
2522
- (instancetype)initWithTitle:(NSAttributedString *)title URL:(nullable NSURL *)URL;
2623

24+
/**
25+
The attribution statement’s attributed title text.
26+
*/
2727
@property (nonatomic) NSAttributedString *title;
28-
@property (nonatomic, nullable) NSURL *URL;
29-
@property (nonatomic, getter=isFeedbackLink) BOOL feedbackLink;
30-
31-
- (nullable NSURL *)feedbackURLAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(double)zoomLevel;
3228

33-
@end
34-
35-
@interface NSMutableArray (MGLAttributionInfoAdditions)
29+
/**
30+
The URL to more information about the entity named in the attribution.
31+
32+
If this property is set, the attribution statement should be displayed as a
33+
hyperlink or action button. Otherwise, if it is `nil`, the attribution
34+
statement should be displayed as plain text.
35+
*/
36+
@property (nonatomic, nullable) NSURL *URL;
3637

3738
/**
38-
Adds the given attribution info object to the receiver as long as it isn’t
39-
redundant to any object already in the receiver. Any existing object that is
40-
redundant to the given object is replaced by the given object.
39+
A Boolean value indicating whether the attribution statement is a shortcut to a
40+
feedback tool.
4141
42-
@param info The info object to add to the receiver.
43-
@return True if the given info object was added to the receiver.
42+
If this property is set, the statement should be treated as a way for the user
43+
to provide feedback rather than an attribution statement.
4444
*/
45-
- (void)growArrayByAddingAttributionInfo:(MGLAttributionInfo *)info;
45+
@property (nonatomic, getter=isFeedbackLink) BOOL feedbackLink;
4646

4747
/**
48-
Adds each of the given attribution info objects to the receiver as long as it
49-
isn’t redundant to any object already in the receiver. Any existing object that
50-
is redundant to the given object is replaced by the given object.
48+
Returns a copy of the `URL` property modified to account for the given center
49+
coordinate and zoom level.
5150
52-
@param infos An array of info objects to add to the receiver.
51+
@param centerCoordinate The map’s center coordinate.
52+
@param zoomLevel The map’s zoom level. See `MGLMapView`’s `zoomLevel` property
53+
for more information.
54+
@return A modified URL containing a fragment that points to the specified
55+
viewport. If the `feedbackLink` property is set to `NO`, this method returns
56+
`nil`.
5357
*/
54-
- (void)growArrayByAddingAttributionInfosFromArray:(NS_ARRAY_OF(MGLAttributionInfo *) *)infos;
58+
- (nullable NSURL *)feedbackURLAtCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(double)zoomLevel;
5559

5660
@end
5761

platform/darwin/src/MGLAttributionInfo.mm

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#import "MGLAttributionInfo.h"
1+
#import "MGLAttributionInfo_Private.h"
22

33
#if TARGET_OS_IPHONE
44
#import <UIKit/UIKit.h>
@@ -7,13 +7,18 @@
77
#endif
88

99
#import "MGLMapCamera.h"
10+
#import "NSArray+MGLAdditions.h"
1011
#import "NSString+MGLAdditions.h"
1112

1213
#include <string>
1314

1415
@implementation MGLAttributionInfo
1516

16-
+ (NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfosFromHTMLString:(NSString *)htmlString fontSize:(CGFloat)fontSize linkColor:(nullable MGLColor *)linkColor {
17+
+ (NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfosFromHTMLString:(nullable NSString *)htmlString fontSize:(CGFloat)fontSize linkColor:(nullable MGLColor *)linkColor {
18+
if (!htmlString) {
19+
return @[];
20+
}
21+
1722
NSDictionary *options = @{
1823
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
1924
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding),
@@ -84,6 +89,18 @@ @implementation MGLAttributionInfo
8489
return infos;
8590
}
8691

92+
+ (NSAttributedString *)attributedStringForAttributionInfos:(NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfos {
93+
NSMutableArray *titles = [NSMutableArray arrayWithCapacity:attributionInfos.count];
94+
for (MGLAttributionInfo *info in attributionInfos) {
95+
NSMutableAttributedString *title = info.title.mutableCopy;
96+
if (info.URL) {
97+
[title addAttribute:NSLinkAttributeName value:info.URL range:title.mgl_wholeRange];
98+
}
99+
[titles addObject:title];
100+
}
101+
return [titles mgl_attributedComponentsJoinedByString:@" "];
102+
}
103+
87104
- (instancetype)initWithTitle:(NSAttributedString *)title URL:(NSURL *)URL {
88105
if (self = [super init]) {
89106
_title = title;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#import <Foundation/Foundation.h>
2+
#import <CoreGraphics/CoreGraphics.h>
3+
#import <CoreLocation/CoreLocation.h>
4+
5+
#import "MGLAttributionInfo.h"
6+
7+
NS_ASSUME_NONNULL_BEGIN
8+
9+
@interface MGLAttributionInfo (Private)
10+
11+
/**
12+
Parses and returns the attribution infos contained in the given HTML source
13+
code string.
14+
15+
@param htmlString The HTML source code to parse.
16+
@param fontSize The default text size in points.
17+
@param linkColor The default link color.
18+
*/
19+
+ (NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfosFromHTMLString:(nullable NSString *)htmlString fontSize:(CGFloat)fontSize linkColor:(nullable MGLColor *)linkColor;
20+
21+
+ (NSAttributedString *)attributedStringForAttributionInfos:(NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfos;
22+
23+
@end
24+
25+
@interface NSMutableArray (MGLAttributionInfoAdditions)
26+
27+
/**
28+
Adds the given attribution info object to the receiver as long as it isn’t
29+
redundant to any object already in the receiver. Any existing object that is
30+
redundant to the given object is replaced by the given object.
31+
32+
@param info The info object to add to the receiver.
33+
@return True if the given info object was added to the receiver.
34+
*/
35+
- (void)growArrayByAddingAttributionInfo:(MGLAttributionInfo *)info;
36+
37+
/**
38+
Adds each of the given attribution info objects to the receiver as long as it
39+
isn’t redundant to any object already in the receiver. Any existing object that
40+
is redundant to the given object is replaced by the given object.
41+
42+
@param infos An array of info objects to add to the receiver.
43+
*/
44+
- (void)growArrayByAddingAttributionInfosFromArray:(NS_ARRAY_OF(MGLAttributionInfo *) *)infos;
45+
46+
@end
47+
48+
NS_ASSUME_NONNULL_END

platform/darwin/src/MGLBackgroundStyleLayer.mm

+19-9
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ - (instancetype)initWithIdentifier:(NSString *)identifier
2626
if (self = [super initWithIdentifier:identifier]) {
2727
auto layer = std::make_unique<mbgl::style::BackgroundLayer>(identifier.UTF8String);
2828
_pendingLayer = std::move(layer);
29-
_rawLayer = _pendingLayer.get();
29+
self.rawLayer = _pendingLayer.get();
3030
}
3131
return self;
3232
}
3333

34+
- (mbgl::style::BackgroundLayer *)rawLayer
35+
{
36+
return (mbgl::style::BackgroundLayer *)super.rawLayer;
37+
}
38+
39+
- (void)setRawLayer:(mbgl::style::BackgroundLayer *)rawLayer
40+
{
41+
super.rawLayer = rawLayer;
42+
}
43+
3444
#pragma mark - Adding to and removing from a map view
3545

3646
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
@@ -52,7 +62,7 @@ - (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLaye
5262
- (void)removeFromMapView:(MGLMapView *)mapView
5363
{
5464
_pendingLayer = nullptr;
55-
_rawLayer = nullptr;
65+
self.rawLayer = nullptr;
5666

5767
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
5868
if (!removedLayer) {
@@ -67,7 +77,7 @@ - (void)removeFromMapView:(MGLMapView *)mapView
6777
removedLayer.release();
6878

6979
_pendingLayer = std::unique_ptr<mbgl::style::BackgroundLayer>(layer);
70-
_rawLayer = _pendingLayer.get();
80+
self.rawLayer = _pendingLayer.get();
7181
}
7282

7383
#pragma mark - Accessing the Paint Attributes
@@ -76,41 +86,41 @@ - (void)setBackgroundColor:(MGLStyleValue<MGLColor *> *)backgroundColor {
7686
MGLAssertStyleLayerIsValid();
7787

7888
auto mbglValue = MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toPropertyValue(backgroundColor);
79-
_rawLayer->setBackgroundColor(mbglValue);
89+
self.rawLayer->setBackgroundColor(mbglValue);
8090
}
8191

8292
- (MGLStyleValue<MGLColor *> *)backgroundColor {
8393
MGLAssertStyleLayerIsValid();
8494

85-
auto propertyValue = _rawLayer->getBackgroundColor() ?: _rawLayer->getDefaultBackgroundColor();
95+
auto propertyValue = self.rawLayer->getBackgroundColor() ?: self.rawLayer->getDefaultBackgroundColor();
8696
return MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toStyleValue(propertyValue);
8797
}
8898

8999
- (void)setBackgroundOpacity:(MGLStyleValue<NSNumber *> *)backgroundOpacity {
90100
MGLAssertStyleLayerIsValid();
91101

92102
auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(backgroundOpacity);
93-
_rawLayer->setBackgroundOpacity(mbglValue);
103+
self.rawLayer->setBackgroundOpacity(mbglValue);
94104
}
95105

96106
- (MGLStyleValue<NSNumber *> *)backgroundOpacity {
97107
MGLAssertStyleLayerIsValid();
98108

99-
auto propertyValue = _rawLayer->getBackgroundOpacity() ?: _rawLayer->getDefaultBackgroundOpacity();
109+
auto propertyValue = self.rawLayer->getBackgroundOpacity() ?: self.rawLayer->getDefaultBackgroundOpacity();
100110
return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue);
101111
}
102112

103113
- (void)setBackgroundPattern:(MGLStyleValue<NSString *> *)backgroundPattern {
104114
MGLAssertStyleLayerIsValid();
105115

106116
auto mbglValue = MGLStyleValueTransformer<std::string, NSString *>().toPropertyValue(backgroundPattern);
107-
_rawLayer->setBackgroundPattern(mbglValue);
117+
self.rawLayer->setBackgroundPattern(mbglValue);
108118
}
109119

110120
- (MGLStyleValue<NSString *> *)backgroundPattern {
111121
MGLAssertStyleLayerIsValid();
112122

113-
auto propertyValue = _rawLayer->getBackgroundPattern() ?: _rawLayer->getDefaultBackgroundPattern();
123+
auto propertyValue = self.rawLayer->getBackgroundPattern() ?: self.rawLayer->getDefaultBackgroundPattern();
114124
return MGLStyleValueTransformer<std::string, NSString *>().toStyleValue(propertyValue);
115125
}
116126

0 commit comments

Comments
 (0)