diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java index c86e17cfb..dc0e2da72 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java @@ -140,8 +140,8 @@ public class ActFmSyncThread { } if (messageBatch.isEmpty() && timeForBackgroundSync()) { - messageBatch.add(BriefMe.instantiateBriefMeForClass(Task.class)); - messageBatch.add(BriefMe.instantiateBriefMeForClass(TagData.class)); + messageBatch.add(BriefMe.instantiateBriefMeForClass(Task.class, NameMaps.PUSHED_AT_TASKS)); + messageBatch.add(BriefMe.instantiateBriefMeForClass(TagData.class, NameMaps.PUSHED_AT_TAGS)); } if (!messageBatch.isEmpty() && checkForToken()) { diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java index 51a626791..440e25688 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java @@ -3,14 +3,14 @@ package com.todoroo.astrid.actfm.sync.messages; import org.json.JSONException; import org.json.JSONObject; +import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.data.RemoteModel; public class BriefMe extends ClientToServerMessage { - public static BriefMe instantiateBriefMeForClass(Class cls) { - // TODO: compute last pushed at value for model class - long pushedAt = 0; + public static BriefMe instantiateBriefMeForClass(Class cls, String pushedAtKey) { + long pushedAt = Preferences.getLong(pushedAtKey, 0); return new BriefMe(cls, null, pushedAt); } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java index c6b63889c..4c17899c3 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java @@ -14,6 +14,7 @@ import com.todoroo.andlib.data.Property.IntegerProperty; import com.todoroo.andlib.data.Property.LongProperty; import com.todoroo.andlib.data.Property.PropertyVisitor; import com.todoroo.andlib.data.Property.StringProperty; +import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.SyncFlags; @@ -36,35 +37,51 @@ public class MakeChanges extends ServerToClientMessage public void processMessage() { JSONArray changes = json.optJSONArray("changes"); String uuid = json.optString("uuid"); - if (dao != null && changes != null && !TextUtils.isEmpty(uuid)) { - try { - TYPE model = dao.getModelClass().newInstance(); - if (changes.length() > 0) { - JSONChangeToPropertyVisitor visitor = new JSONChangeToPropertyVisitor(model); - for (int i = 0; i < changes.length(); i++) { - JSONArray change = changes.optJSONArray(i); - - if (change != null) { - String column = change.optString(0); - Property property = NameMaps.serverColumnNameToLocalProperty(table, column); - if (property != null) { // Unsupported property - property.accept(visitor, change); + if (changes != null && !TextUtils.isEmpty(uuid)) { + if (dao != null) { // Model case + try { + TYPE model = dao.getModelClass().newInstance(); + if (changes.length() > 0) { + JSONChangeToPropertyVisitor visitor = new JSONChangeToPropertyVisitor(model); + for (int i = 0; i < changes.length(); i++) { + JSONArray change = changes.optJSONArray(i); + + if (change != null) { + String column = change.optString(0); + Property property = NameMaps.serverColumnNameToLocalProperty(table, column); + if (property != null) { // Unsupported property + property.accept(visitor, change); + } } } } - } - if (model.getSetValues().size() > 0) { - if (dao.update(RemoteModel.UUID_PROPERTY.eq(uuid), model) <= 0) { // If update doesn't update rows, create a new model - model.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true); - dao.createNew(model); + if (model.getSetValues().size() > 0) { + if (dao.update(RemoteModel.UUID_PROPERTY.eq(uuid), model) <= 0) { // If update doesn't update rows, create a new model + model.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true); + dao.createNew(model); + } } + + } catch (IllegalAccessException e) { + Log.e(ERROR_TAG, "Error instantiating model for MakeChanges", e); + } catch (InstantiationException e) { + Log.e(ERROR_TAG, "Error instantiating model for MakeChanges", e); } + } else if (NameMaps.TABLE_ID_PUSHED_AT.equals(table)) { // pushed_at case + JSONArray change = changes.optJSONArray(0); + if (change != null && change.optString(0).equals(NameMaps.TABLE_ID_PUSHED_AT)) { + long pushedAt = change.optLong(1); + String pushedAtKey = null; + if (NameMaps.TABLE_ID_TASKS.equals(uuid)) + pushedAtKey = NameMaps.PUSHED_AT_TASKS; + else if (NameMaps.TABLE_ID_TAGS.equals(uuid)) + pushedAtKey = NameMaps.PUSHED_AT_TAGS; + + if (pushedAtKey != null) + Preferences.setLong(pushedAtKey, pushedAt); - } catch (IllegalAccessException e) { - Log.e(ERROR_TAG, "Error instantiating model for MakeChanges", e); - } catch (InstantiationException e) { - Log.e(ERROR_TAG, "Error instantiating model for MakeChanges", e); + } } } } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/NameMaps.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/NameMaps.java index 2b749ab3d..9c1eeea51 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/NameMaps.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/NameMaps.java @@ -23,6 +23,10 @@ public class NameMaps { public static final String TABLE_ID_TASKS = "tasks"; public static final String TABLE_ID_TAGS = "tags"; public static final String TABLE_ID_USERS = "users"; + public static final String TABLE_ID_PUSHED_AT = "pushed_at"; + + public static final String PUSHED_AT_TASKS = TABLE_ID_PUSHED_AT + "_" + TABLE_ID_TASKS; + public static final String PUSHED_AT_TAGS = TABLE_ID_PUSHED_AT + "_" + TABLE_ID_TAGS; static { // Hardcoded local tables mapped to corresponding server names diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java index 96cd8ed0d..b9d940b14 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java @@ -3,6 +3,7 @@ package com.todoroo.astrid.actfm.sync.messages; import org.json.JSONObject; import com.todoroo.astrid.core.PluginServices; +import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.Task; @@ -42,6 +43,8 @@ public abstract class ServerToClientMessage { return new MakeChanges(json, PluginServices.getTaskDao()); else if (NameMaps.TABLE_ID_TAGS.equals(table)) return new MakeChanges(json, PluginServices.getTagDataDao()); + else if (NameMaps.TABLE_ID_PUSHED_AT.equals(table)) + return new MakeChanges(json, null); else return null; }