From 9af9dd41157f0dd93ce6a961f274fbc4e79b140c Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 31 Jul 2020 14:26:33 -0500 Subject: [PATCH] Launch coroutine in cleared method --- .../tasklist/DragAndDropRecyclerAdapter.kt | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt index 987006c23..e2f63b276 100644 --- a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt +++ b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt @@ -13,9 +13,7 @@ import com.todoroo.astrid.adapter.TaskAdapter import com.todoroo.astrid.utility.Flags import io.reactivex.disposables.CompositeDisposable import io.reactivex.subjects.PublishSubject -import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import org.tasks.activities.DragAndDropDiffer import org.tasks.data.TaskContainer import org.tasks.preferences.Preferences @@ -215,14 +213,18 @@ class DragAndDropRecyclerAdapter( if (from < to) { to++ } - vh.task.setIndent(targetIndent) - vh.indent = targetIndent - moved(from, to, targetIndent) + scope.launch { + vh.task.setIndent(targetIndent) + vh.indent = targetIndent + moved(from, to, targetIndent) + } } else if (task.getIndent() != targetIndent) { - val position = vh.adapterPosition - vh.task.setIndent(targetIndent) - vh.indent = targetIndent - moved(position, position, targetIndent) + scope.launch { + val position = vh.adapterPosition + vh.task.setIndent(targetIndent) + vh.indent = targetIndent + moved(position, position, targetIndent) + } } } from = -1 @@ -234,7 +236,7 @@ class DragAndDropRecyclerAdapter( throw UnsupportedOperationException() } - private fun moved(fromOrig: Int, to: Int, indent: Int) { + private suspend fun moved(fromOrig: Int, to: Int, indent: Int) { val from = if (fromOrig == to) { to } else if (fromOrig > to && isHeader(fromOrig)) { @@ -242,14 +244,10 @@ class DragAndDropRecyclerAdapter( } else { from } - scope.launch { - withContext(NonCancellable) { - adapter.moved(from, to, indent) - } - val task: TaskContainer = items.removeAt(from) - items.add(if (from < to) to - 1 else to, task) - taskList.loadTaskListContent() - } + adapter.moved(from, to, indent) + val task: TaskContainer = items.removeAt(from) + items.add(if (from < to) to - 1 else to, task) + taskList.loadTaskListContent() } }