diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2016-03-26 21:12:18 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2016-03-29 11:46:26 -0700 |
commit | 4fd875a971db970b2868e2c37a7a3432cc961d64 (patch) | |
tree | 35d6ecf05df9d93e2364126685069aa902d6ef3e /platform/darwin | |
parent | f02bdb8fbba25a0db0042dbbf69e0ab529ac3224 (diff) | |
download | qtlocation-mapboxgl-4fd875a971db970b2868e2c37a7a3432cc961d64.tar.gz |
[ios, osx] Secure coding, copying for offline regions
Implemented NSSecureCoding and NSCopying protocols for MGLTilePyramidOfflineRegion so that it can be used more fluidly by client code.
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/include/MGLTilePyramidOfflineRegion.h | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLTilePyramidOfflineRegion.mm | 54 |
2 files changed, 55 insertions, 1 deletions
diff --git a/platform/darwin/include/MGLTilePyramidOfflineRegion.h b/platform/darwin/include/MGLTilePyramidOfflineRegion.h index 8864645870..4e9f394e74 100644 --- a/platform/darwin/include/MGLTilePyramidOfflineRegion.h +++ b/platform/darwin/include/MGLTilePyramidOfflineRegion.h @@ -9,7 +9,7 @@ NS_ASSUME_NONNULL_BEGIN An offline region defined by a style URL, geographic coordinate bounds, and range of zoom levels. */ -@interface MGLTilePyramidOfflineRegion : NSObject <MGLOfflineRegion> +@interface MGLTilePyramidOfflineRegion : NSObject <MGLOfflineRegion, NSSecureCoding, NSCopying> /** URL of the style whose resources are required for offline viewing. diff --git a/platform/darwin/src/MGLTilePyramidOfflineRegion.mm b/platform/darwin/src/MGLTilePyramidOfflineRegion.mm index 6507ceef77..d1151de094 100644 --- a/platform/darwin/src/MGLTilePyramidOfflineRegion.mm +++ b/platform/darwin/src/MGLTilePyramidOfflineRegion.mm @@ -18,6 +18,10 @@ @synthesize styleURL = _styleURL; ++ (BOOL)supportsSecureCoding { + return YES; +} + - (instancetype)init { [NSException raise:@"Method unavailable" format: @@ -67,4 +71,54 @@ scaleFactor); } +- (nullable instancetype)initWithCoder:(NSCoder *)coder { + NSURL *styleURL = [coder decodeObjectForKey:@"styleURL"]; + CLLocationCoordinate2D sw = CLLocationCoordinate2DMake([coder decodeDoubleForKey:@"southWestLatitude"], + [coder decodeDoubleForKey:@"southWestLongitude"]); + CLLocationCoordinate2D ne = CLLocationCoordinate2DMake([coder decodeDoubleForKey:@"northEastLatitude"], + [coder decodeDoubleForKey:@"northEastLongitude"]); + MGLCoordinateBounds bounds = MGLCoordinateBoundsMake(sw, ne); + double minimumZoomLevel = [coder decodeDoubleForKey:@"minimumZoomLevel"]; + double maximumZoomLevel = [coder decodeDoubleForKey:@"maximumZoomLevel"]; + + return [self initWithStyleURL:styleURL bounds:bounds fromZoomLevel:minimumZoomLevel toZoomLevel:maximumZoomLevel]; +} + +- (void)encodeWithCoder:(NSCoder *)coder +{ + [coder encodeObject:_styleURL forKey:@"styleURL"]; + [coder encodeDouble:_bounds.sw.latitude forKey:@"southWestLatitude"]; + [coder encodeDouble:_bounds.sw.longitude forKey:@"southWestLongitude"]; + [coder encodeDouble:_bounds.ne.latitude forKey:@"northEastLatitude"]; + [coder encodeDouble:_bounds.ne.longitude forKey:@"northEastLongitude"]; + [coder encodeDouble:_maximumZoomLevel forKey:@"maximumZoomLevel"]; + [coder encodeDouble:_minimumZoomLevel forKey:@"minimumZoomLevel"]; +} + +- (id)copyWithZone:(nullable NSZone *)zone { + return [[[self class] allocWithZone:zone] initWithStyleURL:_styleURL bounds:_bounds fromZoomLevel:_minimumZoomLevel toZoomLevel:_maximumZoomLevel]; +} + +- (BOOL)isEqual:(id)other { + if (other == self) { + return YES; + } + if (![other isKindOfClass:[self class]]) { + return NO; + } + + MGLTilePyramidOfflineRegion *otherRegion = other; + return (_minimumZoomLevel == otherRegion->_minimumZoomLevel + && _maximumZoomLevel == otherRegion->_maximumZoomLevel + && MGLCoordinateBoundsEqualToCoordinateBounds(_bounds, otherRegion->_bounds) + && [_styleURL isEqual:otherRegion->_styleURL]); +} + +- (NSUInteger)hash { + return (_styleURL.hash + + @(_bounds.sw.latitude).hash + @(_bounds.sw.longitude).hash + + @(_bounds.ne.latitude).hash + @(_bounds.ne.longitude).hash + + @(_minimumZoomLevel).hash + @(_maximumZoomLevel).hash); +} + @end |