Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update traffic plugin with the Maps SDK v7.0.0 #793

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TrafficActivity : AppCompatActivity(), OnMapReadyCallback {
override fun onMapReady(mapboxMap: MapboxMap) {
this.mapboxMap = mapboxMap
mapboxMap.setStyle(Style.MAPBOX_STREETS) {
this.trafficPlugin = TrafficPlugin(mapView, mapboxMap)
this.trafficPlugin = TrafficPlugin(mapView, mapboxMap, it)
this.trafficPlugin?.setVisibility(true) // Enable the traffic view by default
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.annotation.VisibleForTesting;

import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Style;
Expand All @@ -15,25 +17,26 @@
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.mapboxsdk.style.sources.VectorSource;
import timber.log.Timber;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
import timber.log.Timber;

import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential;
import static com.mapbox.mapboxsdk.style.expressions.Expression.match;
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
import static com.mapbox.mapboxsdk.style.expressions.Expression.literal;
import static com.mapbox.mapboxsdk.style.expressions.Expression.match;
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineOffset;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineOffset;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineOpacity;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility;

/**
Expand All @@ -47,9 +50,11 @@
* Use {@link #isVisible()} to validate if the plugin is active or not.
* </p>
*/
@UiThread
public final class TrafficPlugin {

private final MapboxMap mapboxMap;
private Style style;
private final List<String> layerIds = new ArrayList<>();
private final String belowLayer;
private boolean visible;
Expand All @@ -60,8 +65,8 @@ public final class TrafficPlugin {
* @param mapView the MapView to apply the traffic plugin to
* @param mapboxMap the MapboxMap to apply traffic plugin with
*/
public TrafficPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap) {
this(mapView, mapboxMap, null);
public TrafficPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap, @NonNull Style style) {
this(mapView, mapboxMap, style, null);
}

/**
Expand All @@ -71,12 +76,16 @@ public TrafficPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap) {
* @param mapboxMap the MapboxMap to apply traffic plugin with
* @param belowLayer the layer id where you'd like the traffic to display below
*/
public TrafficPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap,
public TrafficPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap, @NonNull Style style,
@Nullable String belowLayer) {
if (!style.isFullyLoaded()) {
throw new RuntimeException("The style has to be non-null and fully loaded.");
}

this.mapboxMap = mapboxMap;
this.style = style;
this.belowLayer = belowLayer;
mapView.addOnDidFinishLoadingStyleListener(new StyleLoadHandler(this));
updateState();
mapView.addOnWillStartLoadingMapListener(new StyleLoadHandler(this));
}

/**
Expand All @@ -94,32 +103,26 @@ public boolean isVisible() {
* @param visible true for visible, false for none
*/
public void setVisibility(boolean visible) {
Source source = mapboxMap.getStyle().getSource(TrafficData.SOURCE_ID);
this.visible = visible;

if (!style.isFullyLoaded()) {
// We are in progress of loading a new style
return;
}

Source source = style.getSource(TrafficData.SOURCE_ID);
if (source == null) {
initialise();
}

this.visible = visible;
List<Layer> layers = mapboxMap.getStyle().getLayers();
List<Layer> layers = style.getLayers();
for (Layer layer : layers) {
if (layerIds.contains(layer.getId())) {
layer.setProperties(visibility(visible ? Property.VISIBLE : Property.NONE));
}
}
}

/**
* Update the state of the traffic plugin.
*/
private void updateState() {
Source source = mapboxMap.getStyle().getSource(TrafficData.SOURCE_ID);
if (source == null) {
initialise();
return;
}
setVisibility(visible);
}

/**
* Initialise the traffic source and layers.
*/
Expand All @@ -139,7 +142,7 @@ private void initialise() {
*/
private void addTrafficSource() {
VectorSource trafficSource = new VectorSource(TrafficData.SOURCE_ID, TrafficData.SOURCE_URL);
mapboxMap.getStyle().addSource(trafficSource);
style.addSource(trafficSource);
}

/**
Expand Down Expand Up @@ -185,7 +188,7 @@ private void addLocalLayer() {
*/
private String placeLayerBelow() {
if (belowLayer == null || belowLayer.isEmpty()) {
List<Layer> styleLayers = mapboxMap.getStyle().getLayers();
List<Layer> styleLayers = style.getLayers();
Layer layer;
for (int i = styleLayers.size() - 1; i >= 0; i--) {
layer = styleLayers.get(i);
Expand Down Expand Up @@ -315,8 +318,8 @@ private String getLastAddedLayerId() {
* @param idAboveLayer the id of the layer above
*/
private void addTrafficLayersToMap(Layer layerCase, Layer layer, String idAboveLayer) {
mapboxMap.getStyle().addLayerBelow(layerCase, idAboveLayer);
mapboxMap.getStyle().addLayerAbove(layer, layerCase.getId());
style.addLayerBelow(layerCase, idAboveLayer);
style.addLayerAbove(layer, layerCase.getId());
layerIds.add(layerCase.getId());
layerIds.add(layer.getId());
}
Expand Down Expand Up @@ -549,7 +552,7 @@ private static class TrafficColor {
static final int CASE_RED = Color.parseColor("#5f1117");
}

private static class StyleLoadHandler implements MapView.OnDidFinishLoadingStyleListener {
private static class StyleLoadHandler implements MapView.OnWillStartLoadingMapListener {

private WeakReference<TrafficPlugin> trafficPlugin;

Expand All @@ -558,11 +561,21 @@ private static class StyleLoadHandler implements MapView.OnDidFinishLoadingStyle
}

@Override
public void onDidFinishLoadingStyle() {
public void onWillStartLoadingMap() {
TrafficPlugin trafficPlugin = this.trafficPlugin.get();
if (trafficPlugin != null && trafficPlugin.isVisible()) {
trafficPlugin.updateState();
if (trafficPlugin != null) {
trafficPlugin.onWillStartLoadingMap();
}
}
}

private void onWillStartLoadingMap() {
mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
TrafficPlugin.this.style = style;
setVisibility(visible);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Style;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

Expand All @@ -15,29 +17,40 @@
public class TrafficPluginTest {

@Mock
MapView mapView;
private MapView mapView;

@Mock
MapboxMap mapboxMap;
private MapboxMap mapboxMap;

@Mock
private Style style;

@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Test(expected = NullPointerException.class)
@Test(expected = RuntimeException.class)
public void testNonNullAnnotatedArgs() {
new TrafficPlugin(null, null);
new TrafficPlugin(null, null, null);
}

@Test
public void testSanity() {
new TrafficPlugin(mapView, mapboxMap);
Mockito.when(style.isFullyLoaded()).thenReturn(true);
new TrafficPlugin(mapView, mapboxMap, style);
}

@Test
public void testToggle() {
TrafficPlugin trafficPlugin = new TrafficPlugin(mapView, mapboxMap);
Mockito.when(style.isFullyLoaded()).thenReturn(true);
TrafficPlugin trafficPlugin = new TrafficPlugin(mapView, mapboxMap, style);
assertFalse(trafficPlugin.isVisible());
trafficPlugin.setVisibility(true);
assertTrue(trafficPlugin.isVisible());
}

@Test(expected = RuntimeException.class)
public void testNotLoadedStyle() {
Mockito.when(style.isFullyLoaded()).thenReturn(false);
new TrafficPlugin(mapView, mapboxMap, style);
}
}