Initiate sync requests on background thread

pull/1061/head
Alex Baker 4 years ago
parent aa9e0e93b9
commit 6ee6e96a7f

@ -145,15 +145,12 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
private lateinit var callbacks: TaskListFragmentCallbackHandler private lateinit var callbacks: TaskListFragmentCallbackHandler
override fun onRefresh() { override fun onRefresh() {
syncAdapters.sync(true)
lifecycleScope.launch { lifecycleScope.launch {
if (syncAdapters.sync(true)) {
delay(1000) delay(1000)
setSyncOngoing()
} else {
refresh() refresh()
} }
} }
}
private fun setSyncOngoing() { private fun setSyncOngoing() {
AndroidUtilities.assertMainThread() AndroidUtilities.assertMainThread()
@ -492,9 +489,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
for (task in tasks) { for (task in tasks) {
onTaskCreated(task.uuid) onTaskCreated(task.uuid)
} }
lifecycleScope.launch {
syncAdapters.sync() syncAdapters.sync()
}
loadTaskListContent() loadTaskListContent()
} }

@ -5,7 +5,6 @@ import android.content.Intent
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.tasks.R import org.tasks.R
import org.tasks.drive.DriveLoginActivity import org.tasks.drive.DriveLoginActivity
@ -32,15 +31,15 @@ class MainPreferences : BasePreferences() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CALDAV_SETTINGS) { if (requestCode == REQUEST_CALDAV_SETTINGS) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
lifecycleScope.launch(NonCancellable) {
syncAdapters.sync(true) syncAdapters.sync(true)
lifecycleScope.launch {
workManager.updateBackgroundSync() workManager.updateBackgroundSync()
} }
} }
} else if (requestCode == REQUEST_GOOGLE_TASKS) { } else if (requestCode == REQUEST_GOOGLE_TASKS) {
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
lifecycleScope.launch(NonCancellable) {
syncAdapters.sync(true) syncAdapters.sync(true)
lifecycleScope.launch {
workManager.updateBackgroundSync() workManager.updateBackgroundSync()
} }
} else { } else {

@ -94,8 +94,8 @@ class Synchronization : InjectingPreferenceFragment() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CALDAV_SETTINGS) { if (requestCode == REQUEST_CALDAV_SETTINGS) {
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
lifecycleScope.launch(NonCancellable) {
syncAdapters.sync(true) syncAdapters.sync(true)
lifecycleScope.launch {
workManager.updateBackgroundSync() workManager.updateBackgroundSync()
} }
} }

@ -2,8 +2,7 @@ package org.tasks.sync
import com.todoroo.astrid.data.SyncFlags import com.todoroo.astrid.data.SyncFlags
import com.todoroo.astrid.data.Task import com.todoroo.astrid.data.Task
import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.*
import kotlinx.coroutines.withContext
import org.tasks.data.CaldavAccount.Companion.TYPE_CALDAV import org.tasks.data.CaldavAccount.Companion.TYPE_CALDAV
import org.tasks.data.CaldavAccount.Companion.TYPE_ETESYNC import org.tasks.data.CaldavAccount.Companion.TYPE_ETESYNC
import org.tasks.data.CaldavDao 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_CALDAV
import org.tasks.jobs.WorkManager.Companion.TAG_SYNC_ETESYNC import org.tasks.jobs.WorkManager.Companion.TAG_SYNC_ETESYNC
import org.tasks.jobs.WorkManager.Companion.TAG_SYNC_GOOGLE_TASKS import org.tasks.jobs.WorkManager.Companion.TAG_SYNC_GOOGLE_TASKS
import java.util.concurrent.Executors.newSingleThreadExecutor
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -23,13 +23,14 @@ class SyncAdapters @Inject constructor(
private val caldavDao: CaldavDao, private val caldavDao: CaldavDao,
private val googleTaskDao: GoogleTaskDao, private val googleTaskDao: GoogleTaskDao,
private val googleTaskListDao: GoogleTaskListDao) { private val googleTaskListDao: GoogleTaskListDao) {
private val scope = CoroutineScope(newSingleThreadExecutor().asCoroutineDispatcher() + SupervisorJob())
private val googleTasks = Debouncer(TAG_SYNC_GOOGLE_TASKS) { workManager.googleTaskSync(it) } private val googleTasks = Debouncer(TAG_SYNC_GOOGLE_TASKS) { workManager.googleTaskSync(it) }
private val caldav = Debouncer(TAG_SYNC_CALDAV) { workManager.caldavSync(it) } private val caldav = Debouncer(TAG_SYNC_CALDAV) { workManager.caldavSync(it) }
private val eteSync = Debouncer(TAG_SYNC_ETESYNC) { workManager.eteSync(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)) { if (task.checkTransitory(SyncFlags.SUPPRESS_SYNC)) {
return return@launch
} }
if (!task.googleTaskUpToDate(original) if (!task.googleTaskUpToDate(original)
&& googleTaskDao.getAllByTaskId(task.id).isNotEmpty()) { && googleTaskDao.getAllByTaskId(task.id).isNotEmpty()) {
@ -45,32 +46,33 @@ class SyncAdapters @Inject constructor(
} }
} }
suspend fun sync() { fun sync() {
sync(false) sync(false)
} }
suspend fun sync(immediate: Boolean): Boolean = withContext(NonCancellable) { fun sync(immediate: Boolean) = scope.launch {
val googleTasksEnabled = isGoogleTaskSyncEnabled() val googleTasksEnabled = async { isGoogleTaskSyncEnabled() }
if (googleTasksEnabled) { val caldavEnabled = async { isCaldavSyncEnabled() }
val eteSyncEnabled = async { isEteSyncEnabled() }
if (googleTasksEnabled.await()) {
googleTasks.sync(immediate) googleTasks.sync(immediate)
} }
val caldavEnabled = isCaldavSyncEnabled() if (caldavEnabled.await()) {
if (caldavEnabled) {
caldav.sync(immediate) caldav.sync(immediate)
} }
val eteSyncEnabled = isEteSyncEnabled() if (eteSyncEnabled.await()) {
if (eteSyncEnabled) {
eteSync.sync(immediate) 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()
} }
Loading…
Cancel
Save