-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move sync on collections change listener; add delay
- Loading branch information
Showing
3 changed files
with
62 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/src/main/kotlin/at/bitfire/davdroid/repository/SyncOnCollectionsChangeListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 delayedOneTimeSyncWorkerJob: 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. | ||
delayedOneTimeSyncWorkerJob?.cancel() | ||
delayedOneTimeSyncWorkerJob = 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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters