diff options
author | Jesse Crocker <jesse@gaiagps.com> | 2017-03-01 11:15:11 -0700 |
---|---|---|
committer | Jesse Crocker <jesse@gaiagps.com> | 2017-03-01 11:15:11 -0700 |
commit | 9e8dc9a9e3e86adb9987ae69766cc42c7d9efece (patch) | |
tree | f5f0abd4d342c89ad0405d01969f9d6caecc1c90 /platform/darwin/src/MGLOfflinePack.mm | |
parent | 16fb0672e64a72b7400c321d55858b73cd5d8c3f (diff) | |
parent | f28d75dccd9bf4a7615df87faccc5cf5eff8df89 (diff) | |
download | qtlocation-mapboxgl-9e8dc9a9e3e86adb9987ae69766cc42c7d9efece.tar.gz |
Merge remote-tracking branch 'origin/master' into feature/custom-vector-source
Diffstat (limited to 'platform/darwin/src/MGLOfflinePack.mm')
-rw-r--r-- | platform/darwin/src/MGLOfflinePack.mm | 77 |
1 files changed, 50 insertions, 27 deletions
diff --git a/platform/darwin/src/MGLOfflinePack.mm b/platform/darwin/src/MGLOfflinePack.mm index 1f2fd95f2b..60a7b55531 100644 --- a/platform/darwin/src/MGLOfflinePack.mm +++ b/platform/darwin/src/MGLOfflinePack.mm @@ -4,11 +4,13 @@ #import "MGLOfflineRegion_Private.h" #import "MGLTilePyramidOfflineRegion.h" +#import "NSValue+MGLAdditions.h" + #include <mbgl/storage/default_file_source.hpp> /** Assert that the current offline pack is valid. - + This macro should be used at the beginning of any public-facing instance method of `MGLOfflinePack`. For private methods, an assertion is more appropriate. */ @@ -26,18 +28,17 @@ class MBGLOfflineRegionObserver : public mbgl::OfflineRegionObserver { public: MBGLOfflineRegionObserver(MGLOfflinePack *pack_) : pack(pack_) {} - + void statusChanged(mbgl::OfflineRegionStatus status) override; void responseError(mbgl::Response::Error error) override; void mapboxTileCountLimitExceeded(uint64_t limit) override; - + private: __weak MGLOfflinePack *pack = nullptr; }; @interface MGLOfflinePack () -@property (nonatomic, weak, nullable) id <MGLOfflinePackDelegate> delegate; @property (nonatomic, nullable, readwrite) mbgl::OfflineRegion *mbglOfflineRegion; @property (nonatomic, readwrite) MGLOfflinePackProgress progress; @@ -59,7 +60,7 @@ private: if (self = [super init]) { _mbglOfflineRegion = region; _state = MGLOfflinePackStateUnknown; - + mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; mbglFileSource->setOfflineRegionObserver(*_mbglOfflineRegion, std::make_unique<MBGLOfflineRegionObserver>(self)); } @@ -72,7 +73,7 @@ private: - (id <MGLOfflineRegion>)region { MGLAssertOfflinePackIsValid(); - + const mbgl::OfflineRegionDefinition ®ionDefinition = _mbglOfflineRegion->getDefinition(); NSAssert([MGLTilePyramidOfflineRegion conformsToProtocol:@protocol(MGLOfflineRegion_Private)], @"MGLTilePyramidOfflineRegion should conform to MGLOfflineRegion_Private."); return [(id <MGLOfflineRegion_Private>)[MGLTilePyramidOfflineRegion alloc] initWithOfflineRegionDefinition:regionDefinition]; @@ -80,35 +81,35 @@ private: - (NSData *)context { MGLAssertOfflinePackIsValid(); - + const mbgl::OfflineRegionMetadata &metadata = _mbglOfflineRegion->getMetadata(); return [NSData dataWithBytes:&metadata[0] length:metadata.size()]; } - (void)resume { MGLAssertOfflinePackIsValid(); - + self.state = MGLOfflinePackStateActive; - + mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active); } - (void)suspend { MGLAssertOfflinePackIsValid(); - + if (self.state == MGLOfflinePackStateActive) { self.state = MGLOfflinePackStateInactive; _isSuspending = YES; } - + mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive); } - (void)invalidate { NSAssert(_state != MGLOfflinePackStateInvalid, @"Cannot invalidate an already invalid offline pack."); - + self.state = MGLOfflinePackStateInvalid; mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; mbglFileSource->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr); @@ -123,9 +124,9 @@ private: NSAssert(_state == MGLOfflinePackStateInvalid, @"A valid MGLOfflinePack has no mbgl::OfflineRegion."); return; } - + NSAssert(_state != MGLOfflinePackStateInvalid, @"Cannot change the state of an invalid offline pack."); - + if (!_isSuspending || state != MGLOfflinePackStateActive) { _isSuspending = NO; _state = state; @@ -134,9 +135,9 @@ private: - (void)requestProgress { MGLAssertOfflinePackIsValid(); - + mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; - + __weak MGLOfflinePack *weakSelf = self; mbglFileSource->getOfflineRegionStatus(*_mbglOfflineRegion, [&, weakSelf](__unused std::exception_ptr exception, mbgl::optional<mbgl::OfflineRegionStatus> status) { if (status) { @@ -151,21 +152,21 @@ private: - (void)offlineRegionStatusDidChange:(mbgl::OfflineRegionStatus)status { NSAssert(_state != MGLOfflinePackStateInvalid, @"Cannot change update progress of an invalid offline pack."); - + switch (status.downloadState) { case mbgl::OfflineRegionDownloadState::Inactive: self.state = status.complete() ? MGLOfflinePackStateComplete : MGLOfflinePackStateInactive; break; - + case mbgl::OfflineRegionDownloadState::Active: self.state = MGLOfflinePackStateActive; break; } - + if (_isSuspending) { return; } - + MGLOfflinePackProgress progress; progress.countOfResourcesCompleted = status.completedResourceCount; progress.countOfBytesCompleted = status.completedResourceSize; @@ -174,8 +175,30 @@ private: progress.countOfResourcesExpected = status.requiredResourceCount; progress.maximumResourcesExpected = status.requiredResourceCountIsPrecise ? status.requiredResourceCount : UINT64_MAX; self.progress = progress; - - [self.delegate offlinePack:self progressDidChange:progress]; + + NSDictionary *userInfo = @{MGLOfflinePackUserInfoKeyState: @(self.state), + MGLOfflinePackUserInfoKeyProgress: [NSValue valueWithMGLOfflinePackProgress:progress]}; + + NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter]; + [noteCenter postNotificationName:MGLOfflinePackProgressChangedNotification + object:self + userInfo:userInfo]; +} + +- (void)didReceiveError:(NSError *)error { + NSDictionary *userInfo = @{ MGLOfflinePackUserInfoKeyError: error }; + NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter]; + [noteCenter postNotificationName:MGLOfflinePackErrorNotification + object:self + userInfo:userInfo]; +} + +- (void)didReceiveMaximumAllowedMapboxTiles:(uint64_t)limit { + NSDictionary *userInfo = @{ MGLOfflinePackUserInfoKeyMaximumCount: @(limit) }; + NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter]; + [noteCenter postNotificationName:MGLOfflinePackMaximumMapboxTilesReachedNotification + object:self + userInfo:userInfo]; } NSError *MGLErrorFromResponseError(mbgl::Response::Error error) { @@ -184,15 +207,15 @@ NSError *MGLErrorFromResponseError(mbgl::Response::Error error) { case mbgl::Response::Error::Reason::NotFound: errorCode = MGLErrorCodeNotFound; break; - + case mbgl::Response::Error::Reason::Server: errorCode = MGLErrorCodeBadServerResponse; break; - + case mbgl::Response::Error::Reason::Connection: errorCode = MGLErrorCodeConnectionFailed; break; - + default: break; } @@ -211,12 +234,12 @@ void MBGLOfflineRegionObserver::statusChanged(mbgl::OfflineRegionStatus status) void MBGLOfflineRegionObserver::responseError(mbgl::Response::Error error) { dispatch_async(dispatch_get_main_queue(), ^{ - [pack.delegate offlinePack:pack didReceiveError:MGLErrorFromResponseError(error)]; + [pack didReceiveError:MGLErrorFromResponseError(error)]; }); } void MBGLOfflineRegionObserver::mapboxTileCountLimitExceeded(uint64_t limit) { dispatch_async(dispatch_get_main_queue(), ^{ - [pack.delegate offlinePack:pack didReceiveMaximumAllowedMapboxTiles:limit]; + [pack didReceiveMaximumAllowedMapboxTiles:limit]; }); } |