From ee763cfadd921d924fc3405bbbf6eaecaf5c7dad Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 18 Sep 2017 18:24:56 +0300 Subject: [PATCH] Add color syncing --- .../net/osmand/plus/FavouritesDbHelper.java | 11 ++-- .../src/net/osmand/plus/MapMarkersHelper.java | 39 +++++++++++---- .../EditFavoriteGroupDialogFragment.java | 3 +- .../activities/FavoritesTreeFragment.java | 3 +- .../net/osmand/plus/helpers/ColorDialogs.java | 50 +++++++++++++++---- 5 files changed, 79 insertions(+), 27 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java index 709f1664df..f05f4db730 100644 --- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java @@ -177,7 +177,7 @@ public class FavouritesDbHelper { sortAll(); saveCurrentPointsIntoFile(); } - context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE)); + context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE, group.color)); return true; } @@ -272,7 +272,7 @@ public class FavouritesDbHelper { } sortAll(); saveCurrentPointsIntoFile(); - context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(category, category, MarkersSyncGroup.FAVORITES_TYPE)); + context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(category, category, MarkersSyncGroup.FAVORITES_TYPE, p.getColor())); return true; } @@ -280,7 +280,7 @@ public class FavouritesDbHelper { p.setLatitude(lat); p.setLongitude(lon); saveCurrentPointsIntoFile(); - context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(p.getCategory(), p.getCategory(), MarkersSyncGroup.FAVORITES_TYPE)); + context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(p.getCategory(), p.getCategory(), MarkersSyncGroup.FAVORITES_TYPE, p.getColor())); return true; } @@ -597,6 +597,7 @@ public class FavouritesDbHelper { for (FavouritePoint p : gr.points) { p.setColor(color); } + markersHelper.syncGroup(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE, color)); } if (group.visible != visible) { FavoriteGroup gr = flatGroups.get(group.name); @@ -604,7 +605,7 @@ public class FavouritesDbHelper { for (FavouritePoint p : gr.points) { p.setVisible(visible); } - markersHelper.syncGroup(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE)); + markersHelper.syncGroup(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE, group.color)); } if (!group.name.equals(newName)) { FavoriteGroup gr = flatGroups.remove(group.name); @@ -624,7 +625,7 @@ public class FavouritesDbHelper { renamedGroup.points.add(p); } } - MarkersSyncGroup syncGroup = new MarkersSyncGroup(renamedGroup.name, renamedGroup.name, MarkersSyncGroup.FAVORITES_TYPE); + MarkersSyncGroup syncGroup = new MarkersSyncGroup(renamedGroup.name, renamedGroup.name, MarkersSyncGroup.FAVORITES_TYPE, group.color); markersHelper.addMarkersSyncGroup(syncGroup); markersHelper.syncGroup(syncGroup); } diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 8a4f8203d2..8520b5a126 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -10,9 +10,11 @@ import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.LocationPoint; import net.osmand.data.PointDescription; +import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; +import net.osmand.plus.helpers.ColorDialogs; import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; @@ -171,11 +173,20 @@ public class MapMarkersHelper { private String id; private String name; private int type; + private int color; + + public MarkersSyncGroup(@NonNull String id, @NonNull String name, int type, int color) { + this.id = id; + this.name = name; + this.type = type; + this.color = color; + } public MarkersSyncGroup(@NonNull String id, @NonNull String name, int type) { this.id = id; this.name = name; this.type = type; + this.color = -1; } public String getId() { @@ -189,6 +200,10 @@ public class MapMarkersHelper { public int getType() { return type; } + + public int getColor() { + return color; + } } public MapMarkersHelper(OsmandApplication ctx) { @@ -335,7 +350,7 @@ public class MapMarkersHelper { List dbMarkers = markersDbHelper.getMarkersFromGroup(group); if (group.getType() == MarkersSyncGroup.FAVORITES_TYPE) { - FavouritesDbHelper.FavoriteGroup favGroup = ctx.getFavorites().getGroup(group.getName()); + FavoriteGroup favGroup = ctx.getFavorites().getGroup(group.getName()); if (favGroup == null) { return; } @@ -378,10 +393,13 @@ public class MapMarkersHelper { for (MapMarker marker : markers) { if (marker.id.equals(group.getId() + name)) { exists = true; - if (!marker.history && !marker.point.equals(latLon)) { + boolean updateColor = group.getColor() != -1 + && marker.colorIndex != ColorDialogs.getCorrespondingMarkerColorIndex(group.getColor()); + if (!marker.history && (!marker.point.equals(latLon) || updateColor)) { for (MapMarker m : mapMarkers) { if (m.id.equals(marker.id)) { m.point = latLon; + m.colorIndex = ColorDialogs.getCorrespondingMarkerColorIndex(group.getColor()); updateMapMarker(m, true); break; } @@ -598,7 +616,8 @@ public class MapMarkersHelper { private void addMarkers(List points, List historyNames, @Nullable MarkersSyncGroup group) { if (points.size() > 0) { - int colorIndex = -1; + boolean randomColor = group == null || group.getColor() == -1; + int colorIndex = randomColor ? -1 : ColorDialogs.getCorrespondingMarkerColorIndex(group.getColor()); for (int i = 0; i < points.size(); i++) { LatLon point = points.get(i); PointDescription historyName = historyNames.get(i); @@ -611,14 +630,16 @@ public class MapMarkersHelper { if (pointDescription.isLocation() && Algorithms.isEmpty(pointDescription.getName())) { pointDescription.setName(PointDescription.getSearchAddressStr(ctx)); } - if (colorIndex == -1) { - if (mapMarkers.size() > 0) { - colorIndex = (mapMarkers.get(mapMarkers.size() - 1).colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; + if (randomColor) { + if (colorIndex == -1) { + if (mapMarkers.size() > 0) { + colorIndex = (mapMarkers.get(mapMarkers.size() - 1).colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; + } else { + colorIndex = 0; + } } else { - colorIndex = 0; + colorIndex = (colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; } - } else { - colorIndex = (colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; } MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0); diff --git a/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java b/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java index 3baa1d9318..9bb7048e14 100644 --- a/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java @@ -176,7 +176,8 @@ public class EditFavoriteGroupDialogFragment extends BottomSheetDialogFragment { }); final MapMarkersHelper markersHelper = getMyApplication().getMapMarkersHelper(); - final MarkersSyncGroup syncGroup = new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE); + final MarkersSyncGroup syncGroup = + new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE, group.color); boolean groupSyncedWithMarkers = markersHelper.isGroupSynced(syncGroup.getId()); View addToMarkersView = view.findViewById(R.id.add_to_markers_view); diff --git a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java index e187024fd6..3f19935655 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java @@ -407,7 +407,8 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { List names = new LinkedList<>(); for (Map.Entry> entry : favoritesSelected.entrySet()) { FavoriteGroup favGr = helper.getGroup(entry.getKey()); - MarkersSyncGroup syncGr = new MarkersSyncGroup(favGr.name, favGr.name, MarkersSyncGroup.FAVORITES_TYPE); + MarkersSyncGroup syncGr = + new MarkersSyncGroup(favGr.name, favGr.name, MarkersSyncGroup.FAVORITES_TYPE, favGr.color); if (entry.getValue().size() == favGr.points.size()) { markersHelper.addMarkersSyncGroup(syncGr); } diff --git a/OsmAnd/src/net/osmand/plus/helpers/ColorDialogs.java b/OsmAnd/src/net/osmand/plus/helpers/ColorDialogs.java index 6618bad650..7f296866e9 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ColorDialogs.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ColorDialogs.java @@ -1,14 +1,5 @@ package net.osmand.plus.helpers; -import gnu.trove.list.array.TIntArrayList; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; - import android.app.Activity; import android.content.Context; import android.content.res.Resources; @@ -17,13 +8,21 @@ import android.graphics.drawable.Drawable; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView; +import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; -import android.widget.CheckedTextView; import android.widget.Spinner; import android.widget.TextView; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import gnu.trove.list.array.TIntArrayList; + import static android.util.TypedValue.COMPLEX_UNIT_DIP; public class ColorDialogs { @@ -66,6 +65,35 @@ public class ColorDialogs { "brown" }; + public static int getCorrespondingMarkerColorIndex(int paletteColor) { + switch (paletteColor) { + case 0xb4d00d0d: // red + return 3; // R.color.marker_red + + case 0xb4ff5020: // orange + return 2; // R.color.marker_orange + + case 0xb4eeee10: // yellow + return 4; //R.color.marker_yellow + + case 0xb488e030: // lightgreen + case 0xb400842b: // green + return 1; // R.color.marker_green + + case 0xb410c0f0: // lightblue + case 0xb41010a0: // blue + return 0; //R.color.marker_blue + + case 0xb4a71de1: // purple + case 0xb4e044bb: // pink + return 6; // R.color.marker_purple + + case 0xb48e2512: // brown + default: + return 5; //R.color.marker_teal + } + } + public static int getColorByTag(String tag) { String t = tag.toLowerCase(); for (int i = 0; i < paletteColorTags.length; i++) {