Skip to content

Commit 4ee9736

Browse files
Async setStyle (#450)
* Add OnStyleLoadedListener * Add setStyleAsync * Add themed map style async methods
1 parent c781e9e commit 4ee9736

3 files changed

Lines changed: 322 additions & 26 deletions

File tree

core/src/main/java/com/mapzen/android/graphics/MapzenMap.java

Lines changed: 112 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ public boolean onRotate(float x, float y, float rotation) {
149149
= new MapController.SceneLoadListener() {
150150
@Override public void onSceneReady(int sceneId, SceneError sceneError) {
151151
bitmapMarkerManager.restoreMarkers();
152+
if (sceneId == currSceneId) {
153+
if (styleLoadedListener != null) {
154+
styleLoadedListener.onStyleLoaded();
155+
styleLoadedListener = null;
156+
}
157+
currSceneId = Integer.MIN_VALUE;
158+
}
152159
}
153160
};
154161

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

171+
OnStyleLoadedListener styleLoadedListener = null;
172+
int currSceneId = Integer.MIN_VALUE;
173+
164174
/**
165175
* Creates a new map based on the given {@link MapView} and {@link MapController}.
166176
*/
@@ -201,19 +211,32 @@ public OverlayManager getOverlayManager() {
201211
return overlayManager;
202212
}
203213

214+
/**
215+
* Sets the map's underlying stylesheet asynchronously.
216+
*/
217+
public void setStyleAsync(MapStyle mapStyle, OnStyleLoadedListener listener) {
218+
styleLoadedListener = listener;
219+
currSceneId = internalSetStyle(mapStyle, true);
220+
}
221+
204222
/**
205223
* Sets the map's underlying stylesheet.
206224
*/
207225
public void setStyle(MapStyle mapStyle) {
208-
mapStateManager.setMapStyle(mapStyle);
209-
if (currentMapStyleIsThemed()) {
210-
mapStateManager.setLabelLevel(getThemedMapStyle().getDefaultLabelLevel());
211-
mapStateManager.setLod(getThemedMapStyle().getDefaultLod());
212-
mapStateManager.setThemeColor(getThemedMapStyle().getDefaultColor());
213-
loadSceneYaml();
214-
} else {
215-
loadSceneFile();
216-
}
226+
internalSetStyle(mapStyle, false);
227+
}
228+
229+
/**
230+
* Sets the map style with given label level and default detail and theme color values. If the
231+
* label level is not supported by this theme then this method throws an
232+
* {@link IllegalArgumentException}.
233+
* @param themedMapStyle
234+
* @param labelLevel
235+
*/
236+
public void setStyleAndLabelLevelAsync(ThemedMapStyle themedMapStyle, int labelLevel,
237+
OnStyleLoadedListener listener) {
238+
setStyleLabelLevelLodThemeColorAsync(themedMapStyle, labelLevel,
239+
themedMapStyle.getDefaultLod(), themedMapStyle.getDefaultColor(), listener);
217240
}
218241

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

254+
/**
255+
* Sets the map style with given detail level and default label and theme color values. If the
256+
* detail level is not supported by this theme then this method throws an
257+
* {@link IllegalArgumentException}.
258+
* @param themedMapStyle
259+
* @param detailLevel
260+
*/
261+
public void setStyleAndLodAsync(ThemedMapStyle themedMapStyle, int detailLevel,
262+
OnStyleLoadedListener listener) {
263+
setStyleLabelLevelLodThemeColorAsync(themedMapStyle, themedMapStyle.getDefaultLabelLevel(),
264+
detailLevel, themedMapStyle.getDefaultColor(), listener);
265+
}
266+
231267
/**
232268
* Sets the map style with given detail level and default label and theme color values. If the
233269
* detail level is not supported by this theme then this method throws an
@@ -240,6 +276,17 @@ public void setStyleAndLod(ThemedMapStyle themedMapStyle, int detailLevel) {
240276
detailLevel, themedMapStyle.getDefaultColor());
241277
}
242278

279+
/**
280+
* Sets the map style with given theme color and default label and detail levels.
281+
* @param themedMapStyle
282+
* @param color
283+
*/
284+
public void setStyleAndThemeColorAsync(ThemedMapStyle themedMapStyle, ThemeColor color,
285+
OnStyleLoadedListener listener) {
286+
setStyleLabelLevelLodThemeColorAsync(themedMapStyle, themedMapStyle.getDefaultLabelLevel(),
287+
themedMapStyle.getDefaultLod(), color, listener);
288+
}
289+
243290
/**
244291
* Sets the map style with given theme color and default label and detail levels.
245292
* @param themedMapStyle
@@ -250,6 +297,21 @@ public void setStyleAndThemeColor(ThemedMapStyle themedMapStyle, ThemeColor colo
250297
themedMapStyle.getDefaultLod(), color);
251298
}
252299

300+
/**
301+
* Sets the map style with given label level, detail level, and theme color. If either the label
302+
* or detail level are not supported, this method will throw an {@link IllegalArgumentException}.
303+
* @param themedMapStyle
304+
* @param labelLevel
305+
* @param detailLevel
306+
* @param color
307+
*/
308+
public void setStyleLabelLevelLodThemeColorAsync(ThemedMapStyle themedMapStyle, int labelLevel,
309+
int detailLevel, ThemeColor color, OnStyleLoadedListener listener) {
310+
styleLoadedListener = listener;
311+
mapStateManager.setMapStyle(themedMapStyle);
312+
currSceneId = setLabelLevelLodThemeColor(labelLevel, detailLevel, color, true);
313+
}
314+
253315
/**
254316
* Sets the map style with given label level, detail level, and theme color. If either the label
255317
* or detail level are not supported, this method will throw an {@link IllegalArgumentException}.
@@ -261,7 +323,7 @@ public void setStyleAndThemeColor(ThemedMapStyle themedMapStyle, ThemeColor colo
261323
public void setStyleLabelLevelLodThemeColor(ThemedMapStyle themedMapStyle, int labelLevel,
262324
int detailLevel, ThemeColor color) {
263325
mapStateManager.setMapStyle(themedMapStyle);
264-
setLabelLevelLodThemeColor(labelLevel, detailLevel, color);
326+
setLabelLevelLodThemeColor(labelLevel, detailLevel, color, false);
265327
}
266328

267329
/**
@@ -1064,26 +1126,55 @@ private List<SceneUpdate> getGlobalSceneUpdates() {
10641126
mapStateManager.isPathOverlayEnabled());
10651127
}
10661128

1129+
/**
1130+
* Sets the {@link MapStyle} and relevant theme configuration for {@link ThemedMapStyle}s. Loads
1131+
* the scene file either synchronously or asynchronously and returns the sceneId.
1132+
* @param mapStyle
1133+
* @param async
1134+
* @return
1135+
*/
1136+
private int internalSetStyle(MapStyle mapStyle, boolean async) {
1137+
mapStateManager.setMapStyle(mapStyle);
1138+
if (currentMapStyleIsThemed()) {
1139+
mapStateManager.setLabelLevel(getThemedMapStyle().getDefaultLabelLevel());
1140+
mapStateManager.setLod(getThemedMapStyle().getDefaultLod());
1141+
mapStateManager.setThemeColor(getThemedMapStyle().getDefaultColor());
1142+
return loadSceneYaml(async);
1143+
} else {
1144+
return loadSceneFile(async);
1145+
}
1146+
}
1147+
10671148
/**
10681149
* Internal convenience method for loading scene file when the current style is a
10691150
* {@link MapStyle}.
1070-
* Applies all global scene updates.
1151+
* Applies all global scene updates. Loads asynchronously or
1152+
* synchronously. Returns the sceneId for the {@link MapController} scene update.
10711153
*/
1072-
private void loadSceneFile() {
1073-
mapController.loadSceneFile(mapStateManager.getMapStyle().getSceneFile(),
1074-
getGlobalSceneUpdates());
1154+
private int loadSceneFile(boolean async) {
1155+
if (async) {
1156+
return mapController.loadSceneFileAsync(mapStateManager.getMapStyle().getSceneFile(),
1157+
getGlobalSceneUpdates());
1158+
} else {
1159+
return mapController.loadSceneFile(mapStateManager.getMapStyle().getSceneFile(),
1160+
getGlobalSceneUpdates());
1161+
}
10751162
}
10761163

10771164
/**
10781165
* Internal convenience method for loading scene yaml when the current style is a
1079-
* {@link ThemedMapStyle}. Applies all global scene updates.
1080-
* applied.
1166+
* {@link ThemedMapStyle}. Applies all global scene updates. Loads asynchronously or
1167+
* synchronously. Returns the sceneId for the {@link MapController} scene update.
10811168
*/
1082-
private void loadSceneYaml() {
1169+
private int loadSceneYaml(boolean async) {
10831170
String yaml = yamlGenerator.getImportYaml(getThemedMapStyle(), mapStateManager.getLabelLevel(),
10841171
mapStateManager.getLod(), mapStateManager.getThemeColor());
10851172
String resourceRoot = getThemedMapStyle().getStyleRootPath();
1086-
mapController.loadSceneYaml(yaml, resourceRoot, getGlobalSceneUpdates());
1173+
if (async) {
1174+
return mapController.loadSceneYamlAsync(yaml, resourceRoot, getGlobalSceneUpdates());
1175+
} else {
1176+
return mapController.loadSceneYaml(yaml, resourceRoot, getGlobalSceneUpdates());
1177+
}
10871178
}
10881179

10891180
/**
@@ -1147,8 +1238,8 @@ private boolean isValidColor(ThemeColor color) {
11471238
* @param detailLevel
11481239
* @param color
11491240
*/
1150-
private void setLabelLevelLodThemeColor(int labelLevel, int detailLevel,
1151-
ThemeColor color) {
1241+
private int setLabelLevelLodThemeColor(int labelLevel, int detailLevel,
1242+
ThemeColor color, boolean async) {
11521243
if (!isValidLabelLevel(labelLevel)) {
11531244
throw new IllegalArgumentException("Invalid label level for " +
11541245
getThemedMapStyle().getClass().getSimpleName());
@@ -1164,6 +1255,6 @@ private void setLabelLevelLodThemeColor(int labelLevel, int detailLevel,
11641255
mapStateManager.setLabelLevel(labelLevel);
11651256
mapStateManager.setLod(detailLevel);
11661257
mapStateManager.setThemeColor(color);
1167-
loadSceneYaml();
1258+
return loadSceneYaml(async);
11681259
}
11691260
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mapzen.android.graphics;
2+
3+
/**
4+
* Callback used when loading a new {@link com.mapzen.android.graphics.model.MapStyle}
5+
* asynchronously.
6+
*/
7+
public interface OnStyleLoadedListener {
8+
/**
9+
* Called when the style has finished loaded.
10+
*/
11+
void onStyleLoaded();
12+
}

0 commit comments

Comments
 (0)