diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java index 467ba8f4f..691985617 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java @@ -609,7 +609,7 @@ public final class ActFmSyncService { public void fetchTagDataDashboard(boolean manual, final Runnable done) { invokeFetchList("goal", manual, null, new ListItemProcessor() { @Override - protected void mergeAndSave(JSONArray list, HashMap locals) throws JSONException { + protected void mergeAndSave(JSONArray list, HashMap locals, long serverTime) throws JSONException { TagData remote = new TagData(); for(int i = 0; i < list.length(); i++) { JSONObject item = list.getJSONObject(i); @@ -843,7 +843,7 @@ public final class ActFmSyncService { private class UpdateListItemProcessor extends ListItemProcessor { @Override - protected void mergeAndSave(JSONArray list, HashMap locals) throws JSONException { + protected void mergeAndSave(JSONArray list, HashMap locals, long serverTime) throws JSONException { Update remote = new Update(); for(int i = 0; i < list.length(); i++) { JSONObject item = list.getJSONObject(i); @@ -927,13 +927,13 @@ public final class ActFmSyncService { abstract protected Class typeClass(); abstract protected void mergeAndSave(JSONArray list, - HashMap locals) throws JSONException; + HashMap locals, long serverTime) throws JSONException; - public void process(JSONArray list) throws JSONException { + public void process(JSONArray list, long serverTime) throws JSONException { readRemoteIds(list); synchronized (typeClass()) { HashMap locals = getLocalModels(); - mergeAndSave(list, locals); + mergeAndSave(list, locals, serverTime); } } @@ -984,17 +984,21 @@ public final class ActFmSyncService { } @Override - protected void mergeAndSave(JSONArray list, HashMap locals) throws JSONException { + protected void mergeAndSave(JSONArray list, HashMap locals, long serverTime) throws JSONException { Task remote = new Task(); ArrayList metadata = new ArrayList(); HashSet ids = new HashSet(list.length()); + + long timeDelta = serverTime == 0 ? 0 : DateUtilities.now() - serverTime * 1000; + for(int i = 0; i < list.length(); i++) { JSONObject item = list.getJSONObject(i); readIds(locals, item, remote); long serverModificationDate = item.optLong("updated_at") * 1000; - if (serverModificationDate > 0 && modificationDates.containsKey(remote.getId()) && serverModificationDate < modificationDates.get(remote.getId())) { + if (serverModificationDate > 0 && modificationDates.containsKey(remote.getId()) + && serverModificationDate < (modificationDates.get(remote.getId()) - timeDelta)) { ids.add(remote.getId()); continue; // Modified locally more recently than remotely -- don't overwrite changes } @@ -1069,9 +1073,10 @@ public final class ActFmSyncService { JSONObject result = null; try { result = actFmInvoker.invoke(model + "_list", getParams); + long serverTime = result.optLong("time", 0); JSONArray list = result.getJSONArray("list"); - processor.process(list); - Preferences.setLong("actfm_time_" + lastSyncKey, result.optLong("time", 0)); + processor.process(list, serverTime); + Preferences.setLong("actfm_time_" + lastSyncKey, serverTime); Preferences.setLong("actfm_last_" + lastSyncKey, DateUtilities.now()); if(done != null)