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

Commit 0625d4b

Browse files
committed
[ios, macos] features and annotations now conforms to NSSecureCoding
1 parent 56eb9fa commit 0625d4b

26 files changed

+1072
-19
lines changed

platform/darwin/src/MGLFeature.mm

+29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "MGLPolyline+MGLAdditions.h"
1111
#import "MGLPolygon+MGLAdditions.h"
1212
#import "NSDictionary+MGLAdditions.h"
13+
#import "NSArray+MGLAdditions.h"
1314

1415
#import "NSExpression+MGLAdditions.h"
1516

@@ -25,6 +26,10 @@ @implementation MGLPointFeature
2526
@synthesize identifier;
2627
@synthesize attributes;
2728

29+
MGL_DEFINE_FEATURE_INIT_WITH_CODER();
30+
MGL_DEFINE_FEATURE_ENCODE();
31+
MGL_DEFINE_FEATURE_IS_EQUAL();
32+
2833
- (id)attributeForKey:(NSString *)key {
2934
return self.attributes[key];
3035
}
@@ -47,6 +52,10 @@ @implementation MGLPolylineFeature
4752
@synthesize identifier;
4853
@synthesize attributes;
4954

55+
MGL_DEFINE_FEATURE_INIT_WITH_CODER();
56+
MGL_DEFINE_FEATURE_ENCODE();
57+
MGL_DEFINE_FEATURE_IS_EQUAL();
58+
5059
- (id)attributeForKey:(NSString *)key {
5160
return self.attributes[key];
5261
}
@@ -69,6 +78,10 @@ @implementation MGLPolygonFeature
6978
@synthesize identifier;
7079
@synthesize attributes;
7180

81+
MGL_DEFINE_FEATURE_INIT_WITH_CODER();
82+
MGL_DEFINE_FEATURE_ENCODE();
83+
MGL_DEFINE_FEATURE_IS_EQUAL();
84+
7285
- (id)attributeForKey:(NSString *)key {
7386
return self.attributes[key];
7487
}
@@ -91,6 +104,10 @@ @implementation MGLPointCollectionFeature
91104
@synthesize identifier;
92105
@synthesize attributes;
93106

107+
MGL_DEFINE_FEATURE_INIT_WITH_CODER();
108+
MGL_DEFINE_FEATURE_ENCODE();
109+
MGL_DEFINE_FEATURE_IS_EQUAL();
110+
94111
- (id)attributeForKey:(NSString *)key {
95112
return self.attributes[key];
96113
}
@@ -113,6 +130,10 @@ @implementation MGLMultiPolylineFeature
113130
@synthesize identifier;
114131
@synthesize attributes;
115132

133+
MGL_DEFINE_FEATURE_INIT_WITH_CODER();
134+
MGL_DEFINE_FEATURE_ENCODE();
135+
MGL_DEFINE_FEATURE_IS_EQUAL();
136+
116137
- (id)attributeForKey:(NSString *)key {
117138
return self.attributes[key];
118139
}
@@ -135,6 +156,10 @@ @implementation MGLMultiPolygonFeature
135156
@synthesize identifier;
136157
@synthesize attributes;
137158

159+
MGL_DEFINE_FEATURE_INIT_WITH_CODER();
160+
MGL_DEFINE_FEATURE_ENCODE();
161+
MGL_DEFINE_FEATURE_IS_EQUAL();
162+
138163
- (id)attributeForKey:(NSString *)key {
139164
return self.attributes[key];
140165
}
@@ -163,6 +188,10 @@ + (instancetype)shapeCollectionWithShapes:(NSArray *)shapes {
163188
return [super shapeCollectionWithShapes:shapes];
164189
}
165190

191+
MGL_DEFINE_FEATURE_INIT_WITH_CODER();
192+
MGL_DEFINE_FEATURE_ENCODE();
193+
MGL_DEFINE_FEATURE_IS_EQUAL();
194+
166195
- (id)attributeForKey:(NSString *)key {
167196
return self.attributes[key];
168197
}

platform/darwin/src/MGLFeature_Private.h

+28
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,31 @@ mbgl::Feature mbglFeature(mbgl::Feature feature, id identifier, NSDictionary *at
3737
NS_DICTIONARY_OF(NSString *, id) *NSDictionaryFeatureForGeometry(NSDictionary *geometry, NSDictionary *attributes, id identifier);
3838

3939
NS_ASSUME_NONNULL_END
40+
41+
#define MGL_DEFINE_FEATURE_INIT_WITH_CODER() \
42+
- (instancetype)initWithCoder:(NSCoder *)decoder { \
43+
if (self = [super initWithCoder:decoder]) { \
44+
NSSet<Class> *identifierClasses = [NSSet setWithArray:@[[NSString class], [NSNumber class]]]; \
45+
identifier = [decoder decodeObjectOfClasses:identifierClasses forKey:@"identifier"]; \
46+
attributes = [decoder decodeObjectOfClass:[NSDictionary class] forKey:@"attributes"]; \
47+
} \
48+
return self; \
49+
}
50+
51+
#define MGL_DEFINE_FEATURE_ENCODE() \
52+
- (void)encodeWithCoder:(NSCoder *)coder { \
53+
[super encodeWithCoder:coder]; \
54+
[coder encodeObject:identifier forKey:@"identifier"]; \
55+
[coder encodeObject:attributes forKey:@"attributes"]; \
56+
}
57+
58+
#define MGL_DEFINE_FEATURE_IS_EQUAL() \
59+
- (BOOL)isEqual:(id)other { \
60+
if (other == self) return YES; \
61+
if (![other isKindOfClass:[self class]]) return NO; \
62+
__typeof(self) otherFeature = other; \
63+
return [super isEqual:other] && [self geoJSONObject] == [otherFeature geoJSONObject]; \
64+
} \
65+
- (NSUInteger)hash { \
66+
return [super hash] + [[self geoJSONDictionary] hash]; \
67+
}

platform/darwin/src/MGLMultiPoint.mm

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#import "MGLMultiPoint_Private.h"
22
#import "MGLGeometry_Private.h"
3+
#import "MGLShape_Private.h"
4+
#import "NSCoder+MGLAdditions.h"
35
#import "MGLTypes.h"
46

5-
#include <mbgl/util/geo.hpp>
6-
#include <mbgl/util/optional.hpp>
7-
87
@implementation MGLMultiPoint
98
{
109
mbgl::optional<mbgl::LatLngBounds> _bounds;
@@ -27,6 +26,39 @@ - (instancetype)initWithCoordinates:(const CLLocationCoordinate2D *)coords count
2726
return self;
2827
}
2928

29+
- (instancetype)initWithCoder:(NSCoder *)decoder
30+
{
31+
if (self = [super initWithCoder:decoder]) {
32+
_coordinates = [decoder mgl_decodeLocationCoordinates2DForKey:@"coordinates"];
33+
}
34+
return self;
35+
}
36+
37+
- (void)encodeWithCoder:(NSCoder *)coder
38+
{
39+
[super encodeWithCoder:coder];
40+
[coder mgl_encodeLocationCoordinates2D:_coordinates forKey:@"coordinates"];
41+
}
42+
43+
- (BOOL)isEqual:(id)other
44+
{
45+
if (self == other) return YES;
46+
if (![other isKindOfClass:[MGLMultiPoint class]]) return NO;
47+
48+
MGLMultiPoint *otherMultipoint = other;
49+
return ([super isEqual:otherMultipoint]
50+
&& _coordinates == otherMultipoint->_coordinates);
51+
}
52+
53+
- (NSUInteger)hash
54+
{
55+
NSUInteger hash = [super hash];
56+
for (auto coord : _coordinates) {
57+
hash += @(coord.latitude+coord.longitude).hash;
58+
}
59+
return hash;
60+
}
61+
3062
- (CLLocationCoordinate2D)coordinate
3163
{
3264
NSAssert([self pointCount] > 0, @"A multipoint must have coordinates");

platform/darwin/src/MGLPointAnnotation.mm

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import "MGLPointAnnotation.h"
22

33
#import "MGLShape_Private.h"
4+
#import "NSCoder+MGLAdditions.h"
45

56
#import <mbgl/util/geometry.hpp>
67

@@ -9,6 +10,41 @@ @implementation MGLPointAnnotation
910

1011
@synthesize coordinate;
1112

13+
+ (BOOL)supportsSecureCoding
14+
{
15+
return YES;
16+
}
17+
18+
- (instancetype)initWithCoder:(NSCoder *)coder
19+
{
20+
if (self = [super initWithCoder:coder]) {
21+
self.coordinate = [coder decodeMGLCoordinateForKey:@"coordinate"];
22+
}
23+
return self;
24+
}
25+
26+
- (void)encodeWithCoder:(NSCoder *)coder
27+
{
28+
[super encodeWithCoder:coder];
29+
[coder encodeMGLCoordinate:coordinate forKey:@"coordinate"];
30+
}
31+
32+
- (BOOL)isEqual:(id)other
33+
{
34+
if (other == self) return YES;
35+
if (![other isKindOfClass:[MGLPointAnnotation class]]) return NO;
36+
37+
MGLPointAnnotation *otherAnnotation = other;
38+
return ([super isEqual:other]
39+
&& self.coordinate.latitude == otherAnnotation.coordinate.latitude
40+
&& self.coordinate.longitude == otherAnnotation.coordinate.longitude);
41+
}
42+
43+
- (NSUInteger)hash
44+
{
45+
return [super hash] + @(self.coordinate.latitude).hash + @(self.coordinate.longitude).hash;
46+
}
47+
1248
- (NSString *)description
1349
{
1450
return [NSString stringWithFormat:@"<%@: %p; title = %@; subtitle = %@; coordinate = %f, %f>",

platform/darwin/src/MGLPointCollection.mm

+34-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "MGLPointCollection_Private.h"
22
#import "MGLGeometry_Private.h"
3+
#import "NSArray+MGLAdditions.h"
34

45
#import <mbgl/util/geojson.hpp>
56
#import <mbgl/util/geometry.hpp>
@@ -8,12 +9,10 @@
89

910
@implementation MGLPointCollection
1011
{
11-
MGLCoordinateBounds _overlayBounds;
12+
mbgl::optional<mbgl::LatLngBounds> _bounds;
1213
std::vector<CLLocationCoordinate2D> _coordinates;
1314
}
1415

15-
@synthesize overlayBounds = _overlayBounds;
16-
1716
+ (instancetype)pointCollectionWithCoordinates:(const CLLocationCoordinate2D *)coords count:(NSUInteger)count
1817
{
1918
return [[self alloc] initWithCoordinates:coords count:count];
@@ -25,14 +24,41 @@ - (instancetype)initWithCoordinates:(const CLLocationCoordinate2D *)coords count
2524
if (self)
2625
{
2726
_coordinates = { coords, coords + count };
27+
}
28+
return self;
29+
}
30+
31+
- (nullable instancetype)initWithCoder:(NSCoder *)decoder {
32+
if (self = [super initWithCoder:decoder]) {
33+
NSArray *coordinates = [decoder decodeObjectOfClass:[NSArray class] forKey:@"coordinates"];
34+
_coordinates = [coordinates mgl_coordinates];
35+
}
36+
return self;
37+
}
38+
39+
- (void)encodeWithCoder:(NSCoder *)coder {
40+
[super encodeWithCoder:coder];
41+
[coder encodeObject:[NSArray mgl_coordinatesFromCoordinates:_coordinates] forKey:@"coordinates"];
42+
}
43+
44+
- (BOOL)isEqual:(id)other {
45+
if (self == other) return YES;
46+
if (![other isKindOfClass:[MGLPointCollection class]]) return NO;
47+
48+
MGLPointCollection *otherCollection = (MGLPointCollection *)other;
49+
return ([super isEqual:other]
50+
&& ((![self geoJSONDictionary] && ![otherCollection geoJSONDictionary]) || [[self geoJSONDictionary] isEqualToDictionary:[otherCollection geoJSONDictionary]]));
51+
}
52+
53+
- (MGLCoordinateBounds)overlayBounds {
54+
if (!_bounds) {
2855
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();
29-
for (auto coordinate : _coordinates)
30-
{
56+
for (auto coordinate : _coordinates) {
3157
bounds.extend(mbgl::LatLng(coordinate.latitude, coordinate.longitude));
3258
}
33-
_overlayBounds = MGLCoordinateBoundsFromLatLngBounds(bounds);
59+
_bounds = bounds;
3460
}
35-
return self;
61+
return MGLCoordinateBoundsFromLatLngBounds(*_bounds);
3662
}
3763

3864
- (NSUInteger)pointCount
@@ -65,7 +91,7 @@ - (void)getCoordinates:(CLLocationCoordinate2D *)coords range:(NSRange)range
6591

6692
- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds
6793
{
68-
return MGLCoordinateBoundsIntersectsCoordinateBounds(_overlayBounds, overlayBounds);
94+
return MGLCoordinateBoundsIntersectsCoordinateBounds(self.overlayBounds, overlayBounds);
6995
}
7096

7197
- (mbgl::Geometry<double>)geometryObject

platform/darwin/src/MGLPolygon.mm

+55
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ - (instancetype)initWithCoordinates:(const CLLocationCoordinate2D *)coords count
2828
return self;
2929
}
3030

31+
- (instancetype)initWithCoder:(NSCoder *)decoder {
32+
self = [super initWithCoder:decoder];
33+
if (self) {
34+
_interiorPolygons = [decoder decodeObjectOfClass:[NSArray class] forKey:@"interiorPolygons"];
35+
}
36+
return self;
37+
}
38+
39+
- (void)encodeWithCoder:(NSCoder *)coder {
40+
[super encodeWithCoder:coder];
41+
[coder encodeObject:self.interiorPolygons forKey:@"interiorPolygons"];
42+
}
43+
44+
- (BOOL)isEqual:(id)other {
45+
if (self == other) return YES;
46+
if (![other isKindOfClass:[MGLPolygon class]]) return NO;
47+
48+
MGLPolygon *otherPolygon = (MGLPolygon *)other;
49+
return ([super isEqual:otherPolygon] &&
50+
[[self geoJSONDictionary] isEqualToDictionary:[otherPolygon geoJSONDictionary]]);
51+
}
52+
53+
- (NSUInteger)hash {
54+
return [super hash] + [[self geoJSONDictionary] hash];
55+
}
56+
3157
- (mbgl::LinearRing<double>)ring {
3258
NSUInteger count = self.pointCount;
3359
CLLocationCoordinate2D *coordinates = self.coordinates;
@@ -100,6 +126,35 @@ - (instancetype)initWithPolygons:(NS_ARRAY_OF(MGLPolygon *) *)polygons {
100126
return self;
101127
}
102128

129+
- (instancetype)initWithCoder:(NSCoder *)decoder {
130+
if (self = [super initWithCoder:decoder]) {
131+
_polygons = [decoder decodeObjectOfClass:[NSArray class] forKey:@"polygons"];
132+
}
133+
return self;
134+
}
135+
136+
- (void)encodeWithCoder:(NSCoder *)coder {
137+
[super encodeWithCoder:coder];
138+
[coder encodeObject:_polygons forKey:@"polygons"];
139+
}
140+
141+
- (BOOL)isEqual:(id)other {
142+
if (self == other) return YES;
143+
if (![other isKindOfClass:[MGLMultiPolygon class]]) return NO;
144+
145+
MGLMultiPolygon *otherMultiPolygon = other;
146+
return [super isEqual:other]
147+
&& [self.polygons isEqualToArray:otherMultiPolygon.polygons];
148+
}
149+
150+
- (NSUInteger)hash {
151+
NSUInteger hash = [super hash];
152+
for (MGLPolygon *polygon in self.polygons) {
153+
hash += [polygon hash];
154+
}
155+
return hash;
156+
}
157+
103158
- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds {
104159
return MGLCoordinateBoundsIntersectsCoordinateBounds(_overlayBounds, overlayBounds);
105160
}

0 commit comments

Comments
 (0)