Last active
May 26, 2020 18:37
-
-
Save andfs/dc833e70b6d9cfcb1fd3b85865c704c0 to your computer and use it in GitHub Desktop.
Class that overrides NavigationView from Mapbox and is returned to React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.navdemo; | |
import android.Manifest; | |
import android.content.pm.PackageManager; | |
import android.os.Build; | |
import android.os.Handler; | |
import android.os.Looper; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.RequiresApi; | |
import androidx.core.app.ActivityCompat; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.bridge.ReadableArray; | |
import com.facebook.react.common.MapBuilder; | |
import com.facebook.react.uimanager.ViewGroupManager; | |
import com.facebook.react.uimanager.ThemedReactContext; | |
import com.facebook.react.uimanager.annotations.ReactProp; | |
import com.mapbox.geojson.Point; | |
import com.mapbox.mapboxsdk.Mapbox; | |
import com.mapbox.services.android.navigation.ui.v5.NavigationView; | |
import java.util.Map; | |
import javax.annotation.Nonnull; | |
import javax.annotation.Nullable; | |
public class MapboxViewManager extends ViewGroupManager<NavigationView> { | |
public static final String REACT_CLASS = "MapNav"; | |
ReactApplicationContext mCallerContext; | |
// NavigationView navigationView; | |
// Point origin; | |
// Point destination; | |
public MapboxViewManager(ReactApplicationContext reactContext) { | |
mCallerContext = reactContext; | |
new Handler(Looper.getMainLooper()).post(new Runnable() { | |
@Override | |
public void run() { | |
Mapbox.getInstance(reactContext, reactContext.getString(R.string.MAPBOX_ACCESS_TOKEN)); | |
} | |
}); | |
} | |
@Nonnull | |
@Override | |
public String getName() { | |
return REACT_CLASS; | |
} | |
@Nonnull | |
@Override | |
protected NavigationView createViewInstance(@Nonnull ThemedReactContext reactContext) { | |
//return new MapboxNavigationView(context, Fresco.newDraweeControllerBuilder(), null, mCallerContext); | |
return new MapboxNavigationView(reactContext); | |
} | |
@Override | |
public void onDropViewInstance(@NonNull NavigationView view) { | |
view.onStop(); | |
view.onDestroy(); | |
super.onDropViewInstance(view); | |
} | |
@Override | |
public Map<String, Object> getExportedCustomDirectEventTypeConstants() { | |
return MapBuilder.of("onProgressChange", MapBuilder.of("registrationName", "onProgressChange")); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
@ReactProp(name = "origin") | |
public void setOrigin(MapboxNavigationView view, @Nullable ReadableArray sources) { | |
if (sources == null) { | |
view.setOrigin(null); | |
// origin = Point.fromLngLat(sources.getDouble(1), sources.getDouble(0)); | |
return; | |
} | |
view.setOrigin(Point.fromLngLat(sources.getDouble(1), sources.getDouble(0))); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
@ReactProp(name = "destination") | |
public void setDestination(MapboxNavigationView view, @Nullable ReadableArray sources) { | |
if (sources == null) { | |
view.setDestination(null); | |
// destination = Point.fromLngLat(sources.getDouble(1), sources.getDouble(0)); | |
return; | |
} | |
view.setDestination(Point.fromLngLat(sources.getDouble(1), sources.getDouble(0))); | |
} | |
@ReactProp(name = "shouldSimulateRoute") | |
public void setShouldSimulateRoute(MapboxNavigationView view, boolean shouldSimulateRoute) { | |
view.setShouldSimulateRoute(shouldSimulateRoute); | |
} | |
@ReactProp(name = "isMuted") | |
public void setIsMuted(MapboxNavigationView view, boolean isMuted) { | |
view.setIsMuted(isMuted); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment