Use coroutines in TaskDeleter

pull/1051/head
Alex Baker 4 years ago
parent c5503331c1
commit eb38972ac2

@ -305,11 +305,15 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
private fun deleteButtonClick() {
dialogBuilder
.newDialog(R.string.DLG_delete_this_task_question)
.setPositiveButton(android.R.string.ok) { _, _ -> editViewModel.delete() }
.setPositiveButton(android.R.string.ok) { _, _ -> delete() }
.setNegativeButton(android.R.string.cancel, null)
.show()
}
private fun delete() = lifecycleScope.launch {
editViewModel.delete()
}
/*
* ======================================================================
* ========================================== UI component helper classes

@ -45,14 +45,13 @@ import com.todoroo.astrid.service.*
import com.todoroo.astrid.timers.TimerPlugin
import com.todoroo.astrid.utility.Flags
import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.tasks.LocalBroadcastManager
import org.tasks.R
import org.tasks.ShortcutManager
@ -420,12 +419,9 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
}
}
private fun clearCompleted() {
disposables!!.add(
Single.fromCallable { taskDeleter.clearCompleted(filter) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { count: Int -> toaster.longToast(R.string.delete_multiple_tasks_confirmation, count) })
private fun clearCompleted() = lifecycleScope.launch {
val count = taskDeleter.clearCompleted(filter)
toaster.longToast(R.string.delete_multiple_tasks_confirmation, count)
}
@OnClick(R.id.fab)
@ -513,17 +509,15 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
}
}
private fun onTaskDelete(task: Task) {
private suspend fun onTaskDelete(task: Task) {
(activity as MainActivity?)?.taskEditFragment?.let {
if (task.id == it.editViewModel.task?.id) {
it.editViewModel.discard()
}
}
timerPlugin.stopTimer(task)
lifecycleScope.launch {
taskAdapter.onTaskDeleted(task)
loadTaskListContent()
}
taskAdapter.onTaskDeleted(task)
loadTaskListContent()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@ -750,11 +744,13 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
}
}
private fun deleteSelectedItems(tasks: List<Long>) {
private fun deleteSelectedItems(tasks: List<Long>) = lifecycleScope.launch {
finishActionMode()
val result = taskDeleter.markDeleted(tasks)
result.forEach(this::onTaskDelete)
val result = withContext(NonCancellable) {
taskDeleter.markDeleted(tasks)
}
result.forEach { onTaskDelete(it) }
makeSnackbar(R.string.delete_multiple_tasks_confirmation, result.size.toString()).show()
}

@ -1,29 +1,29 @@
package com.todoroo.astrid.service
import com.todoroo.astrid.api.Filter
import com.todoroo.astrid.dao.TaskDaoBlocking
import com.todoroo.astrid.dao.TaskDao
import com.todoroo.astrid.data.Task
import kotlinx.collections.immutable.persistentListOf
import org.tasks.LocalBroadcastManager
import org.tasks.data.*
import org.tasks.db.DbUtils.chunkedMap
import org.tasks.db.QueryUtils
import org.tasks.db.SuspendDbUtils.chunkedMap
import org.tasks.jobs.WorkManager
import org.tasks.preferences.Preferences
import java.util.*
import javax.inject.Inject
class TaskDeleter @Inject constructor(
private val deletionDao: DeletionDaoBlocking,
private val deletionDao: DeletionDao,
private val workManager: WorkManager,
private val taskDao: TaskDaoBlocking,
private val taskDao: TaskDao,
private val localBroadcastManager: LocalBroadcastManager,
private val googleTaskDao: GoogleTaskDaoBlocking,
private val googleTaskDao: GoogleTaskDao,
private val preferences: Preferences) {
fun markDeleted(item: Task) = markDeleted(persistentListOf(item.id))
suspend fun markDeleted(item: Task) = markDeleted(persistentListOf(item.id))
fun markDeleted(taskIds: List<Long>): List<Task> {
suspend fun markDeleted(taskIds: List<Long>): List<Task> {
val ids: MutableSet<Long> = HashSet(taskIds)
ids.addAll(taskIds.chunkedMap(googleTaskDao::getChildren))
ids.addAll(taskIds.chunkedMap(taskDao::getChildren))
@ -34,7 +34,7 @@ class TaskDeleter @Inject constructor(
return ids.chunkedMap(taskDao::fetch)
}
fun clearCompleted(filter: Filter): Int {
suspend fun clearCompleted(filter: Filter): Int {
val deleteFilter = Filter(null, null)
deleteFilter.setFilterQueryOverride(
QueryUtils.removeOrder(QueryUtils.showHiddenAndCompleted(filter.originalSqlQuery)))
@ -45,35 +45,35 @@ class TaskDeleter @Inject constructor(
return completed.size
}
fun delete(task: Task) = delete(task.id)
suspend fun delete(task: Task) = delete(task.id)
fun delete(task: Long) = delete(persistentListOf(task))
suspend fun delete(task: Long) = delete(persistentListOf(task))
fun delete(tasks: List<Long>) {
suspend fun delete(tasks: List<Long>) {
deletionDao.delete(tasks)
workManager.cleanup(tasks)
localBroadcastManager.broadcastRefresh()
}
fun delete(list: GoogleTaskList) {
fun delete(list: GoogleTaskList) = runBlocking {
val tasks = deletionDao.delete(list)
delete(tasks)
localBroadcastManager.broadcastRefreshList()
}
fun delete(list: GoogleTaskAccount) {
fun delete(list: GoogleTaskAccount) = runBlocking {
val tasks = deletionDao.delete(list)
delete(tasks)
localBroadcastManager.broadcastRefreshList()
}
fun delete(list: CaldavCalendar) {
fun delete(list: CaldavCalendar) = runBlocking {
val tasks = deletionDao.delete(list)
delete(tasks)
localBroadcastManager.broadcastRefreshList()
}
fun delete(list: CaldavAccount) {
fun delete(list: CaldavAccount) = runBlocking {
val tasks = deletionDao.delete(list)
delete(tasks)
localBroadcastManager.broadcastRefreshList()

@ -11,7 +11,9 @@ import com.todoroo.andlib.utility.DateUtilities
import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity
import com.todoroo.astrid.service.TaskDeleter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.tasks.R
import org.tasks.Strings.isNullOrEmpty
import org.tasks.caldav.CaldavAccountSettingsActivity
@ -181,8 +183,12 @@ class Synchronization : InjectingPreferenceFragment() {
.newDialog()
.setMessage(R.string.logout_warning, name)
.setPositiveButton(R.string.logout) { _, _ ->
taskDeleter.delete(account)
refresh()
lifecycleScope.launch {
withContext(NonCancellable) {
taskDeleter.delete(account)
}
refresh()
}
}
.setNegativeButton(android.R.string.cancel, null)
.create()

@ -416,8 +416,8 @@ class TaskEditViewModel @ViewModelInject constructor(
return value
}
fun delete() {
task?.let(taskDeleter::markDeleted)
suspend fun delete() {
task?.let { taskDeleter.markDeleted(it) }
discard()
}

Loading…
Cancel
Save