From 7b2334e8a370630ad21621c034ff72f2b343c601 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 5 Nov 2019 12:14:20 -0600 Subject: [PATCH] Add TaskCompleter --- .../java/com/todoroo/astrid/dao/TaskDao.java | 20 +------ .../todoroo/astrid/service/TaskCompleter.java | 52 +++++++++++++++++++ .../astrid/ui/EditTitleControlSet.java | 6 +-- .../java/org/tasks/data/GoogleTaskDao.java | 4 ++ .../tasks/receivers/CompleteTaskReceiver.java | 15 ++---- .../java/org/tasks/tasklist/ViewHolder.java | 10 ++-- .../org/tasks/tasklist/ViewHolderFactory.java | 10 ++-- .../org/tasks/widget/WidgetClickActivity.java | 6 +-- 8 files changed, 76 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/com/todoroo/astrid/service/TaskCompleter.java 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 12fa8cdef..64856460c 100644 --- a/app/src/main/java/com/todoroo/astrid/dao/TaskDao.java +++ b/app/src/main/java/com/todoroo/astrid/dao/TaskDao.java @@ -6,7 +6,6 @@ package com.todoroo.astrid.dao; -import static com.google.common.collect.Lists.newArrayList; import static com.todoroo.andlib.utility.DateUtilities.now; import android.database.Cursor; @@ -194,23 +193,6 @@ public abstract class TaskDao { + "LIMIT 100") public abstract List getAstrid2TaskProviderTasks(); - /** Mark the given task as completed and save it. */ - public void setComplete(Task item, boolean completed) { - List tasks = newArrayList(item); - tasks.addAll(getChildren(item.getId())); - setComplete(tasks, completed ? now() : 0L); - } - - private void setComplete(Iterable tasks, long completionDate) { - for (Task task : tasks) { - task.setCompletionDate(completionDate); - save(task); - } - } - - @Query("SELECT tasks.* FROM tasks JOIN google_tasks ON tasks._id = gt_task WHERE gt_parent = :taskId") - abstract List getChildren(long taskId); - public int count(Filter filter) { Cursor cursor = getCursor(filter.sqlQuery); try { @@ -237,7 +219,7 @@ public abstract class TaskDao { } } - public Cursor getCursor(String queryTemplate) { + private Cursor getCursor(String queryTemplate) { return getCursor(queryTemplate, Task.PROPERTIES); } diff --git a/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.java b/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.java new file mode 100644 index 000000000..601e8baae --- /dev/null +++ b/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.java @@ -0,0 +1,52 @@ +package com.todoroo.astrid.service; + +import static com.todoroo.andlib.utility.DateUtilities.now; +import static java.util.Collections.singletonList; + +import com.todoroo.astrid.dao.TaskDao; +import com.todoroo.astrid.data.Task; +import java.util.List; +import javax.inject.Inject; +import org.tasks.data.CaldavDao; +import org.tasks.data.GoogleTaskDao; +import timber.log.Timber; + +public class TaskCompleter { + + private final TaskDao taskDao; + private final GoogleTaskDao googleTaskDao; + private final CaldavDao caldavDao; + + @Inject + TaskCompleter(TaskDao taskDao, GoogleTaskDao googleTaskDao, CaldavDao caldavDao) { + this.taskDao = taskDao; + this.googleTaskDao = googleTaskDao; + this.caldavDao = caldavDao; + } + + public void setComplete(long taskId) { + Task task = taskDao.fetch(taskId); + if (task != null) { + setComplete(task, true); + } else { + Timber.e("Could not find task with id %s", taskId); + } + } + + public void setComplete(Task item, boolean completed) { + long completionDate = completed ? now() : 0L; + setComplete(singletonList(item), completionDate); + setComplete(googleTaskDao.getChildTasks(item.getId()), completionDate); + List caldavChildren = caldavDao.getChildren(singletonList(item.getId())); + if (!caldavChildren.isEmpty()) { + setComplete(taskDao.fetch(caldavChildren), completionDate); + } + } + + private void setComplete(List tasks, long completionDate) { + for (Task task : tasks) { + task.setCompletionDate(completionDate); + taskDao.save(task); + } + } +} diff --git a/app/src/main/java/com/todoroo/astrid/ui/EditTitleControlSet.java b/app/src/main/java/com/todoroo/astrid/ui/EditTitleControlSet.java index 8f2b67d2d..1987968e8 100644 --- a/app/src/main/java/com/todoroo/astrid/ui/EditTitleControlSet.java +++ b/app/src/main/java/com/todoroo/astrid/ui/EditTitleControlSet.java @@ -21,8 +21,8 @@ import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.OnTextChanged; import com.google.common.base.Strings; -import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.service.TaskCompleter; import javax.inject.Inject; import org.tasks.R; import org.tasks.dialogs.Linkify; @@ -46,7 +46,7 @@ public class EditTitleControlSet extends TaskEditControlFragment { private static final String EXTRA_PRIORITY = "extra_priority"; @Inject @ForApplication Context context; - @Inject TaskDao taskDao; + @Inject TaskCompleter taskCompleter; @Inject Linkify linkify; @BindView(R.id.title) @@ -177,7 +177,7 @@ public class EditTitleControlSet extends TaskEditControlFragment { public void apply(Task task) { task.setTitle(Strings.isNullOrEmpty(title) ? getString(R.string.no_title) : title); if (isComplete != task.isCompleted()) { - taskDao.setComplete(task, isComplete); + taskCompleter.setComplete(task, isComplete); } } } diff --git a/app/src/main/java/org/tasks/data/GoogleTaskDao.java b/app/src/main/java/org/tasks/data/GoogleTaskDao.java index af0d7837a..f3a5d4b8b 100644 --- a/app/src/main/java/org/tasks/data/GoogleTaskDao.java +++ b/app/src/main/java/org/tasks/data/GoogleTaskDao.java @@ -7,6 +7,7 @@ import androidx.room.Query; import androidx.room.RoomWarnings; import androidx.room.Transaction; import androidx.room.Update; +import com.todoroo.astrid.data.Task; import java.util.List; import timber.log.Timber; @@ -94,6 +95,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 tasks.* FROM tasks JOIN google_tasks ON tasks._id = gt_task WHERE gt_parent = :taskId") + public abstract List getChildTasks(long taskId); + @Query("SELECT * FROM google_tasks WHERE gt_parent = :id AND gt_deleted = 0") public abstract List getChildren(Long id); diff --git a/app/src/main/java/org/tasks/receivers/CompleteTaskReceiver.java b/app/src/main/java/org/tasks/receivers/CompleteTaskReceiver.java index 9c5e5917a..14bbbd353 100644 --- a/app/src/main/java/org/tasks/receivers/CompleteTaskReceiver.java +++ b/app/src/main/java/org/tasks/receivers/CompleteTaskReceiver.java @@ -2,8 +2,7 @@ package org.tasks.receivers; import android.content.Context; import android.content.Intent; -import com.todoroo.astrid.dao.TaskDao; -import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.service.TaskCompleter; import io.reactivex.Completable; import io.reactivex.schedulers.Schedulers; import javax.inject.Inject; @@ -15,7 +14,7 @@ public class CompleteTaskReceiver extends InjectingBroadcastReceiver { public static final String TASK_ID = "id"; - @Inject TaskDao taskDao; + @Inject TaskCompleter taskCompleter; @Override public void onReceive(Context context, Intent intent) { @@ -23,15 +22,7 @@ public class CompleteTaskReceiver extends InjectingBroadcastReceiver { long taskId = intent.getLongExtra(TASK_ID, 0); Timber.i("Completing %s", taskId); - Completable.fromAction( - () -> { - Task task = taskDao.fetch(taskId); - if (task != null) { - taskDao.setComplete(task, true); - } else { - Timber.e("Could not find task with id %s", taskId); - } - }) + Completable.fromAction(() -> taskCompleter.setComplete(taskId)) .subscribeOn(Schedulers.io()) .subscribe(); } diff --git a/app/src/main/java/org/tasks/tasklist/ViewHolder.java b/app/src/main/java/org/tasks/tasklist/ViewHolder.java index 36b5da49b..99ef5352e 100644 --- a/app/src/main/java/org/tasks/tasklist/ViewHolder.java +++ b/app/src/main/java/org/tasks/tasklist/ViewHolder.java @@ -26,7 +26,7 @@ import com.google.android.material.chip.Chip; import com.google.android.material.chip.ChipGroup; import com.google.common.collect.Lists; import com.todoroo.astrid.api.Filter; -import com.todoroo.astrid.dao.TaskDao; +import com.todoroo.astrid.service.TaskCompleter; import com.todoroo.astrid.ui.CheckableImageView; import java.util.List; import org.tasks.R; @@ -43,7 +43,7 @@ public class ViewHolder extends RecyclerView.ViewHolder { private final Activity context; private final Preferences preferences; private final int textColorSecondary; - private final TaskDao taskDao; + private final TaskCompleter taskCompleter; private final ViewHolderCallbacks callback; private final DisplayMetrics metrics; private final int background; @@ -97,7 +97,7 @@ public class ViewHolder extends RecyclerView.ViewHolder { ChipProvider chipProvider, int textColorOverdue, int textColorSecondary, - TaskDao taskDao, + TaskCompleter taskCompleter, ViewHolderCallbacks callback, DisplayMetrics metrics, int background, @@ -110,7 +110,7 @@ public class ViewHolder extends RecyclerView.ViewHolder { this.chipProvider = chipProvider; this.textColorOverdue = textColorOverdue; this.textColorSecondary = textColorSecondary; - this.taskDao = taskDao; + this.taskCompleter = taskCompleter; this.callback = callback; this.metrics = metrics; this.background = background; @@ -313,7 +313,7 @@ public class ViewHolder extends RecyclerView.ViewHolder { if (newState != task.isCompleted()) { callback.onCompletedTask(task, newState); - taskDao.setComplete(task.getTask(), newState); + taskCompleter.setComplete(task.getTask(), newState); } // set check box to actual action item state diff --git a/app/src/main/java/org/tasks/tasklist/ViewHolderFactory.java b/app/src/main/java/org/tasks/tasklist/ViewHolderFactory.java index ee57a3881..062d569c9 100644 --- a/app/src/main/java/org/tasks/tasklist/ViewHolderFactory.java +++ b/app/src/main/java/org/tasks/tasklist/ViewHolderFactory.java @@ -10,7 +10,7 @@ import android.content.Context; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.ViewGroup; -import com.todoroo.astrid.dao.TaskDao; +import com.todoroo.astrid.service.TaskCompleter; import javax.inject.Inject; import org.tasks.R; import org.tasks.dialogs.Linkify; @@ -27,7 +27,7 @@ public class ViewHolderFactory { private final Locale locale; private final ChipProvider chipProvider; private final int fontSize; - private final TaskDao taskDao; + private final TaskCompleter taskCompleter; private final DisplayMetrics metrics; private final int background; private final int selectedColor; @@ -41,12 +41,12 @@ public class ViewHolderFactory { Locale locale, Preferences preferences, ChipProvider chipProvider, - TaskDao taskDao, + TaskCompleter taskCompleter, Linkify linkify) { this.context = context; this.locale = locale; this.chipProvider = chipProvider; - this.taskDao = taskDao; + this.taskCompleter = taskCompleter; this.preferences = preferences; this.linkify = linkify; textColorSecondary = getData(context, android.R.attr.textColorSecondary); @@ -69,7 +69,7 @@ public class ViewHolderFactory { chipProvider, textColorOverdue, textColorSecondary, - taskDao, + taskCompleter, callbacks, metrics, background, diff --git a/app/src/main/java/org/tasks/widget/WidgetClickActivity.java b/app/src/main/java/org/tasks/widget/WidgetClickActivity.java index a2a136898..344bac573 100644 --- a/app/src/main/java/org/tasks/widget/WidgetClickActivity.java +++ b/app/src/main/java/org/tasks/widget/WidgetClickActivity.java @@ -3,8 +3,8 @@ package org.tasks.widget; import android.content.Intent; import android.os.Bundle; import com.google.common.base.Strings; -import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.service.TaskCompleter; import javax.inject.Inject; import org.tasks.injection.ActivityComponent; import org.tasks.injection.InjectingAppCompatActivity; @@ -17,7 +17,7 @@ public class WidgetClickActivity extends InjectingAppCompatActivity { public static final String EXTRA_FILTER = "extra_filter"; public static final String EXTRA_TASK = "extra_task"; // $NON-NLS-1$ - @Inject TaskDao taskDao; + @Inject TaskCompleter taskCompleter; @Override protected void onCreate(Bundle savedInstanceState) { @@ -35,7 +35,7 @@ public class WidgetClickActivity extends InjectingAppCompatActivity { switch (action) { case COMPLETE_TASK: - taskDao.setComplete(task, !task.isCompleted()); + taskCompleter.setComplete(task, !task.isCompleted()); break; case EDIT_TASK: startActivity(