Handle paginated google task responses

Closes #178
pull/384/head
Alex Baker 9 years ago
parent 2e2fe75159
commit 96b117b6d9

@ -42,7 +42,7 @@ public class GtasksListService {
*
* @param remoteLists remote information about your lists
*/
public synchronized void updateLists(TaskLists remoteLists) {
public synchronized void updateLists(List<TaskList> remoteLists) {
List<GtasksList> lists = getLists();
Set<Long> previousLists = new HashSet<>();
@ -50,9 +50,8 @@ public class GtasksListService {
previousLists.add(list.getId());
}
List<TaskList> items = remoteLists.getItems();
for(int i = 0; i < items.size(); i++) {
com.google.api.services.tasks.model.TaskList remote = items.get(i);
for(int i = 0; i < remoteLists.size(); i++) {
com.google.api.services.tasks.model.TaskList remote = remoteLists.get(i);
String id = remote.getId();
GtasksList local = null;

@ -73,10 +73,11 @@ public class GtasksInvoker {
}
}
public TaskLists allGtaskLists() throws IOException {
public TaskLists allGtaskLists(String pageToken) throws IOException {
return execute(service
.tasklists()
.list());
.list()
.setPageToken(pageToken));
}
public TaskList getGtaskList(String id) throws IOException {
@ -85,12 +86,13 @@ public class GtasksInvoker {
.get(id));
}
public com.google.api.services.tasks.model.Tasks getAllGtasksFromListId(String listId, boolean includeDeleted, boolean includeHidden, long lastSyncDate) throws IOException {
public com.google.api.services.tasks.model.Tasks getAllGtasksFromListId(String listId, boolean includeDeleted, boolean includeHidden, long lastSyncDate, String pageToken) throws IOException {
return execute(service
.tasks()
.list(listId)
.setShowDeleted(includeDeleted)
.setShowHidden(includeHidden)
.setPageToken(pageToken)
.setUpdatedMin(GtasksApiUtilities.unixTimeToGtasksCompletionTime(lastSyncDate).toStringRfc3339()));
}

@ -8,6 +8,7 @@ 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;
@ -39,6 +40,7 @@ import org.tasks.sync.SyncExecutor;
import org.tasks.time.DateTime;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@ -125,8 +127,14 @@ public class GtasksSyncV2Provider {
try {
TaskLists remoteLists = null;
try {
remoteLists = gtasksInvoker.allGtaskLists();
gtasksListService.updateLists(remoteLists);
List<TaskList> gtaskLists = new ArrayList<>();
String nextPageToken = null;
do {
remoteLists = gtasksInvoker.allGtaskLists(nextPageToken);
gtaskLists.addAll(remoteLists.getItems());
nextPageToken = remoteLists.getNextPageToken();
} while (nextPageToken != null);
gtasksListService.updateLists(gtaskLists);
} catch (IOException e) {
handler.handleException(e);
}
@ -239,10 +247,18 @@ public class GtasksSyncV2Provider {
boolean includeDeletedAndHidden = lastSyncDate != 0;
try {
Tasks taskList = invoker.getAllGtasksFromListId(listId, includeDeletedAndHidden,
includeDeletedAndHidden, lastSyncDate + 1000L);
List<com.google.api.services.tasks.model.Task> tasks = taskList.getItems();
if (tasks != null) {
List<com.google.api.services.tasks.model.Task> tasks = new ArrayList<>();
String nextPageToken = null;
do {
Tasks taskList = invoker.getAllGtasksFromListId(listId, includeDeletedAndHidden,
includeDeletedAndHidden, lastSyncDate + 1000L, nextPageToken);
if (taskList != null) {
tasks.addAll(taskList.getItems());
nextPageToken = taskList.getNextPageToken();
}
} while (nextPageToken != null);
if (!tasks.isEmpty()) {
for (com.google.api.services.tasks.model.Task t : tasks) {
GtasksTaskContainer container = new GtasksTaskContainer(t, listId, gtasksMetadataFactory.createEmptyMetadata(AbstractModel.NO_ID));
gtasksMetadataService.findLocalMatch(container);

Loading…
Cancel
Save