Skip to content

Commit

Permalink
Add mergeOfflineRegions method to enable side-loading tiles (rnmapbox…
Browse files Browse the repository at this point in the history
…#588)

* add mergeOfflineRegions

* fix crash when sideloaded pack on Android has no name
  • Loading branch information
tsemerad authored and kristfal committed Dec 29, 2019
1 parent 2af5a26 commit 254c51d
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,25 @@ public void onError(String error) {
});
}

@ReactMethod
public void mergeOfflineRegions(final String path, final Promise promise) {
activateFileSource();

final OfflineManager offlineManager = OfflineManager.getInstance(mReactContext);

offlineManager.mergeOfflineRegions(path, new OfflineManager.MergeOfflineRegionsCallback() {
@Override
public void onMerge(OfflineRegion[] offlineRegions) {
promise.resolve(null);
}

@Override
public void onError(String error) {
promise.reject("mergeOfflineRegions", error);
}
});
}

@ReactMethod
public void setTileCountLimit(int tileCountLimit) {
OfflineManager offlineManager = OfflineManager.getInstance(mReactContext);
Expand Down
16 changes: 16 additions & 0 deletions docs/OfflineManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ const offlinePack = await MapboxGL.offlineManager.getPack();
```


#### mergeOfflineRegions(path)

Sideloads offline db

##### arguments
| Name | Type | Required | Description |
| ---- | :--: | :------: | :----------: |
| `path` | `String` | `Yes` | Path to offline tile db on file system. |



```javascript
await MapboxGL.offlineManager.mergeOfflineRegions(path);
```


#### setTileCountLimit(limit)

Sets the maximum number of Mapbox-hosted tiles that may be downloaded and stored on the current device.<br/>The Mapbox Terms of Service prohibits changing or bypassing this limit without permission from Mapbox.
Expand Down
23 changes: 23 additions & 0 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5249,6 +5249,29 @@
}
}
},
{
"name": "mergeOfflineRegions",
"description": "Sideloads offline db",
"params": [
{
"name": "path",
"description": "Path to offline tile db on file system.",
"type": {
"name": "String"
},
"optional": false
}
],
"examples": [
"await MapboxGL.offlineManager.mergeOfflineRegions(path);"
],
"returns": {
"description": "",
"type": {
"name": "void"
}
}
},
{
"name": "setTileCountLimit",
"description": "Sets the maximum number of Mapbox-hosted tiles that may be downloaded and stored on the current device.\nThe Mapbox Terms of Service prohibits changing or bypassing this limit without permission from Mapbox.",
Expand Down
30 changes: 30 additions & 0 deletions ios/RCTMGL/MGLOfflineModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,32 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}];
}

RCT_EXPORT_METHOD(mergeOfflineRegions:(NSString *)path
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSString *absolutePath;
if ([path isAbsolutePath]) {
absolutePath = path;
} else {
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *fileName = [path stringByDeletingPathExtension];
NSString *extension = [path pathExtension];
absolutePath = [mainBundle pathForResource:fileName ofType:extension];
if (!absolutePath) {
return reject(@"asset_does_not_exist", [NSString stringWithFormat:@"The given assetName, %@, can't be found in the app's bundle.", path], nil);
}
}

[[MGLOfflineStorage sharedOfflineStorage] addContentsOfFile:absolutePath withCompletionHandler:^(NSURL *fileURL, NSArray<MGLOfflinePack *> *packs, NSError *error) {
if (error != nil) {
reject(@"mergeOfflineRegions", error.description, error);
return;
}
resolve(nil);
}];
}

RCT_EXPORT_METHOD(getPacks:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -306,6 +332,10 @@ - (NSDictionary *)_unarchiveMetadata:(MGLOfflinePack *)pack
return data;
}

if (data == nil) {
return @{};
}

return [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:nil];
Expand Down
4 changes: 2 additions & 2 deletions javascript/modules/offline/OfflinePack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class OfflinePack {

get name() {
const {metadata} = this;
return metadata.name;
return metadata && metadata.name;
}

get bounds() {
return this.pack.bounds;
}

get metadata() {
if (!this._metadata) {
if (!this._metadata && this.pack.metadata) {
this._metadata = JSON.parse(this.pack.metadata);
}
return this._metadata;
Expand Down
14 changes: 14 additions & 0 deletions javascript/modules/offline/offlineManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ class OfflineManager {
return this._offlinePacks[name];
}

/**
* Sideloads offline db
*
* @example
* await MapboxGL.offlineManager.mergeOfflineRegions(path);
*
* @param {String} path Path to offline tile db on file system.
* @return {void}
*/
async mergeOfflineRegions(path) {
await this._initialize();
return MapboxGLOfflineManager.mergeOfflineRegions(path);
}

/**
* Sets the maximum number of Mapbox-hosted tiles that may be downloaded and stored on the current device.
* The Mapbox Terms of Service prohibits changing or bypassing this limit without permission from Mapbox.
Expand Down

0 comments on commit 254c51d

Please sign in to comment.