From f4ad55b42d5fc1a49e19fc06c677d097bdea9a27 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 7 Jul 2020 14:49:56 -0500 Subject: [PATCH] Use coroutines in task completer --- .../astrid/activity/TaskListFragment.kt | 14 +++++------ .../java/com/todoroo/astrid/dao/TaskDao.kt | 8 +------ .../com/todoroo/astrid/dao/TaskDaoBlocking.kt | 2 +- .../todoroo/astrid/service/TaskCompleter.kt | 24 +++++++++++-------- .../tasks/receivers/CompleteTaskReceiver.java | 2 +- .../java/org/tasks/tasklist/TaskViewHolder.kt | 3 --- .../org/tasks/tasklist/ViewHolderFactory.kt | 3 --- .../java/org/tasks/ui/SubtaskControlSet.kt | 4 +++- .../org/tasks/widget/WidgetClickActivity.kt | 4 +++- 9 files changed, 30 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt index 53bb81092..8cf11afef 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -20,7 +20,6 @@ import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.Toolbar import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.fragment.app.Fragment -import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope @@ -41,10 +40,7 @@ import com.todoroo.astrid.api.* import com.todoroo.astrid.core.BuiltInFilterExposer import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.data.Task -import com.todoroo.astrid.service.TaskCreator -import com.todoroo.astrid.service.TaskDeleter -import com.todoroo.astrid.service.TaskDuplicator -import com.todoroo.astrid.service.TaskMover +import com.todoroo.astrid.service.* import com.todoroo.astrid.timers.TimerPlugin import com.todoroo.astrid.utility.Flags import dagger.hilt.android.AndroidEntryPoint @@ -111,6 +107,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL @Inject lateinit var colorProvider: ColorProvider @Inject lateinit var notificationManager: NotificationManager @Inject lateinit var shortcutManager: ShortcutManager + @Inject lateinit var taskCompleter: TaskCompleter @BindView(R.id.swipe_layout) lateinit var swipeRefreshLayout: SwipeRefreshLayout @@ -753,8 +750,11 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL fun clearCollapsed() = taskAdapter.clearCollapsed() override fun onCompletedTask(task: TaskContainer, newState: Boolean) { - taskAdapter.onCompletedTask(task, newState) - loadTaskListContent() + lifecycleScope.launch { + taskCompleter.setComplete(task.getTask(), newState) + taskAdapter.onCompletedTask(task, newState) + loadTaskListContent() + } } override fun onClick(taskViewHolder: TaskViewHolder) { diff --git a/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt b/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt index 39480da58..78304e480 100644 --- a/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt +++ b/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt @@ -11,14 +11,12 @@ import androidx.sqlite.db.SimpleSQLiteQuery import com.todoroo.andlib.sql.Criterion import com.todoroo.andlib.sql.Field import com.todoroo.andlib.sql.Functions -import com.todoroo.andlib.utility.AndroidUtilities.assertNotMainThread import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.api.Filter import com.todoroo.astrid.api.PermaSql import com.todoroo.astrid.data.Task import com.todoroo.astrid.data.Task.Companion.NO_ID import com.todoroo.astrid.helper.UUIDHelper -import kotlinx.coroutines.runBlocking import org.tasks.BuildConfig import org.tasks.data.Place import org.tasks.data.SubtaskInfo @@ -42,10 +40,6 @@ abstract class TaskDao(private val database: Database) { @Query("SELECT * FROM tasks WHERE completed = 0 AND deleted = 0 AND (hideUntil > :now OR dueDate > :now)") internal abstract suspend fun needsRefresh(now: Long = DateUtilities.now()): List - suspend fun fetchBlocking(id: Long) = runBlocking { - fetch(id) - } - @Query("SELECT * FROM tasks WHERE _id = :id LIMIT 1") abstract suspend fun fetch(id: Long): Task? @@ -218,7 +212,7 @@ SELECT EXISTS(SELECT 1 FROM tasks WHERE parent > 0 AND deleted = 0) AS hasSubtas * success. */ - suspend fun save(task: Task) = save(task, fetchBlocking(task.id)) + suspend fun save(task: Task) = save(task, fetch(task.id)) suspend fun save(task: Task, original: Task?) { if (!task.insignificantChange(original)) { diff --git a/app/src/main/java/com/todoroo/astrid/dao/TaskDaoBlocking.kt b/app/src/main/java/com/todoroo/astrid/dao/TaskDaoBlocking.kt index 512a4d603..a7fbdc912 100644 --- a/app/src/main/java/com/todoroo/astrid/dao/TaskDaoBlocking.kt +++ b/app/src/main/java/com/todoroo/astrid/dao/TaskDaoBlocking.kt @@ -22,7 +22,7 @@ class TaskDaoBlocking @Inject constructor(private val dao: TaskDao) { } fun fetchBlocking(id: Long) = runBlocking { - dao.fetchBlocking(id) + dao.fetch(id) } fun fetch(id: Long): Task? = runBlocking { diff --git a/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.kt b/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.kt index 8786a38ac..dce42a6de 100644 --- a/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.kt +++ b/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.kt @@ -1,31 +1,35 @@ package com.todoroo.astrid.service import com.todoroo.andlib.utility.DateUtilities -import com.todoroo.astrid.dao.TaskDaoBlocking +import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.data.Task -import org.tasks.data.GoogleTaskDaoBlocking +import kotlinx.coroutines.runBlocking +import org.tasks.data.GoogleTaskDao import timber.log.Timber import javax.inject.Inject class TaskCompleter @Inject internal constructor( - private val taskDao: TaskDaoBlocking, - private val googleTaskDao: GoogleTaskDaoBlocking) { + private val taskDao: TaskDao, + private val googleTaskDao: GoogleTaskDao) { - fun setComplete(taskId: Long) = - taskDao.fetchBlocking(taskId)?.let { setComplete(it, true) } - ?: Timber.e("Could not find task $taskId") + fun setCompleteBlocking(taskId: Long) = runBlocking { + taskDao.fetch(taskId)?.let { setComplete(it, true) } + ?: Timber.e("Could not find task $taskId") + } - fun setComplete(item: Task, completed: Boolean) { + suspend fun setComplete(item: Task, completed: Boolean) { val completionDate = if (completed) DateUtilities.now() else 0L setComplete(listOf(item), completionDate) val tasks = googleTaskDao.getChildTasks(item.id) - .plus(taskDao.getChildren(item.id).takeIf { it.isNotEmpty() }?.let(taskDao::fetch) + .plus(taskDao.getChildren(item.id) + .takeIf { it.isNotEmpty() } + ?.let { taskDao.fetch(it) } ?: emptyList()) .filter { it.isCompleted != completed } setComplete(tasks, completionDate) } - private fun setComplete(tasks: List, completionDate: Long) { + private suspend fun setComplete(tasks: List, completionDate: Long) { tasks.forEachIndexed { i, task -> task.completionDate = completionDate if (i < tasks.size - 1) { diff --git a/app/src/main/java/org/tasks/receivers/CompleteTaskReceiver.java b/app/src/main/java/org/tasks/receivers/CompleteTaskReceiver.java index 37071bd07..c5620bf62 100644 --- a/app/src/main/java/org/tasks/receivers/CompleteTaskReceiver.java +++ b/app/src/main/java/org/tasks/receivers/CompleteTaskReceiver.java @@ -23,7 +23,7 @@ public class CompleteTaskReceiver extends InjectingBroadcastReceiver { long taskId = intent.getLongExtra(TASK_ID, 0); Timber.i("Completing %s", taskId); - Completable.fromAction(() -> taskCompleter.setComplete(taskId)) + Completable.fromAction(() -> taskCompleter.setCompleteBlocking(taskId)) .subscribeOn(Schedulers.io()) .subscribe(); } diff --git a/app/src/main/java/org/tasks/tasklist/TaskViewHolder.kt b/app/src/main/java/org/tasks/tasklist/TaskViewHolder.kt index dff5f93e3..2880a80dd 100644 --- a/app/src/main/java/org/tasks/tasklist/TaskViewHolder.kt +++ b/app/src/main/java/org/tasks/tasklist/TaskViewHolder.kt @@ -16,7 +16,6 @@ import com.google.android.material.chip.ChipGroup import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.api.Filter import com.todoroo.astrid.core.SortHelper -import com.todoroo.astrid.service.TaskCompleter import com.todoroo.astrid.ui.CheckableImageView import org.tasks.R import org.tasks.data.TaskContainer @@ -39,7 +38,6 @@ class TaskViewHolder internal constructor( private val checkBoxProvider: CheckBoxProvider, private val textColorOverdue: Int, private val textColorSecondary: Int, - private val taskCompleter: TaskCompleter, private val callback: ViewHolderCallbacks, private val metrics: DisplayMetrics, private val background: Int, @@ -240,7 +238,6 @@ class TaskViewHolder internal constructor( fun onCompleteBoxClick() { val newState = completeBox.isChecked if (newState != task.isCompleted) { - taskCompleter.setComplete(task.getTask(), newState) callback.onCompletedTask(task, newState) } diff --git a/app/src/main/java/org/tasks/tasklist/ViewHolderFactory.kt b/app/src/main/java/org/tasks/tasklist/ViewHolderFactory.kt index f186d7726..fd8beb45e 100644 --- a/app/src/main/java/org/tasks/tasklist/ViewHolderFactory.kt +++ b/app/src/main/java/org/tasks/tasklist/ViewHolderFactory.kt @@ -6,7 +6,6 @@ import android.util.DisplayMetrics import android.view.LayoutInflater import android.view.ViewGroup import com.todoroo.andlib.utility.AndroidUtilities -import com.todoroo.astrid.service.TaskCompleter import dagger.hilt.android.qualifiers.ActivityContext import org.tasks.R import org.tasks.dialogs.Linkify @@ -23,7 +22,6 @@ class ViewHolderFactory @Inject constructor( private val preferences: Preferences, private val chipProvider: ChipProvider, private val checkBoxProvider: CheckBoxProvider, - private val taskCompleter: TaskCompleter, private val linkify: Linkify, private val locale: Locale) { private val textColorSecondary: Int = ResourceResolver.getData(context, android.R.attr.textColorSecondary) @@ -51,7 +49,6 @@ class ViewHolderFactory @Inject constructor( checkBoxProvider, textColorOverdue, textColorSecondary, - taskCompleter, callbacks, metrics, background, diff --git a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt index 24a076572..ba88a6a56 100644 --- a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt +++ b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt @@ -207,7 +207,9 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks } override fun complete(task: Task, completed: Boolean) { - taskCompleter.setComplete(task, completed) + lifecycleScope.launch { + taskCompleter.setComplete(task, completed) + } } private inner class RefreshReceiver : BroadcastReceiver() { diff --git a/app/src/main/java/org/tasks/widget/WidgetClickActivity.kt b/app/src/main/java/org/tasks/widget/WidgetClickActivity.kt index 83522c3a4..f81d48c69 100644 --- a/app/src/main/java/org/tasks/widget/WidgetClickActivity.kt +++ b/app/src/main/java/org/tasks/widget/WidgetClickActivity.kt @@ -34,7 +34,9 @@ class WidgetClickActivity : InjectingAppCompatActivity(), OnDismissHandler { val task: Task = intent.getParcelableExtra(EXTRA_TASK)!! when (action) { COMPLETE_TASK -> { - taskCompleter.setComplete(task, !task.isCompleted) + lifecycleScope.launch(NonCancellable) { + taskCompleter.setComplete(task, !task.isCompleted) + } finish() } EDIT_TASK -> {