“Offline pack” more effectively communicates the persistent nature of the downloaded content.
153 lines
6.8 KiB
Plaintext
153 lines
6.8 KiB
Plaintext
#import "MGLOfflineStorage_Private.h"
|
||
|
||
#import "MGLAccountManager_Private.h"
|
||
#import "MGLGeometry_Private.h"
|
||
#import "MGLOfflinePack_Private.h"
|
||
#import "MGLOfflineRegion_Private.h"
|
||
#import "MGLTilePyramidOfflineRegion.h"
|
||
|
||
#include <mbgl/util/string.hpp>
|
||
|
||
@interface MGLOfflineStorage ()
|
||
|
||
@property (nonatomic) mbgl::DefaultFileSource *mbglFileSource;
|
||
|
||
- (instancetype)initWithFileName:(NSString *)fileName NS_DESIGNATED_INITIALIZER;
|
||
|
||
@end
|
||
|
||
@implementation MGLOfflineStorage
|
||
|
||
+ (instancetype)sharedOfflineStorage {
|
||
static dispatch_once_t onceToken;
|
||
static MGLOfflineStorage *sharedOfflineStorage;
|
||
dispatch_once(&onceToken, ^{
|
||
sharedOfflineStorage = [[self alloc] initWithFileName:@"offline.db"];
|
||
});
|
||
return sharedOfflineStorage;
|
||
}
|
||
|
||
- (instancetype)initWithFileName:(NSString *)fileName {
|
||
if (self = [super init]) {
|
||
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
|
||
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||
NSString *cachePath = [paths.firstObject stringByAppendingPathComponent:fileName];
|
||
#elif TARGET_OS_MAC
|
||
NSURL *cacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory
|
||
inDomain:NSUserDomainMask
|
||
appropriateForURL:nil
|
||
create:YES
|
||
error:nil];
|
||
cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:
|
||
[NSBundle mainBundle].bundleIdentifier];
|
||
[[NSFileManager defaultManager] createDirectoryAtURL:cacheDirectoryURL
|
||
withIntermediateDirectories:YES
|
||
attributes:nil
|
||
error:nil];
|
||
NSURL *cacheURL = [cacheDirectoryURL URLByAppendingPathComponent:fileName];
|
||
NSString *cachePath = cacheURL ? cacheURL.path : @"";
|
||
#endif
|
||
_mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String);
|
||
|
||
// Observe for changes to the global access token (and find out the current one).
|
||
[[MGLAccountManager sharedManager] addObserver:self
|
||
forKeyPath:@"accessToken"
|
||
options:(NSKeyValueObservingOptionInitial |
|
||
NSKeyValueObservingOptionNew)
|
||
context:NULL];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (void)dealloc {
|
||
[[MGLAccountManager sharedManager] removeObserver:self forKeyPath:@"accessToken"];
|
||
|
||
delete _mbglFileSource;
|
||
_mbglFileSource = nullptr;
|
||
}
|
||
|
||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NS_DICTIONARY_OF(NSString *, id) *)change context:(__unused void *)context {
|
||
// Synchronize the file source’s access token with the global one in MGLAccountManager.
|
||
if ([keyPath isEqualToString:@"accessToken"] && object == [MGLAccountManager sharedManager]) {
|
||
NSString *accessToken = change[NSKeyValueChangeNewKey];
|
||
if (![accessToken isKindOfClass:[NSNull class]]) {
|
||
self.mbglFileSource->setAccessToken(accessToken.UTF8String);
|
||
}
|
||
}
|
||
}
|
||
|
||
- (void)addPackForRegion:(id <MGLOfflineRegion>)region withContext:(NSData *)context completionHandler:(MGLOfflinePackAdditionCompletionHandler)completion {
|
||
if (![region conformsToProtocol:@protocol(MGLOfflineRegion_Private)]) {
|
||
[NSException raise:@"Unsupported region type" format:
|
||
@"Regions of type %@ are unsupported.", NSStringFromClass([region class])];
|
||
return;
|
||
}
|
||
|
||
const mbgl::OfflineTilePyramidRegionDefinition regionDefinition = [(id <MGLOfflineRegion_Private>)region offlineRegionDefinition];
|
||
mbgl::OfflineRegionMetadata metadata(context.length);
|
||
[context getBytes:&metadata[0] length:metadata.size()];
|
||
self.mbglFileSource->createOfflineRegion(regionDefinition, metadata, [&, completion](std::exception_ptr exception, mbgl::optional<mbgl::OfflineRegion> mbglOfflineRegion) {
|
||
NSError *error;
|
||
if (exception) {
|
||
NSString *errorDescription = @(mbgl::util::toString(exception).c_str());
|
||
error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:errorDescription ? @{
|
||
NSLocalizedDescriptionKey: errorDescription,
|
||
} : nil];
|
||
}
|
||
if (completion) {
|
||
MGLOfflinePack *pack = mbglOfflineRegion ? [[MGLOfflinePack alloc] initWithMBGLRegion:new mbgl::OfflineRegion(std::move(*mbglOfflineRegion))] : nil;
|
||
dispatch_async(dispatch_get_main_queue(), [&, completion, error, pack](void) {
|
||
completion(pack, error);
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
- (void)removePack:(MGLOfflinePack *)pack withCompletionHandler:(MGLOfflinePackRemovalCompletionHandler)completion {
|
||
mbgl::OfflineRegion *mbglOfflineRegion = pack.mbglOfflineRegion;
|
||
[pack invalidate];
|
||
self.mbglFileSource->deleteOfflineRegion(std::move(*mbglOfflineRegion), [&, completion](std::exception_ptr exception) {
|
||
NSError *error;
|
||
if (exception) {
|
||
error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:@{
|
||
NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()),
|
||
}];
|
||
}
|
||
if (completion) {
|
||
dispatch_async(dispatch_get_main_queue(), [&, completion, error](void) {
|
||
completion(error);
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
- (void)getPacksWithCompletionHandler:(MGLOfflinePackListingCompletionHandler)completion {
|
||
self.mbglFileSource->listOfflineRegions([&, completion](std::exception_ptr exception, mbgl::optional<std::vector<mbgl::OfflineRegion>> regions) {
|
||
NSError *error;
|
||
if (exception) {
|
||
error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:@{
|
||
NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()),
|
||
}];
|
||
}
|
||
NSMutableArray *packs;
|
||
if (regions) {
|
||
packs = [NSMutableArray arrayWithCapacity:regions->size()];
|
||
for (mbgl::OfflineRegion ®ion : *regions) {
|
||
MGLOfflinePack *pack = [[MGLOfflinePack alloc] initWithMBGLRegion:new mbgl::OfflineRegion(std::move(region))];
|
||
[packs addObject:pack];
|
||
}
|
||
}
|
||
if (completion) {
|
||
dispatch_async(dispatch_get_main_queue(), [&, completion, error, packs](void) {
|
||
completion(packs, error);
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
- (void)setMaximumAllowedMapboxTiles:(uint64_t)maximumCount {
|
||
_mbglFileSource->setOfflineMapboxTileCountLimit(maximumCount);
|
||
}
|
||
|
||
@end
|