mirror of https://github.com/tasks/tasks
Implemented delayed pushing for task list ordering
parent
565e4adf54
commit
adcf8cce74
@ -0,0 +1,56 @@
|
||||
package com.todoroo.astrid.actfm.sync;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import com.todoroo.andlib.utility.AndroidUtilities;
|
||||
import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage;
|
||||
|
||||
public class ActFmSyncWaitingPool {
|
||||
|
||||
private static volatile ActFmSyncWaitingPool instance;
|
||||
|
||||
public static ActFmSyncWaitingPool getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized(ActFmSyncWaitingPool.class) {
|
||||
if (instance == null) {
|
||||
instance = new ActFmSyncWaitingPool();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static final long WAIT_TIME = 15 * 1000L;
|
||||
|
||||
private final ExecutorService singleThreadPool;
|
||||
private final List<ClientToServerMessage<?>> pendingMessages;
|
||||
private final Runnable delayMessageRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (pendingMessages.isEmpty())
|
||||
return;
|
||||
AndroidUtilities.sleepDeep(WAIT_TIME);
|
||||
while (!pendingMessages.isEmpty()) {
|
||||
ActFmSyncThread.getInstance().enqueueMessage(pendingMessages.remove(0), null);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private ActFmSyncWaitingPool() {
|
||||
super();
|
||||
singleThreadPool = Executors.newSingleThreadExecutor();
|
||||
pendingMessages = Collections.synchronizedList(new LinkedList<ClientToServerMessage<?>>());
|
||||
}
|
||||
|
||||
public synchronized void enqueueMessage(ClientToServerMessage<?> message) {
|
||||
if (!pendingMessages.contains(message)) {
|
||||
pendingMessages.add(message);
|
||||
singleThreadPool.submit(delayMessageRunnable);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.todoroo.astrid.actfm.sync;
|
||||
|
||||
import com.todoroo.astrid.actfm.sync.ActFmSyncThread.ModelType;
|
||||
import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage;
|
||||
import com.todoroo.astrid.data.TaskListMetadata;
|
||||
|
||||
public class TaskListMetadataSyncDatabaseListener extends SyncDatabaseListener<TaskListMetadata> {
|
||||
|
||||
private final ActFmSyncWaitingPool waitingPool;
|
||||
|
||||
public TaskListMetadataSyncDatabaseListener(ActFmSyncThread actFmSyncThread, ActFmSyncWaitingPool waitingPool, ModelType modelType) {
|
||||
super(actFmSyncThread, modelType);
|
||||
this.waitingPool = waitingPool;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void enqueueMessage(TaskListMetadata model, ClientToServerMessage<?> message) {
|
||||
if (model.getSetValues().containsKey(TaskListMetadata.TASK_IDS.name))
|
||||
waitingPool.enqueueMessage(message);
|
||||
else
|
||||
super.enqueueMessage(model, message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.todoroo.astrid.actfm.sync.messages;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.todoroo.astrid.dao.TaskListMetadataDao;
|
||||
import com.todoroo.astrid.dao.TaskListMetadataOutstandingDao;
|
||||
import com.todoroo.astrid.data.TaskListMetadata;
|
||||
import com.todoroo.astrid.data.TaskListMetadataOutstanding;
|
||||
|
||||
public class TaskListMetadataChangesHappened extends ChangesHappened<TaskListMetadata, TaskListMetadataOutstanding> {
|
||||
|
||||
public TaskListMetadataChangesHappened(long id, Class<TaskListMetadata> modelClass, TaskListMetadataDao modelDao, TaskListMetadataOutstandingDao outstandingDao) {
|
||||
super(id, modelClass, modelDao, outstandingDao);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void populateChanges() {
|
||||
super.populateChanges();
|
||||
|
||||
// Collapses/removes redundant task list orders from the list--only send the most recent ordering
|
||||
Set<Long> removedChanges = new HashSet<Long>();
|
||||
boolean foundOrderChange = false;
|
||||
for (int i = changes.size() - 1; i >= 0; i--) {
|
||||
TaskListMetadataOutstanding oe = changes.get(i);
|
||||
if (TaskListMetadata.TASK_IDS.name.equals(oe.getValue(TaskListMetadataOutstanding.COLUMN_STRING))) {
|
||||
if (foundOrderChange) {
|
||||
changes.remove(i);
|
||||
removedChanges.add(oe.getId());
|
||||
} else {
|
||||
foundOrderChange = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
outstandingDao.deleteWhere(TaskListMetadataOutstanding.ID.in(removedChanges.toArray(new Long[removedChanges.size()])));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue