From 6ee6e96a7fd1b65c6b15b1dd0c4a63211ba0f16d Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 31 Jul 2020 12:11:55 -0500 Subject: [PATCH] Initiate sync requests on background thread --- .../astrid/activity/TaskListFragment.kt | 13 +++---- .../org/tasks/preferences/MainPreferences.kt | 9 +++-- .../preferences/fragments/Synchronization.kt | 4 +-- .../main/java/org/tasks/sync/SyncAdapters.kt | 34 ++++++++++--------- 4 files changed, 28 insertions(+), 32 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 005e8e05b..dae9973c6 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -145,13 +145,10 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL private lateinit var callbacks: TaskListFragmentCallbackHandler override fun onRefresh() { + syncAdapters.sync(true) lifecycleScope.launch { - if (syncAdapters.sync(true)) { - delay(1000) - setSyncOngoing() - } else { - refresh() - } + delay(1000) + refresh() } } @@ -492,9 +489,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL for (task in tasks) { onTaskCreated(task.uuid) } - lifecycleScope.launch { - syncAdapters.sync() - } + syncAdapters.sync() loadTaskListContent() } diff --git a/app/src/main/java/org/tasks/preferences/MainPreferences.kt b/app/src/main/java/org/tasks/preferences/MainPreferences.kt index 9ebb3efc9..f168054ca 100644 --- a/app/src/main/java/org/tasks/preferences/MainPreferences.kt +++ b/app/src/main/java/org/tasks/preferences/MainPreferences.kt @@ -5,7 +5,6 @@ import android.content.Intent import androidx.lifecycle.lifecycleScope import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.launch import org.tasks.R import org.tasks.drive.DriveLoginActivity @@ -32,15 +31,15 @@ class MainPreferences : BasePreferences() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (requestCode == REQUEST_CALDAV_SETTINGS) { if (resultCode == RESULT_OK) { - lifecycleScope.launch(NonCancellable) { - syncAdapters.sync(true) + syncAdapters.sync(true) + lifecycleScope.launch { workManager.updateBackgroundSync() } } } else if (requestCode == REQUEST_GOOGLE_TASKS) { if (resultCode == Activity.RESULT_OK) { - lifecycleScope.launch(NonCancellable) { - syncAdapters.sync(true) + syncAdapters.sync(true) + lifecycleScope.launch { workManager.updateBackgroundSync() } } else { diff --git a/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt b/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt index 4695867af..2c838a440 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt @@ -94,8 +94,8 @@ class Synchronization : InjectingPreferenceFragment() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (requestCode == REQUEST_CALDAV_SETTINGS) { if (resultCode == Activity.RESULT_OK) { - lifecycleScope.launch(NonCancellable) { - syncAdapters.sync(true) + syncAdapters.sync(true) + lifecycleScope.launch { workManager.updateBackgroundSync() } } diff --git a/app/src/main/java/org/tasks/sync/SyncAdapters.kt b/app/src/main/java/org/tasks/sync/SyncAdapters.kt index d6c4a46ea..a059ae143 100644 --- a/app/src/main/java/org/tasks/sync/SyncAdapters.kt +++ b/app/src/main/java/org/tasks/sync/SyncAdapters.kt @@ -2,8 +2,7 @@ package org.tasks.sync import com.todoroo.astrid.data.SyncFlags import com.todoroo.astrid.data.Task -import kotlinx.coroutines.NonCancellable -import kotlinx.coroutines.withContext +import kotlinx.coroutines.* import org.tasks.data.CaldavAccount.Companion.TYPE_CALDAV import org.tasks.data.CaldavAccount.Companion.TYPE_ETESYNC import org.tasks.data.CaldavDao @@ -14,6 +13,7 @@ import org.tasks.jobs.WorkManager import org.tasks.jobs.WorkManager.Companion.TAG_SYNC_CALDAV import org.tasks.jobs.WorkManager.Companion.TAG_SYNC_ETESYNC import org.tasks.jobs.WorkManager.Companion.TAG_SYNC_GOOGLE_TASKS +import java.util.concurrent.Executors.newSingleThreadExecutor import javax.inject.Inject import javax.inject.Singleton @@ -23,13 +23,14 @@ class SyncAdapters @Inject constructor( private val caldavDao: CaldavDao, private val googleTaskDao: GoogleTaskDao, private val googleTaskListDao: GoogleTaskListDao) { + private val scope = CoroutineScope(newSingleThreadExecutor().asCoroutineDispatcher() + SupervisorJob()) private val googleTasks = Debouncer(TAG_SYNC_GOOGLE_TASKS) { workManager.googleTaskSync(it) } private val caldav = Debouncer(TAG_SYNC_CALDAV) { workManager.caldavSync(it) } private val eteSync = Debouncer(TAG_SYNC_ETESYNC) { workManager.eteSync(it) } - suspend fun sync(task: Task, original: Task?) { + fun sync(task: Task, original: Task?) = scope.launch { if (task.checkTransitory(SyncFlags.SUPPRESS_SYNC)) { - return + return@launch } if (!task.googleTaskUpToDate(original) && googleTaskDao.getAllByTaskId(task.id).isNotEmpty()) { @@ -45,32 +46,33 @@ class SyncAdapters @Inject constructor( } } - suspend fun sync() { + fun sync() { sync(false) } - suspend fun sync(immediate: Boolean): Boolean = withContext(NonCancellable) { - val googleTasksEnabled = isGoogleTaskSyncEnabled() - if (googleTasksEnabled) { + fun sync(immediate: Boolean) = scope.launch { + val googleTasksEnabled = async { isGoogleTaskSyncEnabled() } + val caldavEnabled = async { isCaldavSyncEnabled() } + val eteSyncEnabled = async { isEteSyncEnabled() } + + if (googleTasksEnabled.await()) { googleTasks.sync(immediate) } - val caldavEnabled = isCaldavSyncEnabled() - if (caldavEnabled) { + if (caldavEnabled.await()) { caldav.sync(immediate) } - val eteSyncEnabled = isEteSyncEnabled() - if (eteSyncEnabled) { + if (eteSyncEnabled.await()) { eteSync.sync(immediate) } - return@withContext googleTasksEnabled || caldavEnabled || eteSyncEnabled } - suspend fun isGoogleTaskSyncEnabled() = googleTaskListDao.getAccounts().isNotEmpty() + private suspend fun isGoogleTaskSyncEnabled() = googleTaskListDao.getAccounts().isNotEmpty() + + private suspend fun isCaldavSyncEnabled() = caldavDao.getAccounts(TYPE_CALDAV).isNotEmpty() - suspend fun isCaldavSyncEnabled() = caldavDao.getAccounts(TYPE_CALDAV).isNotEmpty() + private suspend fun isEteSyncEnabled() = caldavDao.getAccounts(TYPE_ETESYNC).isNotEmpty() - suspend fun isEteSyncEnabled() = caldavDao.getAccounts(TYPE_ETESYNC).isNotEmpty() } \ No newline at end of file