Added a new database listener class that can add changes to the sync queue

pull/14/head
Sam Bosley 12 years ago
parent ace604cd9b
commit ebb1a23ae3

@ -22,7 +22,7 @@ import com.todoroo.astrid.data.TaskOutstanding;
public class ActFmSyncThread {
private final Queue<Pair<Long, Class<? extends RemoteModel>>> changesQueue;
private final Queue<Pair<Long, ModelType>> changesQueue;
private final Object monitor;
private Thread thread;
@ -38,13 +38,18 @@ public class ActFmSyncThread {
@Autowired
private TagOutstandingDao tagOutstandingDao;
public ActFmSyncThread(Queue<Pair<Long, Class<? extends RemoteModel>>> queue, Object syncMonitor) {
public static enum ModelType {
TYPE_TASK,
TYPE_TAG
}
public ActFmSyncThread(Queue<Pair<Long, ModelType>> 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,6 +62,7 @@ public class ActFmSyncThread {
}
private void sync() {
try {
int batchSize = 1;
List<ClientToServerMessage<?>> messages = new LinkedList<ClientToServerMessage<?>>();
while(true) {
@ -72,7 +78,7 @@ public class ActFmSyncThread {
// Stuff in the document
while (messages.size() < batchSize && !changesQueue.isEmpty()) {
Pair<Long, Class<? extends RemoteModel>> tuple = changesQueue.poll();
Pair<Long, ModelType> tuple = changesQueue.poll();
if (tuple != null) {
ClientToServerMessage<?> changes = getChangesHappened(tuple);
if (changes != null)
@ -93,18 +99,25 @@ public class ActFmSyncThread {
messages = new LinkedList<ClientToServerMessage<?>>();
}
}
} catch (Exception e) {
// In the worst case, restart thread if something goes wrong
thread = null;
startSyncThread();
}
private ChangesHappened<?, ?> getChangesHappened(Pair<Long, Class<? extends RemoteModel>> tuple) {
Class<? extends RemoteModel> modelClass = tuple.getRight();
if (modelClass.equals(Task.class)) {
return new ChangesHappened<Task, TaskOutstanding>(tuple.getLeft(), Task.class, taskDao, taskOutstandingDao);
} else if (modelClass.equals(TagData.class)) {
return new ChangesHappened<TagData, TagOutstanding>(tuple.getLeft(), TagData.class, tagDataDao, tagOutstandingDao);
}
private ChangesHappened<?, ?> getChangesHappened(Pair<Long, ModelType> tuple) {
ModelType modelType = tuple.getRight();
switch(modelType) {
case TYPE_TASK:
return new ChangesHappened<Task, TaskOutstanding>(tuple.getLeft(), Task.class, taskDao, taskOutstandingDao);
case TYPE_TAG:
return new ChangesHappened<TagData, TagOutstanding>(tuple.getLeft(), TagData.class, tagDataDao, tagOutstandingDao);
default:
return null;
}
}
private <TYPE extends RemoteModel> BriefMe<TYPE> getBriefMe(Class<TYPE> cls) {
// TODO: compute last pushed at value for model class

@ -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<MTYPE extends RemoteModel> implements ModelUpdateListener<MTYPE> {
private final Queue<Pair<Long, ModelType>> queue;
private final Object monitor;
private final ModelType modelType;
public SyncDatabaseListener(Queue<Pair<Long, ModelType>> 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();
}
}
}
Loading…
Cancel
Save