diff options
Diffstat (limited to 'platform/darwin/src/MGLOfflineStorage.mm')
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 4613b402f..6effd8c3c 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -10,7 +10,6 @@ #import "NSBundle+MGLAdditions.h" #import "NSValue+MGLAdditions.h" #import "NSDate+MGLAdditions.h" -#import "NSData+MGLAdditions.h" #import "MGLLoggingConfiguration_Private.h" #if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR @@ -20,6 +19,8 @@ #include <mbgl/actor/actor.hpp> #include <mbgl/actor/scheduler.hpp> +#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/resource_options.hpp> #include <mbgl/storage/resource_transform.hpp> #include <mbgl/util/chrono.hpp> #include <mbgl/util/run_loop.hpp> @@ -44,7 +45,8 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio @interface MGLOfflineStorage () @property (nonatomic, strong, readwrite) NSMutableArray<MGLOfflinePack *> *packs; -@property (nonatomic) mbgl::DefaultFileSource *mbglFileSource; +@property (nonatomic) std::shared_ptr<mbgl::DefaultFileSource> mbglFileSource; +@property (nonatomic) std::string mbglCachePath; @property (nonatomic, getter=isPaused) BOOL paused; @end @@ -223,7 +225,11 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio [[NSFileManager defaultManager] moveItemAtPath:subdirectorylessCacheURL.path toPath:cachePath error:NULL]; } - _mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String); + _mbglCachePath = cachePath.UTF8String; + mbgl::ResourceOptions options; + options.withCachePath(_mbglCachePath) + .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); + _mbglFileSource = std::static_pointer_cast<mbgl::DefaultFileSource>(mbgl::FileSource::getSharedFileSource(options)); // Observe for changes to the API base URL (and find out the current one). [[MGLAccountManager sharedManager] addObserver:self @@ -250,9 +256,6 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio for (MGLOfflinePack *pack in self.packs) { [pack invalidate]; } - - delete _mbglFileSource; - _mbglFileSource = nullptr; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context { @@ -260,14 +263,14 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio if ([keyPath isEqualToString:@"accessToken"] && object == [MGLAccountManager sharedManager]) { NSString *accessToken = change[NSKeyValueChangeNewKey]; if (![accessToken isKindOfClass:[NSNull class]]) { - self.mbglFileSource->setAccessToken(accessToken.UTF8String); + _mbglFileSource->setAccessToken(accessToken.UTF8String); } } else if ([keyPath isEqualToString:@"apiBaseURL"] && object == [MGLAccountManager sharedManager]) { NSURL *apiBaseURL = change[NSKeyValueChangeNewKey]; if ([apiBaseURL isKindOfClass:[NSNull class]]) { - self.mbglFileSource->setAPIBaseURL(mbgl::util::API_BASE_URL); + _mbglFileSource->setAPIBaseURL(mbgl::util::API_BASE_URL); } else { - self.mbglFileSource->setAPIBaseURL(apiBaseURL.absoluteString.UTF8String); + _mbglFileSource->setAPIBaseURL(apiBaseURL.absoluteString.UTF8String); } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; @@ -333,7 +336,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)_addContentsOfFile:(NSString *)filePath withCompletionHandler:(void (^)(NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error))completion { - self.mbglFileSource->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion, filePath](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { + _mbglFileSource->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion, filePath](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { NSError *error; NSMutableArray *packs; if (!result) { @@ -394,7 +397,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio const mbgl::OfflineRegionDefinition regionDefinition = [(id <MGLOfflineRegion_Private>)region offlineRegionDefinition]; mbgl::OfflineRegionMetadata metadata(context.length); [context getBytes:&metadata[0] length:metadata.size()]; - self.mbglFileSource->createOfflineRegion(regionDefinition, metadata, [&, completion](mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> mbglOfflineRegion) { + _mbglFileSource->createOfflineRegion(regionDefinition, metadata, [&, completion](mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> mbglOfflineRegion) { NSError *error; if (!mbglOfflineRegion) { NSString *errorDescription = @(mbgl::util::toString(mbglOfflineRegion.error()).c_str()); @@ -429,7 +432,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio return; } - self.mbglFileSource->deleteOfflineRegion(std::move(*mbglOfflineRegion), [&, completion](std::exception_ptr exception) { + _mbglFileSource->deleteOfflineRegion(std::move(*mbglOfflineRegion), [&, completion](std::exception_ptr exception) { NSError *error; if (exception) { error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:@{ @@ -455,7 +458,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)getPacksWithCompletionHandler:(void (^)(NSArray<MGLOfflinePack *> *packs, NSError * _Nullable error))completion { - self.mbglFileSource->listOfflineRegions([&, completion](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { + _mbglFileSource->listOfflineRegions([&, completion](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { NSError *error; NSMutableArray *packs; if (!result) { |