From 1fa41f5b4cf84819f4ba31e9550e1f8b16fbe2d4 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 30 Apr 2019 14:47:47 +0200 Subject: [PATCH] add labels to the bottom of pois and last position markers --- .../securesms/map/DataCollector.java | 22 ++++++--- .../securesms/map/MapDataManager.java | 45 ++++++++++++++++--- .../securesms/map/model/MapSource.java | 16 ++++--- 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/org/thoughtcrime/securesms/map/DataCollector.java b/src/org/thoughtcrime/securesms/map/DataCollector.java index 94d20099e..6d9f91a95 100644 --- a/src/org/thoughtcrime/securesms/map/DataCollector.java +++ b/src/org/thoughtcrime/securesms/map/DataCollector.java @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.map; import com.b44t.messenger.DcArray; import com.b44t.messenger.DcContact; import com.b44t.messenger.DcContext; +import com.b44t.messenger.DcMsg; import com.google.gson.JsonObject; import com.mapbox.geojson.Feature; import com.mapbox.geojson.LineString; @@ -18,13 +19,15 @@ import java.util.concurrent.ConcurrentHashMap; import static org.thoughtcrime.securesms.map.MapDataManager.ACCURACY; import static org.thoughtcrime.securesms.map.MapDataManager.CONTACT_ID; +import static org.thoughtcrime.securesms.map.MapDataManager.IS_POI; import static org.thoughtcrime.securesms.map.MapDataManager.LAST_LOCATION; import static org.thoughtcrime.securesms.map.MapDataManager.LAST_POSITION_ICON; +import static org.thoughtcrime.securesms.map.MapDataManager.LAST_POSITION_LABEL; import static org.thoughtcrime.securesms.map.MapDataManager.MARKER_CHAR; import static org.thoughtcrime.securesms.map.MapDataManager.MARKER_ICON; import static org.thoughtcrime.securesms.map.MapDataManager.MARKER_SELECTED; import static org.thoughtcrime.securesms.map.MapDataManager.MESSAGE_ID; -import static org.thoughtcrime.securesms.map.MapDataManager.IS_POI; +import static org.thoughtcrime.securesms.map.MapDataManager.POI_LONG_DESCRIPTION; import static org.thoughtcrime.securesms.map.MapDataManager.TIMESTAMP; /** @@ -83,17 +86,26 @@ public class DataCollector { locations.getMarker(i) != null ? locations.getMarker(i) : ""; + boolean isPoi = locations.isIndependent(i); + int messageId = locations.getMsgId(i); Feature pointFeature = Feature.fromGeometry(point, new JsonObject(), String.valueOf(locations.getLocationId(i))); pointFeature.addBooleanProperty(MARKER_SELECTED, false); pointFeature.addBooleanProperty(LAST_LOCATION, false); pointFeature.addNumberProperty(CONTACT_ID, contactId); pointFeature.addNumberProperty(TIMESTAMP, locations.getTimestamp(i)); - pointFeature.addNumberProperty(MESSAGE_ID, locations.getMsgId(i)); + pointFeature.addNumberProperty(MESSAGE_ID, messageId); pointFeature.addNumberProperty(ACCURACY, locations.getAccuracy(i)); pointFeature.addStringProperty(MARKER_CHAR, codepointChar); - pointFeature.addBooleanProperty(IS_POI,locations.isIndependent(i)); pointFeature.addStringProperty(MARKER_ICON, contactMapMetadata.getMarkerIcon()); + pointFeature.addBooleanProperty(IS_POI, isPoi); + if (isPoi && codepointChar.length() == 0 && messageId != 0) { + //has a long poi label + DcMsg poiMsg = dcContext.getMsg(messageId); + String poiLongDescription = poiMsg.getText(); + pointFeature.addStringProperty(POI_LONG_DESCRIPTION, poiLongDescription); + } + sortedPointFeatures.addFirst(pointFeature); if (!locations.isIndependent(i) && sortedPointFeatures.size() > 1) { @@ -116,6 +128,7 @@ public class DataCollector { for (Feature position : sortedPointFeatures) { if (!position.getBooleanProperty(IS_POI)) { position.addStringProperty(LAST_POSITION_ICON, contactMapMetadata.getMarkerLastPositon()); + position.addStringProperty(LAST_POSITION_LABEL, contactMapMetadata.getDisplayName()); position.removeProperty(MARKER_ICON); position.addBooleanProperty(LAST_LOCATION, true); lastPositions.put(contactId, position); @@ -134,8 +147,7 @@ public class DataCollector { } DcContact contact = dcContext.getContact(contactId); - MapSource contactMapSource = new MapSource(contactId); - contactMapSource.setColor(contact.getColor()); + MapSource contactMapSource = new MapSource(contact); contactMapSources.put(contactId, contactMapSource); return contactMapSource; } diff --git a/src/org/thoughtcrime/securesms/map/MapDataManager.java b/src/org/thoughtcrime/securesms/map/MapDataManager.java index f5a8f2cf0..34192a445 100644 --- a/src/org/thoughtcrime/securesms/map/MapDataManager.java +++ b/src/org/thoughtcrime/securesms/map/MapDataManager.java @@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.map; import android.content.Context; import android.graphics.Bitmap; -import android.graphics.Color; import android.support.annotation.NonNull; import android.util.Log; @@ -38,6 +37,7 @@ import static com.b44t.messenger.DcContext.DC_GCL_ADD_SELF; import static com.mapbox.mapboxsdk.style.expressions.Expression.all; import static com.mapbox.mapboxsdk.style.expressions.Expression.eq; import static com.mapbox.mapboxsdk.style.expressions.Expression.get; +import static com.mapbox.mapboxsdk.style.expressions.Expression.has; import static com.mapbox.mapboxsdk.style.expressions.Expression.length; import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; import static com.mapbox.mapboxsdk.style.expressions.Expression.neq; @@ -46,6 +46,8 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.switchCase; import static com.mapbox.mapboxsdk.style.expressions.Expression.toBool; import static com.mapbox.mapboxsdk.style.layers.Property.ICON_ANCHOR_BOTTOM_LEFT; import static com.mapbox.mapboxsdk.style.layers.Property.NONE; +import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_CENTER; +import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_TOP; import static com.mapbox.mapboxsdk.style.layers.Property.VISIBLE; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAnchor; @@ -58,9 +60,12 @@ import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineOpacity; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAllowOverlap; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAnchor; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textColor; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textIgnorePlacement; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textOffset; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textSize; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; import static org.thoughtcrime.securesms.map.model.MapSource.INFO_WINDOW_LAYER; import static org.thoughtcrime.securesms.map.model.MapSource.LINE_FEATURE_LIST; @@ -80,9 +85,11 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate, GenerateIn public static final String MESSAGE_ID = "MESSAGE_ID"; public static final String ACCURACY = "ACCURACY"; public static final String MARKER_CHAR = "MARKER_CHAR"; + public static final String POI_LONG_DESCRIPTION = "POI_LONG_DESCRIPTION"; public static final String MARKER_ICON = "MARKER_ICON"; public static final String IS_POI = "IS_POI"; public static final String LAST_POSITION_ICON = "LAST_POSITION_ICON"; + public static final String LAST_POSITION_LABEL = "LAST_POSITION_LABEL"; private static final String INFO_WINDOW_SRC = "INFO_WINDOW_SRC"; private static final String LAST_POSITION_LAYER = "LAST_POSITION_LAYER"; private static final String LAST_POSITION_SOURCE = "LAST_POSITION_SRC"; @@ -362,7 +369,12 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate, GenerateIn /* all info window and marker image to appear at the same time*/ iconAllowOverlap(true), iconIgnorePlacement(true), - iconSize(markerSize) + iconSize(markerSize), + textField(get(LAST_POSITION_LABEL)), + textAnchor(TEXT_ANCHOR_TOP), + textOffset(new Float[]{0.0f, 1.0f}), + textAllowOverlap(true), + textIgnorePlacement(true) ).withFilter(filterProvider.getTimeFilter()), INFO_WINDOW_LAYER); } @@ -408,16 +420,37 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate, GenerateIn LAST_POSITION_LAYER); + Expression textField = switchCase(eq(length(get(MARKER_CHAR)), 1), get(MARKER_CHAR), + get(POI_LONG_DESCRIPTION)); + Float[] offset = new Float[] {0.0f, 1.25f}; + Float[] zeroOffset = new Float[] {0.0f, 0.0f}; + Expression textOffset = switchCase( + has(POI_LONG_DESCRIPTION), literal(offset), + literal(zeroOffset)); + Expression textColor = switchCase( + has(POI_LONG_DESCRIPTION), literal("#000000"), + literal("#FFFFFF") + ); + Expression textAnchor = switchCase( + has(POI_LONG_DESCRIPTION), literal(TEXT_ANCHOR_TOP), + literal(TEXT_ANCHOR_CENTER) + ); + Expression textSize = switchCase( + has(POI_LONG_DESCRIPTION), literal(12.0f), + literal(15.0f) + ); + mapboxStyle.addLayerBelow(new SymbolLayer(source.getMarkerLayer(), source.getMarkerSource()) .withProperties( iconImage(markerIcon), iconSize(markerSize), - textAllowOverlap(true), - textIgnorePlacement(true), iconIgnorePlacement(false), iconAllowOverlap(false), - textField(get(MARKER_CHAR)), - textColor("#FFFFFF")) + textField(textField), + textOffset(textOffset), + textAnchor(textAnchor), + textSize(textSize), + textColor(textColor)) .withFilter(all(filterProvider.getMarkerFilter(), not(get(LAST_LOCATION)))), LAST_POSITION_LAYER); diff --git a/src/org/thoughtcrime/securesms/map/model/MapSource.java b/src/org/thoughtcrime/securesms/map/model/MapSource.java index c6e89bd69..b6b6adc9a 100644 --- a/src/org/thoughtcrime/securesms/map/model/MapSource.java +++ b/src/org/thoughtcrime/securesms/map/model/MapSource.java @@ -2,6 +2,8 @@ package org.thoughtcrime.securesms.map.model; import android.graphics.Color; +import com.b44t.messenger.DcContact; + /** * Created by cyberta on 07.03.19. */ @@ -27,12 +29,14 @@ public class MapSource { private final String markerLastPositon; private final String markerFeatureCollection; private final String lineFeatureCollection; + private final String displayName; private int color; private int colorArgb; private int contactId; - public MapSource(int contactId) { + public MapSource(DcContact contact) { + int contactId = contact.getId(); markerSource = MARKER_POSITION_SOURCE + "_" + contactId; lineSource = LINE_SOURCE + "_" + contactId; markerLayer = MARKER_LAYER + "_" + contactId; @@ -42,10 +46,8 @@ public class MapSource { markerFeatureCollection = MARKER_FEATURE_LIST + "_" + contactId; lineFeatureCollection = LINE_FEATURE_LIST + "_" + contactId; this.contactId = contactId; - } - - public void setColor(int color) { - this.color = color; + displayName = contact.getDisplayName(); + color = contact.getColor(); colorArgb = Color.argb(0xFF, Color.red(color), Color.green(color), Color.blue(color)); } @@ -87,4 +89,8 @@ public class MapSource { public int getContactId() { return contactId; } + public String getDisplayName() { + return displayName; + } + }