mirror of
https://github.com/status-im/react-native-mapbox-gl.git
synced 2026-08-30 12:31:11 +00:00
Make offline packs more robust.
- Separate the access token initialization and offline maps initialization. - Make the initialization functions return promise so that the consumer code can know when mapbox and the offline packs are ready to be used. - Expose setConnected method. - Update docs.
This commit is contained in:
+20
-4
@@ -33,6 +33,7 @@ import com.facebook.react.bridge.ReadableNativeMap;
|
||||
import com.facebook.react.bridge.WritableArray;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.react.bridge.WritableNativeArray;
|
||||
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
||||
import com.facebook.react.modules.core.RCTNativeAppEventEmitter;
|
||||
import com.facebook.react.uimanager.annotations.ReactProp;
|
||||
import com.mapbox.mapboxsdk.MapboxAccountManager;
|
||||
@@ -138,15 +139,19 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
|
||||
// Access Token
|
||||
|
||||
@ReactMethod
|
||||
public void setAccessToken(final String accessToken) {
|
||||
public void setAccessToken(final String accessToken, final Promise promise) {
|
||||
if (accessToken == null || accessToken.length() == 0 || accessToken.equals("your-mapbox.com-access-token")) {
|
||||
throw new JSApplicationIllegalArgumentException("Invalid access token. Register to mapbox.com and request an access token, then pass it to setAccessToken()");
|
||||
}
|
||||
if (initialized) {
|
||||
String oldToken = MapboxAccountManager.getInstance().getAccessToken();
|
||||
if (!oldToken.equals(accessToken)) {
|
||||
throw new JSApplicationIllegalArgumentException("Mapbox access token cannot be initialized twice with different values");
|
||||
JSApplicationIllegalArgumentException error =
|
||||
new JSApplicationIllegalArgumentException("Mapbox access token cannot be initialized twice with different values");
|
||||
promise.reject(error);
|
||||
throw error;
|
||||
}
|
||||
promise.resolve(null);
|
||||
return;
|
||||
}
|
||||
initialized = true;
|
||||
@@ -154,9 +159,15 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
|
||||
@Override
|
||||
public void run() {
|
||||
MapboxAccountManager.start(context.getApplicationContext(), accessToken);
|
||||
promise.resolve(null);
|
||||
}
|
||||
});
|
||||
initializeOfflinePacks();
|
||||
}
|
||||
|
||||
// Connected
|
||||
@ReactMethod
|
||||
public void setConnected(boolean connected) {
|
||||
MapboxAccountManager.getInstance().setConnected(connected);
|
||||
}
|
||||
|
||||
// Metrics
|
||||
@@ -264,7 +275,7 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
class OfflineRegionsInitialRequest implements OfflineManager.ListOfflineRegionsCallback {
|
||||
ReactNativeMapboxGLModule module;
|
||||
private final ReactNativeMapboxGLModule module;
|
||||
|
||||
OfflineRegionsInitialRequest(ReactNativeMapboxGLModule module) {
|
||||
this.module = module;
|
||||
@@ -293,6 +304,10 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.context
|
||||
.getJSModule(RCTNativeAppEventEmitter.class)
|
||||
.emit("MapboxOfflinePacksLoaded", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -301,6 +316,7 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
void initializeOfflinePacks() {
|
||||
final ReactNativeMapboxGLModule _this = this;
|
||||
mainHandler.post(new Runnable() {
|
||||
|
||||
Reference in New Issue
Block a user