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 e503d54f7..6aea12941 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java @@ -22,7 +22,7 @@ import com.todoroo.astrid.data.TaskOutstanding; public class ActFmSyncThread { - private final Queue>> changesQueue; + private final Queue> changesQueue; private final Object monitor; private Thread thread; @@ -38,13 +38,18 @@ public class ActFmSyncThread { @Autowired private TagOutstandingDao tagOutstandingDao; - public ActFmSyncThread(Queue>> queue, Object syncMonitor) { + public static enum ModelType { + TYPE_TASK, + TYPE_TAG + } + + public ActFmSyncThread(Queue> queue, Object syncMonitor) { DependencyInjectionService.getInstance().inject(this); this.changesQueue = queue; this.monitor = syncMonitor; } - public synchronized void startThread() { + public synchronized void startSyncThread() { if (thread == null || !thread.isAlive()) { thread = new Thread(new Runnable() { @Override @@ -57,53 +62,61 @@ public class ActFmSyncThread { } private void sync() { - int batchSize = 1; - List> messages = new LinkedList>(); - while(true) { - synchronized(monitor) { - while (changesQueue.isEmpty() && !timeForBackgroundSync()) { - try { - monitor.wait(); - } catch (InterruptedException e) { - // Ignored + try { + int batchSize = 1; + List> messages = new LinkedList>(); + while(true) { + synchronized(monitor) { + while (changesQueue.isEmpty() && !timeForBackgroundSync()) { + try { + monitor.wait(); + } catch (InterruptedException e) { + // Ignored + } } } - } - // Stuff in the document - while (messages.size() < batchSize && !changesQueue.isEmpty()) { - Pair> tuple = changesQueue.poll(); - if (tuple != null) { - ClientToServerMessage changes = getChangesHappened(tuple); - if (changes != null) - messages.add(changes); + // Stuff in the document + while (messages.size() < batchSize && !changesQueue.isEmpty()) { + Pair tuple = changesQueue.poll(); + if (tuple != null) { + ClientToServerMessage changes = getChangesHappened(tuple); + if (changes != null) + messages.add(changes); + } } - } - if (messages.isEmpty() && timeForBackgroundSync()) { - messages.add(getBriefMe(Task.class)); - messages.add(getBriefMe(TagData.class)); - } + if (messages.isEmpty() && timeForBackgroundSync()) { + messages.add(getBriefMe(Task.class)); + messages.add(getBriefMe(TagData.class)); + } - if (!messages.isEmpty()) { - // Get List responses - // foreach response response.process - // if (responses.didntFinish) batchSize = Math.max(batchSize / 2, 1) - // else batchSize = min(batchSize, messages.size()) * 2 - messages = new LinkedList>(); + if (!messages.isEmpty()) { + // Get List responses + // foreach response response.process + // if (responses.didntFinish) batchSize = Math.max(batchSize / 2, 1) + // else batchSize = min(batchSize, messages.size()) * 2 + messages = new LinkedList>(); + } } + } catch (Exception e) { + // In the worst case, restart thread if something goes wrong + thread = null; + startSyncThread(); } + } - private ChangesHappened getChangesHappened(Pair> tuple) { - Class modelClass = tuple.getRight(); - if (modelClass.equals(Task.class)) { + private ChangesHappened getChangesHappened(Pair tuple) { + ModelType modelType = tuple.getRight(); + switch(modelType) { + case TYPE_TASK: return new ChangesHappened(tuple.getLeft(), Task.class, taskDao, taskOutstandingDao); - } else if (modelClass.equals(TagData.class)) { + case TYPE_TAG: return new ChangesHappened(tuple.getLeft(), TagData.class, tagDataDao, tagOutstandingDao); + default: + return null; } - - return null; } private BriefMe getBriefMe(Class cls) { diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/SyncDatabaseListener.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/SyncDatabaseListener.java new file mode 100644 index 000000000..4511bc446 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/SyncDatabaseListener.java @@ -0,0 +1,30 @@ +package com.todoroo.astrid.actfm.sync; + +import java.util.Queue; + +import com.todoroo.andlib.data.DatabaseDao.ModelUpdateListener; +import com.todoroo.andlib.utility.Pair; +import com.todoroo.astrid.actfm.sync.ActFmSyncThread.ModelType; +import com.todoroo.astrid.data.RemoteModel; + +public class SyncDatabaseListener implements ModelUpdateListener { + + private final Queue> queue; + private final Object monitor; + private final ModelType modelType; + + public SyncDatabaseListener(Queue> queue, Object syncMonitor, ModelType modelType) { + this.queue = queue; + this.monitor = syncMonitor; + this.modelType = modelType; + } + + @Override + public void onModelUpdated(MTYPE model) { + queue.add(Pair.create(model.getId(), modelType)); + synchronized(monitor) { + monitor.notifyAll(); + } + } + +}