summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Guerra <fabian.guerra@mapbox.com>2018-09-12 16:42:49 -0700
committerFabian Guerra <fabian.guerra@mapbox.com>2018-09-13 15:42:02 -0700
commitb755da9ce33e668ccead3cdaebebc45166e94c96 (patch)
tree7c96c8819f726833625ee70962ca46243ef3428b
parentf3da3b880d954ad6bd38390675050942389b0103 (diff)
downloadqtlocation-mapboxgl-b755da9ce33e668ccead3cdaebebc45166e94c96.tar.gz
[ios, macos] Add packs parameter to MGLBatchedOfflinePackAdditionCompletionHandler.
-rw-r--r--platform/darwin/src/MGLOfflineStorage.h5
-rw-r--r--platform/darwin/src/MGLOfflineStorage.mm61
-rw-r--r--platform/darwin/test/MGLOfflineStorageTests.mm49
3 files changed, 58 insertions, 57 deletions
diff --git a/platform/darwin/src/MGLOfflineStorage.h b/platform/darwin/src/MGLOfflineStorage.h
index 3b942b1e6d..1ea8cece07 100644
--- a/platform/darwin/src/MGLOfflineStorage.h
+++ b/platform/darwin/src/MGLOfflineStorage.h
@@ -137,10 +137,13 @@ typedef void (^MGLOfflinePackRemovalCompletionHandler)(NSError * _Nullable error
/**
A block to be called once the contents of a file are copied into the current packs.
+ @param fileURL Contains the URL to the file packs used.
+ @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 (^MGLBatchedOfflinePackAdditionCompletionHandler)(NSError * _Nullable error);
+typedef void (^MGLBatchedOfflinePackAdditionCompletionHandler)(NSURL *fileURL, NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error);
/**
The type of resource that is requested.
diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm
index e55d4214d2..7b10f869a6 100644
--- a/platform/darwin/src/MGLOfflineStorage.mm
+++ b/platform/darwin/src/MGLOfflineStorage.mm
@@ -267,36 +267,55 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio
#pragma mark Offline merge methods
- (void)addContentsOfFile:(NSString *)filePath withCompletionHandler:(MGLBatchedOfflinePackAdditionCompletionHandler)completion {
+ NSURL *fileURL = [NSURL fileURLWithPath:filePath];
+
+ [self addContentsOfURL:fileURL withCompletionHandler:completion];
+
+}
+
+- (void)addContentsOfURL:(NSURL *)fileURL withCompletionHandler:(MGLBatchedOfflinePackAdditionCompletionHandler)completion {
+
NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager isWritableFileAtPath:filePath]) {
- [NSException raise:NSInvalidArgumentException format:@"The file path must be writable"];
+
+ if (!fileURL.isFileURL) {
+ [NSException raise:NSInvalidArgumentException format:@"%@ must be a valid file path", fileURL.absoluteString];
+ }
+ if (![fileManager isWritableFileAtPath:fileURL.path]) {
+ [NSException raise:NSInvalidArgumentException format:@"The file path: %@ must be writable", fileURL.absoluteString];
}
__weak MGLOfflineStorage *weakSelf = self;
- [self _addContentsOfFile:filePath withCompletionHandler:^(NSError * _Nullable error) {
- if (error && completion) {
- completion(error);
- } else {
+ [self _addContentsOfFile:fileURL.path withCompletionHandler:^(NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error) {
+ if (packs) {
+ NSMutableDictionary *packsDictionary = [NSMutableDictionary dictionary];
+ NSMutableDictionary *packsIndex = [NSMutableDictionary dictionary];
+ NSInteger index = 0;
+
MGLOfflineStorage *strongSelf = weakSelf;
- [strongSelf getPacksWithCompletionHandler:^(NSArray<MGLOfflinePack *> *packs, NSError * _Nullable error) {
- for (MGLOfflinePack *pack in strongSelf.packs) {
- [pack invalidate];
- }
- strongSelf.packs = [packs mutableCopy];
- if (completion) {
- completion(error);
+ for (MGLOfflinePack *pack in strongSelf.packs) {
+ [packsDictionary setObject:pack forKey:@(pack.mbglOfflineRegion->getID())];
+ [packsIndex setObject:@(index) forKey:@(pack.mbglOfflineRegion->getID())];
+ index++;
+ }
+
+ for (MGLOfflinePack *pack in packs) {
+ MGLOfflinePack *existingPack = [packsDictionary objectForKey:@(pack.mbglOfflineRegion->getID())];
+ if (existingPack) {
+ [existingPack invalidate];
+ NSNumber *index = [packsIndex objectForKey:@(pack.mbglOfflineRegion->getID())];
+ [[strongSelf mutableArrayValueForKey:@"packs"] replaceObjectAtIndex:index.unsignedIntegerValue withObject:pack];
+ } else {
+ [[strongSelf mutableArrayValueForKey:@"packs"] addObject:pack];
}
- }];
+ }
+ }
+ if (completion) {
+ completion(fileURL, packs, error);
}
-
}];
}
-- (void)addContentsOfURL:(NSURL *)fileURL withCompletionHandler:(MGLBatchedOfflinePackAdditionCompletionHandler)completion {
- [self addContentsOfFile:fileURL.absoluteString withCompletionHandler:completion];
-}
-
-- (void)_addContentsOfFile:(NSString *)filePath withCompletionHandler:(MGLBatchedOfflinePackAdditionCompletionHandler)completion {
+- (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](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) {
NSError *error;
NSMutableArray *packs;
@@ -314,7 +333,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio
}
if (completion) {
dispatch_async(dispatch_get_main_queue(), [&, completion, error, packs](void) {
- completion(error);
+ completion(packs, error);
});
}
});
diff --git a/platform/darwin/test/MGLOfflineStorageTests.mm b/platform/darwin/test/MGLOfflineStorageTests.mm
index d63909dedb..a4953f9e2a 100644
--- a/platform/darwin/test/MGLOfflineStorageTests.mm
+++ b/platform/darwin/test/MGLOfflineStorageTests.mm
@@ -265,33 +265,7 @@
}
- (void)testAddingFileContent {
-
- NSURL *styleURL = [MGLStyle streetsStyleURLWithVersion:MGLStyleDefaultVersion];
-
- // Barcelona.
- MGLCoordinateBounds bounds = {
- { .latitude = 41.2724, .longitude = 2.0407 },
- { .latitude = 41.4664, .longitude = 2.2680 },
- };
- double zoomLevel = 20;
- MGLTilePyramidOfflineRegion *region = [[MGLTilePyramidOfflineRegion alloc] initWithStyleURL:styleURL bounds:bounds fromZoomLevel:zoomLevel toZoomLevel:zoomLevel];
-
- NSString *nameKey = @"Name";
- NSString *name = @"Barcelona";
-
- NSData *context = [NSKeyedArchiver archivedDataWithRootObject:@{
- nameKey: name,
- }];
-
-
- XCTestExpectation *additionCompletionHandlerExpectation = [self expectationWithDescription:@"add pack completion handler"];
- [[MGLOfflineStorage sharedOfflineStorage] addPackForRegion:region withContext:context completionHandler:^(MGLOfflinePack * _Nullable completionHandlerPack, NSError * _Nullable error) {
- XCTAssertNotNil(completionHandlerPack, @"Added pack should exist.");
- XCTAssertEqual(completionHandlerPack.state, MGLOfflinePackStateInactive, @"New pack should initially have inactive state.");
- [additionCompletionHandlerExpectation fulfill];
- }];
- [self waitForExpectationsWithTimeout:2 handler:nil];
-
+ NSUInteger countOfPacks = [MGLOfflineStorage sharedOfflineStorage].packs.count;
// Valid database
{
NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"barcelona" ofType:@"db"]];
@@ -310,33 +284,38 @@
XCTestExpectation *fileAdditionCompletionHandlerExpectation = [self expectationWithDescription:@"add database content completion handler"];
MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
- [os addContentsOfFile:filePath withCompletionHandler:^(NSError * _Nullable error) {
+ [os addContentsOfFile:filePath withCompletionHandler:^(NSURL *fileURL, NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error) {
+ XCTAssertNotNil(fileURL, @"The fileURL should not be nil.");
+ XCTAssertNotNil(packs, @"Adding the contents of the barcelona.db should update one pack.");
XCTAssertNil(error, @"Adding contents to a file should not return an error.");
[fileAdditionCompletionHandlerExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:2 handler:nil];
+ // Depending on the database it may update or add a pack. For this case specifically the offline database updates the current pack.
+ XCTAssertEqual([MGLOfflineStorage sharedOfflineStorage].packs.count, countOfPacks, @"Adding contents of barcelona.db should not add any new pack.");
}
// Invalid database type
{
NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"one-liner" ofType:@"json"]];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
- NSString *filePath = [documentDir stringByAppendingPathComponent:@"barcelona.db"];
+ NSString *filePath = [documentDir stringByAppendingPathComponent:@"on-liner-copy.json"];
NSFileManager *fileManager = [NSFileManager defaultManager];
-
+
BOOL exists = [fileManager fileExistsAtPath:filePath];
if (exists) {
[fileManager removeItemAtPath:filePath error:nil];
}
-
+
NSError *error;
- [fileManager moveItemAtURL:resourceURL toURL:[NSURL fileURLWithPath:filePath] error:&error];
+ [fileManager copyItemAtURL:resourceURL toURL:[NSURL fileURLWithPath:filePath] error:&error];
- XCTestExpectation *fileAdditionCompletionHandlerExpectation = [self expectationWithDescription:@"add database content completion handler"];
+ XCTestExpectation *invalidFileCompletionHandlerExpectation = [self expectationWithDescription:@"invalid content database completion handler"];
MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
- [os addContentsOfFile:filePath withCompletionHandler:^(NSError * _Nullable error) {
+ [os addContentsOfFile:filePath withCompletionHandler:^(NSURL *fileURL, NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error) {
XCTAssertNotNil(error, @"Passing an invalid offline database file should return an error.");
- [fileAdditionCompletionHandlerExpectation fulfill];
+ XCTAssertNil(packs, @"Passing an invalid offline database file should not add packs to the offline database.");
+ [invalidFileCompletionHandlerExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:2 handler:nil];
}