Look for a pushed_at value in server message batches

pull/14/head
Sam Bosley 13 years ago
parent d3488416d0
commit f9232b7bf4

@ -1,6 +1,7 @@
package com.todoroo.astrid.actfm.sync; package com.todoroo.astrid.actfm.sync;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -14,6 +15,7 @@ import android.util.Log;
import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.actfm.sync.messages.BriefMe; import com.todoroo.astrid.actfm.sync.messages.BriefMe;
import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage; import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage;
import com.todoroo.astrid.actfm.sync.messages.NameMaps; import com.todoroo.astrid.actfm.sync.messages.NameMaps;
@ -166,11 +168,18 @@ public class ActFmSyncThread {
JSONObject response = actFmInvoker.postSync(payload, token); JSONObject response = actFmInvoker.postSync(payload, token);
// process responses // process responses
JSONArray serverMessagesJson = response.optJSONArray("messages"); JSONArray serverMessagesJson = response.optJSONArray("messages");
String modelPushedAtString = response.optString("pushed_at");
long modelPushedAt = 0;
try {
modelPushedAt = DateUtilities.parseIso8601(modelPushedAtString);
} catch (ParseException e) {
// Unparseable date
}
if (serverMessagesJson != null) { if (serverMessagesJson != null) {
for (int i = 0; i < serverMessagesJson.length(); i++) { for (int i = 0; i < serverMessagesJson.length(); i++) {
JSONObject serverMessageJson = serverMessagesJson.optJSONObject(i); JSONObject serverMessageJson = serverMessagesJson.optJSONObject(i);
if (serverMessageJson != null) { if (serverMessageJson != null) {
ServerToClientMessage serverMessage = ServerToClientMessage.instantiateMessage(serverMessageJson); ServerToClientMessage serverMessage = ServerToClientMessage.instantiateMessage(serverMessageJson, modelPushedAt);
if (serverMessage != null) { if (serverMessage != null) {
serverMessage.processMessage(); serverMessage.processMessage();
} else { } else {

@ -8,6 +8,7 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.Property.StringProperty; import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.dao.RemoteModelDao;
@ -21,11 +22,13 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
private final RemoteModelDao<TYPE> dao; private final RemoteModelDao<TYPE> dao;
private final String table; private final String table;
private final long pushedAt;
public MakeChanges(JSONObject json, RemoteModelDao<TYPE> dao) { public MakeChanges(JSONObject json, RemoteModelDao<TYPE> dao, long pushedAt) {
super(json); super(json);
table = json.optString("table"); this.table = json.optString("table");
this.dao = dao; this.dao = dao;
this.pushedAt = pushedAt;
} }
@Override @Override
@ -47,6 +50,9 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
} }
nonColumnChanges(changes, model); nonColumnChanges(changes, model);
LongProperty pushedAtProperty = (LongProperty) NameMaps.serverColumnNameToLocalProperty(table, "pushed_at");
if (pushedAtProperty != null && pushedAt > 0)
model.setValue(pushedAtProperty, pushedAt);
StringProperty uuidProperty = (StringProperty) NameMaps.serverColumnNameToLocalProperty(table, "uuid"); StringProperty uuidProperty = (StringProperty) NameMaps.serverColumnNameToLocalProperty(table, "uuid");
if (!model.getSetValues().containsKey(uuidProperty.name)) if (!model.getSetValues().containsKey(uuidProperty.name))

@ -24,10 +24,10 @@ public abstract class ServerToClientMessage {
this.json = json; this.json = json;
} }
public static ServerToClientMessage instantiateMessage(JSONObject json) { public static ServerToClientMessage instantiateMessage(JSONObject json, long pushedAt) {
String type = json.optString("type"); String type = json.optString("type");
if (TYPE_MAKE_CHANGES.equals(type)) if (TYPE_MAKE_CHANGES.equals(type))
return instantiateMakeChanges(json); return instantiateMakeChanges(json, pushedAt);
else if (TYPE_ACKNOWLEDGE_CHANGE.equals(type)) else if (TYPE_ACKNOWLEDGE_CHANGE.equals(type))
return new AcknowledgeChange(json); return new AcknowledgeChange(json);
else if (TYPE_USER_DATA.equals(type)) else if (TYPE_USER_DATA.equals(type))
@ -40,14 +40,14 @@ public abstract class ServerToClientMessage {
return null; return null;
} }
private static MakeChanges<?> instantiateMakeChanges(JSONObject json) { private static MakeChanges<?> instantiateMakeChanges(JSONObject json, long pushedAt) {
String table = json.optString("table"); String table = json.optString("table");
if (NameMaps.TABLE_ID_TASKS.equals(table)) if (NameMaps.TABLE_ID_TASKS.equals(table))
return new MakeChanges<Task>(json, PluginServices.getTaskDao()); return new MakeChanges<Task>(json, PluginServices.getTaskDao(), pushedAt);
else if (NameMaps.TABLE_ID_TAGS.equals(table)) else if (NameMaps.TABLE_ID_TAGS.equals(table))
return new MakeChanges<TagData>(json, PluginServices.getTagDataDao()); return new MakeChanges<TagData>(json, PluginServices.getTagDataDao(), pushedAt);
else if (NameMaps.TABLE_ID_PUSHED_AT.equals(table)) else if (NameMaps.TABLE_ID_PUSHED_AT.equals(table))
return new MakeChanges<RemoteModel>(json, null); return new MakeChanges<RemoteModel>(json, null, 0);
else else
return null; return null;
} }

Loading…
Cancel
Save