Skip to content

Commit

Permalink
Decrease delay
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Jan 21, 2025
1 parent 7f58df8 commit f9a0940
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
*/

package at.bitfire.davdroid.repository

import android.accounts.Account
import at.bitfire.davdroid.sync.worker.SyncWorkerManager
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dagger.multibindings.IntoSet
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.logging.Logger
import javax.inject.Inject

/**
* Enqueues a sync worker after a short delay when the collection list changes.
*/
class SyncOnCollectionsChangeListener @Inject constructor(
private val workerManager: SyncWorkerManager,
private val logger: Logger
): DavCollectionRepository.OnChangeListener {

var job: Job? = null

override fun onCollectionsChanged(account: Account?) {
account?.let {
// Start sync after a short delay to avoid multiple syncs in a short time when multiple
// collections change quickly, e.g. at collection refresh or users first time setup.
job?.cancel()
job = CoroutineScope(Dispatchers.IO).launch {
delay(7000)
logger.info("Collections changed, scheduling sync")
workerManager.enqueueOneTimeAllAuthorities(it)
}
}
}



/**
* Hilt module that registers [SyncOnCollectionsChangeListener] in [DavCollectionRepository].
*/
@Module
@InstallIn(SingletonComponent::class)
interface SyncOnCollectionsChangeListenerModule {
@Binds
@IntoSet
fun listener(impl: SyncOnCollectionsChangeListener): DavCollectionRepository.OnChangeListener
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.work.WorkManager
import androidx.work.WorkQuery
import androidx.work.WorkRequest
import at.bitfire.davdroid.push.PushNotificationManager
import at.bitfire.davdroid.repository.DavCollectionRepository
import at.bitfire.davdroid.sync.SyncDataType
import at.bitfire.davdroid.sync.TasksAppManager
import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.INPUT_ACCOUNT_NAME
Expand All @@ -37,13 +36,8 @@ import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.INPUT_UPLOAD
import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.InputResync
import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.NO_RESYNC
import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.commonTag
import dagger.Binds
import dagger.Lazy
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import dagger.multibindings.IntoSet
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -149,7 +143,7 @@ class SyncWorkerManager @Inject constructor(
}
WorkManager.getInstance(context).enqueueUniqueWork(
name,
/* If sync is already running, just continue.
/* If sync is already running, continue that sync and don't append a new one.
Existing retried work will not be replaced (for instance when
PeriodicSyncWorker enqueues another scheduled sync). */
ExistingWorkPolicy.KEEP,
Expand Down Expand Up @@ -291,28 +285,4 @@ class SyncWorkerManager @Inject constructor(
}
}

/**
* Listener that enqueues a push registration worker when the collection list changes.
*/
class CollectionsListener @Inject constructor(
private val workerManager: SyncWorkerManager
): DavCollectionRepository.OnChangeListener {

override fun onCollectionsChanged(account: Account?) {
account?.let { workerManager.enqueueOneTimeAllAuthorities(it) }
}

}

/**
* Hilt module that registers [CollectionsListener] in [DavCollectionRepository].
*/
@Module
@InstallIn(SingletonComponent::class)
interface SyncWorkerManagerModule {
@Binds
@IntoSet
fun listener(impl: CollectionsListener): DavCollectionRepository.OnChangeListener
}

}

0 comments on commit f9a0940

Please sign in to comment.