From 72efc18009d8b9b4ce8a917f5d457fec240d3869 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 10 Jun 2019 16:28:50 -0500 Subject: [PATCH] Include Google Task children when moving tasks --- .../java/com/todoroo/astrid/dao/TaskDao.java | 4 +++ .../com/todoroo/astrid/service/TaskMover.java | 33 +++++++++++++++---- .../main/java/org/tasks/data/CaldavDao.java | 3 ++ .../java/org/tasks/data/GoogleTaskDao.java | 9 +++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/dao/TaskDao.java b/app/src/main/java/com/todoroo/astrid/dao/TaskDao.java index ddb427606..b5482842c 100644 --- a/app/src/main/java/com/todoroo/astrid/dao/TaskDao.java +++ b/app/src/main/java/com/todoroo/astrid/dao/TaskDao.java @@ -65,6 +65,10 @@ public abstract class TaskDao { @Query("SELECT * FROM tasks WHERE _id IN (:taskIds)") public abstract List fetch(List taskIds); + @Query( + "SELECT tasks.* FROM tasks LEFT JOIN google_tasks ON gt_task = _id WHERE _id IN (:taskIds) AND gt_parent NOT IN (:taskIds)") + public abstract List fetchExcludingChildren(List taskIds); + @Query("SELECT COUNT(1) FROM tasks WHERE timerStart > 0 AND deleted = 0") public abstract int activeTimers(); diff --git a/app/src/main/java/com/todoroo/astrid/service/TaskMover.java b/app/src/main/java/com/todoroo/astrid/service/TaskMover.java index 77710e9ef..94eb5e97c 100644 --- a/app/src/main/java/com/todoroo/astrid/service/TaskMover.java +++ b/app/src/main/java/com/todoroo/astrid/service/TaskMover.java @@ -1,6 +1,11 @@ package com.todoroo.astrid.service; +import static com.google.common.collect.Iterables.concat; +import static com.google.common.collect.Iterables.transform; +import static com.google.common.collect.Lists.transform; import static com.todoroo.andlib.utility.DateUtilities.now; +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; import com.todoroo.astrid.api.CaldavFilter; import com.todoroo.astrid.api.Filter; @@ -44,7 +49,7 @@ public class TaskMover { } public void move(List tasks, Filter selectedList) { - List fetch = taskDao.fetch(tasks); + List fetch = taskDao.fetchExcludingChildren(tasks); for (Task task : fetch) { performMove(task, selectedList); task.putTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC, true); @@ -76,6 +81,7 @@ public class TaskMover { private void performMove(Task task, Filter selectedList) { long id = task.getId(); GoogleTask googleTask = googleTaskDao.getByTaskId(id); + List googleTaskChildren = emptyList(); if (googleTask != null && selectedList instanceof GtasksFilter && googleTask.getListId().equals(((GtasksFilter) selectedList).getRemoteId())) { @@ -89,8 +95,8 @@ public class TaskMover { } task.putTransitory(SyncFlags.FORCE_SYNC, true); if (googleTask != null) { - googleTask.setDeleted(now()); - googleTaskDao.update(googleTask); + googleTaskChildren = googleTaskDao.getChildren(id); + googleTaskDao.markDeleted(now(), id); } if (caldavTask != null) { @@ -99,12 +105,25 @@ public class TaskMover { } if (selectedList instanceof GtasksFilter) { - googleTaskDao.insertAndShift( - new GoogleTask(id, ((GtasksFilter) selectedList).getRemoteId()), - preferences.addGoogleTasksToTop()); + String listId = ((GtasksFilter) selectedList).getRemoteId(); + googleTaskDao.insertAndShift(new GoogleTask(id, listId), preferences.addGoogleTasksToTop()); + if (!googleTaskChildren.isEmpty()) { + googleTaskDao.insert( + transform( + googleTaskChildren, + child -> { + GoogleTask newChild = new GoogleTask(child.getTask(), listId); + newChild.setOrder(child.getOrder()); + newChild.setParent(id); + return newChild; + })); + } } else if (selectedList instanceof CaldavFilter) { + String listId = ((CaldavFilter) selectedList).getUuid(); caldavDao.insert( - new CaldavTask(id, ((CaldavFilter) selectedList).getUuid(), UUIDHelper.newUUID())); + transform( + concat(singletonList(id), transform(googleTaskChildren, GoogleTask::getId)), + _id -> new CaldavTask(_id, listId, UUIDHelper.newUUID()))); } } } diff --git a/app/src/main/java/org/tasks/data/CaldavDao.java b/app/src/main/java/org/tasks/data/CaldavDao.java index ea302358a..9dc9b7a60 100644 --- a/app/src/main/java/org/tasks/data/CaldavDao.java +++ b/app/src/main/java/org/tasks/data/CaldavDao.java @@ -42,6 +42,9 @@ public interface CaldavDao { @Insert long insert(CaldavTask caldavTask); + @Insert + void insert(Iterable tasks); + @Update void update(CaldavTask caldavTask); diff --git a/app/src/main/java/org/tasks/data/GoogleTaskDao.java b/app/src/main/java/org/tasks/data/GoogleTaskDao.java index 5eccf54ee..af0d7837a 100644 --- a/app/src/main/java/org/tasks/data/GoogleTaskDao.java +++ b/app/src/main/java/org/tasks/data/GoogleTaskDao.java @@ -16,6 +16,9 @@ public abstract class GoogleTaskDao { @Insert public abstract void insert(GoogleTask task); + @Insert + public abstract void insert(Iterable tasks); + @Transaction public void insertAndShift(GoogleTask task, boolean top) { if (top) { @@ -70,6 +73,9 @@ public abstract class GoogleTaskDao { @Update public abstract void update(GoogleTask googleTask); + @Query("UPDATE google_tasks SET gt_deleted = :now WHERE gt_task = :task OR gt_parent = :task") + public abstract void markDeleted(long now, long task); + @Delete public abstract void delete(GoogleTask deleted); @@ -88,6 +94,9 @@ public abstract class GoogleTaskDao { @Query("SELECT gt_task FROM google_tasks WHERE gt_parent IN (:ids)") public abstract List getChildren(List ids); + @Query("SELECT * FROM google_tasks WHERE gt_parent = :id AND gt_deleted = 0") + public abstract List getChildren(Long id); + @Query( "SELECT IFNULL(MAX(gt_order), -1) + 1 FROM google_tasks WHERE gt_list_id = :listId AND gt_parent = :parent") public abstract long getBottom(String listId, long parent);