diff options
Diffstat (limited to 'platform/darwin/src/MGLOfflineStorage.mm')
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 106 |
1 files changed, 97 insertions, 9 deletions
diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 6effd8c3ce..93f986a518 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -48,7 +48,6 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio @property (nonatomic) std::shared_ptr<mbgl::DefaultFileSource> mbglFileSource; @property (nonatomic) std::string mbglCachePath; @property (nonatomic, getter=isPaused) BOOL paused; - @end @implementation MGLOfflineStorage { @@ -341,10 +340,11 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio NSMutableArray *packs; if (!result) { NSString *description = [NSString stringWithFormat:NSLocalizedStringWithDefaultValue(@"ADD_FILE_CONTENTS_FAILED_DESC", @"Foundation", nil, @"Unable to add offline packs from the file at %@.", @"User-friendly error description"), filePath]; - error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:@{ - NSLocalizedDescriptionKey: description, - NSLocalizedFailureReasonErrorKey: @(mbgl::util::toString(result.error()).c_str()) - }]; + error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed + userInfo:@{ + NSLocalizedDescriptionKey: description, + NSLocalizedFailureReasonErrorKey: @(mbgl::util::toString(result.error()).c_str()) + }]; } else { auto& regions = result.value(); packs = [NSMutableArray arrayWithCapacity:regions.size()]; @@ -401,7 +401,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio NSError *error; if (!mbglOfflineRegion) { NSString *errorDescription = @(mbgl::util::toString(mbglOfflineRegion.error()).c_str()); - error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:errorDescription ? @{ + error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:errorDescription ? @{ NSLocalizedDescriptionKey: errorDescription, } : nil]; } @@ -431,11 +431,10 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio completion(nil); return; } - _mbglFileSource->deleteOfflineRegion(std::move(*mbglOfflineRegion), [&, completion](std::exception_ptr exception) { NSError *error; if (exception) { - error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:@{ + error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:@{ NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()), }]; } @@ -445,6 +444,29 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio }); } }); + +} + +- (void)invalidatePack:(MGLOfflinePack *)pack withCompletionHandler:(void (^)(NSError * _Nullable))completion { + mbgl::OfflineRegion& region = *pack.mbglOfflineRegion; + NSError *error; + if (!pack.mbglOfflineRegion) { + completion(nil); + return; + } + + _mbglFileSource->invalidateOfflineRegion(region, [&](std::exception_ptr exception) { + if (exception) { + error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:@{ + NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()), + }]; + } + }); + if (completion) { + dispatch_async(dispatch_get_main_queue(), [&, completion, error](void) { + completion(error); + }); + } } - (void)reloadPacks { @@ -462,7 +484,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio NSError *error; NSMutableArray *packs; if (!result) { - error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:@{ + error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeUnknown userInfo:@{ NSLocalizedDescriptionKey: @(mbgl::util::toString(result.error()).c_str()), }]; } else { @@ -486,6 +508,72 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio _mbglFileSource->setOfflineMapboxTileCountLimit(maximumCount); } +#pragma mark - Ambient Cache management + +- (void)setMaximumAmbientCacheSize:(NSUInteger)cacheSize withCompletionHandler:(void (^)(NSError * _Nullable))completion { + _mbglFileSource->setMaximumAmbientCacheSize(cacheSize, [&, completion](std::exception_ptr exception) { + NSError *error; + if (completion) { + if (exception) { + error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:@{ + NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()), + }]; + } + dispatch_sync(dispatch_get_main_queue(), ^ { + completion(error); + }); + } + }); +} + +- (void)invalidateAmbientCacheWithCompletionHandler:(void (^)(NSError *_Nullable))completion { + _mbglFileSource->invalidateAmbientCache([&, completion](std::exception_ptr exception){ + NSError *error; + if (completion) { + if (exception) { + // Convert std::exception_ptr to an NSError. + error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:@{ + NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()), + }]; + } + dispatch_async(dispatch_get_main_queue(), ^ { + completion(error); + }); + } + }); +} + +- (void)clearAmbientCacheWithCompletionHandler:(void (^)(NSError *_Nullable error))completion { + _mbglFileSource->clearAmbientCache([&, completion](std::exception_ptr exception){ + NSError *error; + if (completion) { + if (exception) { + error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:@{ + NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()), + }]; + } + dispatch_async(dispatch_get_main_queue(), [&, completion, error](void) { + completion(error); + }); + } + }); +} + +- (void)resetDatabaseWithCompletionHandler:(void (^)(NSError *_Nullable error))completion { + _mbglFileSource->resetDatabase([&, completion](std::exception_ptr exception) { + NSError *error; + if (completion) { + if (exception) { + error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeUnknown userInfo:@{ + NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()), + }]; + } + dispatch_async(dispatch_get_main_queue(), ^{ + completion(error); + }); + } + }); +} #pragma mark - - (unsigned long long)countOfBytesCompleted { |