change getMetricsEnabled to return a promise

After upgrading to Android SDK 5.0, we cannot put metricsEnabled to
constants of the module. So we change getMetricsEnabled to return a
promise instead of value directly.
This commit is contained in:
Jack Feng
2017-09-01 10:10:27 +08:00
parent 91074ef739
commit ac31979da4
4 changed files with 19 additions and 12 deletions
+1 -1
View File
@@ -399,7 +399,7 @@ If you hide the attribution button, you need to provide the user with a way to
opt-out of telemetry. For this, you need to add `MGLMapboxMetricsEnabledSettingShownInApp`
as `YES` in `Info.plist`, then create a switch that toggles metrics.
To get the current state of metrics, use `Mapbox.getMetricsEnabled()`.
To get the current state of metrics, use `Mapbox.getMetricsEnabled()`. It will return a promise. Before using it, don't forget to set an access token with `Mapbox.setAccessToken(accessToken)`.
To enable or disable metrics, use `Mapbox.setMetricsEnabled(enabled: boolean)`.
@@ -5,6 +5,7 @@ import android.os.Handler;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.JSApplicationCausedNativeException;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
@@ -130,8 +131,6 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
// Other constants
constants.put("unknownResourceCount", Long.MAX_VALUE);
// FIXME you cannot get telemetry enabled status before you set access token
// constants.put("metricsEnabled", MapboxTelemetry.getInstance().isTelemetryEnabled());
constants.put("userTrackingMode", userTrackingMode);
constants.put("mapStyles", mapStyles);
@@ -177,6 +176,15 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
// Metrics
@ReactMethod
public void getMetricsEnabled(final Promise promise) {
try {
promise.resolve(MapboxTelemetry.getInstance().isTelemetryEnabled());
} catch (NullPointerException e) {
promise.reject(new JSApplicationCausedNativeException("You should call getMetricsEnabled after setAccessToken"));
}
}
@ReactMethod
public void setMetricsEnabled(boolean value) {
MapboxTelemetry.getInstance().setTelemetryEnabled(value);
+1 -4
View File
@@ -73,15 +73,12 @@ if (Platform.OS === 'android') {
// Metrics
let _metricsEnabled = MapboxGLManager.metricsEnabled;
function setMetricsEnabled(enabled: boolean) {
_metricsEnabled = enabled;
MapboxGLManager.setMetricsEnabled(enabled);
}
function getMetricsEnabled() {
return _metricsEnabled;
return MapboxGLManager.getMetricsEnabled();
}
// Access token
+7 -5
View File
@@ -111,21 +111,23 @@ RCT_CUSTOM_VIEW_PROPERTY(contentInset, UIEdgeInsetsMake, RCTMapboxGL)
@"complete": [NSNumber numberWithUnsignedInt:MGLOfflinePackStateComplete],
@"invalid": [NSNumber numberWithUnsignedInt:MGLOfflinePackStateInvalid]
},
@"unknownResourceCount": @(UINT64_MAX),
@"metricsEnabled": @([RCTMapboxGLManager metricsEnabled])
@"unknownResourceCount": @(UINT64_MAX)
};
};
// Metrics
+ (BOOL)metricsEnabled
RCT_EXPORT_METHOD(getMetricsEnabled:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
NSNumber * nr = [ud valueForKey:@"MGLMapboxMetricsEnabled"];
if (!nr || ![nr isKindOfClass:[NSNumber class]]) {
return YES;
resolve(@YES);
return;
}
return nr.boolValue;
resolve([NSNumber numberWithBool:nr.boolValue]);
}
RCT_EXPORT_METHOD(setMetricsEnabled:(BOOL)enabled)