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 * @param remoteLists remote information about your lists
*/ */
public synchronized void updateLists(TaskLists remoteLists) { public synchronized void updateLists(List<TaskList> remoteLists) {
List<GtasksList> lists = getLists(); List<GtasksList> lists = getLists();
Set<Long> previousLists = new HashSet<>(); Set<Long> previousLists = new HashSet<>();
@ -50,9 +50,8 @@ public class GtasksListService {
previousLists.add(list.getId()); previousLists.add(list.getId());
} }
List<TaskList> items = remoteLists.getItems(); for(int i = 0; i < remoteLists.size(); i++) {
for(int i = 0; i < items.size(); i++) { com.google.api.services.tasks.model.TaskList remote = remoteLists.get(i);
com.google.api.services.tasks.model.TaskList remote = items.get(i);
String id = remote.getId(); String id = remote.getId();
GtasksList local = null; 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 return execute(service
.tasklists() .tasklists()
.list()); .list()
.setPageToken(pageToken));
} }
public TaskList getGtaskList(String id) throws IOException { public TaskList getGtaskList(String id) throws IOException {
@ -85,12 +86,13 @@ public class GtasksInvoker {
.get(id)); .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 return execute(service
.tasks() .tasks()
.list(listId) .list(listId)
.setShowDeleted(includeDeleted) .setShowDeleted(includeDeleted)
.setShowHidden(includeHidden) .setShowHidden(includeHidden)
.setPageToken(pageToken)
.setUpdatedMin(GtasksApiUtilities.unixTimeToGtasksCompletionTime(lastSyncDate).toStringRfc3339())); .setUpdatedMin(GtasksApiUtilities.unixTimeToGtasksCompletionTime(lastSyncDate).toStringRfc3339()));
} }

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

Loading…
Cancel
Save