From fcc1511c4834bbf17b17331a01cd13614c4b6e55 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 30 Jul 2020 13:48:03 -0500 Subject: [PATCH] Debounce sync requests --- app/src/main/java/org/tasks/sync/Debouncer.kt | 20 ++++++++++ .../main/java/org/tasks/sync/SyncAdapters.kt | 37 ++++++++++++------- 2 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/org/tasks/sync/Debouncer.kt diff --git a/app/src/main/java/org/tasks/sync/Debouncer.kt b/app/src/main/java/org/tasks/sync/Debouncer.kt new file mode 100644 index 000000000..65720a73d --- /dev/null +++ b/app/src/main/java/org/tasks/sync/Debouncer.kt @@ -0,0 +1,20 @@ +package org.tasks.sync + +import kotlinx.coroutines.delay +import timber.log.Timber + +class Debouncer constructor(private val tag: String, private val block: suspend (Boolean) -> Unit) { + private var count = 0 + + suspend fun sync(immediate: Boolean) { + val thisCount = ++count + + delay(1000) + + if (immediate || thisCount == count) { + block.invoke(immediate) + } else { + Timber.v("debouncing $tag") + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/sync/SyncAdapters.kt b/app/src/main/java/org/tasks/sync/SyncAdapters.kt index cb7abc28e..d6c4a46ea 100644 --- a/app/src/main/java/org/tasks/sync/SyncAdapters.kt +++ b/app/src/main/java/org/tasks/sync/SyncAdapters.kt @@ -9,14 +9,23 @@ import org.tasks.data.CaldavAccount.Companion.TYPE_ETESYNC import org.tasks.data.CaldavDao import org.tasks.data.GoogleTaskDao import org.tasks.data.GoogleTaskListDao +import org.tasks.data.OpenTaskDao 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 javax.inject.Inject +import javax.inject.Singleton +@Singleton class SyncAdapters @Inject constructor( - private val workManager: WorkManager, + workManager: WorkManager, private val caldavDao: CaldavDao, private val googleTaskDao: GoogleTaskDao, private val googleTaskListDao: GoogleTaskListDao) { + 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?) { if (task.checkTransitory(SyncFlags.SUPPRESS_SYNC)) { @@ -24,14 +33,14 @@ class SyncAdapters @Inject constructor( } if (!task.googleTaskUpToDate(original) && googleTaskDao.getAllByTaskId(task.id).isNotEmpty()) { - workManager.googleTaskSync(false) + googleTasks.sync(false) } if (task.checkTransitory(SyncFlags.FORCE_CALDAV_SYNC) || !task.caldavUpToDate(original)) { if (caldavDao.isAccountType(task.id, TYPE_CALDAV)) { - workManager.caldavSync(false) + caldav.sync(false) } if (caldavDao.isAccountType(task.id, TYPE_ETESYNC)) { - workManager.eteSync(false) + eteSync.sync(false) } } } @@ -41,22 +50,22 @@ class SyncAdapters @Inject constructor( } suspend fun sync(immediate: Boolean): Boolean = withContext(NonCancellable) { - val googleTasks = isGoogleTaskSyncEnabled() - if (googleTasks) { - workManager.googleTaskSync(immediate) + val googleTasksEnabled = isGoogleTaskSyncEnabled() + if (googleTasksEnabled) { + googleTasks.sync(immediate) } - val caldav = isCaldavSyncEnabled() - if (caldav) { - workManager.caldavSync(immediate) + val caldavEnabled = isCaldavSyncEnabled() + if (caldavEnabled) { + caldav.sync(immediate) } - val eteSync = isEteSyncEnabled() - if (eteSync) { - workManager.eteSync(immediate) + val eteSyncEnabled = isEteSyncEnabled() + if (eteSyncEnabled) { + eteSync.sync(immediate) } - return@withContext googleTasks || caldav || eteSync + return@withContext googleTasksEnabled || caldavEnabled || eteSyncEnabled } suspend fun isGoogleTaskSyncEnabled() = googleTaskListDao.getAccounts().isNotEmpty()