Move noncancellable context to save

pull/1051/head
Alex Baker 4 years ago
parent 0226440833
commit 43814b0810

@ -27,9 +27,7 @@ import com.todoroo.astrid.data.Task
import com.todoroo.astrid.service.TaskCreator import com.todoroo.astrid.service.TaskCreator
import com.todoroo.astrid.timers.TimerControlSet.TimerControlSetCallback import com.todoroo.astrid.timers.TimerControlSet.TimerControlSetCallback
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.tasks.LocalBroadcastManager import org.tasks.LocalBroadcastManager
import org.tasks.R import org.tasks.R
import org.tasks.activities.TagSettingsActivity import org.tasks.activities.TagSettingsActivity
@ -175,10 +173,11 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
val intent = intent val intent = intent
val openFilter = intent.hasExtra(OPEN_FILTER) val openFilter = intent.hasExtra(OPEN_FILTER)
val loadFilter = intent.hasExtra(LOAD_FILTER) val loadFilter = intent.hasExtra(LOAD_FILTER)
val tef = taskEditFragment if (openFilter || loadFilter) {
if (tef != null && (openFilter || loadFilter)) { taskEditFragment?.let {
lifecycleScope.launch(NonCancellable) { lifecycleScope.launch {
tef.save() it.save()
}
} }
} }
if (loadFilter || !openFilter && filter == null) { if (loadFilter || !openFilter && filter == null) {
@ -312,9 +311,7 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
lifecycleScope.launch { lifecycleScope.launch {
taskEditFragment?.let { taskEditFragment?.let {
it.editViewModel.cleared.removeObservers(this@MainActivity) it.editViewModel.cleared.removeObservers(this@MainActivity)
withContext(NonCancellable) { it.save()
it.save()
}
} }
clearUi() clearUi()
val fragment = newTaskEditFragment( val fragment = newTaskEditFragment(
@ -344,7 +341,7 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
} }
taskEditFragment?.let { taskEditFragment?.let {
if (preferences.backButtonSavesTask()) { if (preferences.backButtonSavesTask()) {
lifecycleScope.launch(NonCancellable) { lifecycleScope.launch {
it.save() it.save()
} }
} else { } else {

@ -103,7 +103,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
val toolbar = binding.toolbar val toolbar = binding.toolbar
toolbar.navigationIcon = context.getDrawable(R.drawable.ic_outline_save_24px) toolbar.navigationIcon = context.getDrawable(R.drawable.ic_outline_save_24px)
toolbar.setNavigationOnClickListener { toolbar.setNavigationOnClickListener {
lifecycleScope.launch(NonCancellable) { lifecycleScope.launch {
save() save()
} }
} }
@ -153,7 +153,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
binding.fab.setImageResource(R.drawable.ic_outline_check_box_24px) binding.fab.setImageResource(R.drawable.ic_outline_check_box_24px)
} else { } else {
editViewModel.completed = true editViewModel.completed = true
lifecycleScope.launch(NonCancellable) { lifecycleScope.launch {
save() save()
} }
} }
@ -251,7 +251,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
} }
/** Save task model from values in UI components */ /** Save task model from values in UI components */
suspend fun save() { suspend fun save() = withContext(NonCancellable) {
val saved = editViewModel.save() val saved = editViewModel.save()
if (saved && editViewModel.isNew) { if (saved && editViewModel.isNew) {
(activity as MainActivity?)?.taskListFragment?.let { taskListFragment -> (activity as MainActivity?)?.taskListFragment?.let { taskListFragment ->

Loading…
Cancel
Save