mirror of
https://git.mihon.tech/mihonapp/mihon
synced 2024-11-23 05:35:51 +03:00
Clean up WorkManager usages a bit
This commit is contained in:
parent
cdc160afc2
commit
859601a46e
7 changed files with 48 additions and 42 deletions
|
@ -7,11 +7,11 @@ import androidx.work.CoroutineWorker
|
||||||
import androidx.work.ExistingWorkPolicy
|
import androidx.work.ExistingWorkPolicy
|
||||||
import androidx.work.NetworkType
|
import androidx.work.NetworkType
|
||||||
import androidx.work.OneTimeWorkRequestBuilder
|
import androidx.work.OneTimeWorkRequestBuilder
|
||||||
import androidx.work.WorkManager
|
|
||||||
import androidx.work.WorkerParameters
|
import androidx.work.WorkerParameters
|
||||||
import eu.kanade.domain.track.model.toDbTrack
|
import eu.kanade.domain.track.model.toDbTrack
|
||||||
import eu.kanade.domain.track.store.DelayedTrackingStore
|
import eu.kanade.domain.track.store.DelayedTrackingStore
|
||||||
import eu.kanade.tachiyomi.data.track.TrackManager
|
import eu.kanade.tachiyomi.data.track.TrackManager
|
||||||
|
import eu.kanade.tachiyomi.util.system.workManager
|
||||||
import logcat.LogPriority
|
import logcat.LogPriority
|
||||||
import tachiyomi.core.util.lang.withIOContext
|
import tachiyomi.core.util.lang.withIOContext
|
||||||
import tachiyomi.core.util.system.logcat
|
import tachiyomi.core.util.system.logcat
|
||||||
|
@ -74,8 +74,7 @@ class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters)
|
||||||
.addTag(TAG)
|
.addTag(TAG)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
WorkManager.getInstance(context)
|
context.workManager.enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, request)
|
||||||
.enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, request)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,6 @@ import androidx.compose.ui.text.font.FontFamily
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.asFlow
|
import androidx.lifecycle.asFlow
|
||||||
import androidx.work.WorkInfo
|
import androidx.work.WorkInfo
|
||||||
import androidx.work.WorkManager
|
|
||||||
import androidx.work.WorkQuery
|
import androidx.work.WorkQuery
|
||||||
import cafe.adriel.voyager.core.model.ScreenModel
|
import cafe.adriel.voyager.core.model.ScreenModel
|
||||||
import cafe.adriel.voyager.core.model.coroutineScope
|
import cafe.adriel.voyager.core.model.coroutineScope
|
||||||
|
@ -38,6 +37,7 @@ import cafe.adriel.voyager.navigator.LocalNavigator
|
||||||
import cafe.adriel.voyager.navigator.currentOrThrow
|
import cafe.adriel.voyager.navigator.currentOrThrow
|
||||||
import eu.kanade.presentation.util.Screen
|
import eu.kanade.presentation.util.Screen
|
||||||
import eu.kanade.tachiyomi.R
|
import eu.kanade.tachiyomi.R
|
||||||
|
import eu.kanade.tachiyomi.util.system.workManager
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
@ -122,7 +122,7 @@ object WorkerInfoScreen : Screen() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Model(context: Context) : ScreenModel {
|
private class Model(context: Context) : ScreenModel {
|
||||||
private val workManager = WorkManager.getInstance(context)
|
private val workManager = context.workManager
|
||||||
|
|
||||||
val finished = workManager
|
val finished = workManager
|
||||||
.getWorkInfosLiveData(WorkQuery.fromStates(WorkInfo.State.SUCCEEDED, WorkInfo.State.FAILED, WorkInfo.State.CANCELLED))
|
.getWorkInfosLiveData(WorkQuery.fromStates(WorkInfo.State.SUCCEEDED, WorkInfo.State.FAILED, WorkInfo.State.CANCELLED))
|
||||||
|
|
|
@ -3,7 +3,6 @@ package eu.kanade.tachiyomi
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import androidx.work.WorkManager
|
|
||||||
import eu.kanade.domain.base.BasePreferences
|
import eu.kanade.domain.base.BasePreferences
|
||||||
import eu.kanade.domain.source.service.SourcePreferences
|
import eu.kanade.domain.source.service.SourcePreferences
|
||||||
import eu.kanade.domain.ui.UiPreferences
|
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.preference.plusAssign
|
||||||
import eu.kanade.tachiyomi.util.system.DeviceUtil
|
import eu.kanade.tachiyomi.util.system.DeviceUtil
|
||||||
import eu.kanade.tachiyomi.util.system.toast
|
import eu.kanade.tachiyomi.util.system.toast
|
||||||
|
import eu.kanade.tachiyomi.util.system.workManager
|
||||||
import tachiyomi.core.preference.PreferenceStore
|
import tachiyomi.core.preference.PreferenceStore
|
||||||
import tachiyomi.core.preference.getEnum
|
import tachiyomi.core.preference.getEnum
|
||||||
import tachiyomi.domain.backup.service.BackupPreferences
|
import tachiyomi.domain.backup.service.BackupPreferences
|
||||||
|
@ -331,8 +331,8 @@ object Migrations {
|
||||||
}
|
}
|
||||||
if (oldVersion < 97) {
|
if (oldVersion < 97) {
|
||||||
// Removed background jobs
|
// Removed background jobs
|
||||||
WorkManager.getInstance(context).cancelAllWorkByTag("UpdateChecker")
|
context.workManager.cancelAllWorkByTag("UpdateChecker")
|
||||||
WorkManager.getInstance(context).cancelAllWorkByTag("ExtensionUpdate")
|
context.workManager.cancelAllWorkByTag("ExtensionUpdate")
|
||||||
prefs.edit {
|
prefs.edit {
|
||||||
remove("automatic_ext_updates")
|
remove("automatic_ext_updates")
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,13 @@ import androidx.work.ExistingWorkPolicy
|
||||||
import androidx.work.ForegroundInfo
|
import androidx.work.ForegroundInfo
|
||||||
import androidx.work.OneTimeWorkRequestBuilder
|
import androidx.work.OneTimeWorkRequestBuilder
|
||||||
import androidx.work.PeriodicWorkRequestBuilder
|
import androidx.work.PeriodicWorkRequestBuilder
|
||||||
import androidx.work.WorkInfo
|
|
||||||
import androidx.work.WorkManager
|
|
||||||
import androidx.work.WorkerParameters
|
import androidx.work.WorkerParameters
|
||||||
import androidx.work.workDataOf
|
import androidx.work.workDataOf
|
||||||
import com.hippo.unifile.UniFile
|
import com.hippo.unifile.UniFile
|
||||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
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.notificationManager
|
||||||
|
import eu.kanade.tachiyomi.util.system.workManager
|
||||||
import logcat.LogPriority
|
import logcat.LogPriority
|
||||||
import tachiyomi.core.util.system.logcat
|
import tachiyomi.core.util.system.logcat
|
||||||
import tachiyomi.domain.backup.service.BackupPreferences
|
import tachiyomi.domain.backup.service.BackupPreferences
|
||||||
|
@ -55,19 +55,20 @@ class BackupCreateJob(private val context: Context, workerParams: WorkerParamete
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getForegroundInfo(): ForegroundInfo {
|
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 {
|
companion object {
|
||||||
fun isManualJobRunning(context: Context): Boolean {
|
fun isManualJobRunning(context: Context): Boolean {
|
||||||
val list = WorkManager.getInstance(context).getWorkInfosByTag(TAG_MANUAL).get()
|
return context.workManager.isRunning(TAG_MANUAL)
|
||||||
return list.find { it.state == WorkInfo.State.RUNNING } != null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setupTask(context: Context, prefInterval: Int? = null) {
|
fun setupTask(context: Context, prefInterval: Int? = null) {
|
||||||
val backupPreferences = Injekt.get<BackupPreferences>()
|
val backupPreferences = Injekt.get<BackupPreferences>()
|
||||||
val interval = prefInterval ?: backupPreferences.backupInterval().get()
|
val interval = prefInterval ?: backupPreferences.backupInterval().get()
|
||||||
val workManager = WorkManager.getInstance(context)
|
|
||||||
if (interval > 0) {
|
if (interval > 0) {
|
||||||
val request = PeriodicWorkRequestBuilder<BackupCreateJob>(
|
val request = PeriodicWorkRequestBuilder<BackupCreateJob>(
|
||||||
interval.toLong(),
|
interval.toLong(),
|
||||||
|
@ -79,9 +80,9 @@ class BackupCreateJob(private val context: Context, workerParams: WorkerParamete
|
||||||
.setInputData(workDataOf(IS_AUTO_BACKUP_KEY to true))
|
.setInputData(workDataOf(IS_AUTO_BACKUP_KEY to true))
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
workManager.enqueueUniquePeriodicWork(TAG_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request)
|
context.workManager.enqueueUniquePeriodicWork(TAG_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request)
|
||||||
} else {
|
} 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)
|
.addTag(TAG_MANUAL)
|
||||||
.setInputData(inputData)
|
.setInputData(inputData)
|
||||||
.build()
|
.build()
|
||||||
WorkManager.getInstance(context).enqueueUniqueWork(TAG_MANUAL, ExistingWorkPolicy.KEEP, request)
|
context.workManager.enqueueUniqueWork(TAG_MANUAL, ExistingWorkPolicy.KEEP, request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,13 @@ import androidx.work.CoroutineWorker
|
||||||
import androidx.work.ExistingWorkPolicy
|
import androidx.work.ExistingWorkPolicy
|
||||||
import androidx.work.ForegroundInfo
|
import androidx.work.ForegroundInfo
|
||||||
import androidx.work.OneTimeWorkRequestBuilder
|
import androidx.work.OneTimeWorkRequestBuilder
|
||||||
import androidx.work.WorkInfo
|
|
||||||
import androidx.work.WorkManager
|
|
||||||
import androidx.work.WorkerParameters
|
import androidx.work.WorkerParameters
|
||||||
import androidx.work.workDataOf
|
import androidx.work.workDataOf
|
||||||
import eu.kanade.tachiyomi.R
|
import eu.kanade.tachiyomi.R
|
||||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
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.notificationManager
|
||||||
|
import eu.kanade.tachiyomi.util.system.workManager
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import logcat.LogPriority
|
import logcat.LogPriority
|
||||||
import tachiyomi.core.util.system.logcat
|
import tachiyomi.core.util.system.logcat
|
||||||
|
@ -60,8 +60,7 @@ class BackupRestoreJob(private val context: Context, workerParams: WorkerParamet
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun isRunning(context: Context): Boolean {
|
fun isRunning(context: Context): Boolean {
|
||||||
val list = WorkManager.getInstance(context).getWorkInfosByTag(TAG).get()
|
return context.workManager.isRunning(TAG)
|
||||||
return list.find { it.state == WorkInfo.State.RUNNING } != null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun start(context: Context, uri: Uri) {
|
fun start(context: Context, uri: Uri) {
|
||||||
|
@ -72,12 +71,11 @@ class BackupRestoreJob(private val context: Context, workerParams: WorkerParamet
|
||||||
.addTag(TAG)
|
.addTag(TAG)
|
||||||
.setInputData(inputData)
|
.setInputData(inputData)
|
||||||
.build()
|
.build()
|
||||||
WorkManager.getInstance(context)
|
context.workManager.enqueueUniqueWork(TAG, ExistingWorkPolicy.KEEP, request)
|
||||||
.enqueueUniqueWork(TAG, ExistingWorkPolicy.KEEP, request)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stop(context: Context) {
|
fun stop(context: Context) {
|
||||||
WorkManager.getInstance(context).cancelUniqueWork(TAG)
|
context.workManager.cancelUniqueWork(TAG)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ import androidx.work.NetworkType
|
||||||
import androidx.work.OneTimeWorkRequestBuilder
|
import androidx.work.OneTimeWorkRequestBuilder
|
||||||
import androidx.work.PeriodicWorkRequestBuilder
|
import androidx.work.PeriodicWorkRequestBuilder
|
||||||
import androidx.work.WorkInfo
|
import androidx.work.WorkInfo
|
||||||
import androidx.work.WorkManager
|
|
||||||
import androidx.work.WorkQuery
|
import androidx.work.WorkQuery
|
||||||
import androidx.work.WorkerParameters
|
import androidx.work.WorkerParameters
|
||||||
import androidx.work.workDataOf
|
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.storage.getUriCompat
|
||||||
import eu.kanade.tachiyomi.util.system.createFileInCacheDir
|
import eu.kanade.tachiyomi.util.system.createFileInCacheDir
|
||||||
import eu.kanade.tachiyomi.util.system.isConnectedToWifi
|
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.CancellationException
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.awaitAll
|
import kotlinx.coroutines.awaitAll
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
|
@ -47,7 +47,6 @@ import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.supervisorScope
|
import kotlinx.coroutines.supervisorScope
|
||||||
import kotlinx.coroutines.sync.Semaphore
|
import kotlinx.coroutines.sync.Semaphore
|
||||||
import kotlinx.coroutines.sync.withPermit
|
import kotlinx.coroutines.sync.withPermit
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import logcat.LogPriority
|
import logcat.LogPriority
|
||||||
import tachiyomi.core.preference.getAndSet
|
import tachiyomi.core.preference.getAndSet
|
||||||
import tachiyomi.core.util.lang.withIOContext
|
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
|
// Find a running manual worker. If exists, try again later
|
||||||
val otherRunningWorker = withContext(Dispatchers.IO) {
|
if (context.workManager.isRunning(WORK_NAME_MANUAL)) {
|
||||||
WorkManager.getInstance(context)
|
|
||||||
.getWorkInfosByTag(WORK_NAME_MANUAL)
|
|
||||||
.get()
|
|
||||||
.find { it.state == WorkInfo.State.RUNNING }
|
|
||||||
}
|
|
||||||
if (otherRunningWorker != null) {
|
|
||||||
return Result.retry()
|
return Result.retry()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -167,7 +160,10 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
|
||||||
|
|
||||||
override suspend fun getForegroundInfo(): ForegroundInfo {
|
override suspend fun getForegroundInfo(): ForegroundInfo {
|
||||||
val notifier = LibraryUpdateNotifier(context)
|
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"
|
private const val KEY_TARGET = "target"
|
||||||
|
|
||||||
fun cancelAllWorks(context: Context) {
|
fun cancelAllWorks(context: Context) {
|
||||||
WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
|
context.workManager.cancelAllWorkByTag(TAG)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setupTask(
|
fun setupTask(
|
||||||
|
@ -568,9 +564,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
|
||||||
.setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.MINUTES)
|
.setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.MINUTES)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORK_NAME_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request)
|
context.workManager.enqueueUniquePeriodicWork(WORK_NAME_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request)
|
||||||
} else {
|
} 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,
|
category: Category? = null,
|
||||||
target: Target = Target.CHAPTERS,
|
target: Target = Target.CHAPTERS,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
val wm = WorkManager.getInstance(context)
|
val wm = context.workManager
|
||||||
val infos = wm.getWorkInfosByTag(TAG).get()
|
if (wm.isRunning(TAG)) {
|
||||||
if (infos.find { it.state == WorkInfo.State.RUNNING } != null) {
|
|
||||||
// Already running either as a scheduled or manual job
|
// Already running either as a scheduled or manual job
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -601,7 +596,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stop(context: Context) {
|
fun stop(context: Context) {
|
||||||
val wm = WorkManager.getInstance(context)
|
val wm = context.workManager
|
||||||
val workQuery = WorkQuery.Builder.fromTags(listOf(TAG))
|
val workQuery = WorkQuery.Builder.fromTags(listOf(TAG))
|
||||||
.addStates(listOf(WorkInfo.State.RUNNING))
|
.addStates(listOf(WorkInfo.State.RUNNING))
|
||||||
.build()
|
.build()
|
||||||
|
|
|
@ -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 }
|
||||||
|
}
|
Loading…
Reference in a new issue