Added new gtasks sync metadata for saving last sync time

pull/14/head
Tim Su 13 years ago
parent 7ac45fde56
commit 07669aadbb

@ -97,13 +97,22 @@ abstract public class SyncMetadataService<TYPE extends SyncContainer> {
if(lastSyncDate == 0) if(lastSyncDate == 0)
tasks = taskDao.query(Query.select(Task.ID).where(Criterion.none)); tasks = taskDao.query(Query.select(Task.ID).where(Criterion.none));
else else
tasks = taskDao.query(Query.select(Task.ID).where(Criterion.and( tasks = taskDao.query(Query.select(Task.ID).where(
Task.MODIFICATION_DATE.gt(lastSyncDate), Task.MODIFICATION_DATE.gt(lastSyncDate)).orderBy(Order.asc(Task.ID)));
Task.LAST_SYNC.lte(lastSyncDate))).orderBy(Order.asc(Task.ID))); tasks = filterLocallyUpdated(tasks, lastSyncDate);
return joinWithMetadata(tasks, true, properties); return joinWithMetadata(tasks, true, properties);
} }
/**
* @param tasks
* @param lastSyncDate
*/
protected TodorooCursor<Task> filterLocallyUpdated(TodorooCursor<Task> tasks, long lastSyncDate) {
// override hook
return tasks;
}
private TodorooCursor<Task> joinWithMetadata(TodorooCursor<Task> tasks, private TodorooCursor<Task> joinWithMetadata(TodorooCursor<Task> tasks,
boolean both, Property<?>... properties) { boolean both, Property<?>... properties) {
try { try {

@ -4,6 +4,7 @@
package com.todoroo.astrid.gtasks; package com.todoroo.astrid.gtasks;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -74,6 +75,22 @@ public final class GtasksMetadataService extends SyncMetadataService<GtasksTaskC
return GtasksMetadata.ID.neq(""); //$NON-NLS-1$ return GtasksMetadata.ID.neq(""); //$NON-NLS-1$
} }
@Override
protected TodorooCursor<Task> filterLocallyUpdated(TodorooCursor<Task> tasks, long lastSyncDate) {
HashSet<Long> taskIds = new HashSet<Long>();
for(tasks.moveToFirst(); !tasks.isAfterLast(); tasks.moveToNext())
taskIds.add(tasks.get(Task.ID));
TodorooCursor<Metadata> metadata = metadataDao.query(Query.select(Metadata.TASK).where(
Criterion.and(MetadataCriteria.withKey(GtasksSyncMetadata.METADATA_KEY),
GtasksSyncMetadata.LAST_SYNC.lt(lastSyncDate))));
for(metadata.moveToFirst(); !metadata.isAfterLast(); metadata.moveToNext())
taskIds.remove(metadata.get(Metadata.TASK));
return taskDao.query(Query.select(Task.ID).where(
Task.ID.in(taskIds.toArray(new Long[taskIds.size()]))));
}
// --- list iterating helpers // --- list iterating helpers

@ -0,0 +1,49 @@
package com.todoroo.astrid.gtasks;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.dao.MetadataDao;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.data.Metadata;
/**
* Metadata entries for synchronizing a GTasks Task
* @author Tim Su <tim@todoroo.com>
*
*/
public class GtasksSyncMetadata {
/** metadata key */
public static final String METADATA_KEY = "gtasks-sync"; //$NON-NLS-1$
/** last sync date*/
public static final LongProperty LAST_SYNC = new LongProperty(Metadata.TABLE,
Metadata.VALUE1.name);
/**
* Helper to set value
* @param metadataDao
* @param id
* @param property
* @param now
*/
public static <T> void set(MetadataDao metadataDao, long taskId,
Property<T> property, T value) {
TodorooCursor<Metadata> cursor = metadataDao.query(Query.select(Metadata.PROPERTIES).
where(MetadataCriteria.byTaskAndwithKey(taskId, METADATA_KEY)));
Metadata metadata = new Metadata();
if(cursor.getCount() == 0) {
metadata.setValue(Metadata.TASK, taskId);
metadata.setValue(Metadata.KEY, METADATA_KEY);
} else {
cursor.moveToFirst();
metadata.readFromCursor(cursor);
}
metadata.setValue(property, value);
metadataDao.persist(metadata);
}
}

@ -21,6 +21,7 @@ import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gtasks.GtasksMetadata; import com.todoroo.astrid.gtasks.GtasksMetadata;
import com.todoroo.astrid.gtasks.GtasksMetadataService; import com.todoroo.astrid.gtasks.GtasksMetadataService;
import com.todoroo.astrid.gtasks.GtasksPreferenceService; import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.GtasksSyncMetadata;
import com.todoroo.astrid.gtasks.GtasksTaskListUpdater; import com.todoroo.astrid.gtasks.GtasksTaskListUpdater;
import com.todoroo.astrid.gtasks.api.GtasksApiUtilities; import com.todoroo.astrid.gtasks.api.GtasksApiUtilities;
import com.todoroo.astrid.gtasks.api.GtasksService; import com.todoroo.astrid.gtasks.api.GtasksService;
@ -253,7 +254,7 @@ public final class GtasksSyncOnSaveService {
} }
task.setValue(Task.MODIFICATION_DATE, DateUtilities.now()); task.setValue(Task.MODIFICATION_DATE, DateUtilities.now());
task.setValue(Task.LAST_SYNC, DateUtilities.now()); GtasksSyncMetadata.set(metadataDao, task.getId(), GtasksSyncMetadata.LAST_SYNC, DateUtilities.now());
Flags.set(Flags.GTASKS_SUPPRESS_SYNC); Flags.set(Flags.GTASKS_SUPPRESS_SYNC);
taskDao.saveExisting(task); taskDao.saveExisting(task);
} }

Loading…
Cancel
Save