Skip to content

Commit

Permalink
Re-implement delayed source update (#45)
Browse files Browse the repository at this point in the history
Re-implement delayed source update (including tests)
  • Loading branch information
fynngodau authored Aug 3, 2023
1 parent fb44634 commit f29bb19
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ public class <%- camelize(type) %>ManagerTest {
<%- camelize(type) %>Options options = new <%- camelize(type) %>Options().withLatLngs(latLngs).with<%- camelize(property.name) %>(<%- defaultValueJava(property) %>);
<% } -%>
<%- type %>Manager.create(options);
<%- type %>Manager.updateSourceNow();
verify(<%- type %>Layer, times(1)).setProperties(argThat(new PropertyValueMatcher(<%- camelizeWithLeadingLowercase(property.name) %>(get("<%- property.name %>")))));
<%- type %>Manager.create(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public T create(S options) {
T t = options.build(currentId, this);
annotations.put(t.getId(), t);
currentId++;
internalUpdateSource();
updateSource();
return t;
}

Expand All @@ -158,7 +158,7 @@ public List<T> create(List<S> optionsList) {
annotations.put(annotation.getId(), annotation);
currentId++;
}
internalUpdateSource();
updateSource();
return annotationList;
}

Expand All @@ -171,7 +171,7 @@ public List<T> create(List<S> optionsList) {
public void delete(T annotation) {
annotations.remove(annotation.getId());
draggableAnnotationController.onAnnotationDeleted(annotation);
internalUpdateSource();
updateSource();
}

/**
Expand All @@ -185,7 +185,7 @@ public void delete(List<T> annotationList) {
annotations.remove(annotation.getId());
draggableAnnotationController.onAnnotationDeleted(annotation);
}
internalUpdateSource();
updateSource();
}

/**
Expand All @@ -206,7 +206,7 @@ public void deleteAll() {
public void update(T annotation) {
if (annotations.containsValue(annotation)) {
annotations.put(annotation.getId(), annotation);
internalUpdateSource();
updateSource();
} else {
Logger.e(TAG, "Can't update annotation: "
+ annotation.toString()
Expand All @@ -224,35 +224,35 @@ public void update(List<T> annotationList) {
for (T annotation : annotationList) {
annotations.put(annotation.getId(), annotation);
}
internalUpdateSource();
updateSource();
}

/**
* Trigger an update to the underlying source
* Trigger an update to the underlying source. The update is delayed until after
* the next UI draw to batch multiple actions.
*/
public void updateSource() {
postUpdateSource();
}

void postUpdateSource() {
// Only schedule a new refresh if not already scheduled
if (isSourceUpToDate.compareAndSet(true, false)) {
mapView.post(new Runnable() {
@Override
public void run() {
internalUpdateSource();
isSourceUpToDate.set(true);
mapView.post(() -> {
isSourceUpToDate.set(true);

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

updateSourceNow();
});
}
}

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

/**
* Undelayed source update, only used for testing and by {@link #updateSource()}.
*/
@VisibleForTesting
void updateSourceNow() {
List<Feature> features = new ArrayList<>();
T t;
for (int i = 0; i < annotations.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ boolean onMove(MoveGestureDetector detector) {
draggedAnnotation.setGeometry(
shiftedGeometry
);
draggedAnnotationManager.postUpdateSource();
draggedAnnotationManager.updateSource();
for (OnAnnotationDragListener d : (List<OnAnnotationDragListener>) draggedAnnotationManager.getDragListeners()) {
d.onAnnotationDrag(draggedAnnotation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public void testCircleRadiusLayerProperty() {

CircleOptions options = new CircleOptions().withLatLng(new LatLng()).withCircleRadius(2.0f);
circleManager.create(options);
circleManager.updateSourceNow();
verify(circleLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(circleRadius(get("circle-radius")))));

circleManager.create(options);
Expand All @@ -251,6 +252,7 @@ public void testCircleColorLayerProperty() {

CircleOptions options = new CircleOptions().withLatLng(new LatLng()).withCircleColor("rgba(0, 0, 0, 1)");
circleManager.create(options);
circleManager.updateSourceNow();
verify(circleLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(circleColor(get("circle-color")))));

circleManager.create(options);
Expand All @@ -264,6 +266,7 @@ public void testCircleBlurLayerProperty() {

CircleOptions options = new CircleOptions().withLatLng(new LatLng()).withCircleBlur(2.0f);
circleManager.create(options);
circleManager.updateSourceNow();
verify(circleLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(circleBlur(get("circle-blur")))));

circleManager.create(options);
Expand All @@ -277,6 +280,7 @@ public void testCircleOpacityLayerProperty() {

CircleOptions options = new CircleOptions().withLatLng(new LatLng()).withCircleOpacity(2.0f);
circleManager.create(options);
circleManager.updateSourceNow();
verify(circleLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(circleOpacity(get("circle-opacity")))));

circleManager.create(options);
Expand All @@ -290,6 +294,7 @@ public void testCircleStrokeWidthLayerProperty() {

CircleOptions options = new CircleOptions().withLatLng(new LatLng()).withCircleStrokeWidth(2.0f);
circleManager.create(options);
circleManager.updateSourceNow();
verify(circleLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(circleStrokeWidth(get("circle-stroke-width")))));

circleManager.create(options);
Expand All @@ -303,6 +308,7 @@ public void testCircleStrokeColorLayerProperty() {

CircleOptions options = new CircleOptions().withLatLng(new LatLng()).withCircleStrokeColor("rgba(0, 0, 0, 1)");
circleManager.create(options);
circleManager.updateSourceNow();
verify(circleLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(circleStrokeColor(get("circle-stroke-color")))));

circleManager.create(options);
Expand All @@ -316,6 +322,7 @@ public void testCircleStrokeOpacityLayerProperty() {

CircleOptions options = new CircleOptions().withLatLng(new LatLng()).withCircleStrokeOpacity(2.0f);
circleManager.create(options);
circleManager.updateSourceNow();
verify(circleLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(circleStrokeOpacity(get("circle-stroke-opacity")))));

circleManager.create(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void gestureOnMoveTest() {

assertTrue(moved);
verify(annotation).setGeometry(geometry);
verify(annotationManager).postUpdateSource();
verify(annotationManager).updateSource();
verify(dragListener, times(1)).onAnnotationDrag(annotation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public void testFillOpacityLayerProperty() {
latLngs.add(innerLatLngs);
FillOptions options = new FillOptions().withLatLngs(latLngs).withFillOpacity(2.0f);
fillManager.create(options);
fillManager.updateSourceNow();
verify(fillLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(fillOpacity(get("fill-opacity")))));

fillManager.create(options);
Expand All @@ -327,6 +328,7 @@ public void testFillColorLayerProperty() {
latLngs.add(innerLatLngs);
FillOptions options = new FillOptions().withLatLngs(latLngs).withFillColor("rgba(0, 0, 0, 1)");
fillManager.create(options);
fillManager.updateSourceNow();
verify(fillLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(fillColor(get("fill-color")))));

fillManager.create(options);
Expand All @@ -346,6 +348,7 @@ public void testFillOutlineColorLayerProperty() {
latLngs.add(innerLatLngs);
FillOptions options = new FillOptions().withLatLngs(latLngs).withFillOutlineColor("rgba(0, 0, 0, 1)");
fillManager.create(options);
fillManager.updateSourceNow();
verify(fillLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(fillOutlineColor(get("fill-outline-color")))));

fillManager.create(options);
Expand All @@ -365,6 +368,7 @@ public void testFillPatternLayerProperty() {
latLngs.add(innerLatLngs);
FillOptions options = new FillOptions().withLatLngs(latLngs).withFillPattern("pedestrian-polygon");
fillManager.create(options);
fillManager.updateSourceNow();
verify(fillLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(fillPattern(get("fill-pattern")))));

fillManager.create(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public void testLineJoinLayerProperty() {
latLngs.add(new LatLng(1, 1));
LineOptions options = new LineOptions().withLatLngs(latLngs).withLineJoin(LINE_JOIN_BEVEL);
lineManager.create(options);
lineManager.updateSourceNow();
verify(lineLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(lineJoin(get("line-join")))));

lineManager.create(options);
Expand All @@ -288,6 +289,7 @@ public void testLineOpacityLayerProperty() {
latLngs.add(new LatLng(1, 1));
LineOptions options = new LineOptions().withLatLngs(latLngs).withLineOpacity(2.0f);
lineManager.create(options);
lineManager.updateSourceNow();
verify(lineLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(lineOpacity(get("line-opacity")))));

lineManager.create(options);
Expand All @@ -304,6 +306,7 @@ public void testLineColorLayerProperty() {
latLngs.add(new LatLng(1, 1));
LineOptions options = new LineOptions().withLatLngs(latLngs).withLineColor("rgba(0, 0, 0, 1)");
lineManager.create(options);
lineManager.updateSourceNow();
verify(lineLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(lineColor(get("line-color")))));

lineManager.create(options);
Expand All @@ -320,6 +323,7 @@ public void testLineWidthLayerProperty() {
latLngs.add(new LatLng(1, 1));
LineOptions options = new LineOptions().withLatLngs(latLngs).withLineWidth(2.0f);
lineManager.create(options);
lineManager.updateSourceNow();
verify(lineLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(lineWidth(get("line-width")))));

lineManager.create(options);
Expand All @@ -336,6 +340,7 @@ public void testLineGapWidthLayerProperty() {
latLngs.add(new LatLng(1, 1));
LineOptions options = new LineOptions().withLatLngs(latLngs).withLineGapWidth(2.0f);
lineManager.create(options);
lineManager.updateSourceNow();
verify(lineLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(lineGapWidth(get("line-gap-width")))));

lineManager.create(options);
Expand All @@ -352,6 +357,7 @@ public void testLineOffsetLayerProperty() {
latLngs.add(new LatLng(1, 1));
LineOptions options = new LineOptions().withLatLngs(latLngs).withLineOffset(2.0f);
lineManager.create(options);
lineManager.updateSourceNow();
verify(lineLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(lineOffset(get("line-offset")))));

lineManager.create(options);
Expand All @@ -368,6 +374,7 @@ public void testLineBlurLayerProperty() {
latLngs.add(new LatLng(1, 1));
LineOptions options = new LineOptions().withLatLngs(latLngs).withLineBlur(2.0f);
lineManager.create(options);
lineManager.updateSourceNow();
verify(lineLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(lineBlur(get("line-blur")))));

lineManager.create(options);
Expand All @@ -384,6 +391,7 @@ public void testLinePatternLayerProperty() {
latLngs.add(new LatLng(1, 1));
LineOptions options = new LineOptions().withLatLngs(latLngs).withLinePattern("pedestrian-polygon");
lineManager.create(options);
lineManager.updateSourceNow();
verify(lineLayer, times(1)).setProperties(argThat(new PropertyValueMatcher(linePattern(get("line-pattern")))));

lineManager.create(options);
Expand Down
Loading

0 comments on commit f29bb19

Please sign in to comment.