From 5b54aaf40819bc0909558824117b077566d43522 Mon Sep 17 00:00:00 2001 From: Sarah Lensing Date: Tue, 16 May 2017 12:18:22 -0600 Subject: [PATCH] pass global variable scene updates when switching styles (#371) --- config/checkstyle/checkstyle-suppressions.xml | 1 + .../android/graphics/GraphicsModule.java | 8 +++++ .../android/graphics/MapInitializer.java | 17 +++++----- .../mapzen/android/graphics/MapzenMap.java | 15 +++++++-- .../android/graphics/SceneUpdateManager.java | 29 ++++++++++++++++ .../android/graphics/MapInitializerTest.java | 6 ++-- .../android/graphics/MapzenMapTest.java | 18 +++++++++- .../graphics/SceneUpdateManagerTest.java | 33 +++++++++++++++++++ .../mapzen/android/graphics/TestMapView.java | 9 +++-- 9 files changed, 120 insertions(+), 16 deletions(-) create mode 100644 core/src/main/java/com/mapzen/android/graphics/SceneUpdateManager.java create mode 100644 core/src/test/java/com/mapzen/android/graphics/SceneUpdateManagerTest.java diff --git a/config/checkstyle/checkstyle-suppressions.xml b/config/checkstyle/checkstyle-suppressions.xml index e5beb7e2..803c4ac2 100644 --- a/config/checkstyle/checkstyle-suppressions.xml +++ b/config/checkstyle/checkstyle-suppressions.xml @@ -17,4 +17,5 @@ + diff --git a/core/src/main/java/com/mapzen/android/graphics/GraphicsModule.java b/core/src/main/java/com/mapzen/android/graphics/GraphicsModule.java index 9aaeedd5..ab759dbe 100644 --- a/core/src/main/java/com/mapzen/android/graphics/GraphicsModule.java +++ b/core/src/main/java/com/mapzen/android/graphics/GraphicsModule.java @@ -26,4 +26,12 @@ @Provides @Singleton public MapStateManager providesMapStateManager() { return new MapStateManager(); } + + /** + * Returns the object used create scene updates for global variables on a {@link MapzenMap}. + * @return + */ + @Provides @Singleton public SceneUpdateManager providesSceneUpdateManager() { + return new SceneUpdateManager(); + } } diff --git a/core/src/main/java/com/mapzen/android/graphics/MapInitializer.java b/core/src/main/java/com/mapzen/android/graphics/MapInitializer.java index 936d4b30..8d38f8a7 100644 --- a/core/src/main/java/com/mapzen/android/graphics/MapInitializer.java +++ b/core/src/main/java/com/mapzen/android/graphics/MapInitializer.java @@ -9,7 +9,7 @@ import android.content.Context; -import java.util.ArrayList; +import java.util.List; import java.util.Locale; import javax.inject.Inject; @@ -18,8 +18,6 @@ * Class responsible for initializing the map. */ public class MapInitializer { - static final String STYLE_GLOBAL_VAR_API_KEY = "global.sdk_mapzen_api_key"; - static final String STYLE_GLOBAL_VAR_LANGUAGE = "global.ux_language"; private Context context; @@ -29,17 +27,21 @@ public class MapInitializer { private MapStateManager mapStateManager; + private SceneUpdateManager sceneUpdateManager; + private Locale locale = Locale.getDefault(); /** * Creates a new instance. */ @Inject MapInitializer(Context context, TileHttpHandler tileHttpHandler, - MapDataManager mapDataManager, MapStateManager mapStateManager) { + MapDataManager mapDataManager, MapStateManager mapStateManager, + SceneUpdateManager sceneUpdateManager) { this.context = context; this.tileHttpHandler = tileHttpHandler; this.mapDataManager = mapDataManager; this.mapStateManager = mapStateManager; + this.sceneUpdateManager = sceneUpdateManager; } /** @@ -84,17 +86,16 @@ private void loadMap(final MapView mapView, MapStyle mapStyle, boolean styleExpl } private void loadMap(final MapView mapView, String sceneFile, final OnMapReadyCallback callback) { - final ArrayList sceneUpdates = new ArrayList<>(1); final String apiKey = MapzenManager.instance(context).getApiKey(); - sceneUpdates.add(new SceneUpdate(STYLE_GLOBAL_VAR_API_KEY, apiKey)); - sceneUpdates.add(new SceneUpdate(STYLE_GLOBAL_VAR_LANGUAGE, locale.getLanguage())); + final List sceneUpdates = sceneUpdateManager.getUpdatesFor(apiKey, locale); getTangramView(mapView).getMapAsync(new com.mapzen.tangram.MapView.OnMapReadyCallback() { @Override public void onMapReady(MapController mapController) { mapController.setHttpHandler(tileHttpHandler); + MapzenManager mapzenManager = MapzenManager.instance(mapView.getContext()); callback.onMapReady( new MapzenMap(mapView, mapController, new OverlayManager(mapView, mapController, mapDataManager, mapStateManager), mapStateManager, new LabelPickHandler(mapView), - new MarkerManager(mapController))); + new MarkerManager(mapController), sceneUpdateManager, locale, mapzenManager)); } }, sceneFile, sceneUpdates); } diff --git a/core/src/main/java/com/mapzen/android/graphics/MapzenMap.java b/core/src/main/java/com/mapzen/android/graphics/MapzenMap.java index 574acf83..de488224 100644 --- a/core/src/main/java/com/mapzen/android/graphics/MapzenMap.java +++ b/core/src/main/java/com/mapzen/android/graphics/MapzenMap.java @@ -1,5 +1,6 @@ package com.mapzen.android.graphics; +import com.mapzen.android.core.MapzenManager; import com.mapzen.android.graphics.model.BitmapMarker; import com.mapzen.android.graphics.model.CameraType; import com.mapzen.android.graphics.model.EaseType; @@ -25,6 +26,7 @@ import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; /** @@ -41,6 +43,9 @@ public class MapzenMap { private final MapStateManager mapStateManager; private final LabelPickHandler labelPickHandler; private final MarkerManager markerManager; + private final SceneUpdateManager sceneUpdateManager; + private final MapzenManager mapzenManager; + private Locale locale; boolean pickFeatureOnSingleTapConfirmed = false; boolean pickLabelOnSingleTapConfirmed = false; @@ -140,13 +145,17 @@ public boolean onRotate(float x, float y, float rotation) { */ MapzenMap(MapView mapView, MapController mapController, OverlayManager overlayManager, MapStateManager mapStateManager, LabelPickHandler labelPickHandler, - MarkerManager markerManager) { + MarkerManager markerManager, SceneUpdateManager sceneUpdateManager, Locale locale, + MapzenManager mapzenManager) { this.mapView = mapView; this.mapController = mapController; this.overlayManager = overlayManager; this.mapStateManager = mapStateManager; this.labelPickHandler = labelPickHandler; this.markerManager = markerManager; + this.sceneUpdateManager = sceneUpdateManager; + this.locale = locale; + this.mapzenManager = mapzenManager; mapView.setMapzenMap(this); mapController.setPanResponder(internalPanResponder); mapController.setRotateResponder(internalRotateResponder); @@ -173,7 +182,9 @@ public OverlayManager getOverlayManager() { */ public void setStyle(MapStyle mapStyle) { mapStateManager.setMapStyle(mapStyle); - mapController.loadSceneFile(mapStyle.getSceneFile()); + String apiKey = mapzenManager.getApiKey(); + List globalSceneUpdates = sceneUpdateManager.getUpdatesFor(apiKey, locale); + mapController.loadSceneFile(mapStyle.getSceneFile(), globalSceneUpdates); } /** diff --git a/core/src/main/java/com/mapzen/android/graphics/SceneUpdateManager.java b/core/src/main/java/com/mapzen/android/graphics/SceneUpdateManager.java new file mode 100644 index 00000000..1d3dee10 --- /dev/null +++ b/core/src/main/java/com/mapzen/android/graphics/SceneUpdateManager.java @@ -0,0 +1,29 @@ +package com.mapzen.android.graphics; + +import com.mapzen.tangram.SceneUpdate; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +/** + * Manages creation of {@link SceneUpdate}s for api keys and languages. + */ +class SceneUpdateManager { + + static final String STYLE_GLOBAL_VAR_API_KEY = "global.sdk_mapzen_api_key"; + static final String STYLE_GLOBAL_VAR_LANGUAGE = "global.ux_language"; + + /** + * Creates {@link SceneUpdate}s given an api key and locale. + * @param apiKey user's api key. + * @param locale user's preferred map locale. + * @return scene updates for api key and locale. + */ + List getUpdatesFor(String apiKey, Locale locale) { + final ArrayList sceneUpdates = new ArrayList<>(2); + sceneUpdates.add(new SceneUpdate(STYLE_GLOBAL_VAR_API_KEY, apiKey)); + sceneUpdates.add(new SceneUpdate(STYLE_GLOBAL_VAR_LANGUAGE, locale.getLanguage())); + return sceneUpdates; + } +} diff --git a/core/src/test/java/com/mapzen/android/graphics/MapInitializerTest.java b/core/src/test/java/com/mapzen/android/graphics/MapInitializerTest.java index 6fe03dfc..e4e1e9e0 100644 --- a/core/src/test/java/com/mapzen/android/graphics/MapInitializerTest.java +++ b/core/src/test/java/com/mapzen/android/graphics/MapInitializerTest.java @@ -20,8 +20,8 @@ import java.util.Locale; import static com.mapzen.TestHelper.getMockContext; -import static com.mapzen.android.graphics.MapInitializer.STYLE_GLOBAL_VAR_API_KEY; -import static com.mapzen.android.graphics.MapInitializer.STYLE_GLOBAL_VAR_LANGUAGE; +import static com.mapzen.android.graphics.SceneUpdateManager.STYLE_GLOBAL_VAR_API_KEY; +import static com.mapzen.android.graphics.SceneUpdateManager.STYLE_GLOBAL_VAR_LANGUAGE; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; @@ -40,7 +40,7 @@ public class MapInitializerTest { @Before public void setUp() throws Exception { CoreDI.init(getMockContext()); mapInitializer = new MapInitializer(mock(Context.class), mock(TileHttpHandler.class), - new MapDataManager(), new MapStateManager()); + new MapDataManager(), new MapStateManager(), new SceneUpdateManager()); } @Test public void shouldNotBeNull() throws Exception { diff --git a/core/src/test/java/com/mapzen/android/graphics/MapzenMapTest.java b/core/src/test/java/com/mapzen/android/graphics/MapzenMapTest.java index 20aaf6cd..ab05993b 100644 --- a/core/src/test/java/com/mapzen/android/graphics/MapzenMapTest.java +++ b/core/src/test/java/com/mapzen/android/graphics/MapzenMapTest.java @@ -1,5 +1,6 @@ package com.mapzen.android.graphics; +import com.mapzen.android.core.MapzenManager; import com.mapzen.android.graphics.model.BitmapMarker; import com.mapzen.android.graphics.model.CameraType; import com.mapzen.android.graphics.model.EaseType; @@ -7,6 +8,7 @@ import com.mapzen.android.graphics.model.MarkerManager; import com.mapzen.android.graphics.model.Polygon; import com.mapzen.android.graphics.model.Polyline; +import com.mapzen.android.graphics.model.WalkaboutStyle; import com.mapzen.tangram.LabelPickResult; import com.mapzen.tangram.LngLat; import com.mapzen.tangram.MapController; @@ -25,11 +27,13 @@ import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyFloat; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doCallRealMethod; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -49,6 +53,9 @@ public class MapzenMapTest { private LabelPickHandler labelPickHandler; private MapStateManager mapStateManager; private MarkerManager markerManager; + private SceneUpdateManager sceneUpdateManager; + private Locale locale; + private MapzenManager mapzenManager; @Before public void setUp() throws Exception { mapView = new TestMapView(); @@ -73,8 +80,11 @@ public class MapzenMapTest { mapStateManager = new MapStateManager(); labelPickHandler = new LabelPickHandler(mapView); markerManager = new MarkerManager(mapController); + sceneUpdateManager = new SceneUpdateManager(); + locale = new Locale("en_us"); + mapzenManager = mock(MapzenManager.class); map = new MapzenMap(mapView, mapController, overlayManager, mapStateManager, labelPickHandler, - markerManager); + markerManager, sceneUpdateManager, locale, mapzenManager); } @Test public void shouldNotBeNull() throws Exception { @@ -583,6 +593,12 @@ public class MapzenMapTest { assertThat(mapStateManager.getTilt()).isEqualTo(1.57f); } + @Test public void setStyle_shouldSetStyleAndGlobalVariables() throws Exception { + map.setStyle(new WalkaboutStyle()); + verify(mapController).loadSceneFile( + anyString(), any(List.class)); + } + public class TestRotateResponder implements TouchInput.RotateResponder { boolean rotated = false; diff --git a/core/src/test/java/com/mapzen/android/graphics/SceneUpdateManagerTest.java b/core/src/test/java/com/mapzen/android/graphics/SceneUpdateManagerTest.java new file mode 100644 index 00000000..97f1e585 --- /dev/null +++ b/core/src/test/java/com/mapzen/android/graphics/SceneUpdateManagerTest.java @@ -0,0 +1,33 @@ +package com.mapzen.android.graphics; + +import com.mapzen.tangram.SceneUpdate; + +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.Locale; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SceneUpdateManagerTest { + + SceneUpdateManager manager; + String apiKey; + Locale locale; + + @Before public void setUp() throws Exception { + manager = new SceneUpdateManager(); + apiKey = "test_key"; + locale = new Locale("fr_fr"); + } + + @Test public void shouldReturnCorrectKeysAndValues() { + List updates = manager.getUpdatesFor(apiKey, locale); + assertThat(updates.size()).isEqualTo(2); + assertThat(updates.get(0).getPath()).isEqualTo("global.sdk_mapzen_api_key"); + assertThat(updates.get(0).getValue()).isEqualTo(apiKey); + assertThat(updates.get(1).getPath()).isEqualTo("global.ux_language"); + assertThat(updates.get(1).getValue()).isEqualTo(locale.getLanguage()); + } +} diff --git a/core/src/test/java/com/mapzen/android/graphics/TestMapView.java b/core/src/test/java/com/mapzen/android/graphics/TestMapView.java index 3546d477..1055fa38 100644 --- a/core/src/test/java/com/mapzen/android/graphics/TestMapView.java +++ b/core/src/test/java/com/mapzen/android/graphics/TestMapView.java @@ -1,5 +1,6 @@ package com.mapzen.android.graphics; +import com.mapzen.android.core.MapzenManager; import com.mapzen.android.graphics.model.MapStyle; import com.mapzen.android.graphics.model.MarkerManager; import com.mapzen.tangram.MapController; @@ -7,6 +8,8 @@ import android.content.Context; import android.support.annotation.NonNull; +import java.util.Locale; + import static com.mapzen.TestHelper.getMockContext; import static org.mockito.Mockito.mock; @@ -20,13 +23,15 @@ public class TestMapView extends MapView { @Override public void getMapAsync(@NonNull OnMapReadyCallback callback) { callback.onMapReady(new MapzenMap(mock(MapView.class), mock(MapController.class), mock(OverlayManager.class), mock(MapStateManager.class), mock(LabelPickHandler.class), - mock(MarkerManager.class))); + mock(MarkerManager.class), mock(SceneUpdateManager.class), new Locale("en_us"), mock( + MapzenManager.class))); } @Override public void getMapAsync(MapStyle mapStyle, @NonNull OnMapReadyCallback callback) { callback.onMapReady(new MapzenMap(mock(MapView.class), mock(MapController.class), mock(OverlayManager.class), mock(MapStateManager.class), mock(LabelPickHandler.class), - mock(MarkerManager.class))); + mock(MarkerManager.class), mock(SceneUpdateManager.class), new Locale("en_us"), mock( + MapzenManager.class))); } @Override public TangramMapView getTangramMapView() {