Add fixes and improvements

This commit is contained in:
Alexander Sytnyk 2017-09-11 19:15:49 +03:00
parent afc32a880e
commit 962d732aa6
6 changed files with 123 additions and 117 deletions

View file

@ -112,16 +112,11 @@ public class FavouritesDbHelper {
} }
public void delete(Set<FavoriteGroup> groupsToDelete, Set<FavouritePoint> favoritesSelected) { public void delete(Set<FavoriteGroup> groupsToDelete, Set<FavouritePoint> favoritesSelected) {
MapMarkersHelper markersHelper = context.getMapMarkersHelper();
if (favoritesSelected != null) { if (favoritesSelected != null) {
for (FavouritePoint p : favoritesSelected) { for (FavouritePoint p : favoritesSelected) {
FavoriteGroup group = flatGroups.get(p.getCategory()); FavoriteGroup group = flatGroups.get(p.getCategory());
if (group != null) { if (group != null) {
group.points.remove(p); group.points.remove(p);
long groupId = markersHelper.getGroupId(group.name);
if (groupId != -1) {
markersHelper.removeMarker(p.getLatitude(), p.getLongitude(), groupId);
}
} }
cachedFavoritePoints.remove(p); cachedFavoritePoints.remove(p);
} }

View file

@ -17,14 +17,15 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
public class MapMarkersHelper { public class MapMarkersHelper {
public static final int MAP_MARKERS_COLORS_COUNT = 7; public static final int MAP_MARKERS_COLORS_COUNT = 7;
private List<MapMarker> mapMarkers = new ArrayList<>(); private List<MapMarker> mapMarkers = new LinkedList<>();
private List<MapMarker> mapMarkersHistory = new ArrayList<>(); private List<MapMarker> mapMarkersHistory = new LinkedList<>();
private OsmandSettings settings; private OsmandSettings settings;
private List<MapMarkerChangedListener> listeners = new ArrayList<>(); private List<MapMarkerChangedListener> listeners = new ArrayList<>();
private OsmandApplication ctx; private OsmandApplication ctx;
@ -38,7 +39,7 @@ public class MapMarkersHelper {
} }
public static class MapMarker implements LocationPoint { public static class MapMarker implements LocationPoint {
public long id; public String id;
public LatLon point; public LatLon point;
private PointDescription pointDescription; private PointDescription pointDescription;
public int colorIndex; public int colorIndex;
@ -48,8 +49,9 @@ public class MapMarkersHelper {
public int dist; public int dist;
public long creationDate; public long creationDate;
public long visitedDate; public long visitedDate;
public long nextKey; public String nextKey;
public long groupKey = -1; public long groupKey = -1;
public String groupName;
public MapMarker(LatLon point, PointDescription name, int colorIndex, public MapMarker(LatLon point, PointDescription name, int colorIndex,
boolean selected, int index) { boolean selected, int index) {
@ -194,6 +196,26 @@ public class MapMarkersHelper {
} }
} }
public void checkAndFixActiveMarkersOrderIfNeeded() {
if (!mapMarkers.isEmpty()) {
if (mapMarkers.size() > 1) {
for (int i = 0; i < mapMarkers.size() - 1; i++) {
MapMarker first = mapMarkers.get(i);
MapMarker second = mapMarkers.get(i + 1);
if (!first.nextKey.equals(second.id)) {
markersDbHelper.changeActiveMarkerPosition(first, second);
first.nextKey = second.id;
}
}
}
MapMarker tail = mapMarkers.get(mapMarkers.size() - 1);
if (!tail.nextKey.equals(MapMarkersDbHelper.TAIL_NEXT_VALUE)) {
markersDbHelper.changeActiveMarkerPosition(tail, null);
}
}
}
private void sortMarkers(List<MapMarker> markers, final boolean history) { private void sortMarkers(List<MapMarker> markers, final boolean history) {
Collections.sort(markers, new Comparator<MapMarker>() { Collections.sort(markers, new Comparator<MapMarker>() {
@Override @Override
@ -214,18 +236,19 @@ public class MapMarkersHelper {
private void lookupAddress(final MapMarker mapMarker) { private void lookupAddress(final MapMarker mapMarker) {
if (mapMarker != null && mapMarker.pointDescription.isSearchingAddress(ctx)) { if (mapMarker != null && mapMarker.pointDescription.isSearchingAddress(ctx)) {
cancelPointAddressRequests(mapMarker.point); cancelPointAddressRequests(mapMarker.point);
GeocodingLookupService.AddressLookupRequest lookupRequest = new GeocodingLookupService.AddressLookupRequest(mapMarker.point, new GeocodingLookupService.OnAddressLookupResult() { GeocodingLookupService.AddressLookupRequest lookupRequest =
@Override new GeocodingLookupService.AddressLookupRequest(mapMarker.point, new GeocodingLookupService.OnAddressLookupResult() {
public void geocodingDone(String address) { @Override
if (Algorithms.isEmpty(address)) { public void geocodingDone(String address) {
mapMarker.pointDescription.setName(PointDescription.getAddressNotFoundStr(ctx)); if (Algorithms.isEmpty(address)) {
} else { mapMarker.pointDescription.setName(PointDescription.getAddressNotFoundStr(ctx));
mapMarker.pointDescription.setName(address); } else {
} mapMarker.pointDescription.setName(address);
markersDbHelper.updateMarker(mapMarker); }
updateMarker(mapMarker); markersDbHelper.updateMarker(mapMarker);
} updateMarker(mapMarker);
}, null); }
}, null);
ctx.getGeocodingLookupService().lookupAddress(lookupRequest); ctx.getGeocodingLookupService().lookupAddress(lookupRequest);
} }
} }
@ -234,11 +257,11 @@ public class MapMarkersHelper {
return markersDbHelper.getGroupId(name); return markersDbHelper.getGroupId(name);
} }
public void removeMarker(double lat, double lon, long groupId) { // public void removeMarker(double lat, double lon, long groupId) {
markersDbHelper.removeMarker(lat, lon, groupId); // markersDbHelper.removeMarker(lat, lon, groupId);
loadMarkers(); // loadMarkers();
refresh(); // refresh();
} // }
@Nullable @Nullable
public String getGroupName(long id) { public String getGroupName(long id) {
@ -249,17 +272,24 @@ public class MapMarkersHelper {
if (marker != null) { if (marker != null) {
cancelPointAddressRequests(marker.point); cancelPointAddressRequests(marker.point);
markersDbHelper.moveMarkerToHistory(marker); markersDbHelper.moveMarkerToHistory(marker);
loadMarkers(); mapMarkers.remove(marker);
marker.history = true;
marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
mapMarkersHistory.add(marker);
checkAndFixActiveMarkersOrderIfNeeded();
sortMarkers(mapMarkersHistory, true);
refresh(); refresh();
} }
} }
public void restoreMarkerFromHistory(MapMarker marker, int position) { public void restoreMarkerFromHistory(MapMarker marker, int position) {
if (marker != null) { if (marker != null) {
MapMarker next = position == mapMarkers.size() ? null : mapMarkers.get(position);
markersDbHelper.restoreMapMarkerFromHistory(marker); markersDbHelper.restoreMapMarkerFromHistory(marker);
markersDbHelper.changeActiveMarkerPosition(marker, next); mapMarkersHistory.remove(marker);
loadMarkers(); marker.history = false;
mapMarkers.add(position, marker);
checkAndFixActiveMarkersOrderIfNeeded();
sortMarkers(mapMarkersHistory, true);
refresh(); refresh();
} }
} }
@ -267,7 +297,7 @@ public class MapMarkersHelper {
public void removeMarkerFromHistory(MapMarker marker) { public void removeMarkerFromHistory(MapMarker marker) {
if (marker != null) { if (marker != null) {
markersDbHelper.removeMarkerFromHistory(marker); markersDbHelper.removeMarkerFromHistory(marker);
loadMarkers(); mapMarkersHistory.remove(marker);
refresh(); refresh();
} }
} }
@ -326,17 +356,20 @@ public class MapMarkersHelper {
public void reverseActiveMarkersOrder() { public void reverseActiveMarkersOrder() {
cancelAddressRequests(); cancelAddressRequests();
Collections.reverse(mapMarkers);
markersDbHelper.reverseActiveMarkersOrder(); checkAndFixActiveMarkersOrderIfNeeded();
loadMarkers();
} }
public void removeActiveMarkers() { public void moveAllActiveMarkersToHistory() {
cancelAddressRequests(); cancelAddressRequests();
markersDbHelper.moveAllActiveMarkersToHistory(); markersDbHelper.moveAllActiveMarkersToHistory();
for (MapMarker marker : mapMarkers) {
marker.history = true;
marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
}
mapMarkersHistory.addAll(mapMarkers);
mapMarkers.clear(); mapMarkers.clear();
mapMarkersHistory.clear(); sortMarkers(mapMarkersHistory, true);
mapMarkersHistory.addAll(markersDbHelper.getMarkersHistory());
refresh(); refresh();
} }
@ -388,9 +421,12 @@ public class MapMarkersHelper {
if (groups != null) { if (groups != null) {
marker.groupKey = markersDbHelper.createGroupIfNeeded(groups.get(i)); marker.groupKey = markersDbHelper.createGroupIfNeeded(groups.get(i));
} }
marker.history = false;
marker.nextKey = MapMarkersDbHelper.TAIL_NEXT_VALUE;
markersDbHelper.addMarker(marker); markersDbHelper.addMarker(marker);
mapMarkers.add(marker);
checkAndFixActiveMarkersOrderIfNeeded();
} }
loadMarkers();
} }
} }
@ -406,26 +442,24 @@ public class MapMarkersHelper {
public void moveMapMarker(@Nullable MapMarker marker, LatLon latLon) { public void moveMapMarker(@Nullable MapMarker marker, LatLon latLon) {
if (marker != null) { if (marker != null) {
marker.point = new LatLon(latLon.getLatitude(), latLon.getLongitude()); LatLon point = new LatLon(latLon.getLatitude(), latLon.getLongitude());
int index = mapMarkers.indexOf(marker);
if (index != -1) {
mapMarkers.get(index).point = point;
}
marker.point = point;
markersDbHelper.updateMarker(marker); markersDbHelper.updateMarker(marker);
loadMarkers(); checkAndFixActiveMarkersOrderIfNeeded();
refresh(); refresh();
} }
} }
public void changeActiveMarkerPositionInDb(int currentPosInMapMarkers) {
MapMarker moved = mapMarkers.get(currentPosInMapMarkers);
markersDbHelper.changeActiveMarkerPosition(moved,
currentPosInMapMarkers == mapMarkers.size() - 1 ? null : mapMarkers.get(currentPosInMapMarkers + 1));
loadMarkers();
}
public void moveMarkerToTop(MapMarker marker) { public void moveMarkerToTop(MapMarker marker) {
int i = mapMarkers.indexOf(marker); int i = mapMarkers.indexOf(marker);
if (i != -1 && mapMarkers.size() > 1) { if (i != -1 && mapMarkers.size() > 1) {
mapMarkers.remove(i); mapMarkers.remove(i);
markersDbHelper.changeActiveMarkerPosition(marker, mapMarkers.get(0)); mapMarkers.add(0, marker);
loadMarkers(); checkAndFixActiveMarkersOrderIfNeeded();
refresh(); refresh();
} }
} }

View file

@ -354,7 +354,7 @@ public class MapMarkerDialogHelper {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
listAdapter.notifyDataSetInvalidated(); listAdapter.notifyDataSetInvalidated();
markersHelper.removeActiveMarkers(); markersHelper.moveAllActiveMarkersToHistory();
if (markersHelper.getMapMarkersHistory().size() == 0) { if (markersHelper.getMapMarkersHistory().size() == 0) {
mapActivity.getDashboard().hideDashboard(); mapActivity.getDashboard().hideDashboard();
} else if (helperCallbacks != null) { } else if (helperCallbacks != null) {

View file

@ -68,7 +68,7 @@ public class MapMarkersActiveFragment extends Fragment implements OsmAndCompassL
toPosition = holder.getAdapterPosition(); toPosition = holder.getAdapterPosition();
if (toPosition >= 0 && fromPosition >= 0 && toPosition != fromPosition) { if (toPosition >= 0 && fromPosition >= 0 && toPosition != fromPosition) {
hideSnackbar(); hideSnackbar();
mapActivity.getMyApplication().getMapMarkersHelper().changeActiveMarkerPositionInDb(toPosition); mapActivity.getMyApplication().getMapMarkersHelper().checkAndFixActiveMarkersOrderIfNeeded();
} }
} }
}); });

View file

@ -1,7 +1,6 @@
package net.osmand.plus.mapmarkers; package net.osmand.plus.mapmarkers;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.util.LongSparseArray;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
@ -13,39 +12,43 @@ import net.osmand.plus.api.SQLiteAPI.SQLiteCursor;
import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.SearchHistoryHelper;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
public class MapMarkersDbHelper { public class MapMarkersDbHelper {
private static final int DB_VERSION = 3; private static final int DB_VERSION = 4;
public static final String DB_NAME = "map_markers_db"; public static final String DB_NAME = "map_markers_db";
private static final String MARKERS_TABLE_NAME = "map_markers"; private static final String MARKERS_TABLE_NAME = "map_markers";
private static final String MARKERS_COL_ID = "marker_id"; private static final String MARKERS_COL_ID = "marker_id";
private static final String MARKERS_COL_LAT = "marker_latitude"; private static final String MARKERS_COL_LAT = "marker_lat";
private static final String MARKERS_COL_LON = "marker_longitude"; private static final String MARKERS_COL_LON = "marker_lon";
private static final String MARKERS_COL_DESCRIPTION = "marker_description"; private static final String MARKERS_COL_DESCRIPTION = "marker_description";
private static final String MARKERS_COL_ACTIVE = "marker_active"; private static final String MARKERS_COL_ACTIVE = "marker_active";
private static final String MARKERS_COL_ADDED = "marker_added"; private static final String MARKERS_COL_ADDED = "marker_added";
private static final String MARKERS_COL_VISITED = "marker_visited"; private static final String MARKERS_COL_VISITED = "marker_visited";
private static final String MARKERS_COL_GROUP_NAME = "group_name";
private static final String MARKERS_COL_GROUP_KEY = "group_key"; private static final String MARKERS_COL_GROUP_KEY = "group_key";
private static final String MARKERS_COL_COLOR = "marker_color"; 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_NEXT_KEY = "marker_next_key";
private static final String MARKERS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + private static final String MARKERS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " +
MARKERS_TABLE_NAME + " (" + MARKERS_TABLE_NAME + " (" +
MARKERS_COL_ID + " long PRIMARY KEY, " + MARKERS_COL_ID + " TEXT PRIMARY KEY, " +
MARKERS_COL_LAT + " double, " + MARKERS_COL_LAT + " double, " +
MARKERS_COL_LON + " double, " + MARKERS_COL_LON + " double, " +
MARKERS_COL_DESCRIPTION + " TEXT, " + MARKERS_COL_DESCRIPTION + " TEXT, " +
MARKERS_COL_ACTIVE + " int, " + // 1 = true, 0 = false MARKERS_COL_ACTIVE + " int, " + // 1 = true, 0 = false
MARKERS_COL_ADDED + " long, " + MARKERS_COL_ADDED + " long, " +
MARKERS_COL_VISITED + " long, " + MARKERS_COL_VISITED + " long, " +
MARKERS_COL_GROUP_NAME + " TEXT, " +
MARKERS_COL_GROUP_KEY + " long, " + MARKERS_COL_GROUP_KEY + " long, " +
MARKERS_COL_COLOR + " int, " + MARKERS_COL_COLOR + " int, " +
MARKERS_COL_NEXT_KEY + " long);"; MARKERS_COL_NEXT_KEY + " TEXT);";
private static final String MARKERS_TABLE_SELECT = "SELECT " + private static final String MARKERS_TABLE_SELECT = "SELECT " +
MARKERS_COL_ID + ", " + MARKERS_COL_ID + ", " +
@ -55,6 +58,7 @@ public class MapMarkersDbHelper {
MARKERS_COL_ACTIVE + ", " + MARKERS_COL_ACTIVE + ", " +
MARKERS_COL_ADDED + ", " + MARKERS_COL_ADDED + ", " +
MARKERS_COL_VISITED + ", " + MARKERS_COL_VISITED + ", " +
MARKERS_COL_GROUP_NAME + ", " +
MARKERS_COL_GROUP_KEY + ", " + MARKERS_COL_GROUP_KEY + ", " +
MARKERS_COL_COLOR + ", " + MARKERS_COL_COLOR + ", " +
MARKERS_COL_NEXT_KEY + MARKERS_COL_NEXT_KEY +
@ -74,8 +78,8 @@ public class MapMarkersDbHelper {
GROUPS_COL_NAME + GROUPS_COL_NAME +
" FROM " + GROUPS_TABLE_NAME; " FROM " + GROUPS_TABLE_NAME;
private static final int TAIL_NEXT_VALUE = 0; public static final String TAIL_NEXT_VALUE = "tail_next";
private static final int HISTORY_NEXT_VALUE = -1; public static final String HISTORY_NEXT_VALUE = "history_next";
private final OsmandApplication context; private final OsmandApplication context;
@ -145,14 +149,6 @@ public class MapMarkersDbHelper {
} }
} }
public void reverseActiveMarkersOrder() {
List<MapMarker> markers = getActiveMarkers();
removeAllActiveMarkers();
for (int i = markers.size() - 1; i >= 0; i--) {
addMarker(markers.get(i));
}
}
public long createGroupIfNeeded(String name) { public long createGroupIfNeeded(String name) {
long res = -1; long res = -1;
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
@ -198,15 +194,11 @@ public class MapMarkersDbHelper {
} else { } else {
currentTime = System.currentTimeMillis(); currentTime = System.currentTimeMillis();
} }
marker.id = Long.parseLong(String.valueOf(currentTime) + String.valueOf(new Random().nextInt(900) + 100)); marker.id = String.valueOf(currentTime) + String.valueOf(new Random().nextInt(900) + 100);
marker.creationDate = currentTime; marker.creationDate = currentTime;
double lat = marker.getLatitude();
double lon = marker.getLongitude();
String descr = PointDescription.serializeToString(marker.getOriginalPointDescription()); String descr = PointDescription.serializeToString(marker.getOriginalPointDescription());
int active = marker.history ? 0 : 1; int active = marker.history ? 0 : 1;
long visited = saveExisting ? currentTime : 0; long visited = saveExisting ? currentTime : 0;
long groupKey = marker.groupKey;
int colorIndex = marker.colorIndex;
PointDescription pointDescription = marker.getOriginalPointDescription(); PointDescription pointDescription = marker.getOriginalPointDescription();
if (pointDescription != null && !pointDescription.isSearchingAddress(context)) { if (pointDescription != null && !pointDescription.isSearchingAddress(context)) {
@ -219,8 +211,9 @@ public class MapMarkersDbHelper {
"WHERE " + MARKERS_COL_NEXT_KEY + " = ?", new Object[]{marker.id, TAIL_NEXT_VALUE}); "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, lat, lon, descr, active, currentTime, visited, groupKey, colorIndex, 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}); marker.history ? HISTORY_NEXT_VALUE : TAIL_NEXT_VALUE});
} }
@ -265,7 +258,7 @@ public class MapMarkersDbHelper {
public List<MapMarker> getActiveMarkers() { public List<MapMarker> getActiveMarkers() {
List<MapMarker> res = new LinkedList<>(); List<MapMarker> res = new LinkedList<>();
LongSparseArray<MapMarker> markers = new LongSparseArray<>(); HashMap<String, MapMarker> markers = new LinkedHashMap<>();
SQLiteConnection db = openConnection(true); SQLiteConnection db = openConnection(true);
if (db != null) { if (db != null) {
try { try {
@ -274,29 +267,30 @@ public class MapMarkersDbHelper {
if (query.moveToFirst()) { if (query.moveToFirst()) {
do { do {
MapMarker marker = readItem(query); MapMarker marker = readItem(query);
markers.put(marker.nextKey, marker); markers.put(marker.id, marker);
} while (query.moveToNext()); } while (query.moveToNext());
} }
query.close(); query.close();
} finally { } finally {
db.close(); db.close();
} }
buildLinkedList(markers, res, markers.get(TAIL_NEXT_VALUE)); buildLinkedList(markers, res);
} }
return res; return res;
} }
private MapMarker readItem(SQLiteCursor query) { private MapMarker readItem(SQLiteCursor query) {
long id = query.getLong(0); String id = query.getString(0);
double lat = query.getDouble(1); double lat = query.getDouble(1);
double lon = query.getDouble(2); double lon = query.getDouble(2);
String desc = query.getString(3); String desc = query.getString(3);
boolean active = query.getInt(4) == 1; boolean active = query.getInt(4) == 1;
long added = query.getLong(5); long added = query.getLong(5);
long visited = query.getLong(6); long visited = query.getLong(6);
long groupKey = query.getLong(7); String groupName = query.getString(7);
int colorIndex = query.getInt(8); long groupKey = query.getLong(8);
long nextKey = query.getLong(9); int colorIndex = query.getInt(9);
String nextKey = query.getString(10);
LatLon latLon = new LatLon(lat, lon); LatLon latLon = new LatLon(lat, lon);
MapMarker marker = new MapMarker(latLon, PointDescription.deserializeFromString(desc, latLon), MapMarker marker = new MapMarker(latLon, PointDescription.deserializeFromString(desc, latLon),
@ -305,19 +299,23 @@ public class MapMarkersDbHelper {
marker.history = !active; marker.history = !active;
marker.creationDate = added; marker.creationDate = added;
marker.visitedDate = visited; marker.visitedDate = visited;
marker.groupName = groupName;
marker.groupKey = groupKey; marker.groupKey = groupKey;
marker.nextKey = nextKey; marker.nextKey = nextKey;
return marker; return marker;
} }
private void buildLinkedList(LongSparseArray<MapMarker> markers, List<MapMarker> res, MapMarker marker) { private void buildLinkedList(HashMap<String, MapMarker> markers, List<MapMarker> res) {
if (marker != null) { if (!markers.isEmpty()) {
res.add(0, marker); for (MapMarker marker : markers.values()) {
MapMarker prev = markers.get(marker.id); if (!markers.keySet().contains(marker.nextKey)) {
if (prev != null) { res.add(0, marker);
buildLinkedList(markers, res, prev); markers.remove(marker.id);
break;
}
} }
buildLinkedList(markers, res);
} }
} }
@ -343,12 +341,6 @@ public class MapMarkersDbHelper {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if (db != null) { if (db != null) {
try { try {
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + MARKERS_COL_NEXT_KEY + " = ? " +
"WHERE " + MARKERS_COL_NEXT_KEY + " = ?", new Object[]{moved.nextKey, moved.id});
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + MARKERS_COL_NEXT_KEY + " = ? " +
"WHERE " + MARKERS_COL_NEXT_KEY + " = ?", new Object[]{moved.id, next == null ? TAIL_NEXT_VALUE : next.id});
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + MARKERS_COL_NEXT_KEY + " = ? " + db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + MARKERS_COL_NEXT_KEY + " = ? " +
"WHERE " + MARKERS_COL_ID + " = ?", new Object[]{next == null ? TAIL_NEXT_VALUE : next.id, moved.id}); "WHERE " + MARKERS_COL_ID + " = ?", new Object[]{next == null ? TAIL_NEXT_VALUE : next.id, moved.id});
} finally { } finally {
@ -357,18 +349,6 @@ public class MapMarkersDbHelper {
} }
} }
private void removeAllActiveMarkers() {
SQLiteConnection db = openConnection(true);
if (db != null) {
try {
db.execSQL("DELETE FROM " + MARKERS_TABLE_NAME + " WHERE " + MARKERS_COL_ACTIVE + " = ?",
new Object[]{1});
} finally {
db.close();
}
}
}
public void removeMarker(double lat, double lon, long groupId) { public void removeMarker(double lat, double lon, long groupId) {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if (db != null) { if (db != null) {
@ -407,13 +387,11 @@ public class MapMarkersDbHelper {
try { try {
marker.visitedDate = System.currentTimeMillis(); marker.visitedDate = System.currentTimeMillis();
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + MARKERS_COL_NEXT_KEY + " = ? " +
"WHERE " + MARKERS_COL_NEXT_KEY + " = ?", new Object[]{marker.nextKey, marker.id});
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " +
MARKERS_COL_ACTIVE + " = ?, " + MARKERS_COL_ACTIVE + " = ?, " +
MARKERS_COL_VISITED + " = ? " + MARKERS_COL_VISITED + " = ?, " +
"WHERE " + MARKERS_COL_ID + " = ?", new Object[]{0, marker.visitedDate, marker.id}); MARKERS_COL_NEXT_KEY + " = ? " +
"WHERE " + MARKERS_COL_ID + " = ?", new Object[]{0, marker.visitedDate, HISTORY_NEXT_VALUE, marker.id});
} finally { } finally {
db.close(); db.close();
} }
@ -427,8 +405,9 @@ public class MapMarkersDbHelper {
long visitedDate = System.currentTimeMillis(); long visitedDate = System.currentTimeMillis();
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " +
MARKERS_COL_ACTIVE + " = ?, " + MARKERS_COL_ACTIVE + " = ?, " +
MARKERS_COL_VISITED + " = ? " + MARKERS_COL_VISITED + " = ?, " +
"WHERE " + MARKERS_COL_ACTIVE + " = ?", new Object[]{0, visitedDate, 1}); MARKERS_COL_NEXT_KEY + " = ? " +
"WHERE " + MARKERS_COL_ACTIVE + " = ?", new Object[]{0, visitedDate, HISTORY_NEXT_VALUE, 1});
} finally { } finally {
db.close(); db.close();
} }
@ -439,13 +418,11 @@ public class MapMarkersDbHelper {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if (db != null) { if (db != null) {
try { try {
List<MapMarker> active = getActiveMarkers();
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " +
MARKERS_COL_ACTIVE + " = ?, " + MARKERS_COL_ACTIVE + " = ? " +
MARKERS_COL_NEXT_KEY + " = ? " +
"WHERE " + MARKERS_COL_ID + " = ? " + "WHERE " + MARKERS_COL_ID + " = ? " +
"AND " + MARKERS_COL_ACTIVE + " = ?", "AND " + MARKERS_COL_ACTIVE + " = ?",
new Object[]{1, active.size() > 0 ? active.get(0).id : TAIL_NEXT_VALUE, marker.id, 0}); new Object[]{1, marker.id, 0});
} finally { } finally {
db.close(); db.close();
} }

View file

@ -161,7 +161,7 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm
@Override @Override
public void moveAllToHistoryOnClick() { public void moveAllToHistoryOnClick() {
mapActivity.getMyApplication().getMapMarkersHelper().removeActiveMarkers(); mapActivity.getMyApplication().getMapMarkersHelper().moveAllActiveMarkersToHistory();
activeFragment.updateAdapter(); activeFragment.updateAdapter();
} }
}; };