Skip to content

Commit

Permalink
feat: delay media sync notification
Browse files Browse the repository at this point in the history
to avoid showing it if sync is too fast, which can occur when there is nothing to sync
  • Loading branch information
BrayanDSO committed Apr 6, 2024
1 parent 3bbc63d commit 0cd83d9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/worker/SyncMediaWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class SyncMediaWorker(

override suspend fun doWork(): Result {
Timber.v("SyncMediaWorker::doWork")
trySetForeground(getForegroundInfo())

val auth = syncAuth {
hkey = inputData.getString(HKEY_KEY)!!
Expand All @@ -68,7 +67,13 @@ class SyncMediaWorker(
try {
val backend = CollectionManager.getBackend()
backend.syncMedia(auth)
monitorProgress(backend)

delay(1000) // avoid notifications if sync occurs too quickly
if (backend.mediaSyncStatus().active) {
Timber.i("Showing SyncMediaWorker's notification")
trySetForeground(getForegroundInfo())
monitorProgress(backend)
}
} catch (cancellationException: CancellationException) {
Timber.w(cancellationException)
cancelMediaSync(CollectionManager.getBackend())
Expand Down Expand Up @@ -113,7 +118,7 @@ class SyncMediaWorker(
setOngoing(true)
setProgress(0, 0, true)
addAction(R.drawable.close_icon, cancelTitle, cancelIntent)
foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_DEFERRED
}
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ForegroundInfo(NotificationId.SYNC_MEDIA, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
Expand Down

0 comments on commit 0cd83d9

Please sign in to comment.