From 43814b08108ce5a6740bd6ece3905bf0da00ab97 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Wed, 8 Jul 2020 17:49:19 -0500 Subject: [PATCH] Move noncancellable context to save --- .../com/todoroo/astrid/activity/MainActivity.kt | 17 +++++++---------- .../todoroo/astrid/activity/TaskEditFragment.kt | 6 +++--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt index 3a6c2c30e..06ca9772e 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt @@ -27,9 +27,7 @@ import com.todoroo.astrid.data.Task import com.todoroo.astrid.service.TaskCreator import com.todoroo.astrid.timers.TimerControlSet.TimerControlSetCallback import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import org.tasks.LocalBroadcastManager import org.tasks.R import org.tasks.activities.TagSettingsActivity @@ -175,10 +173,11 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl val intent = intent val openFilter = intent.hasExtra(OPEN_FILTER) val loadFilter = intent.hasExtra(LOAD_FILTER) - val tef = taskEditFragment - if (tef != null && (openFilter || loadFilter)) { - lifecycleScope.launch(NonCancellable) { - tef.save() + if (openFilter || loadFilter) { + taskEditFragment?.let { + lifecycleScope.launch { + it.save() + } } } if (loadFilter || !openFilter && filter == null) { @@ -312,9 +311,7 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl lifecycleScope.launch { taskEditFragment?.let { it.editViewModel.cleared.removeObservers(this@MainActivity) - withContext(NonCancellable) { - it.save() - } + it.save() } clearUi() val fragment = newTaskEditFragment( @@ -344,7 +341,7 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl } taskEditFragment?.let { if (preferences.backButtonSavesTask()) { - lifecycleScope.launch(NonCancellable) { + lifecycleScope.launch { it.save() } } else { diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt index 74fe66a5b..0c4d15e3e 100755 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt @@ -103,7 +103,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener { val toolbar = binding.toolbar toolbar.navigationIcon = context.getDrawable(R.drawable.ic_outline_save_24px) toolbar.setNavigationOnClickListener { - lifecycleScope.launch(NonCancellable) { + lifecycleScope.launch { save() } } @@ -153,7 +153,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener { binding.fab.setImageResource(R.drawable.ic_outline_check_box_24px) } else { editViewModel.completed = true - lifecycleScope.launch(NonCancellable) { + lifecycleScope.launch { save() } } @@ -251,7 +251,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener { } /** Save task model from values in UI components */ - suspend fun save() { + suspend fun save() = withContext(NonCancellable) { val saved = editViewModel.save() if (saved && editViewModel.isNew) { (activity as MainActivity?)?.taskListFragment?.let { taskListFragment ->