Skip to content

Commit

Permalink
Merge pull request #339 from onaio/edit-foci-boundary-enhancements
Browse files Browse the repository at this point in the history
Edit foci boundary enhancements
  • Loading branch information
Rkareko authored Jul 22, 2020
2 parents d4d76cb + c017036 commit 61823dc
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 4 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ apply plugin: 'realm-android'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'

version '0.8.7'
version '0.8.8'

project.version = this.version

Expand Down
20 changes: 20 additions & 0 deletions library/src/main/java/io/ona/kujaku/interfaces/IKujakuMapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.ona.kujaku.listeners.BoundsChangeListener;
import io.ona.kujaku.listeners.LocationClientStartedCallback;
import io.ona.kujaku.listeners.OnFeatureClickListener;
import io.ona.kujaku.listeners.OnFeatureLongClickListener;
import io.ona.kujaku.listeners.TrackingServiceListener;
import io.ona.kujaku.location.KujakuLocation;
import io.ona.kujaku.services.configurations.TrackingServiceUIConfiguration;
Expand Down Expand Up @@ -188,6 +189,25 @@ public interface IKujakuMapView extends IKujakuMapViewLowLevel {
*/
void setOnFeatureClickListener(@NonNull OnFeatureClickListener onFeatureClickListener, @Nullable Expression expressionFilter, @Nullable String... layerIds);

/**
* Sets an {@link OnFeatureLongClickListener} which will be fired when a feature on the map in either of the {@code layerIds}
* is long touched/clicked
*
* @param onFeatureLongClickListener
* @param layerIds
*/
void setOnFeatureLongClickListener(@NonNull OnFeatureLongClickListener onFeatureLongClickListener, @Nullable String... layerIds);

/**
* Sets an {@link OnFeatureLongClickListener} which will be fired when a feature on the map in either of the {@code layerIds}
* is long touched/clicked and/or fulfilling the filter defined in {@code filter}
*
* @param onFeatureLongClickListener
* @param expressionFilter
* @param layerIds
*/
void setOnFeatureLongClickListener(@NonNull OnFeatureLongClickListener onFeatureLongClickListener, @Nullable Expression expressionFilter, @Nullable String... layerIds);

/**
* Checks if the map warms GPS(this just means the location service that is going to be used).
* Warming the GPS in this case means that it starts the location services as soon as you open
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.ona.kujaku.listeners;

import android.support.annotation.NonNull;

import com.mapbox.geojson.Feature;

import java.util.List;

/**
* Created by Richard Kareko on 5/18/20.
*/

public interface OnFeatureLongClickListener {

/**
* Called when a features(s) is long clicked on the map and adheres to params passed in
* {@link io.ona.kujaku.views.KujakuMapView#setOnFeatureLongClickListener(OnFeatureLongClickListener, String...)}
* or {@link io.ona.kujaku.views.KujakuMapView#setOnFeatureLongClickListener(OnFeatureLongClickListener, com.mapbox.mapboxsdk.style.expressions.Expression, String...)}
*
* @param features
*/
void onFeatureLongClick(@NonNull List<Feature> features);
}
11 changes: 11 additions & 0 deletions library/src/main/java/io/ona/kujaku/manager/DrawingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Geometry;
import com.mapbox.geojson.MultiPolygon;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.mapboxsdk.geometry.LatLng;
Expand Down Expand Up @@ -217,6 +218,16 @@ public boolean startDrawing(@Nullable FillBoundaryLayer fillBoundaryLayer) {
List<Point> points = polygon.coordinates().get(0);
this.startDrawingPoints(points);
return true;
} else if (geometry instanceof MultiPolygon) {
// hide layer
fillBoundaryLayer.disableLayerOnMap(mapboxMap);

MultiPolygon multiPolygon = (MultiPolygon) geometry;
for (List<List<Point>> polygonCoordinates : multiPolygon.coordinates()) {
List<Point> points = polygonCoordinates.get(0);
this.startDrawingPoints(points);
}
return true;
}
}

Expand Down
26 changes: 26 additions & 0 deletions library/src/main/java/io/ona/kujaku/views/KujakuMapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import io.ona.kujaku.listeners.BoundsChangeListener;
import io.ona.kujaku.listeners.LocationClientStartedCallback;
import io.ona.kujaku.listeners.OnFeatureClickListener;
import io.ona.kujaku.listeners.OnFeatureLongClickListener;
import io.ona.kujaku.listeners.OnKujakuLayerClickListener;
import io.ona.kujaku.listeners.OnKujakuLayerLongClickListener;
import io.ona.kujaku.listeners.OnLocationChanged;
Expand Down Expand Up @@ -165,9 +166,13 @@ public class KujakuMapView extends MapView implements IKujakuMapView, MapboxMap.
private BoundsChangeListener boundsChangeListener;

private OnFeatureClickListener onFeatureClickListener;
private OnFeatureLongClickListener onFeatureLongClickListener;
private String[] featureClickLayerIdFilters;
private Expression featureClickExpressionFilter;

private String[] featureLongClickLayerIdFilters;
private Expression featureLongClickExpressionFilter;

private OnKujakuLayerClickListener onKujakuLayerClickListener;
private OnKujakuLayerLongClickListener onKujakuLayerLongClickListener;

Expand Down Expand Up @@ -830,6 +835,18 @@ public void setOnFeatureClickListener(@NonNull OnFeatureClickListener onFeatureC
this.featureClickExpressionFilter = expressionFilter;
}

@Override
public void setOnFeatureLongClickListener(@NonNull OnFeatureLongClickListener onFeatureLongClickListener, @Nullable String... layerIds) {
this.setOnFeatureLongClickListener(onFeatureLongClickListener, null, layerIds);
}

@Override
public void setOnFeatureLongClickListener(@NonNull OnFeatureLongClickListener onFeatureLongClickListener, @Nullable Expression expressionFilter, @Nullable String... layerIds) {
this.onFeatureLongClickListener = onFeatureLongClickListener;
this.featureLongClickLayerIdFilters = layerIds;
this.featureLongClickExpressionFilter = expressionFilter;
}

/**
* Set listener when pressing a KujakuLayer
*
Expand Down Expand Up @@ -1125,6 +1142,15 @@ public boolean onMapClick(@NonNull LatLng point) {
public boolean onMapLongClick(@NonNull LatLng point) {
PointF pixel = mapboxMap.getProjection().toScreenLocation(point);

if (onFeatureLongClickListener != null) {
List<com.mapbox.geojson.Feature> features = mapboxMap.queryRenderedFeatures(pixel, featureLongClickExpressionFilter, featureLongClickLayerIdFilters);

if (features.size() > 0) {
onFeatureLongClickListener.onFeatureLongClick(features);
}
}


if (onKujakuLayerLongClickListener != null) {
KujakuLayer layer = KujakuLayer.getKujakuLayerSelected(pixel, kujakuLayers, mapboxMap);
if (layer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.MultiPolygon;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.mapboxsdk.geometry.LatLng;
Expand Down Expand Up @@ -78,7 +79,26 @@ public void startDrawingWithPoints() {

@Test
public void startDrawingWithExistingLayer() {
manager.startDrawing(getFillBoundaryLayer());
manager.startDrawing(getFillBoundaryLayer(true));

Assert.assertTrue(manager.isDrawingEnabled());
Assert.assertNull(manager.getCurrentKujakuCircle());
// Middle Circles created between each point
Assert.assertEquals(8, manager.getKujakuCircles().size());

Assert.assertFalse(manager.getKujakuCircles().get(0).isMiddleCircle());
Assert.assertTrue(manager.getKujakuCircles().get(1).isMiddleCircle());
Assert.assertFalse(manager.getKujakuCircles().get(2).isMiddleCircle());
Assert.assertTrue(manager.getKujakuCircles().get(3).isMiddleCircle());
Assert.assertFalse(manager.getKujakuCircles().get(4).isMiddleCircle());
Assert.assertTrue(manager.getKujakuCircles().get(5).isMiddleCircle());
Assert.assertFalse(manager.getKujakuCircles().get(6).isMiddleCircle());
Assert.assertTrue(manager.getKujakuCircles().get(7).isMiddleCircle());
}

@Test
public void startDrawingWithExistingMultipolygonLayer() {
manager.startDrawing(getFillBoundaryLayer(false));

Assert.assertTrue(manager.isDrawingEnabled());
Assert.assertNull(manager.getCurrentKujakuCircle());
Expand Down Expand Up @@ -206,18 +226,36 @@ public void areMiddleCircles() {
Assert.assertTrue(manager.getKujakuCircles().get(7).isMiddleCircle());
}

private FillBoundaryLayer getFillBoundaryLayer() {
@Test
public void unSetCurrentCircleDraggable() {
manager.create(DrawingManager.getKujakuCircleOptions().withLatLng(new LatLng(1,1)));
Circle circle = manager.getKujakuCircles().get(0).getCircle();
manager.setDraggable(true, circle);

Assert.assertTrue(manager.getCurrentKujakuCircle().getCircle().isDraggable());
manager.unsetCurrentCircleDraggable();
Assert.assertNull(manager.getCurrentKujakuCircle());
}


private FillBoundaryLayer getFillBoundaryLayer(boolean isPolygon) {
List<Feature> features = new ArrayList<Feature>();
List<List<Point>> lists = new ArrayList<>();
List<Point> points = new ArrayList<>();
Polygon polygon = null;

points.add(Point.fromLngLat(-11,15));
points.add(Point.fromLngLat(-5,15));
points.add(Point.fromLngLat(-5,11));
points.add(Point.fromLngLat(-11,11));
lists.add(points);

features.add(Feature.fromGeometry(Polygon.fromLngLats(lists)));
polygon = Polygon.fromLngLats(lists);
if (!isPolygon) {
features.add(Feature.fromGeometry(polygon));
} else {
features.add(Feature.fromGeometry(MultiPolygon.fromPolygon(polygon)));
}

FeatureCollection featureCollection = FeatureCollection.fromFeatures(features);
FillBoundaryLayer.Builder builder = new FillBoundaryLayer.Builder(featureCollection);
Expand Down
14 changes: 14 additions & 0 deletions library/src/test/java/io/ona/kujaku/views/KujakuMapViewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import io.ona.kujaku.BaseTest;
import io.ona.kujaku.interfaces.ILocationClient;
import io.ona.kujaku.listeners.LocationClientStartedCallback;
import io.ona.kujaku.listeners.OnFeatureLongClickListener;
import io.ona.kujaku.location.clients.GoogleLocationClient;
import io.ona.kujaku.test.shadows.ShadowKujakuMapView;
import io.ona.kujaku.test.shadows.ShadowMapView;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -193,4 +195,16 @@ public void getLocationClientShouldCallCallbackWhenLocationClientIsNullAndWarmLo
.onStarted(Mockito.any(ILocationClient.class));
assertEquals(0, callbacks.size());
}

@Test
public void setOnFeatureLongClickListener() {
assertNull(ReflectionHelpers.getField(kujakuMapView,"onFeatureLongClickListener"));
assertNull(ReflectionHelpers.getField(kujakuMapView,"featureLongClickLayerIdFilters"));
OnFeatureLongClickListener onFeatureLongClickListenerMock = Mockito.mock(OnFeatureLongClickListener.class);
String[] layerIds = {"id1"};

kujakuMapView.setOnFeatureLongClickListener(onFeatureLongClickListenerMock, layerIds);
assertEquals(onFeatureLongClickListenerMock, ReflectionHelpers.getField(kujakuMapView,"onFeatureLongClickListener"));
assertEquals(layerIds, ReflectionHelpers.getField(kujakuMapView,"featureLongClickLayerIdFilters"));
}
}

0 comments on commit 61823dc

Please sign in to comment.