From 2b14185b09a5781d5192d8a9b51fe209b2c6e66f Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Fri, 29 Sep 2017 16:58:44 +0300 Subject: [PATCH] Save selections in DB --- .../src/net/osmand/plus/MapMarkersHelper.java | 18 ++++++++----- .../plus/mapmarkers/MapMarkersDbHelper.java | 26 ++++++++++++++----- .../plus/mapmarkers/PlanRouteFragment.java | 2 +- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 09fef6e6c1..754822a9c9 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -338,7 +338,7 @@ public class MapMarkersHelper { mapMarker.pointDescription.setName(address); } markersDbHelper.updateMarker(mapMarker); - updateMarker(mapMarker); + refreshMarker(mapMarker); } }, null); ctx.getGeocodingLookupService().lookupAddress(lookupRequest); @@ -553,13 +553,19 @@ public class MapMarkersHelper { public void deselectAllActiveMarkers() { for (MapMarker m : mapMarkers) { - m.selected = false; + if (m.selected) { + m.selected = false; + markersDbHelper.updateMarker(m); + } } } public void selectAllActiveMarkers() { for (MapMarker m : mapMarkers) { - m.selected = true; + if (!m.selected) { + m.selected = true; + markersDbHelper.updateMarker(m); + } } } @@ -807,20 +813,20 @@ public class MapMarkersHelper { listeners.remove(l); } - private void updateMarker(MapMarker marker) { + private void refreshMarker(MapMarker marker) { for (MapMarkerChangedListener l : listeners) { l.onMapMarkerChanged(marker); } } - private void updateMarkers() { + private void refreshMarkers() { for (MapMarkerChangedListener l : listeners) { l.onMapMarkersChanged(); } } public void refresh() { - updateMarkers(); + refreshMarkers(); } private void cancelAddressRequests() { diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java index 490c78da22..c6a7a27b51 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java @@ -21,7 +21,7 @@ import java.util.Random; public class MapMarkersDbHelper { - private static final int DB_VERSION = 9; + private static final int DB_VERSION = 10; public static final String DB_NAME = "map_markers_db"; private static final String MARKERS_TABLE_NAME = "map_markers"; @@ -37,6 +37,7 @@ public class MapMarkersDbHelper { private static final String MARKERS_COL_COLOR = "marker_color"; private static final String MARKERS_COL_NEXT_KEY = "marker_next_key"; private static final String MARKERS_COL_DISABLED = "marker_disabled"; + private static final String MARKERS_COL_SELECTED = "marker_selected"; private static final String MARKERS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + MARKERS_TABLE_NAME + " (" + @@ -51,7 +52,8 @@ public class MapMarkersDbHelper { MARKERS_COL_GROUP_KEY + " TEXT, " + MARKERS_COL_COLOR + " int, " + MARKERS_COL_NEXT_KEY + " TEXT, " + - MARKERS_COL_DISABLED + " int);"; + MARKERS_COL_DISABLED + " int, " + // 1 = true, 0 = false + MARKERS_COL_SELECTED + " int);"; // 1 = true, 0 = false private static final String MARKERS_TABLE_SELECT = "SELECT " + MARKERS_COL_ID + ", " + @@ -65,7 +67,8 @@ public class MapMarkersDbHelper { MARKERS_COL_GROUP_KEY + ", " + MARKERS_COL_COLOR + ", " + MARKERS_COL_NEXT_KEY + ", " + - MARKERS_COL_DISABLED + + MARKERS_COL_DISABLED + ", " + + MARKERS_COL_SELECTED + " FROM " + MARKERS_TABLE_NAME; private static final String GROUPS_TABLE_NAME = "map_markers_groups"; @@ -134,6 +137,12 @@ public class MapMarkersDbHelper { " SET " + MARKERS_COL_DISABLED + " = ? " + "WHERE " + MARKERS_COL_DISABLED + " IS NULL", new Object[]{0}); } + if (oldVersion < 10) { + db.execSQL("ALTER TABLE " + MARKERS_TABLE_NAME + " ADD " + MARKERS_COL_SELECTED + " int"); + db.execSQL("UPDATE " + MARKERS_TABLE_NAME + + " SET" + MARKERS_COL_SELECTED + " = ? " + + "WHERE " + MARKERS_COL_SELECTED + " IS NULL", new Object[]{0}); + } } private void saveExistingMarkersToDb() { @@ -319,10 +328,10 @@ public class MapMarkersDbHelper { "WHERE " + MARKERS_COL_NEXT_KEY + " = ?", new Object[]{marker.id, TAIL_NEXT_VALUE}); } - db.execSQL("INSERT INTO " + MARKERS_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + db.execSQL("INSERT INTO " + MARKERS_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{marker.id, marker.getLatitude(), marker.getLongitude(), descr, active, currentTime, visited, marker.groupName, marker.groupKey, marker.colorIndex, - marker.history ? HISTORY_NEXT_VALUE : TAIL_NEXT_VALUE, 0}); + marker.history ? HISTORY_NEXT_VALUE : TAIL_NEXT_VALUE, 0, 0}); } public List getMarkersFromGroup(MarkersSyncGroup group) { @@ -398,6 +407,7 @@ public class MapMarkersDbHelper { String groupKey = query.getString(8); int colorIndex = query.getInt(9); String nextKey = query.getString(10); + boolean selected = query.getInt(12) == 1; LatLon latLon = new LatLon(lat, lon); MapMarker marker = new MapMarker(latLon, PointDescription.deserializeFromString(desc, latLon), @@ -409,6 +419,7 @@ public class MapMarkersDbHelper { marker.groupName = groupName; marker.groupKey = groupKey; marker.nextKey = nextKey; + marker.selected = selected; return marker; } @@ -437,9 +448,10 @@ public class MapMarkersDbHelper { MARKERS_COL_LAT + " = ?, " + MARKERS_COL_LON + " = ?, " + MARKERS_COL_DESCRIPTION + " = ?, " + - MARKERS_COL_COLOR + " = ? " + + MARKERS_COL_COLOR + " = ?, " + + MARKERS_COL_SELECTED + " = ? " + "WHERE " + MARKERS_COL_ID + " = ?", - new Object[]{marker.getLatitude(), marker.getLongitude(), descr, marker.colorIndex, marker.id}); + new Object[]{marker.getLatitude(), marker.getLongitude(), descr, marker.colorIndex, marker.selected, marker.id}); } finally { db.close(); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java index f698b996cb..dd5c0206ed 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java @@ -209,6 +209,7 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene MapMarker marker = adapter.getItem(pos); selectedCount = marker.selected ? selectedCount - 1 : selectedCount + 1; marker.selected = !marker.selected; + markersHelper.updateMapMarker(marker, false); } adapter.notifyItemChanged(pos); updateSelectButton(); @@ -655,7 +656,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene if (markersListOpened) { hideMarkersList(); } - markersHelper.deselectAllActiveMarkers(); activity.getSupportFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss(); }