Let DownloadJob own the downloader lifecycle

Assisted-by: Claude:claude-opus-5-5
This commit is contained in:
AntsyLich
2026-10-01 02:40:14 +06:00
parent 6dfa10ffac
commit 01d302febd
3 changed files with 36 additions and 74 deletions
@@ -13,31 +13,27 @@ import androidx.work.WorkManager
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import dev.zacsweers.metro.Inject import dev.zacsweers.metro.Inject
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.util.system.NetworkState
import eu.kanade.tachiyomi.util.system.activeNetworkState import eu.kanade.tachiyomi.util.system.activeNetworkState
import eu.kanade.tachiyomi.util.system.networkStateFlow
import eu.kanade.tachiyomi.util.system.notificationBuilder import eu.kanade.tachiyomi.util.system.notificationBuilder
import eu.kanade.tachiyomi.util.system.setForegroundSafely import eu.kanade.tachiyomi.util.system.setForegroundSafely
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combineTransform
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import mihon.app.di.AppGraph import mihon.app.di.AppGraph
import mihon.core.metro.metroGraph import mihon.core.metro.metroGraph
import tachiyomi.domain.download.service.DownloadPreferences import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.i18n.R import tachiyomi.i18n.R
import kotlin.time.Duration.Companion.seconds
/** /**
* This worker is used to manage the downloader. The system can decide to stop the worker, in * This worker owns the lifecycle of the downloader: it starts the downloader and stops it when the
* which case the downloader is also stopped. It's also stopped while there's no network available. * worker is stopped by the system or there's no suitable network available.
*/ */
class DownloadJob(context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) { class DownloadJob(context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {
private val graph: AppGraph = context.metroGraph() private val graph: AppGraph = context.metroGraph()
@Inject private lateinit var downloadManager: DownloadManager @Inject private lateinit var downloader: Downloader
@Inject private lateinit var downloadPreferences: DownloadPreferences @Inject private lateinit var downloadPreferences: DownloadPreferences
@@ -62,48 +58,35 @@ class DownloadJob(context: Context, workerParams: WorkerParameters) : CoroutineW
} }
override suspend fun doWork(): Result { override suspend fun doWork(): Result {
var networkCheck = checkNetworkState( var networkIssue = networkIssue()
applicationContext.activeNetworkState(), if (networkIssue != null) {
downloadPreferences.downloadOnlyOverWifi.get(), downloader.stop(networkIssue)
)
var active = networkCheck && downloadManager.downloaderStart()
if (!active) {
return Result.failure() return Result.failure()
} }
setForegroundSafely() if (!downloader.start()) return Result.failure()
coroutineScope { try {
combineTransform( setForegroundSafely()
applicationContext.networkStateFlow(),
downloadPreferences.downloadOnlyOverWifi.changes(),
transform = { a, b -> emit(checkNetworkState(a, b)) },
)
.onEach { networkCheck = it }
.launchIn(this)
}
// Keep the worker running when needed while (networkIssue == null && downloader.isRunning) {
while (active) { delay(1.seconds)
active = !isStopped && downloadManager.isRunning && networkCheck networkIssue = networkIssue()
}
} finally {
if (downloader.isRunning && (networkIssue != null || isStopped)) downloader.stop(networkIssue)
} }
return Result.success() return Result.success()
} }
private fun checkNetworkState(state: NetworkState, requireWifi: Boolean): Boolean { private fun networkIssue(): String? {
return if (state.isOnline) { val state = applicationContext.activeNetworkState()
val noWifi = requireWifi && !state.isWifi return when {
if (noWifi) { !state.isOnline -> applicationContext.getString(R.string.download_notifier_no_network)
downloadManager.downloaderStop( downloadPreferences.downloadOnlyOverWifi.get() && !state.isWifi ->
applicationContext.getString(R.string.download_notifier_text_only_wifi), applicationContext.getString(R.string.download_notifier_text_only_wifi)
) else -> null
}
!noWifi
} else {
downloadManager.downloaderStop(applicationContext.getString(R.string.download_notifier_no_network))
false
} }
} }
@@ -118,18 +101,6 @@ class DownloadJob(context: Context, workerParams: WorkerParameters) : CoroutineW
.enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, request) .enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, request)
} }
fun stop(context: Context) {
WorkManager.getInstance(context)
.cancelUniqueWork(TAG)
}
fun isRunning(context: Context): Boolean {
return WorkManager.getInstance(context)
.getWorkInfosForUniqueWork(TAG)
.get()
.let { list -> list.count { it.state == WorkInfo.State.RUNNING } == 1 }
}
fun isRunningFlow(context: Context): Flow<Boolean> { fun isRunningFlow(context: Context): Flow<Boolean> {
return WorkManager.getInstance(context) return WorkManager.getInstance(context)
.getWorkInfosForUniqueWorkLiveData(TAG) .getWorkInfosForUniqueWorkLiveData(TAG)
@@ -52,30 +52,19 @@ class DownloadManager(
private val pendingDeleter: DownloadPendingDeleter, private val pendingDeleter: DownloadPendingDeleter,
) { ) {
val isRunning: Boolean
get() = downloader.isRunning
val queueState val queueState
get() = downloader.queueState get() = downloader.queueState
// For use by DownloadService only
fun downloaderStart() = downloader.start()
fun downloaderStop(reason: String? = null) = downloader.stop(reason)
val isDownloaderRunning val isDownloaderRunning
get() = DownloadJob.isRunningFlow(context) get() = DownloadJob.isRunningFlow(context)
/** /**
* Tells the downloader to begin downloads. * Starts the download worker, which runs the downloader.
*/ */
fun startDownloads() { fun startDownloads() {
if (downloader.isRunning) return if (downloader.isRunning) return
if (DownloadJob.isRunning(context)) { DownloadJob.start(context)
downloader.start()
} else {
DownloadJob.start(context)
}
} }
/** /**
@@ -141,7 +130,9 @@ class DownloadManager(
* @param autoStart whether to start the downloader after enqueing the chapters. * @param autoStart whether to start the downloader after enqueing the chapters.
*/ */
suspend fun downloadChapters(manga: Manga, chapters: List<Chapter>, autoStart: Boolean = true) { suspend fun downloadChapters(manga: Manga, chapters: List<Chapter>, autoStart: Boolean = true) {
downloader.queueChapters(manga, chapters, autoStart) if (downloader.queueChapters(manga, chapters, autoStart)) {
startDownloads()
}
} }
/** /**
@@ -155,7 +146,7 @@ class DownloadManager(
addAll(0, downloads) addAll(0, downloads)
reorderQueue(this) reorderQueue(this)
} }
if (!DownloadJob.isRunning(context)) startDownloads() startDownloads()
} }
/** /**
@@ -154,8 +154,6 @@ class Downloader(
} }
isPaused = false isPaused = false
DownloadJob.stop(context)
} }
/** /**
@@ -264,11 +262,12 @@ class Downloader(
* @param manga the manga of the chapters to download. * @param manga the manga of the chapters to download.
* @param chapters the list of chapters to download. * @param chapters the list of chapters to download.
* @param autoStart whether to start the downloader after enqueing the chapters. * @param autoStart whether to start the downloader after enqueing the chapters.
* @return true if the downloader should be started.
*/ */
suspend fun queueChapters(manga: Manga, chapters: List<Chapter>, autoStart: Boolean) { suspend fun queueChapters(manga: Manga, chapters: List<Chapter>, autoStart: Boolean): Boolean {
if (chapters.isEmpty()) return if (chapters.isEmpty()) return false
val source = sourceManager.get(manga.source) as? HttpSource ?: return val source = sourceManager.get(manga.source) as? HttpSource ?: return false
val wasEmpty = queueState.value.isEmpty() val wasEmpty = queueState.value.isEmpty()
val chaptersToQueue = chapters.asSequence() val chaptersToQueue = chapters.asSequence()
// Filter out those already downloaded. // Filter out those already downloaded.
@@ -305,9 +304,10 @@ class Downloader(
NotificationHandler.openUrl(context, LibraryUpdateNotifier.HELP_WARNING_URL), NotificationHandler.openUrl(context, LibraryUpdateNotifier.HELP_WARNING_URL),
) )
} }
DownloadJob.start(context) return true
} }
} }
return false
} }
/** /**