Added Offline Pack State to progress object and normalized Android RegionStatus output with iOS

This commit is contained in:
Alex Tarsha
2017-06-15 02:21:45 -06:00
parent b46ea8da99
commit 72a4e6da5f
4 changed files with 65 additions and 2 deletions
+9
View File
@@ -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
@@ -104,6 +104,7 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
HashMap<String, Object> userTrackingMode = new HashMap<String, Object>();
HashMap<String, Object> mapStyles = new HashMap<String, Object>();
HashMap<String, Object> userLocationVerticalAlignment = new HashMap<String, Object>();
HashMap<String, Object> offlinePackState = new HashMap<String, Object>();
// 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());
+2 -2
View File
@@ -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,
+9
View File
@@ -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),