diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncMonitor.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncMonitor.java new file mode 100644 index 000000000..1d4c85716 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncMonitor.java @@ -0,0 +1,10 @@ +package com.todoroo.astrid.actfm.sync; + +public class ActFmSyncMonitor { + private ActFmSyncMonitor() {/**/} + private static final ActFmSyncMonitor INSTANCE = new ActFmSyncMonitor(); + + public static ActFmSyncMonitor getInstance() { + return INSTANCE; + } +} \ No newline at end of file diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java new file mode 100644 index 000000000..ffb86b3d4 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java @@ -0,0 +1,101 @@ +package com.todoroo.astrid.actfm.sync; + +import java.util.ArrayList; +import java.util.List; +import java.util.Queue; + +import com.todoroo.andlib.service.Autowired; +import com.todoroo.andlib.service.DependencyInjectionService; +import com.todoroo.andlib.utility.Pair; +import com.todoroo.astrid.actfm.sync.messages.ChangesHappened; +import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage; +import com.todoroo.astrid.dao.TagDataDao; +import com.todoroo.astrid.dao.TagOutstandingDao; +import com.todoroo.astrid.dao.TaskDao; +import com.todoroo.astrid.dao.TaskOutstandingDao; +import com.todoroo.astrid.data.RemoteModel; +import com.todoroo.astrid.data.TagData; +import com.todoroo.astrid.data.TagOutstanding; +import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.data.TaskOutstanding; + +public class ActFmSyncThread { + + private final Queue>> queue; + private final Object monitor; + private Thread thread; + + @Autowired + private TaskDao taskDao; + + @Autowired + private TagDataDao tagDataDao; + + @Autowired + private TaskOutstandingDao taskOutstandingDao; + + @Autowired + private TagOutstandingDao tagOutstandingDao; + + public ActFmSyncThread(Queue>> queue, Object syncMonitor) { + DependencyInjectionService.getInstance().inject(this); + this.queue = queue; + this.monitor = syncMonitor; + } + + public synchronized void startThread() { + if (thread == null || !thread.isAlive()) { + thread = new Thread(new Runnable() { + @Override + public void run() { + sync(); + } + }); + thread.start(); + } + } + + private void sync() { + int batchSize = 1; + List> messages = new ArrayList>(); + while(true) { + synchronized(monitor) { + while (queue.isEmpty() && !timeForBackgroundSync()) { + try { + monitor.wait(); + } catch (InterruptedException e) { + // Ignored + } + } + } + + // Stuff in the document + while (messages.size() < batchSize && !queue.isEmpty()) { + Pair> tuple = queue.poll(); + if (tuple != null) { + messages.add(getChangesHappened(tuple)); + } + } + + if (messages.isEmpty() && timeForBackgroundSync()) { + // Populate messages with BriefMes + } + } + } + + private ChangesHappened getChangesHappened(Pair> tuple) { + Class modelClass = tuple.getRight(); + if (modelClass.equals(Task.class)) { + return new ChangesHappened(tuple.getLeft(), Task.class, taskDao, taskOutstandingDao); + } else if (modelClass.equals(TagData.class)) { + return new ChangesHappened(tuple.getLeft(), TagData.class, tagDataDao, tagOutstandingDao); + } + + return null; + } + + private boolean timeForBackgroundSync() { + return true; + } + +}