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

@ -8,6 +8,8 @@ package com.todoroo.astrid.gtasks.sync;
import android.content.Context;
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.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.TodorooCursor;
@ -49,7 +51,9 @@ import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject;
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.newDateTime;
@Singleton
public class GtasksSyncV2Provider {
@ -132,35 +136,58 @@ public class GtasksSyncV2Provider {
public void run() {
callback.started();
String authToken = getValidatedAuthToken();
final GtasksInvoker invoker = new GtasksInvoker(gtasksTokenValidator, authToken);
try {
gtasksListService.updateLists(invoker.allGtaskLists());
} catch (IOException e) {
handler.handleException("gtasks-sync=io", e); //$NON-NLS-1$
}
List<StoreObject> lists = gtasksListService.getLists();
if (lists.isEmpty()) {
finishSync(callback);
return;
}
final AtomicInteger finisher = new AtomicInteger(lists.size());
// TODO: Check timestamps from invoker.allGtaskLists and pare down lists to sync
for (final StoreObject list : lists) {
executor.execute(callback, new Runnable() {
@Override
public void run() {
synchronizeListHelper(list, invoker, handler);
if (finisher.decrementAndGet() == 0) {
pushUpdated(invoker);
finishSync(callback);
}
String authToken = getValidatedAuthToken();
final GtasksInvoker invoker = new GtasksInvoker(gtasksTokenValidator, authToken);
TaskLists remoteLists = null;
try {
remoteLists = invoker.allGtaskLists();
gtasksListService.updateLists(remoteLists);
} catch (IOException e) {
handler.handleException("gtasks-sync=io", e); //$NON-NLS-1$
}
if (remoteLists == null) {
finishSync(callback);
return;
}
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);
}
});
}
if (listsToUpdate.isEmpty()) {
finishSync(callback);
return;
}
final AtomicInteger finisher = new AtomicInteger(listsToUpdate.size());
for (final StoreObject list : listsToUpdate) {
executor.execute(callback, new Runnable() {
@Override
public void run() {
synchronizeListHelper(list, invoker, handler);
if (finisher.decrementAndGet() == 0) {
pushUpdated(invoker);
finishSync(callback);
}
}
});
}
} catch(Exception e) {
handler.handleException("gtasks-sync=io", e); //$NON-NLS-1$
callback.finished();
}
}
});

Loading…
Cancel
Save