Only sync gtask lists with updates

pull/253/head
Alex Baker 11 years ago
parent add0edd175
commit bbaabf8770

@ -10,6 +10,9 @@ import com.google.api.services.tasks.model.TaskLists;
import com.todoroo.astrid.dao.StoreObjectDao; import com.todoroo.astrid.dao.StoreObjectDao;
import com.todoroo.astrid.data.StoreObject; import com.todoroo.astrid.data.StoreObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -21,28 +24,19 @@ import javax.inject.Singleton;
@Singleton @Singleton
public class GtasksListService { public class GtasksListService {
private static final Logger log = LoggerFactory.getLogger(GtasksListService.class);
public static final StoreObject LIST_NOT_FOUND_OBJECT = null; public static final StoreObject LIST_NOT_FOUND_OBJECT = null;
private final StoreObjectDao storeObjectDao; private final StoreObjectDao storeObjectDao;
private List<StoreObject> lists;
@Inject @Inject
public GtasksListService(StoreObjectDao storeObjectDao) { public GtasksListService(StoreObjectDao storeObjectDao) {
this.storeObjectDao = storeObjectDao; this.storeObjectDao = storeObjectDao;
} }
private void readLists() {
if(lists != null) {
return;
}
lists = storeObjectDao.getByType(GtasksList.TYPE);
}
public List<StoreObject> getLists() { public List<StoreObject> getLists() {
readLists(); return storeObjectDao.getByType(GtasksList.TYPE);
return lists;
} }
/** /**
@ -51,7 +45,7 @@ public class GtasksListService {
* @param remoteLists remote information about your lists * @param remoteLists remote information about your lists
*/ */
public synchronized void updateLists(TaskLists remoteLists) { public synchronized void updateLists(TaskLists remoteLists) {
readLists(); List<StoreObject> lists = getLists();
Set<Long> previousLists = new HashSet<>(); Set<Long> previousLists = new HashSet<>();
for(StoreObject list : lists) { for(StoreObject list : lists) {
@ -59,7 +53,6 @@ public class GtasksListService {
} }
List<TaskList> items = remoteLists.getItems(); List<TaskList> items = remoteLists.getItems();
List<StoreObject> newLists = new ArrayList<>();
for(int i = 0; i < items.size(); i++) { for(int i = 0; i < items.size(); i++) {
com.google.api.services.tasks.model.TaskList remote = items.get(i); com.google.api.services.tasks.model.TaskList remote = items.get(i);
@ -72,19 +65,20 @@ public class GtasksListService {
} }
} }
String title = remote.getTitle();
if(local == null) { if(local == null) {
log.debug("Adding new gtask list {}", title);
local = new StoreObject(); local = new StoreObject();
local.setValue(GtasksList.LAST_SYNC, 0L);
} }
local.setType(GtasksList.TYPE); local.setType(GtasksList.TYPE);
local.setValue(GtasksList.REMOTE_ID, id); local.setValue(GtasksList.REMOTE_ID, id);
local.setValue(GtasksList.NAME, remote.getTitle()); local.setValue(GtasksList.NAME, title);
local.setValue(GtasksList.ORDER, i); local.setValue(GtasksList.ORDER, i);
storeObjectDao.persist(local); storeObjectDao.persist(local);
previousLists.remove(local.getId()); previousLists.remove(local.getId());
newLists.add(local);
} }
lists = newLists;
// check for lists that aren't on remote server // check for lists that aren't on remote server
for(Long listId : previousLists) { for(Long listId : previousLists) {
@ -93,8 +87,7 @@ public class GtasksListService {
} }
public StoreObject getList(String listId) { public StoreObject getList(String listId) {
readLists(); for(StoreObject list : getLists()) {
for(StoreObject list : lists) {
if (list != null && list.getValue(GtasksList.REMOTE_ID).equals(listId)) { if (list != null && list.getValue(GtasksList.REMOTE_ID).equals(listId)) {
return list; return list;
} }

@ -8,6 +8,8 @@ package com.todoroo.astrid.gtasks.sync;
import android.content.Context; import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import com.google.api.services.tasks.model.TaskList;
import com.google.api.services.tasks.model.TaskLists;
import com.google.api.services.tasks.model.Tasks; import com.google.api.services.tasks.model.Tasks;
import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.data.TodorooCursor;
@ -49,7 +51,9 @@ import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import static com.google.common.collect.Lists.newArrayList;
import static org.tasks.date.DateTimeUtils.newDate; import static org.tasks.date.DateTimeUtils.newDate;
import static org.tasks.date.DateTimeUtils.newDateTime;
@Singleton @Singleton
public class GtasksSyncV2Provider { public class GtasksSyncV2Provider {
@ -132,25 +136,44 @@ public class GtasksSyncV2Provider {
public void run() { public void run() {
callback.started(); callback.started();
try {
String authToken = getValidatedAuthToken(); String authToken = getValidatedAuthToken();
final GtasksInvoker invoker = new GtasksInvoker(gtasksTokenValidator, authToken); final GtasksInvoker invoker = new GtasksInvoker(gtasksTokenValidator, authToken);
TaskLists remoteLists = null;
try { try {
gtasksListService.updateLists(invoker.allGtaskLists()); remoteLists = invoker.allGtaskLists();
gtasksListService.updateLists(remoteLists);
} catch (IOException e) { } catch (IOException e) {
handler.handleException("gtasks-sync=io", e); //$NON-NLS-1$ handler.handleException("gtasks-sync=io", e); //$NON-NLS-1$
} }
List<StoreObject> lists = gtasksListService.getLists(); if (remoteLists == null) {
if (lists.isEmpty()) {
finishSync(callback); finishSync(callback);
return; return;
} }
final AtomicInteger finisher = new AtomicInteger(lists.size()); List<StoreObject> listsToUpdate = newArrayList();
for (TaskList remoteList : remoteLists.getItems()) {
StoreObject localList = gtasksListService.getList(remoteList.getId());
String listName = localList.getValue(GtasksList.NAME);
Long lastSync = localList.getValue(GtasksList.LAST_SYNC);
long lastUpdate = remoteList.getUpdated().getValue();
if (lastSync < lastUpdate) {
listsToUpdate.add(localList);
log.debug("{} out of date [local={}] [remote={}]", listName, newDateTime(lastSync), newDateTime(lastUpdate));
} else {
log.debug("{} up to date", listName);
}
}
// TODO: Check timestamps from invoker.allGtaskLists and pare down lists to sync if (listsToUpdate.isEmpty()) {
finishSync(callback);
return;
}
final AtomicInteger finisher = new AtomicInteger(listsToUpdate.size());
for (final StoreObject list : lists) { for (final StoreObject list : listsToUpdate) {
executor.execute(callback, new Runnable() { executor.execute(callback, new Runnable() {
@Override @Override
public void run() { public void run() {
@ -162,6 +185,10 @@ public class GtasksSyncV2Provider {
} }
}); });
} }
} catch(Exception e) {
handler.handleException("gtasks-sync=io", e); //$NON-NLS-1$
callback.finished();
}
} }
}); });
} }

Loading…
Cancel
Save