From f2e17386ef8d471ec51e265786a179075a2fb540 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Fri, 8 Jul 2011 14:36:29 -0700 Subject: [PATCH] Fixed bugs related to task creation with order --- .../astrid/gtasks/api/CreateRequest.java | 26 +++++++++- .../astrid/gtasks/api/GtasksService.java | 13 ++++- .../gtasks/sync/GtasksSyncProvider.java | 52 +++++++++++++++---- 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/api/CreateRequest.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/api/CreateRequest.java index 5ae93f158..27e0cbc0f 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/api/CreateRequest.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/api/CreateRequest.java @@ -10,13 +10,35 @@ import com.google.api.services.tasks.v1.model.Task; */ public class CreateRequest extends PushRequest { - public CreateRequest(GtasksService service, String listId, Task toUpdate) { + private String parent; + private String priorSiblingId; + + public CreateRequest(GtasksService service, String listId, Task toUpdate, String parent, String priorSiblingId) { super(service, listId, toUpdate); + this.parent = parent; + this.priorSiblingId = priorSiblingId; } @Override public Task executePush() throws IOException { - return service.createGtask(listId, toPush); + return service.createGtask(listId, toPush, parent, priorSiblingId); + } + + public String getParent() { + return parent; + } + + public void setParent(String parent) { + this.parent = parent; } + public String getPriorSiblingId() { + return priorSiblingId; + } + + public void setPriorSiblingId(String priorSiblingId) { + this.priorSiblingId = priorSiblingId; + } + + } diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/api/GtasksService.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/api/GtasksService.java index b89a150d8..affb21e85 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/api/GtasksService.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/api/GtasksService.java @@ -6,6 +6,7 @@ import com.google.api.client.extensions.android2.AndroidHttp; import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource; import com.google.api.client.json.jackson.JacksonFactory; import com.google.api.services.tasks.v1.Tasks; +import com.google.api.services.tasks.v1.Tasks.TasksOperations.Insert; import com.google.api.services.tasks.v1.Tasks.TasksOperations.List; import com.google.api.services.tasks.v1.Tasks.TasksOperations.Move; import com.google.api.services.tasks.v1.model.Task; @@ -135,11 +136,19 @@ public class GtasksService { } public Task createGtask(String listId, Task task) throws IOException { + return createGtask(listId, task, null, null); + } + + public Task createGtask(String listId, Task task, String parent, String priorSiblingId) throws IOException { + Insert insertOp = service.tasks.insert(listId, task); + insertOp.parent = parent; + insertOp.previous = priorSiblingId; + Task toReturn; try { - toReturn = service.tasks.insert(listId, task).execute(); + toReturn = insertOp.execute(); } catch (IOException e) { - toReturn = service.tasks.insert(listId, task).execute(); + toReturn = insertOp.execute(); } return toReturn; } diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncProvider.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncProvider.java index 33009e50c..b9d03defe 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncProvider.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncProvider.java @@ -80,6 +80,7 @@ public class GtasksSyncProvider extends SyncProvider { /** tasks to read id for */ ArrayList createdWithoutId; + ArrayList createdWithoutParent; Semaphore pushedTaskSemaphore = new Semaphore(0); AtomicInteger pushedTaskCount = new AtomicInteger(0); @@ -160,6 +161,7 @@ public class GtasksSyncProvider extends SyncProvider { if(Constants.DEBUG) Log.e("gtasks-debug", "- -------- SYNC STARTED"); createdWithoutId = new ArrayList(); + createdWithoutParent = new ArrayList(); try { TaskLists allTaskLists = taskService.allGtaskLists(); @@ -229,22 +231,27 @@ public class GtasksSyncProvider extends SyncProvider { return; } + // first, pull all tasks. then we can write them // include deleted tasks so we can delete them in astrid data.remoteUpdated = readAllRemoteTasks(true); // match remote tasks to locally created tasks HashMap locals = new HashMap(); + HashMap localIdsToRemoteIds = new HashMap(); for(GtasksTaskContainer task : createdWithoutId) { - locals.put(task.task.getValue(Task.TITLE), task); + locals.put(task.gtaskMetadata.getValue(GtasksMetadata.ID), task); + localIdsToRemoteIds.put(task.task.getId(), task.gtaskMetadata.getValue(GtasksMetadata.ID)); } + verifyCreatedOrder(locals, localIdsToRemoteIds); + for(GtasksTaskContainer remote : data.remoteUpdated) { if(remote.task.getId() < 1) { - GtasksTaskContainer local = locals.get(remote.task.getValue(Task.TITLE)); + GtasksTaskContainer local = locals.get(remote.gtaskMetadata.getValue(GtasksMetadata.ID)); if(local != null) { if(Constants.DEBUG) - Log.e("gtasks-debug", "FOUND LOCAL - " + remote.task.getValue(Task.TITLE)); + Log.e("gtasks-debug", "FOUND LOCAL - " + remote.task.getId()); remote.task.setId(local.task.getId()); } } @@ -253,6 +260,20 @@ public class GtasksSyncProvider extends SyncProvider { super.readRemotelyUpdated(data); } + private void verifyCreatedOrder(HashMap locals, + HashMap localIdsToRemoteIds) throws IOException { + for (GtasksTaskContainer t : createdWithoutParent) { + String toMove = t.gtaskMetadata.getValue(GtasksMetadata.ID); + String listId = t.gtaskMetadata.getValue(GtasksMetadata.LIST_ID); + long parentTask = t.gtaskMetadata.getValue(GtasksMetadata.PARENT_TASK); + if (parentTask > 0) { + String remoteParent = localIdsToRemoteIds.get(parentTask); + MoveRequest move = new MoveRequest(taskService, toMove, listId, remoteParent, null); + move.executePush(); + } + } + } + // ---------------------------------------------------------------------- // ------------------------------------------------------------ sync data @@ -395,10 +416,12 @@ public class GtasksSyncProvider extends SyncProvider { local.gtaskMetadata.setValue(GtasksMetadata.LIST_ID, listId); createdWithoutId.add(local); + if (local.gtaskMetadata.containsNonNullValue(GtasksMetadata.PARENT_TASK)) { + createdWithoutParent.add(local); + } com.google.api.services.tasks.v1.model.Task createdTask = new com.google.api.services.tasks.v1.model.Task(); - createdTask.parent = local.parentId; - CreateRequest createRequest = new CreateRequest(taskService, listId, createdTask); + CreateRequest createRequest = new CreateRequest(taskService, listId, createdTask, local.parentId, local.priorSiblingId); updateTaskHelper(local, null, createRequest); return local; }//*/ @@ -443,16 +466,23 @@ public class GtasksSyncProvider extends SyncProvider { new Thread(new Runnable() { @Override public void run() { + String newIdTask = idTask; try { - if(!TextUtils.isEmpty(idTask) && (remote == null || local.parentId != remote.parentId || + if (request instanceof CreateRequest) { + com.google.api.services.tasks.v1.model.Task createResult = request.executePush(); + newIdTask = createResult.id; + local.gtaskMetadata.setValue(GtasksMetadata.ID, newIdTask); + } + if(!TextUtils.isEmpty(newIdTask) && (remote == null || local.parentId != remote.parentId || local.priorSiblingId != remote.priorSiblingId)) { if(Constants.DEBUG) - Log.e("gtasks-debug", "ACTION: move(1) - " + idTask + ", " + local.parentId + ", " + local.priorSiblingId); + Log.e("gtasks-debug", "ACTION: move(1) - " + newIdTask + ", " + local.parentId + ", " + local.priorSiblingId); //This case basically defaults to whatever local settings are. Future versions could try and merge better - MoveRequest moveRequest = new MoveRequest(taskService, idTask, idList, local.parentId, local.priorSiblingId); + MoveRequest moveRequest = new MoveRequest(taskService, newIdTask, idList, local.parentId, local.priorSiblingId); moveRequest.executePush(); - } else { + } + if (request instanceof UpdateRequest) { request.executePush(); } @@ -460,9 +490,9 @@ public class GtasksSyncProvider extends SyncProvider { if(remote != null && !idList.equals(remote.gtaskMetadata.getValue( GtasksMetadata.LIST_ID))) { if(Constants.DEBUG) - Log.e("gtasks-debug", "ACTION: moveTask(5), " + idTask + ", " + idList + " to " + + Log.e("gtasks-debug", "ACTION: moveTask(5), " + newIdTask + ", " + idList + " to " + remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID)); - MoveListRequest moveList = new MoveListRequest(taskService, idTask, remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID), idList, null); + MoveListRequest moveList = new MoveListRequest(taskService, newIdTask, remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID), idList, null); com.google.api.services.tasks.v1.model.Task result = moveList.executePush(); local.gtaskMetadata.setValue(GtasksMetadata.ID, result.id); remote.gtaskMetadata.setValue(GtasksMetadata.ID, result.id);