diff --git a/API.md b/API.md index a8052c5..da70453 100644 --- a/API.md +++ b/API.md @@ -473,6 +473,7 @@ A progress object has the following shape: { name: 'test', // The name this pack was registered with metadata, // The value that was previously passed as metadata + state: 0, // The current state of the offline pack. 0 = Unknown, 1 = Inactive, 2 = Active, 3 = Complete, 4 = Invalid (iOS Only) countOfBytesCompleted: 0, // The number of bytes downloaded for this pack countOfResourcesCompleted: 0, // The number of tiles that have been downloaded for this pack countOfResourcesExpected: 0, // The estimated minimum number of total tiles in this pack @@ -480,6 +481,14 @@ A progress object has the following shape: } ``` +The offline pack progress object's `state` property can be compared against the following constants: + +* `Mapbox.offlinePackState.unknown` +* `Mapbox.offlinePackState.inactive` +* `Mapbox.offlinePackState.active` +* `Mapbox.offlinePackState.complete` +* `Mapbox.offlinePackState.invalid` + #### Subscribing to progress notifications ```javascript diff --git a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java index 5e2c511..9e986da 100644 --- a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java +++ b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java @@ -104,6 +104,7 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule { HashMap userTrackingMode = new HashMap(); HashMap mapStyles = new HashMap(); HashMap userLocationVerticalAlignment = new HashMap(); + HashMap offlinePackState = new HashMap(); // User tracking constants userTrackingMode.put("none", 0); @@ -124,6 +125,13 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule { userLocationVerticalAlignment.put("top", 1); userLocationVerticalAlignment.put("bottom", 2); + // Offline Pack State constants + offlinePackState.put("unknown", 0); + offlinePackState.put("inactive", 1); + offlinePackState.put("active", 2); + offlinePackState.put("complete", 3); + offlinePackState.put("invalid", 4); + // Other constants constants.put("unknownResourceCount", Long.MAX_VALUE); constants.put("metricsEnabled", MapboxEventManager.getMapboxEventManager().isTelemetryEnabled()); @@ -131,6 +139,7 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule { constants.put("userTrackingMode", userTrackingMode); constants.put("mapStyles", mapStyles); constants.put("userLocationVerticalAlignment", userLocationVerticalAlignment); + constants.put("offlinePackState", offlinePackState); return constants; } @@ -331,6 +340,7 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule { e.printStackTrace(); } + result.putInt("state", normalizeOfflineRegionState(status)); result.putInt("countOfBytesCompleted", (int)status.getCompletedResourceSize()); result.putInt("countOfResourcesCompleted", (int)status.getCompletedResourceCount()); result.putInt("countOfResourcesExpected", (int)status.getRequiredResourceCount()); @@ -339,6 +349,41 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule { return result; } + /* + * Normalizes offline region status state for the sake of parity with iOS for React Native + * Essentially we force Android state to be the same as iOS state for ease of cross-platform development + * + * On iOS: + * 0: Unknown + * 1: Inactive + * 2: Active + * 3: Complete + * 4: Invalid (iOS ONLY) + * + * On Android: + * 0: Inactive (Complete is inactive, AND countOfResourcesCompleted == countOfResourcesExpected) + * 1: Active + */ + static int normalizeOfflineRegionState(OfflineRegionStatus status) { + int state = (int)status.getDownloadState(); + boolean isComplete = (boolean)status.isComplete(); + + switch (state) { + case 0: + if (isComplete) { + state = 3; + } + break; + case 1: + state = 2; + break; + default: + state = 0; + } + + return state; + } + static String getOfflineRegionName(OfflineRegion region) { try { ByteArrayInputStream bis = new ByteArrayInputStream(region.getMetadata()); diff --git a/index.js b/index.js index e899ddd..b53fc32 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ import isEqual from 'lodash/isEqual'; import Annotation from './Annotation'; const { MapboxGLManager } = NativeModules; -const { mapStyles, userTrackingMode, userLocationVerticalAlignment, unknownResourceCount } = MapboxGLManager; +const { mapStyles, userTrackingMode, userLocationVerticalAlignment, offlinePackState, unknownResourceCount } = MapboxGLManager; // Deprecation @@ -461,7 +461,7 @@ const MapboxGLView = requireNativeComponent('RCTMapboxGL', MapView, { const Mapbox = { MapView, Annotation, - mapStyles, userTrackingMode, userLocationVerticalAlignment, unknownResourceCount, + mapStyles, userTrackingMode, userLocationVerticalAlignment, offlinePackState, unknownResourceCount, getMetricsEnabled, setMetricsEnabled, setAccessToken, addOfflinePack, getOfflinePacks, removeOfflinePack, diff --git a/ios/RCTMapboxGL/RCTMapboxGLManager.m b/ios/RCTMapboxGL/RCTMapboxGLManager.m index 201eca1..b7e364f 100644 --- a/ios/RCTMapboxGL/RCTMapboxGLManager.m +++ b/ios/RCTMapboxGL/RCTMapboxGLManager.m @@ -104,6 +104,13 @@ RCT_CUSTOM_VIEW_PROPERTY(contentInset, UIEdgeInsetsMake, RCTMapboxGL) @"center": @(MGLAnnotationVerticalAlignmentCenter), @"bottom": @(MGLAnnotationVerticalAlignmentBottom) }, + @"offlinePackState": @{ + @"unknown": [NSNumber numberWithUnsignedInt:MGLOfflinePackStateUnknown], + @"inactive": [NSNumber numberWithUnsignedInt:MGLOfflinePackStateInactive], + @"active": [NSNumber numberWithUnsignedInt:MGLOfflinePackStateActive], + @"complete": [NSNumber numberWithUnsignedInt:MGLOfflinePackStateComplete], + @"invalid": [NSNumber numberWithUnsignedInt:MGLOfflinePackStateInvalid] + }, @"unknownResourceCount": @(UINT64_MAX), @"metricsEnabled": @([RCTMapboxGLManager metricsEnabled]) }; @@ -220,6 +227,7 @@ RCT_EXPORT_METHOD(setAccessToken:(nonnull NSString *)accessToken) NSDictionary *event = @{ @"name": userInfo[@"name"], @"metadata": userInfo[@"metadata"], + @"state": @(pack.state), @"countOfResourcesCompleted": @(progress.countOfResourcesCompleted), @"countOfResourcesExpected": @(progress.countOfResourcesExpected), @"countOfBytesCompleted": @(progress.countOfBytesCompleted), @@ -365,6 +373,7 @@ RCT_REMAP_METHOD(addOfflinePack, NSMutableDictionary *userInfo = [NSKeyedUnarchiver unarchiveObjectWithData:pack.context]; [callbackArray addObject:@{ @"name": userInfo[@"name"], @"metadata": userInfo[@"metadata"], + @"state": @(pack.state), @"countOfBytesCompleted": @(pack.progress.countOfBytesCompleted), @"countOfResourcesCompleted": @(pack.progress.countOfResourcesCompleted), @"countOfResourcesExpected": @(pack.progress.countOfResourcesExpected),