瀏覽代碼

Clean up WorkManager usages a bit

arkon 2 年之前
父節點
當前提交
859601a46e

+ 2 - 3
app/src/main/java/eu/kanade/domain/track/service/DelayedTrackingUpdateJob.kt

@@ -7,11 +7,11 @@ import androidx.work.CoroutineWorker
 import androidx.work.ExistingWorkPolicy
 import androidx.work.NetworkType
 import androidx.work.OneTimeWorkRequestBuilder
-import androidx.work.WorkManager
 import androidx.work.WorkerParameters
 import eu.kanade.domain.track.model.toDbTrack
 import eu.kanade.domain.track.store.DelayedTrackingStore
 import eu.kanade.tachiyomi.data.track.TrackManager
+import eu.kanade.tachiyomi.util.system.workManager
 import logcat.LogPriority
 import tachiyomi.core.util.lang.withIOContext
 import tachiyomi.core.util.system.logcat
@@ -74,8 +74,7 @@ class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters)
                 .addTag(TAG)
                 .build()
 
-            WorkManager.getInstance(context)
-                .enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, request)
+            context.workManager.enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, request)
         }
     }
 }

+ 2 - 2
app/src/main/java/eu/kanade/presentation/more/settings/screen/WorkerInfoScreen.kt

@@ -29,7 +29,6 @@ import androidx.compose.ui.text.font.FontFamily
 import androidx.compose.ui.unit.dp
 import androidx.lifecycle.asFlow
 import androidx.work.WorkInfo
-import androidx.work.WorkManager
 import androidx.work.WorkQuery
 import cafe.adriel.voyager.core.model.ScreenModel
 import cafe.adriel.voyager.core.model.coroutineScope
@@ -38,6 +37,7 @@ import cafe.adriel.voyager.navigator.LocalNavigator
 import cafe.adriel.voyager.navigator.currentOrThrow
 import eu.kanade.presentation.util.Screen
 import eu.kanade.tachiyomi.R
+import eu.kanade.tachiyomi.util.system.workManager
 import kotlinx.coroutines.flow.SharingStarted
 import kotlinx.coroutines.flow.map
 import kotlinx.coroutines.flow.stateIn
@@ -122,7 +122,7 @@ object WorkerInfoScreen : Screen() {
     }
 
     private class Model(context: Context) : ScreenModel {
-        private val workManager = WorkManager.getInstance(context)
+        private val workManager = context.workManager
 
         val finished = workManager
             .getWorkInfosLiveData(WorkQuery.fromStates(WorkInfo.State.SUCCEEDED, WorkInfo.State.FAILED, WorkInfo.State.CANCELLED))

+ 3 - 3
app/src/main/java/eu/kanade/tachiyomi/Migrations.kt

@@ -3,7 +3,6 @@ package eu.kanade.tachiyomi
 import android.content.Context
 import androidx.core.content.edit
 import androidx.preference.PreferenceManager
-import androidx.work.WorkManager
 import eu.kanade.domain.base.BasePreferences
 import eu.kanade.domain.source.service.SourcePreferences
 import eu.kanade.domain.ui.UiPreferences
@@ -20,6 +19,7 @@ import eu.kanade.tachiyomi.util.preference.minusAssign
 import eu.kanade.tachiyomi.util.preference.plusAssign
 import eu.kanade.tachiyomi.util.system.DeviceUtil
 import eu.kanade.tachiyomi.util.system.toast
+import eu.kanade.tachiyomi.util.system.workManager
 import tachiyomi.core.preference.PreferenceStore
 import tachiyomi.core.preference.getEnum
 import tachiyomi.domain.backup.service.BackupPreferences
@@ -331,8 +331,8 @@ object Migrations {
             }
             if (oldVersion < 97) {
                 // Removed background jobs
-                WorkManager.getInstance(context).cancelAllWorkByTag("UpdateChecker")
-                WorkManager.getInstance(context).cancelAllWorkByTag("ExtensionUpdate")
+                context.workManager.cancelAllWorkByTag("UpdateChecker")
+                context.workManager.cancelAllWorkByTag("ExtensionUpdate")
                 prefs.edit {
                     remove("automatic_ext_updates")
                 }

+ 10 - 9
app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt

@@ -9,13 +9,13 @@ import androidx.work.ExistingWorkPolicy
 import androidx.work.ForegroundInfo
 import androidx.work.OneTimeWorkRequestBuilder
 import androidx.work.PeriodicWorkRequestBuilder
-import androidx.work.WorkInfo
-import androidx.work.WorkManager
 import androidx.work.WorkerParameters
 import androidx.work.workDataOf
 import com.hippo.unifile.UniFile
 import eu.kanade.tachiyomi.data.notification.Notifications
+import eu.kanade.tachiyomi.util.system.isRunning
 import eu.kanade.tachiyomi.util.system.notificationManager
+import eu.kanade.tachiyomi.util.system.workManager
 import logcat.LogPriority
 import tachiyomi.core.util.system.logcat
 import tachiyomi.domain.backup.service.BackupPreferences
@@ -55,19 +55,20 @@ class BackupCreateJob(private val context: Context, workerParams: WorkerParamete
     }
 
     override suspend fun getForegroundInfo(): ForegroundInfo {
-        return ForegroundInfo(Notifications.ID_BACKUP_PROGRESS, notifier.showBackupProgress().build())
+        return ForegroundInfo(
+            Notifications.ID_BACKUP_PROGRESS,
+            notifier.showBackupProgress().build(),
+        )
     }
 
     companion object {
         fun isManualJobRunning(context: Context): Boolean {
-            val list = WorkManager.getInstance(context).getWorkInfosByTag(TAG_MANUAL).get()
-            return list.find { it.state == WorkInfo.State.RUNNING } != null
+            return context.workManager.isRunning(TAG_MANUAL)
         }
 
         fun setupTask(context: Context, prefInterval: Int? = null) {
             val backupPreferences = Injekt.get<BackupPreferences>()
             val interval = prefInterval ?: backupPreferences.backupInterval().get()
-            val workManager = WorkManager.getInstance(context)
             if (interval > 0) {
                 val request = PeriodicWorkRequestBuilder<BackupCreateJob>(
                     interval.toLong(),
@@ -79,9 +80,9 @@ class BackupCreateJob(private val context: Context, workerParams: WorkerParamete
                     .setInputData(workDataOf(IS_AUTO_BACKUP_KEY to true))
                     .build()
 
-                workManager.enqueueUniquePeriodicWork(TAG_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request)
+                context.workManager.enqueueUniquePeriodicWork(TAG_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request)
             } else {
-                workManager.cancelUniqueWork(TAG_AUTO)
+                context.workManager.cancelUniqueWork(TAG_AUTO)
             }
         }
 
@@ -95,7 +96,7 @@ class BackupCreateJob(private val context: Context, workerParams: WorkerParamete
                 .addTag(TAG_MANUAL)
                 .setInputData(inputData)
                 .build()
-            WorkManager.getInstance(context).enqueueUniqueWork(TAG_MANUAL, ExistingWorkPolicy.KEEP, request)
+            context.workManager.enqueueUniqueWork(TAG_MANUAL, ExistingWorkPolicy.KEEP, request)
         }
     }
 }

+ 5 - 7
app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreJob.kt

@@ -7,13 +7,13 @@ import androidx.work.CoroutineWorker
 import androidx.work.ExistingWorkPolicy
 import androidx.work.ForegroundInfo
 import androidx.work.OneTimeWorkRequestBuilder
-import androidx.work.WorkInfo
-import androidx.work.WorkManager
 import androidx.work.WorkerParameters
 import androidx.work.workDataOf
 import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.data.notification.Notifications
+import eu.kanade.tachiyomi.util.system.isRunning
 import eu.kanade.tachiyomi.util.system.notificationManager
+import eu.kanade.tachiyomi.util.system.workManager
 import kotlinx.coroutines.CancellationException
 import logcat.LogPriority
 import tachiyomi.core.util.system.logcat
@@ -60,8 +60,7 @@ class BackupRestoreJob(private val context: Context, workerParams: WorkerParamet
 
     companion object {
         fun isRunning(context: Context): Boolean {
-            val list = WorkManager.getInstance(context).getWorkInfosByTag(TAG).get()
-            return list.find { it.state == WorkInfo.State.RUNNING } != null
+            return context.workManager.isRunning(TAG)
         }
 
         fun start(context: Context, uri: Uri) {
@@ -72,12 +71,11 @@ class BackupRestoreJob(private val context: Context, workerParams: WorkerParamet
                 .addTag(TAG)
                 .setInputData(inputData)
                 .build()
-            WorkManager.getInstance(context)
-                .enqueueUniqueWork(TAG, ExistingWorkPolicy.KEEP, request)
+            context.workManager.enqueueUniqueWork(TAG, ExistingWorkPolicy.KEEP, request)
         }
 
         fun stop(context: Context) {
-            WorkManager.getInstance(context).cancelUniqueWork(TAG)
+            context.workManager.cancelUniqueWork(TAG)
         }
     }
 }

+ 13 - 18
app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt

@@ -11,7 +11,6 @@ import androidx.work.NetworkType
 import androidx.work.OneTimeWorkRequestBuilder
 import androidx.work.PeriodicWorkRequestBuilder
 import androidx.work.WorkInfo
-import androidx.work.WorkManager
 import androidx.work.WorkQuery
 import androidx.work.WorkerParameters
 import androidx.work.workDataOf
@@ -37,8 +36,9 @@ import eu.kanade.tachiyomi.util.shouldDownloadNewChapters
 import eu.kanade.tachiyomi.util.storage.getUriCompat
 import eu.kanade.tachiyomi.util.system.createFileInCacheDir
 import eu.kanade.tachiyomi.util.system.isConnectedToWifi
+import eu.kanade.tachiyomi.util.system.isRunning
+import eu.kanade.tachiyomi.util.system.workManager
 import kotlinx.coroutines.CancellationException
-import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.async
 import kotlinx.coroutines.awaitAll
 import kotlinx.coroutines.coroutineScope
@@ -47,7 +47,6 @@ import kotlinx.coroutines.runBlocking
 import kotlinx.coroutines.supervisorScope
 import kotlinx.coroutines.sync.Semaphore
 import kotlinx.coroutines.sync.withPermit
-import kotlinx.coroutines.withContext
 import logcat.LogPriority
 import tachiyomi.core.preference.getAndSet
 import tachiyomi.core.util.lang.withIOContext
@@ -116,13 +115,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
             }
 
             // Find a running manual worker. If exists, try again later
-            val otherRunningWorker = withContext(Dispatchers.IO) {
-                WorkManager.getInstance(context)
-                    .getWorkInfosByTag(WORK_NAME_MANUAL)
-                    .get()
-                    .find { it.state == WorkInfo.State.RUNNING }
-            }
-            if (otherRunningWorker != null) {
+            if (context.workManager.isRunning(WORK_NAME_MANUAL)) {
                 return Result.retry()
             }
         }
@@ -167,7 +160,10 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
 
     override suspend fun getForegroundInfo(): ForegroundInfo {
         val notifier = LibraryUpdateNotifier(context)
-        return ForegroundInfo(Notifications.ID_LIBRARY_PROGRESS, notifier.progressNotificationBuilder.build())
+        return ForegroundInfo(
+            Notifications.ID_LIBRARY_PROGRESS,
+            notifier.progressNotificationBuilder.build(),
+        )
     }
 
     /**
@@ -539,7 +535,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
         private const val KEY_TARGET = "target"
 
         fun cancelAllWorks(context: Context) {
-            WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
+            context.workManager.cancelAllWorkByTag(TAG)
         }
 
         fun setupTask(
@@ -568,9 +564,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
                     .setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.MINUTES)
                     .build()
 
-                WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORK_NAME_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request)
+                context.workManager.enqueueUniquePeriodicWork(WORK_NAME_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request)
             } else {
-                WorkManager.getInstance(context).cancelUniqueWork(WORK_NAME_AUTO)
+                context.workManager.cancelUniqueWork(WORK_NAME_AUTO)
             }
         }
 
@@ -579,9 +575,8 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
             category: Category? = null,
             target: Target = Target.CHAPTERS,
         ): Boolean {
-            val wm = WorkManager.getInstance(context)
-            val infos = wm.getWorkInfosByTag(TAG).get()
-            if (infos.find { it.state == WorkInfo.State.RUNNING } != null) {
+            val wm = context.workManager
+            if (wm.isRunning(TAG)) {
                 // Already running either as a scheduled or manual job
                 return false
             }
@@ -601,7 +596,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
         }
 
         fun stop(context: Context) {
-            val wm = WorkManager.getInstance(context)
+            val wm = context.workManager
             val workQuery = WorkQuery.Builder.fromTags(listOf(TAG))
                 .addStates(listOf(WorkInfo.State.RUNNING))
                 .build()

+ 13 - 0
app/src/main/java/eu/kanade/tachiyomi/util/system/WorkManagerExtensions.kt

@@ -0,0 +1,13 @@
+package eu.kanade.tachiyomi.util.system
+
+import android.content.Context
+import androidx.work.WorkInfo
+import androidx.work.WorkManager
+
+val Context.workManager: WorkManager
+    get() = WorkManager.getInstance(this)
+
+fun WorkManager.isRunning(tag: String): Boolean {
+    val list = this.getWorkInfosByTag(tag).get()
+    return list.any { it.state == WorkInfo.State.RUNNING }
+}