Debounce sync requests

pull/1061/head
Alex Baker 4 years ago
parent 7078dfa2bf
commit fcc1511c48

@ -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")
}
}
}

@ -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()

Loading…
Cancel
Save