Add TaskCompleter

pull/848/head^2
Alex Baker 5 years ago
parent c5e4825822
commit 7b2334e8a3

@ -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<Task> getAstrid2TaskProviderTasks();
/** Mark the given task as completed and save it. */
public void setComplete(Task item, boolean completed) {
List<Task> tasks = newArrayList(item);
tasks.addAll(getChildren(item.getId()));
setComplete(tasks, completed ? now() : 0L);
}
private void setComplete(Iterable<Task> 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<Task> 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);
}

@ -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<Long> caldavChildren = caldavDao.getChildren(singletonList(item.getId()));
if (!caldavChildren.isEmpty()) {
setComplete(taskDao.fetch(caldavChildren), completionDate);
}
}
private void setComplete(List<Task> tasks, long completionDate) {
for (Task task : tasks) {
task.setCompletionDate(completionDate);
taskDao.save(task);
}
}
}

@ -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);
}
}
}

@ -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<Long> getChildren(List<Long> ids);
@Query("SELECT tasks.* FROM tasks JOIN google_tasks ON tasks._id = gt_task WHERE gt_parent = :taskId")
public abstract List<Task> getChildTasks(long taskId);
@Query("SELECT * FROM google_tasks WHERE gt_parent = :id AND gt_deleted = 0")
public abstract List<GoogleTask> getChildren(Long id);

@ -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();
}

@ -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

@ -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,

@ -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(

Loading…
Cancel
Save