From a3dd5c1e92862f47239cfe10aa6631a92164fb07 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 2 Mar 2020 19:04:57 -0500 Subject: [PATCH] Tweak WorkManager task cancellation, add flex times --- app/build.gradle | 4 +++- .../kanade/tachiyomi/data/backup/BackupCreatorJob.kt | 10 +++++----- .../kanade/tachiyomi/data/library/LibraryUpdateJob.kt | 10 +++++----- .../eu/kanade/tachiyomi/data/updater/UpdaterJob.kt | 4 +++- .../tachiyomi/ui/setting/SettingsBackupController.kt | 7 +------ .../tachiyomi/ui/setting/SettingsLibraryController.kt | 7 +------ 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b3754f863d..466f50b076 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -163,7 +163,9 @@ dependencies { implementation 'org.jsoup:jsoup:1.12.1' // Job scheduling - implementation 'android.arch.work:work-runtime:2.3.2' + final workManagerVersion = '2.3.2' + implementation "android.arch.work:work-runtime:$workManagerVersion" + implementation "android.arch.work:work-runtime-ktx:$workManagerVersion" // Changelog implementation 'com.github.gabrielemariotti.changeloglib:changelog:2.1.0' diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt index 13b1df8650..81a5c16cc0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt @@ -32,16 +32,16 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet val preferences = Injekt.get() val interval = prefInterval ?: preferences.backupInterval().getOrDefault() if (interval > 0) { - val request = PeriodicWorkRequestBuilder(interval.toLong(), TimeUnit.HOURS) + val request = PeriodicWorkRequestBuilder( + interval.toLong(), TimeUnit.HOURS, + 10, TimeUnit.MINUTES) .addTag(TAG) .build() WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) + } else { + WorkManager.getInstance().cancelAllWorkByTag(TAG) } } - - fun cancelTask() { - WorkManager.getInstance().cancelAllWorkByTag(TAG) - } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt index 47a131def3..e1dced1ef9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt @@ -41,17 +41,17 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet .setRequiresCharging(acRestriction) .build() - val request = PeriodicWorkRequestBuilder(interval.toLong(), TimeUnit.HOURS) + val request = PeriodicWorkRequestBuilder( + interval.toLong(), TimeUnit.HOURS, + 10, TimeUnit.MINUTES) .addTag(TAG) .setConstraints(constraints) .build() WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) + } else { + WorkManager.getInstance().cancelAllWorkByTag(TAG) } } - - fun cancelTask() { - WorkManager.getInstance().cancelAllWorkByTag(TAG) - } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt index 3b9a1f2059..b0c6e9e072 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt @@ -62,7 +62,9 @@ class UpdaterJob(private val context: Context, workerParams: WorkerParameters) : .setRequiredNetworkType(NetworkType.CONNECTED) .build() - val request = PeriodicWorkRequestBuilder(1, TimeUnit.DAYS) + val request = PeriodicWorkRequestBuilder( + 1, TimeUnit.DAYS, + 1, TimeUnit.HOURS) .addTag(TAG) .setConstraints(constraints) .build() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index dfc293f018..a5e738ef81 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -105,13 +105,8 @@ class SettingsBackupController : SettingsController() { summary = "%s" onChange { newValue -> - // Always cancel the previous task, it seems that sometimes they are not updated - BackupCreatorJob.cancelTask() - val interval = (newValue as String).toInt() - if (interval > 0) { - BackupCreatorJob.setupTask(interval) - } + BackupCreatorJob.setupTask(interval) true } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt index 41e24d15d8..6f7876292f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt @@ -86,13 +86,8 @@ class SettingsLibraryController : SettingsController() { summary = "%s" onChange { newValue -> - // Always cancel the previous task, it seems that sometimes they are not updated. - LibraryUpdateJob.cancelTask() - val interval = (newValue as String).toInt() - if (interval > 0) { - LibraryUpdateJob.setupTask(interval) - } + LibraryUpdateJob.setupTask(interval) true } }