From 26ba5c2375e604fdb65c36c095cfe6f9210c3ad1 Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Sat, 26 Oct 2013 07:13:53 +0200 Subject: fix #3377: OC waypoints get duplicated * delete outdated waypoints from database after saving --- main/src/cgeo/geocaching/DataStore.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'main/src/cgeo/geocaching/DataStore.java') diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java index 4677f03..a231e8b 100644 --- a/main/src/cgeo/geocaching/DataStore.java +++ b/main/src/cgeo/geocaching/DataStore.java @@ -1191,10 +1191,12 @@ public class DataStore { List waypoints = cache.getWaypoints(); if (CollectionUtils.isNotEmpty(waypoints)) { + final ArrayList currentWaypointIds = new ArrayList(); ContentValues values = new ContentValues(); long timeStamp = System.currentTimeMillis(); for (Waypoint oneWaypoint : waypoints) { if (oneWaypoint.isUserDefined()) { + currentWaypointIds.add(Integer.toString(oneWaypoint.getId())); continue; } @@ -1216,13 +1218,28 @@ public class DataStore { } else { database.update(dbTableWaypoints, values, "_id = ?", new String[] { Integer.toString(oneWaypoint.getId(), 10) }); } + currentWaypointIds.add(Integer.toString(oneWaypoint.getId())); } + + removeOutdatedWaypointsOfCache(cache, currentWaypointIds); } } /** + * remove all waypoints of the given cache, where the id is not in the given list + * + * @param cache + * @param remainingWaypointIds + * ids of waypoints which shall not be deleted + */ + private static void removeOutdatedWaypointsOfCache(final @NonNull Geocache cache, final @NonNull Collection remainingWaypointIds) { + final String idList = StringUtils.join(remainingWaypointIds, ','); + database.delete(dbTableWaypoints, "geocode = ? AND _id NOT in (" + idList + ")", new String[] { cache.getGeocode() }); + } + + /** * Save coordinates into a ContentValues - * + * * @param values * a ContentValues to save coordinates in * @param oneWaypoint -- cgit v1.1