Make DownloadManager the sole entry point for DownloadService (#9140)
* Rename functions for DownloadService internal use * Call DownloadService.start via DownloadManager * Inline DownloadService.stop into pauseDownloads * Inline DownloadService.stop into clearQueue NotificationReceiver will now also stop the DownloadService when receiving ACTION_CLEAR_DOWNLOADS. * Provide DownloadService.isRunning via DownloadManager
This commit is contained in:
parent
7ec87e76db
commit
86b9262a7e
9 changed files with 34 additions and 44 deletions
|
@ -48,22 +48,18 @@ class DownloadManager(
|
|||
val queue: DownloadQueue
|
||||
get() = downloader.queue
|
||||
|
||||
/**
|
||||
* Tells the downloader to begin downloads.
|
||||
*
|
||||
* @return true if it's started, false otherwise (empty queue).
|
||||
*/
|
||||
fun startDownloads(): Boolean {
|
||||
return downloader.start()
|
||||
}
|
||||
// For use by DownloadService only
|
||||
fun downloaderStart() = downloader.start()
|
||||
fun downloaderStop(reason: String? = null) = downloader.stop(reason)
|
||||
|
||||
val isDownloaderRunning
|
||||
get() = DownloadService.isRunning
|
||||
|
||||
/**
|
||||
* Tells the downloader to stop downloads.
|
||||
*
|
||||
* @param reason an optional reason for being stopped, used to notify the user.
|
||||
* Tells the downloader to begin downloads.
|
||||
*/
|
||||
fun stopDownloads(reason: String? = null) {
|
||||
downloader.stop(reason)
|
||||
fun startDownloads() {
|
||||
DownloadService.start(context)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,6 +67,7 @@ class DownloadManager(
|
|||
*/
|
||||
fun pauseDownloads() {
|
||||
downloader.pause()
|
||||
DownloadService.stop(context)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,6 +75,7 @@ class DownloadManager(
|
|||
*/
|
||||
fun clearQueue() {
|
||||
downloader.clearQueue()
|
||||
DownloadService.stop(context)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -95,7 +95,7 @@ class DownloadService : Service() {
|
|||
override fun onDestroy() {
|
||||
scope.cancel()
|
||||
_isRunning.value = false
|
||||
downloadManager.stopDownloads()
|
||||
downloadManager.downloaderStop()
|
||||
if (wakeLock.isHeld) {
|
||||
wakeLock.release()
|
||||
}
|
||||
|
@ -111,8 +111,8 @@ class DownloadService : Service() {
|
|||
return null
|
||||
}
|
||||
|
||||
private fun stopDownloads(@StringRes string: Int) {
|
||||
downloadManager.stopDownloads(getString(string))
|
||||
private fun downloaderStop(@StringRes string: Int) {
|
||||
downloadManager.downloaderStop(getString(string))
|
||||
}
|
||||
|
||||
private fun listenNetworkChanges() {
|
||||
|
@ -122,13 +122,13 @@ class DownloadService : Service() {
|
|||
withUIContext {
|
||||
if (isOnline()) {
|
||||
if (downloadPreferences.downloadOnlyOverWifi().get() && !isConnectedToWifi()) {
|
||||
stopDownloads(R.string.download_notifier_text_only_wifi)
|
||||
downloaderStop(R.string.download_notifier_text_only_wifi)
|
||||
} else {
|
||||
val started = downloadManager.startDownloads()
|
||||
val started = downloadManager.downloaderStart()
|
||||
if (!started) stopSelf()
|
||||
}
|
||||
} else {
|
||||
stopDownloads(R.string.download_notifier_no_network)
|
||||
downloaderStop(R.string.download_notifier_no_network)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,6 @@ import eu.kanade.domain.track.model.toDomainTrack
|
|||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.cache.CoverCache
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
||||
import eu.kanade.tachiyomi.data.preference.DEVICE_BATTERY_NOT_LOW
|
||||
import eu.kanade.tachiyomi.data.preference.DEVICE_CHARGING
|
||||
|
@ -309,7 +308,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
|
|||
if (newUpdates.isNotEmpty()) {
|
||||
notifier.showUpdateNotifications(newUpdates)
|
||||
if (hasDownloads.get()) {
|
||||
DownloadService.start(context)
|
||||
downloadManager.startDownloads()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@ import eu.kanade.domain.download.service.DownloadPreferences
|
|||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.backup.BackupRestoreService
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
|
||||
import eu.kanade.tachiyomi.data.updater.AppUpdateService
|
||||
import eu.kanade.tachiyomi.source.SourceManager
|
||||
|
@ -56,12 +55,9 @@ class NotificationReceiver : BroadcastReceiver() {
|
|||
// Dismiss notification
|
||||
ACTION_DISMISS_NOTIFICATION -> dismissNotification(context, intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1))
|
||||
// Resume the download service
|
||||
ACTION_RESUME_DOWNLOADS -> DownloadService.start(context)
|
||||
ACTION_RESUME_DOWNLOADS -> downloadManager.startDownloads()
|
||||
// Pause the download service
|
||||
ACTION_PAUSE_DOWNLOADS -> {
|
||||
DownloadService.stop(context)
|
||||
downloadManager.pauseDownloads()
|
||||
}
|
||||
ACTION_PAUSE_DOWNLOADS -> downloadManager.pauseDownloads()
|
||||
// Clear the download queue
|
||||
ACTION_CLEAR_DOWNLOADS -> downloadManager.clearQueue()
|
||||
// Launch share activity and dismiss notification
|
||||
|
|
|
@ -31,7 +31,6 @@ import androidx.compose.ui.geometry.Offset
|
|||
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
||||
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.platform.LocalLayoutDirection
|
||||
import androidx.compose.ui.res.stringResource
|
||||
|
@ -51,7 +50,6 @@ import eu.kanade.presentation.components.AppBar
|
|||
import eu.kanade.presentation.components.OverflowMenu
|
||||
import eu.kanade.presentation.util.Screen
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.databinding.DownloadListBinding
|
||||
import tachiyomi.core.util.lang.launchUI
|
||||
import tachiyomi.presentation.core.components.Pill
|
||||
|
@ -64,7 +62,6 @@ object DownloadQueueScreen : Screen() {
|
|||
|
||||
@Composable
|
||||
override fun Content() {
|
||||
val context = LocalContext.current
|
||||
val navigator = LocalNavigator.currentOrThrow
|
||||
val scope = rememberCoroutineScope()
|
||||
val screenModel = rememberScreenModel { DownloadQueueScreenModel() }
|
||||
|
@ -182,7 +179,7 @@ object DownloadQueueScreen : Screen() {
|
|||
androidx.compose.material3.DropdownMenuItem(
|
||||
text = { Text(text = stringResource(R.string.action_cancel_all)) },
|
||||
onClick = {
|
||||
screenModel.clearQueue(context)
|
||||
screenModel.clearQueue()
|
||||
closeMenu()
|
||||
},
|
||||
)
|
||||
|
@ -198,7 +195,7 @@ object DownloadQueueScreen : Screen() {
|
|||
enter = fadeIn(),
|
||||
exit = fadeOut(),
|
||||
) {
|
||||
val isRunning by DownloadService.isRunning.collectAsState()
|
||||
val isRunning by screenModel.isDownloaderRunning.collectAsState()
|
||||
ExtendedFloatingActionButton(
|
||||
text = {
|
||||
val id = if (isRunning) {
|
||||
|
@ -218,10 +215,9 @@ object DownloadQueueScreen : Screen() {
|
|||
},
|
||||
onClick = {
|
||||
if (isRunning) {
|
||||
DownloadService.stop(context)
|
||||
screenModel.pauseDownloads()
|
||||
} else {
|
||||
DownloadService.start(context)
|
||||
screenModel.startDownloads()
|
||||
}
|
||||
},
|
||||
expanded = fabExpanded,
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
package eu.kanade.tachiyomi.ui.download
|
||||
|
||||
import android.content.Context
|
||||
import android.view.MenuItem
|
||||
import cafe.adriel.voyager.core.model.ScreenModel
|
||||
import cafe.adriel.voyager.core.model.coroutineScope
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.data.download.model.Download
|
||||
import eu.kanade.tachiyomi.databinding.DownloadListBinding
|
||||
import eu.kanade.tachiyomi.source.model.Page
|
||||
|
@ -135,15 +133,21 @@ class DownloadQueueScreenModel(
|
|||
adapter = null
|
||||
}
|
||||
|
||||
val isDownloaderRunning
|
||||
get() = downloadManager.isDownloaderRunning
|
||||
|
||||
fun getDownloadStatusFlow() = downloadManager.queue.statusFlow()
|
||||
fun getDownloadProgressFlow() = downloadManager.queue.progressFlow()
|
||||
|
||||
fun startDownloads() {
|
||||
downloadManager.startDownloads()
|
||||
}
|
||||
|
||||
fun pauseDownloads() {
|
||||
downloadManager.pauseDownloads()
|
||||
}
|
||||
|
||||
fun clearQueue(context: Context) {
|
||||
DownloadService.stop(context)
|
||||
fun clearQueue() {
|
||||
downloadManager.clearQueue()
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ import eu.kanade.presentation.manga.components.ChapterDownloadAction
|
|||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.download.DownloadCache
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.data.download.model.Download
|
||||
import eu.kanade.tachiyomi.data.track.EnhancedTrackService
|
||||
import eu.kanade.tachiyomi.data.track.TrackManager
|
||||
|
@ -589,7 +588,7 @@ class MangaInfoScreenModel(
|
|||
ChapterDownloadAction.START -> {
|
||||
startDownload(items.map { it.chapter }, false)
|
||||
if (items.any { it.downloadState == Download.State.ERROR }) {
|
||||
DownloadService.start(context)
|
||||
downloadManager.startDownloads()
|
||||
}
|
||||
}
|
||||
ChapterDownloadAction.START_NOW -> {
|
||||
|
|
|
@ -23,7 +23,6 @@ import eu.kanade.presentation.more.MoreScreen
|
|||
import eu.kanade.presentation.util.Tab
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.ui.category.CategoryScreen
|
||||
import eu.kanade.tachiyomi.ui.download.DownloadQueueScreen
|
||||
import eu.kanade.tachiyomi.ui.setting.SettingsScreen
|
||||
|
@ -94,7 +93,7 @@ private class MoreScreenModel(
|
|||
// Handle running/paused status change and queue progress updating
|
||||
coroutineScope.launchIO {
|
||||
combine(
|
||||
DownloadService.isRunning,
|
||||
downloadManager.isDownloaderRunning,
|
||||
downloadManager.queue.state,
|
||||
) { isRunning, downloadQueue -> Pair(isRunning, downloadQueue.size) }
|
||||
.collectLatest { (isDownloading, downloadQueueSize) ->
|
||||
|
|
|
@ -18,7 +18,6 @@ import eu.kanade.presentation.manga.components.ChapterDownloadAction
|
|||
import eu.kanade.presentation.updates.UpdatesUiModel
|
||||
import eu.kanade.tachiyomi.data.download.DownloadCache
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.data.download.model.Download
|
||||
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
|
||||
import eu.kanade.tachiyomi.source.SourceManager
|
||||
|
@ -168,7 +167,7 @@ class UpdatesScreenModel(
|
|||
ChapterDownloadAction.START -> {
|
||||
downloadChapters(items)
|
||||
if (items.any { it.downloadStateProvider() == Download.State.ERROR }) {
|
||||
DownloadService.start(Injekt.get<Application>())
|
||||
downloadManager.startDownloads()
|
||||
}
|
||||
}
|
||||
ChapterDownloadAction.START_NOW -> {
|
||||
|
|
Reference in a new issue