diff options
| -rw-r--r-- | main/src/cgeo/geocaching/CacheDetailActivity.java | 3 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgCache.java | 87 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 2 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeowaypoint.java | 13 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/cgeowaypointadd.java | 8 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/GPXParser.java | 2 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/cgBaseTest.java | 2 |
8 files changed, 102 insertions, 20 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index 53cb2a9..145135c 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -10,6 +10,7 @@ import cgeo.geocaching.connector.ConnectorFactory; import cgeo.geocaching.connector.IConnector; import cgeo.geocaching.enumerations.LoadFlags; import cgeo.geocaching.enumerations.LoadFlags.RemoveFlag; +import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.GeopointFormatter; @@ -503,11 +504,13 @@ public class CacheDetailActivity extends AbstractActivity { break; case CONTEXT_MENU_WAYPOINT_DUPLICATE: if (cache.duplicateWaypoint(index)) { + app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); notifyDataSetChanged(); } break; case CONTEXT_MENU_WAYPOINT_DELETE: if (cache.deleteWaypoint(index)) { + app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); notifyDataSetChanged(); } break; diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index 8db3494..3c24cdb 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -28,7 +28,6 @@ import cgeo.geocaching.utils.BaseUtils; import cgeo.geocaching.utils.CancellableHandler; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -977,7 +976,7 @@ public class cgBase { // res is null during the unit tests final cgWaypoint waypoint = new cgWaypoint(res != null ? res.getString(R.string.cache_coordinates_original) : "res = null", WaypointType.WAYPOINT, false); waypoint.setCoords(new Geopoint(originalCoords)); - cache.addWaypoint(waypoint); + cache.addWaypoint(waypoint, false); cache.setUserModifiedCoords(true); } } catch (Geopoint.GeopointException e) { @@ -1048,7 +1047,7 @@ public class cgBase { // waypoint note waypoint.setNote(BaseUtils.getMatch(wp[3], GCConstants.PATTERN_WPNOTE, waypoint.getNote())); - cache.addWaypoint(waypoint); + cache.addWaypoint(waypoint, false); } } } diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index 9a2733f..8196f07 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -923,7 +923,15 @@ public class cgCache implements ICache { return Collections.unmodifiableList(waypoints); } - public void setWaypoints(List<cgWaypoint> waypoints) { + /** + * @param waypoints + * List of waypoints to set for cache + * @param saveToDatabase + * Indicates whether to add the waypoints to the database. Should be false if + * called while loading or building a cache + * @return <code>true</code> if waypoints successfully added to waypoint database + */ + public boolean setWaypoints(List<cgWaypoint> waypoints, boolean saveToDatabase) { this.waypoints = waypoints; finalDefined = false; if (waypoints != null) { @@ -934,6 +942,12 @@ public class cgCache implements ICache { } } } + + if (saveToDatabase) { + return cgeoapplication.getInstance().saveWaypoints(geocode, waypoints, false); + } + + return false; } public List<cgLog> getLogs() { @@ -1128,15 +1142,51 @@ public class cgCache implements ICache { this.storageLocation.add(sl); } - public void addWaypoint(final cgWaypoint waypoint) { + /** + * @param waypoint + * Waypoint to add to the cache + * @param saveToDatabase + * Indicates whether to add the waypoint to the database. Should be false if + * called while loading or building a cache + * @return <code>true</code> if waypoint successfully added to waypoint database + */ + public boolean addWaypoint(final cgWaypoint waypoint, boolean saveToDatabase) { if (null == waypoints) { waypoints = new ArrayList<cgWaypoint>(); } - waypoints.add(waypoint); + waypoint.setGeocode(geocode); - if (waypoint.isFinalWithCoords()) { - finalDefined = true; + + if (waypoint.getId() <= 0) { // this is a new waypoint + waypoints.add(waypoint); + if (waypoint.isFinalWithCoords()) { + finalDefined = true; + } + } else { // this is a waypoint being edited + int index = 0; + for (cgWaypoint wp : waypoints) { // remove old version of waypoint + if (wp.getId() == waypoint.getId()) { + waypoints.remove(index); + break; + } + index++; + } + waypoints.add(waypoint); + // when waypoint was edited, finalDefined may have changed. check all waypoints and set again + finalDefined = false; + for (cgWaypoint wp : waypoints) { + if (wp.isFinalWithCoords()) { + finalDefined = true; + break; + } + } + } + + if (saveToDatabase) { + return cgeoapplication.getInstance().saveOwnWaypoint(waypoint.getId(), geocode, waypoint); } + + return false; } public boolean hasWaypoints() { @@ -1189,6 +1239,7 @@ public class cgCache implements ICache { * delete a user defined waypoint * * @param index + * of the waypoint in cache's waypoint list * @return <code>true</code>, if the waypoint was deleted */ public boolean deleteWaypoint(int index) { @@ -1216,6 +1267,30 @@ public class cgCache implements ICache { } /** + * delete a user defined waypoint + * + * @param waypoint + * to be removed from cache + * @return <code>true</code>, if the waypoint was deleted + */ + public boolean deleteWaypoint(cgWaypoint waypoint) { + if (waypoint.getId() <= 0) { + return false; + } + + int index = 0; + + for (cgWaypoint wp : waypoints) { + if (wp.getId() == waypoint.getId()) { + return deleteWaypoint(index); + } + index++; + } + + return false; + } + + /** * @param index * @return waypoint or <code>null</code> */ @@ -1256,7 +1331,7 @@ public class cgCache implements ICache { final String name = cgeoapplication.getInstance().getString(R.string.cache_personal_note) + " " + count; final cgWaypoint waypoint = new cgWaypoint(name, WaypointType.WAYPOINT, false); waypoint.setCoords(point); - addWaypoint(waypoint); + addWaypoint(waypoint, false); count++; } } catch (GeopointParser.ParseException e) { diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 83247c1..ac0be8f 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -1960,7 +1960,7 @@ public class cgData { if (loadFlags.contains(LoadFlag.LOAD_WAYPOINTS)) { final List<cgWaypoint> waypoints = loadWaypoints(cache.getGeocode()); if (CollectionUtils.isNotEmpty(waypoints)) { - cache.setWaypoints(waypoints); + cache.setWaypoints(waypoints, false); } } diff --git a/main/src/cgeo/geocaching/cgeowaypoint.java b/main/src/cgeo/geocaching/cgeowaypoint.java index 624a9fe..453308e 100644 --- a/main/src/cgeo/geocaching/cgeowaypoint.java +++ b/main/src/cgeo/geocaching/cgeowaypoint.java @@ -2,7 +2,8 @@ package cgeo.geocaching; import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; -import cgeo.geocaching.enumerations.LoadFlags.RemoveFlag; +import cgeo.geocaching.enumerations.LoadFlags; +import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import org.apache.commons.lang3.StringUtils; @@ -319,12 +320,12 @@ public class cgeowaypoint extends AbstractActivity { private class deleteWaypointListener implements View.OnClickListener { public void onClick(View arg0) { - if (app.deleteWaypoint(id)) { - String geocode = waypoint.getGeocode(); + String geocode = waypoint.getGeocode(); + cgCache cache = app.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS); + if (null != cache && cache.deleteWaypoint(waypoint)) { + app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); + StaticMapsProvider.removeWpStaticMaps(id, geocode); - if (!StringUtils.isEmpty(geocode)) { - app.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVE_CACHE)); - } finish(); return; diff --git a/main/src/cgeo/geocaching/cgeowaypointadd.java b/main/src/cgeo/geocaching/cgeowaypointadd.java index e7d00c4..9c00241 100644 --- a/main/src/cgeo/geocaching/cgeowaypointadd.java +++ b/main/src/cgeo/geocaching/cgeowaypointadd.java @@ -3,6 +3,7 @@ package cgeo.geocaching; import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.enumerations.LoadFlags; +import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.DistanceParser; import cgeo.geocaching.geopoint.Geopoint; @@ -27,6 +28,7 @@ import android.widget.EditText; import android.widget.Spinner; import java.util.ArrayList; +import java.util.EnumSet; import java.util.List; public class cgeowaypointadd extends AbstractActivity { @@ -419,10 +421,12 @@ public class cgeowaypointadd extends AbstractActivity { waypoint.setNote(note); waypoint.setId(id); - if (app.saveOwnWaypoint(id, geocode, waypoint)) { + cgCache cache = app.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS); + if (null != cache && cache.addWaypoint(waypoint, true)) { + app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); StaticMapsProvider.removeWpStaticMaps(id, geocode); if (Settings.isStoreOfflineWpMaps()) { - StaticMapsProvider.storeWaypointStaticMap(app.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB), cgeowaypointadd.this, waypoint, false); + StaticMapsProvider.storeWaypointStaticMap(cache, cgeowaypointadd.this, waypoint, false); } finish(); return; diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java index 081a28a..10f3fa8 100644 --- a/main/src/cgeo/geocaching/files/GPXParser.java +++ b/main/src/cgeo/geocaching/files/GPXParser.java @@ -320,7 +320,7 @@ public abstract class GPXParser extends FileParser { mergedWayPoints.addAll(cacheForWaypoint.getWaypoints()); cgWaypoint.mergeWayPoints(mergedWayPoints, Collections.singletonList(waypoint), true); - cacheForWaypoint.setWaypoints(mergedWayPoints); + cacheForWaypoint.setWaypoints(mergedWayPoints, false); result.put(cacheGeocodeForWaypoint, cacheForWaypoint); showProgressMessage(progressHandler, progressStream.getProgress()); } diff --git a/tests/src/cgeo/geocaching/cgBaseTest.java b/tests/src/cgeo/geocaching/cgBaseTest.java index b9c3586..e6aa862 100644 --- a/tests/src/cgeo/geocaching/cgBaseTest.java +++ b/tests/src/cgeo/geocaching/cgBaseTest.java @@ -134,7 +134,7 @@ public class cgBaseTest extends AndroidTestCase { private static void assertWaypointsFromNote(final cgCache cache, Geopoint[] expected, String note) { cache.setPersonalNote(note); - cache.setWaypoints(new ArrayList<cgWaypoint>()); + cache.setWaypoints(new ArrayList<cgWaypoint>(), false); cache.parseWaypointsFromNote(); assertEquals(expected.length, cache.getWaypoints().size()); for (int i = 0; i < expected.length; i++) { |
