From 1b393dfea036c8cacf1d0d7f2f312035c021d101 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 26 Mar 2013 17:18:51 -0700 Subject: [PATCH] More new logic for keeping track of if last history request had more data --- .../astrid/actfm/TagCommentsFragment.java | 3 ++- .../todoroo/astrid/actfm/TagViewFragment.java | 3 ++- .../astrid/actfm/TaskCommentsFragment.java | 3 ++- .../actfm/sync/messages/FetchHistory.java | 18 ++++++++++++++++-- .../todoroo/astrid/notes/EditNoteActivity.java | 11 ++++++++--- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/TagCommentsFragment.java b/astrid/plugin-src/com/todoroo/astrid/actfm/TagCommentsFragment.java index ad32a446d..c0bc1219c 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/TagCommentsFragment.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/TagCommentsFragment.java @@ -130,7 +130,8 @@ public class TagCommentsFragment extends CommentsFragment { protected void performFetch(boolean manual, SyncMessageCallback done) { if (tagData != null) { ActFmSyncThread.getInstance().enqueueMessage(new BriefMe(UserActivity.class, null, tagData.getValue(TagData.USER_ACTIVITIES_PUSHED_AT), BriefMe.TAG_ID_KEY, tagData.getUuid()), done); - new FetchHistory(tagDataDao, TagData.HISTORY_FETCH_DATE, NameMaps.TABLE_ID_TAGS, tagData.getUuid(), null, tagData.getValue(TagData.HISTORY_FETCH_DATE), true, done).execute(); + new FetchHistory(tagDataDao, TagData.HISTORY_FETCH_DATE, TagData.HISTORY_HAS_MORE, NameMaps.TABLE_ID_TAGS, + tagData.getUuid(), null, tagData.getValue(TagData.HISTORY_FETCH_DATE), 0, true, done).execute(); } } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/TagViewFragment.java b/astrid/plugin-src/com/todoroo/astrid/actfm/TagViewFragment.java index 93c880e18..1628abd16 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/TagViewFragment.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/TagViewFragment.java @@ -456,7 +456,8 @@ public class TagViewFragment extends TaskListFragment { ActFmSyncThread.getInstance().enqueueMessage(new BriefMe(TagData.class, tagData.getUuid(), tagData.getValue(TagData.PUSHED_AT)), callback); ActFmSyncThread.getInstance().enqueueMessage(new BriefMe(UserActivity.class, null, tagData.getValue(TagData.USER_ACTIVITIES_PUSHED_AT), BriefMe.TAG_ID_KEY, tagData.getUuid()), callback); ActFmSyncThread.getInstance().enqueueMessage(new BriefMe(TaskListMetadata.class, null, tagData.getValue(TagData.METADATA_PUSHED_AT), BriefMe.TAG_ID_KEY, tagData.getUuid()), callback); - new FetchHistory(tagDataDao, TagData.HISTORY_FETCH_DATE, NameMaps.TABLE_ID_TAGS, tagData.getUuid(), null, tagData.getValue(TagData.HISTORY_FETCH_DATE), true, null).execute(); + new FetchHistory(tagDataDao, TagData.HISTORY_FETCH_DATE, TagData.HISTORY_HAS_MORE, NameMaps.TABLE_ID_TAGS, + tagData.getUuid(), null, tagData.getValue(TagData.HISTORY_FETCH_DATE), 0, true, null).execute(); } } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/TaskCommentsFragment.java b/astrid/plugin-src/com/todoroo/astrid/actfm/TaskCommentsFragment.java index 56c4c9439..cde9e9cf2 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/TaskCommentsFragment.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/TaskCommentsFragment.java @@ -83,7 +83,8 @@ public class TaskCommentsFragment extends CommentsFragment { protected void performFetch(boolean manual, SyncMessageCallback done) { if (task != null) { ActFmSyncThread.getInstance().enqueueMessage(new BriefMe(UserActivity.class, null, task.getValue(Task.USER_ACTIVITIES_PUSHED_AT), BriefMe.TASK_ID_KEY, task.getUuid()), done); - new FetchHistory(taskDao, Task.HISTORY_FETCH_DATE, NameMaps.TABLE_ID_TASKS, task.getUuid(), task.getValue(Task.TITLE), task.getValue(Task.HISTORY_FETCH_DATE), false, done).execute(); + new FetchHistory(taskDao, Task.HISTORY_FETCH_DATE, Task.HISTORY_HAS_MORE, NameMaps.TABLE_ID_TASKS, + task.getUuid(), task.getValue(Task.TITLE), task.getValue(Task.HISTORY_FETCH_DATE), 0, false, done).execute(); } } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/FetchHistory.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/FetchHistory.java index 5d430eef3..4b57b1251 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/FetchHistory.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/FetchHistory.java @@ -10,6 +10,7 @@ import org.json.JSONObject; import android.text.TextUtils; import android.util.Log; +import com.todoroo.andlib.data.Property.IntegerProperty; import com.todoroo.andlib.data.Property.LongProperty; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; @@ -29,10 +30,12 @@ public class FetchHistory { private final RemoteModelDao dao; private final LongProperty historyTimeProperty; + private final IntegerProperty historyHasMoreProperty; private final String table; private final String uuid; private final String taskTitle; private final long modifiedAfter; + private final int offset; private final boolean includeTaskHistory; private final SyncMessageCallback done; @@ -48,15 +51,17 @@ public class FetchHistory { @Autowired private ActFmPreferenceService actFmPreferenceService; - public FetchHistory(RemoteModelDao dao, LongProperty historyTimeProperty, - String table, String uuid, String taskTitle, long modifiedAfter, boolean includeTaskHistory, SyncMessageCallback done) { + public FetchHistory(RemoteModelDao dao, LongProperty historyTimeProperty, IntegerProperty historyHasMoreProperty, + String table, String uuid, String taskTitle, long modifiedAfter, int offset, boolean includeTaskHistory, SyncMessageCallback done) { DependencyInjectionService.getInstance().inject(this); this.dao = dao; this.historyTimeProperty = historyTimeProperty; + this.historyHasMoreProperty = historyHasMoreProperty; this.table = table; this.uuid = uuid; this.taskTitle = taskTitle; this.modifiedAfter = modifiedAfter; + this.offset = offset; this.includeTaskHistory = includeTaskHistory; this.done = done; } @@ -88,11 +93,19 @@ public class FetchHistory { params.add("modified_after"); params.add(modifiedAfter / 1000L); } + if (offset > 0) { + params.add("offset"); params.add(offset); + } + params.add("token"); params.add(token); try { JSONObject result = actFmInvoker.invoke("model_history_list", params.toArray(new Object[params.size()])); JSONArray list = result.optJSONArray("list"); + boolean hasMore = result.optBoolean("has_more"); long time = result.optLong("time") * 1000; + if (hasMore && offset == 0) { + historyDao.deleteWhere(History.TARGET_ID.eq(uuid)); + } if (list != null) { for (int i = 0; i < list.length(); i++) { JSONObject historyJson = list.optJSONObject(i); @@ -129,6 +142,7 @@ public class FetchHistory { try { template = dao.getModelClass().newInstance(); template.setValue(historyTimeProperty, time); + template.setValue(historyHasMoreProperty, hasMore ? 1 : 0); dao.update(RemoteModel.UUID_PROPERTY.eq(uuid), template); } catch (InstantiationException e) { Log.e(ERROR_TAG, "Error instantiating model for recording time", e); diff --git a/astrid/plugin-src/com/todoroo/astrid/notes/EditNoteActivity.java b/astrid/plugin-src/com/todoroo/astrid/notes/EditNoteActivity.java index 73585aba7..eafdf2bae 100644 --- a/astrid/plugin-src/com/todoroo/astrid/notes/EditNoteActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/notes/EditNoteActivity.java @@ -152,9 +152,13 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene return R.drawable.camera_button; } + private void fetchTask(long id) { + task = PluginServices.getTaskService().fetchById(id, Task.NOTES, Task.ID, Task.UUID, Task.TITLE, Task.HISTORY_FETCH_DATE, Task.HISTORY_HAS_MORE, Task.USER_ACTIVITIES_PUSHED_AT, Task.ATTACHMENTS_PUSHED_AT); + } + public void loadViewForTaskID(long t){ try { - task = PluginServices.getTaskService().fetchById(t, Task.NOTES, Task.ID, Task.UUID, Task.TITLE, Task.HISTORY_FETCH_DATE, Task.USER_ACTIVITIES_PUSHED_AT, Task.ATTACHMENTS_PUSHED_AT); + fetchTask(t); } catch (SQLiteException e) { StartupService.handleSQLiteError(ContextManager.getContext(), e); } @@ -421,7 +425,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene public void run() { if (task == null) return; - task = PluginServices.getTaskService().fetchById(task.getId(), Task.NOTES, Task.ID, Task.UUID, Task.TITLE, Task.HISTORY_FETCH_DATE, Task.USER_ACTIVITIES_PUSHED_AT, Task.ATTACHMENTS_PUSHED_AT); + fetchTask(task.getId()); if (task == null) return; setUpListAdapter(); @@ -438,7 +442,8 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene ActFmSyncThread.getInstance().enqueueMessage(new BriefMe(UserActivity.class, null, task.getValue(Task.USER_ACTIVITIES_PUSHED_AT), BriefMe.TASK_ID_KEY, task.getUuid()), callback); ActFmSyncThread.getInstance().enqueueMessage(new BriefMe(TaskAttachment.class, null, task.getValue(Task.ATTACHMENTS_PUSHED_AT), BriefMe.TASK_ID_KEY, task.getUuid()), callback); - new FetchHistory(taskDao, Task.HISTORY_FETCH_DATE, NameMaps.TABLE_ID_TASKS, task.getUuid(), task.getValue(Task.TITLE), task.getValue(Task.HISTORY_FETCH_DATE), false, callback).execute(); + new FetchHistory(taskDao, Task.HISTORY_FETCH_DATE, Task.HISTORY_HAS_MORE, NameMaps.TABLE_ID_TASKS, + task.getUuid(), task.getValue(Task.TITLE), task.getValue(Task.HISTORY_FETCH_DATE), 0, false, callback).execute(); } private void addComment() {