summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Guerra <fabian.guerra@mapbox.com>2018-08-31 14:19:04 -0700
committerFabian Guerra <fabian.guerra@mapbox.com>2018-09-13 15:42:02 -0700
commit68e26dbd1afcdd23dbbd1fa9aca94eb5f2d446b5 (patch)
tree86fbbb5091f6a15411228d376795b2d12813d64c
parent9efdc4c6c32d37c9fa680c301a60442b50348941 (diff)
downloadqtlocation-mapboxgl-68e26dbd1afcdd23dbbd1fa9aca94eb5f2d446b5.tar.gz
[ios, macos] Add offline regions merge.
-rw-r--r--platform/darwin/src/MGLOfflineStorage.h25
-rw-r--r--platform/darwin/src/MGLOfflineStorage.mm28
2 files changed, 53 insertions, 0 deletions
diff --git a/platform/darwin/src/MGLOfflineStorage.h b/platform/darwin/src/MGLOfflineStorage.h
index f8ea6e7453..99c14afe52 100644
--- a/platform/darwin/src/MGLOfflineStorage.h
+++ b/platform/darwin/src/MGLOfflineStorage.h
@@ -135,6 +135,16 @@ typedef void (^MGLOfflinePackAdditionCompletionHandler)(MGLOfflinePack * _Nullab
typedef void (^MGLOfflinePackRemovalCompletionHandler)(NSError * _Nullable error);
/**
+ A block to be called once the contents of a file are copied into the current packs.
+
+ @param packs Contains an array of all known offline packs, or `nil` if there was
+ an error creating or adding the pack.
+ @param error Contains a pointer to an error object (if any) indicating why the
+ pack could not be created or added.
+ */
+typedef void (^MGLOfflinePacksAdditionCompletionHandler)(NSArray<MGLOfflinePack *> *packs, NSError * _Nullable error);
+
+/**
The type of resource that is requested.
*/
typedef NS_ENUM(NSUInteger, MGLResourceKind) {
@@ -174,6 +184,21 @@ MGL_EXPORT
*/
@property (class, nonatomic, readonly) MGLOfflineStorage *sharedOfflineStorage;
+#pragma mark - Adding Contents of File
+
+/**
+ Adds the contents of the file path.
+
+ Once the content is added, then the `completion` block is executed with the
+ packs passed in.
+
+ @param filePath A string representation of the file path. The file path must be
+ writable as schema updates may be perfomed.
+ @param completion The completion handler to call once the file has been added.
+ This handler is executed asynchronously on the main queue.
+ */
+- (void)addContentesOfFile:(NSString *)filePath withCompletionHandler:(nullable MGLOfflinePacksAdditionCompletionHandler)completion;
+
#pragma mark - Accessing the Delegate
/**
diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm
index 93a6da36c4..616cce160e 100644
--- a/platform/darwin/src/MGLOfflineStorage.mm
+++ b/platform/darwin/src/MGLOfflineStorage.mm
@@ -264,6 +264,34 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio
}
}
+#pragma mark Offline merge methods
+
+- (void)addContentesOfFile:(NSString *)filePath withCompletionHandler:(MGLOfflinePacksAdditionCompletionHandler)completion {
+ NSFileManager *fileManager = [NSFileManager defaultManager];
+ NSAssert([fileManager isWritableFileAtPath:filePath], @"The file %@ must be writable.", filePath);
+ self.mbglFileSource->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) {
+ NSError *error;
+ NSMutableArray *packs;
+ if (!result) {
+ error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:@{
+ NSLocalizedDescriptionKey: @(mbgl::util::toString(result.error()).c_str()),
+ }];
+ } else {
+ auto& regions = result.value();
+ packs = [NSMutableArray arrayWithCapacity:regions.size()];
+ for (auto &region : 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);
+ });
+ }
+ });
+}
+
#pragma mark Pack management methods
- (void)addPackForRegion:(id <MGLOfflineRegion>)region withContext:(NSData *)context completionHandler:(MGLOfflinePackAdditionCompletionHandler)completion {