Skip to content

Commit 9299af1

Browse files
committed
feat: implement #45
1 parent 05a3659 commit 9299af1

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

src/ui-mapbox/common.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,15 @@ export interface SetViewportOptions {
229229
// ------------------------------------------------------------
230230

231231
export interface DeleteOfflineRegionOptions {
232+
/**
233+
* The id of the offline region to delete.
234+
*/
235+
id?: string;
236+
232237
/**
233238
* The name of the offline region to delete.
234239
*/
235-
name: string;
240+
name?: string;
236241
}
237242

238243
// ------------------------------------------------------------
@@ -369,6 +374,9 @@ export interface OfflineRegion {
369374
minZoom: number;
370375
maxZoom: number;
371376
style: MapStyle;
377+
metadata?: any;
378+
pixelRatio?: any;
379+
type?: any;
372380
}
373381

374382
// ------------------------------------------------------------

src/ui-mapbox/index.android.ts

+30-11
Original file line numberDiff line numberDiff line change
@@ -2167,7 +2167,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
21672167
});
21682168
}
21692169

2170-
downloadOfflineRegion(options: DownloadOfflineRegionOptions): Promise<void> {
2170+
downloadOfflineRegion(options: DownloadOfflineRegionOptions): Promise<any> {
21712171
return new Promise((resolve, reject) => {
21722172
try {
21732173
const styleURL = this._getMapStyle(options.style);
@@ -2181,8 +2181,11 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
21812181

21822182
const offlineRegionDefinition = new com.mapbox.mapboxsdk.offline.OfflineTilePyramidRegionDefinition(styleURL, bounds, options.minZoom, options.maxZoom, retinaFactor);
21832183

2184-
const info = '{name:"' + options.name + '"}';
2185-
const infoStr = new java.lang.String(info);
2184+
const info = {
2185+
name: options.name,
2186+
...options.metadata
2187+
};
2188+
const infoStr = new java.lang.String(JSON.stringify(info));
21862189
const encodedMetadata = infoStr.getBytes();
21872190

21882191
if (!this._accessToken && !options.accessToken) {
@@ -2229,7 +2232,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
22292232
}
22302233

22312234
if (status.isComplete()) {
2232-
resolve();
2235+
resolve(status);
22332236
} else if (status.isRequiredResourceCountPrecise()) {
22342237
}
22352238
},
@@ -2280,8 +2283,10 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
22802283
const name = this._getRegionName(offlineRegion);
22812284
const offlineRegionDefinition = offlineRegion.getDefinition();
22822285
const bounds = offlineRegionDefinition.getBounds();
2286+
const metadata = this._getRegionMetadata(offlineRegion);
22832287

22842288
regions.push({
2289+
id: offlineRegion.getID(),
22852290
name,
22862291
style: offlineRegionDefinition.getStyleURL(),
22872292
minZoom: offlineRegionDefinition.getMinZoom(),
@@ -2291,7 +2296,10 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
22912296
east: bounds.getLonEast(),
22922297
south: bounds.getLatSouth(),
22932298
west: bounds.getLonWest()
2294-
}
2299+
},
2300+
metadata,
2301+
pixelRatio: offlineRegionDefinition.getPixelRatio(),
2302+
type: offlineRegionDefinition.getType()
22952303
});
22962304
}
22972305
}
@@ -2311,8 +2319,8 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
23112319
deleteOfflineRegion(options: DeleteOfflineRegionOptions): Promise<void> {
23122320
return new Promise((resolve, reject) => {
23132321
try {
2314-
if (!options || !options.name) {
2315-
reject("Pass in the 'name' param");
2322+
if (!options || (!options.id && !options.name)) {
2323+
reject("Pass in the 'id' or 'name' param");
23162324
return;
23172325
}
23182326

@@ -2327,8 +2335,8 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
23272335
if (offlineRegions !== null) {
23282336
for (let i = 0; i < offlineRegions.length; i++) {
23292337
const offlineRegion = offlineRegions[i];
2330-
const name = this._getRegionName(offlineRegion);
2331-
if (name === options.name) {
2338+
const regionId = options.id ? offlineRegion.getID() : this._getRegionName(offlineRegion);
2339+
if (regionId === (options.id || options.name)) {
23322340
found = true;
23332341
offlineRegion.delete(
23342342
new com.mapbox.mapboxsdk.offline.OfflineRegion.OfflineRegionDeleteCallback({
@@ -2353,7 +2361,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
23532361
);
23542362
} catch (ex) {
23552363
if (Trace.isEnabled()) {
2356-
CLog(CLogTypes.info, 'Error in mapbox.listOfflineRegions: ' + ex);
2364+
CLog(CLogTypes.info, 'Error in mapbox.deleteOfflineRegion: ' + ex);
23572365
}
23582366
reject(ex);
23592367
}
@@ -2954,7 +2962,18 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
29542962
const metadata = offlineRegion.getMetadata();
29552963
const jsonStr = new java.lang.String(metadata, 'UTF-8');
29562964
const jsonObj = new org.json.JSONObject(jsonStr as any as string);
2957-
return jsonObj.getString('name');
2965+
try {
2966+
return jsonObj.getString('name');
2967+
} catch (error) {
2968+
return '';
2969+
}
2970+
}
2971+
2972+
_getRegionMetadata(offlineRegion: com.mapbox.mapboxsdk.offline.OfflineRegion) {
2973+
const metadata = offlineRegion.getMetadata();
2974+
const jsonStr = new java.lang.String(metadata, 'UTF-8');
2975+
const jsonObj = new org.json.JSONObject(jsonStr as any as string);
2976+
return JSON.parse(jsonObj.toString());
29582977
}
29592978

29602979
/**

src/ui-mapbox/index.ios.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2601,8 +2601,8 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
26012601
deleteOfflineRegion(options: DeleteOfflineRegionOptions): Promise<void> {
26022602
return new Promise((resolve, reject) => {
26032603
try {
2604-
if (!options || !options.name) {
2605-
reject("Pass in the 'name' param");
2604+
if (!options || (!options.id && !options.name)) {
2605+
reject("Pass in the 'id' or 'name' param");
26062606
return;
26072607
}
26082608

@@ -2611,8 +2611,8 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
26112611
for (let i = 0; i < packs.count; i++) {
26122612
const pack = packs.objectAtIndex(i);
26132613
const userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(pack.context);
2614-
const name = userInfo.objectForKey('name');
2615-
if (name === options.name) {
2614+
const regionId = options.id ? userInfo.objectForKey('id') : userInfo.objectForKey('name');
2615+
if (regionId === (options.id || options.name)) {
26162616
found = true;
26172617
MGLOfflineStorage.sharedOfflineStorage.removePackWithCompletionHandler(pack, (error: NSError) => {
26182618
if (error) {

0 commit comments

Comments
 (0)