From ac31979da406f2d9f2f8b9ab487df6aa199efbaa Mon Sep 17 00:00:00 2001 From: Jack Feng Date: Tue, 16 May 2017 10:53:20 +0800 Subject: [PATCH] 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. --- API.md | 2 +- .../ReactNativeMapboxGLModule.java | 12 ++++++++++-- index.js | 5 +---- ios/RCTMapboxGL/RCTMapboxGLManager.m | 12 +++++++----- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/API.md b/API.md index c7c1769..b810825 100644 --- a/API.md +++ b/API.md @@ -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)`. diff --git a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java index 71ed25d..ceaaeb6 100644 --- a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java +++ b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java @@ -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); diff --git a/index.js b/index.js index ce43a97..37a6e10 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/ios/RCTMapboxGL/RCTMapboxGLManager.m b/ios/RCTMapboxGL/RCTMapboxGLManager.m index 56e1e05..5f89888 100644 --- a/ios/RCTMapboxGL/RCTMapboxGLManager.m +++ b/ios/RCTMapboxGL/RCTMapboxGLManager.m @@ -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)