Skip to content

Commit

Permalink
Async setStyle (#450)
Browse files Browse the repository at this point in the history
* Add OnStyleLoadedListener

* Add setStyleAsync

* Add themed map style async methods
  • Loading branch information
sarahsnow1 committed Sep 14, 2017
1 parent c781e9e commit 4ee9736
Show file tree
Hide file tree
Showing 3 changed files with 322 additions and 26 deletions.
133 changes: 112 additions & 21 deletions core/src/main/java/com/mapzen/android/graphics/MapzenMap.java
Expand Up @@ -149,6 +149,13 @@ public boolean onRotate(float x, float y, float rotation) {
= new MapController.SceneLoadListener() {
@Override public void onSceneReady(int sceneId, SceneError sceneError) {
bitmapMarkerManager.restoreMarkers();
if (sceneId == currSceneId) {
if (styleLoadedListener != null) {
styleLoadedListener.onStyleLoaded();
styleLoadedListener = null;
}
currSceneId = Integer.MIN_VALUE;
}
}
};

Expand All @@ -161,6 +168,9 @@ public boolean onRotate(float x, float y, float rotation) {
}
});

OnStyleLoadedListener styleLoadedListener = null;
int currSceneId = Integer.MIN_VALUE;

/**
* Creates a new map based on the given {@link MapView} and {@link MapController}.
*/
Expand Down Expand Up @@ -201,19 +211,32 @@ public OverlayManager getOverlayManager() {
return overlayManager;
}

/**
* Sets the map's underlying stylesheet asynchronously.
*/
public void setStyleAsync(MapStyle mapStyle, OnStyleLoadedListener listener) {
styleLoadedListener = listener;
currSceneId = internalSetStyle(mapStyle, true);
}

/**
* Sets the map's underlying stylesheet.
*/
public void setStyle(MapStyle mapStyle) {
mapStateManager.setMapStyle(mapStyle);
if (currentMapStyleIsThemed()) {
mapStateManager.setLabelLevel(getThemedMapStyle().getDefaultLabelLevel());
mapStateManager.setLod(getThemedMapStyle().getDefaultLod());
mapStateManager.setThemeColor(getThemedMapStyle().getDefaultColor());
loadSceneYaml();
} else {
loadSceneFile();
}
internalSetStyle(mapStyle, false);
}

/**
* Sets the map style with given label level and default detail and theme color values. If the
* label level is not supported by this theme then this method throws an
* {@link IllegalArgumentException}.
* @param themedMapStyle
* @param labelLevel
*/
public void setStyleAndLabelLevelAsync(ThemedMapStyle themedMapStyle, int labelLevel,
OnStyleLoadedListener listener) {
setStyleLabelLevelLodThemeColorAsync(themedMapStyle, labelLevel,
themedMapStyle.getDefaultLod(), themedMapStyle.getDefaultColor(), listener);
}

/**
Expand All @@ -228,6 +251,19 @@ public void setStyleAndLabelLevel(ThemedMapStyle themedMapStyle, int labelLevel)
themedMapStyle.getDefaultLod(), themedMapStyle.getDefaultColor());
}

/**
* Sets the map style with given detail level and default label and theme color values. If the
* detail level is not supported by this theme then this method throws an
* {@link IllegalArgumentException}.
* @param themedMapStyle
* @param detailLevel
*/
public void setStyleAndLodAsync(ThemedMapStyle themedMapStyle, int detailLevel,
OnStyleLoadedListener listener) {
setStyleLabelLevelLodThemeColorAsync(themedMapStyle, themedMapStyle.getDefaultLabelLevel(),
detailLevel, themedMapStyle.getDefaultColor(), listener);
}

/**
* Sets the map style with given detail level and default label and theme color values. If the
* detail level is not supported by this theme then this method throws an
Expand All @@ -240,6 +276,17 @@ public void setStyleAndLod(ThemedMapStyle themedMapStyle, int detailLevel) {
detailLevel, themedMapStyle.getDefaultColor());
}

/**
* Sets the map style with given theme color and default label and detail levels.
* @param themedMapStyle
* @param color
*/
public void setStyleAndThemeColorAsync(ThemedMapStyle themedMapStyle, ThemeColor color,
OnStyleLoadedListener listener) {
setStyleLabelLevelLodThemeColorAsync(themedMapStyle, themedMapStyle.getDefaultLabelLevel(),
themedMapStyle.getDefaultLod(), color, listener);
}

/**
* Sets the map style with given theme color and default label and detail levels.
* @param themedMapStyle
Expand All @@ -250,6 +297,21 @@ public void setStyleAndThemeColor(ThemedMapStyle themedMapStyle, ThemeColor colo
themedMapStyle.getDefaultLod(), color);
}

/**
* Sets the map style with given label level, detail level, and theme color. If either the label
* or detail level are not supported, this method will throw an {@link IllegalArgumentException}.
* @param themedMapStyle
* @param labelLevel
* @param detailLevel
* @param color
*/
public void setStyleLabelLevelLodThemeColorAsync(ThemedMapStyle themedMapStyle, int labelLevel,
int detailLevel, ThemeColor color, OnStyleLoadedListener listener) {
styleLoadedListener = listener;
mapStateManager.setMapStyle(themedMapStyle);
currSceneId = setLabelLevelLodThemeColor(labelLevel, detailLevel, color, true);
}

/**
* Sets the map style with given label level, detail level, and theme color. If either the label
* or detail level are not supported, this method will throw an {@link IllegalArgumentException}.
Expand All @@ -261,7 +323,7 @@ public void setStyleAndThemeColor(ThemedMapStyle themedMapStyle, ThemeColor colo
public void setStyleLabelLevelLodThemeColor(ThemedMapStyle themedMapStyle, int labelLevel,
int detailLevel, ThemeColor color) {
mapStateManager.setMapStyle(themedMapStyle);
setLabelLevelLodThemeColor(labelLevel, detailLevel, color);
setLabelLevelLodThemeColor(labelLevel, detailLevel, color, false);
}

/**
Expand Down Expand Up @@ -1064,26 +1126,55 @@ private List<SceneUpdate> getGlobalSceneUpdates() {
mapStateManager.isPathOverlayEnabled());
}

/**
* Sets the {@link MapStyle} and relevant theme configuration for {@link ThemedMapStyle}s. Loads
* the scene file either synchronously or asynchronously and returns the sceneId.
* @param mapStyle
* @param async
* @return
*/
private int internalSetStyle(MapStyle mapStyle, boolean async) {
mapStateManager.setMapStyle(mapStyle);
if (currentMapStyleIsThemed()) {
mapStateManager.setLabelLevel(getThemedMapStyle().getDefaultLabelLevel());
mapStateManager.setLod(getThemedMapStyle().getDefaultLod());
mapStateManager.setThemeColor(getThemedMapStyle().getDefaultColor());
return loadSceneYaml(async);
} else {
return loadSceneFile(async);
}
}

/**
* Internal convenience method for loading scene file when the current style is a
* {@link MapStyle}.
* Applies all global scene updates.
* Applies all global scene updates. Loads asynchronously or
* synchronously. Returns the sceneId for the {@link MapController} scene update.
*/
private void loadSceneFile() {
mapController.loadSceneFile(mapStateManager.getMapStyle().getSceneFile(),
getGlobalSceneUpdates());
private int loadSceneFile(boolean async) {
if (async) {
return mapController.loadSceneFileAsync(mapStateManager.getMapStyle().getSceneFile(),
getGlobalSceneUpdates());
} else {
return mapController.loadSceneFile(mapStateManager.getMapStyle().getSceneFile(),
getGlobalSceneUpdates());
}
}

/**
* Internal convenience method for loading scene yaml when the current style is a
* {@link ThemedMapStyle}. Applies all global scene updates.
* applied.
* {@link ThemedMapStyle}. Applies all global scene updates. Loads asynchronously or
* synchronously. Returns the sceneId for the {@link MapController} scene update.
*/
private void loadSceneYaml() {
private int loadSceneYaml(boolean async) {
String yaml = yamlGenerator.getImportYaml(getThemedMapStyle(), mapStateManager.getLabelLevel(),
mapStateManager.getLod(), mapStateManager.getThemeColor());
String resourceRoot = getThemedMapStyle().getStyleRootPath();
mapController.loadSceneYaml(yaml, resourceRoot, getGlobalSceneUpdates());
if (async) {
return mapController.loadSceneYamlAsync(yaml, resourceRoot, getGlobalSceneUpdates());
} else {
return mapController.loadSceneYaml(yaml, resourceRoot, getGlobalSceneUpdates());
}
}

/**
Expand Down Expand Up @@ -1147,8 +1238,8 @@ private boolean isValidColor(ThemeColor color) {
* @param detailLevel
* @param color
*/
private void setLabelLevelLodThemeColor(int labelLevel, int detailLevel,
ThemeColor color) {
private int setLabelLevelLodThemeColor(int labelLevel, int detailLevel,
ThemeColor color, boolean async) {
if (!isValidLabelLevel(labelLevel)) {
throw new IllegalArgumentException("Invalid label level for " +
getThemedMapStyle().getClass().getSimpleName());
Expand All @@ -1164,6 +1255,6 @@ private void setLabelLevelLodThemeColor(int labelLevel, int detailLevel,
mapStateManager.setLabelLevel(labelLevel);
mapStateManager.setLod(detailLevel);
mapStateManager.setThemeColor(color);
loadSceneYaml();
return loadSceneYaml(async);
}
}
@@ -0,0 +1,12 @@
package com.mapzen.android.graphics;

/**
* Callback used when loading a new {@link com.mapzen.android.graphics.model.MapStyle}
* asynchronously.
*/
public interface OnStyleLoadedListener {
/**
* Called when the style has finished loaded.
*/
void onStyleLoaded();
}

0 comments on commit 4ee9736

Please sign in to comment.