Minor cleanup

This commit is contained in:
arkon 2022-11-27 17:05:04 -05:00
parent 5076ab3049
commit 3480b45098
14 changed files with 74 additions and 91 deletions

View File

@ -107,16 +107,16 @@ private suspend fun CoroutineDispatcher.acquireTransactionThread(
try { try {
dispatch(EmptyCoroutineContext) { dispatch(EmptyCoroutineContext) {
runBlocking { runBlocking {
// Thread acquired, resume coroutine. // Thread acquired, resume coroutine
continuation.resume(coroutineContext[ContinuationInterceptor]!!) continuation.resume(coroutineContext[ContinuationInterceptor]!!)
controlJob.join() controlJob.join()
} }
} }
} catch (ex: RejectedExecutionException) { } catch (ex: RejectedExecutionException) {
// Couldn't acquire a thread, cancel coroutine. // Couldn't acquire a thread, cancel coroutine
continuation.cancel( continuation.cancel(
IllegalStateException( IllegalStateException(
"Unable to acquire a thread to perform the database transaction.", "Unable to acquire a thread to perform the database transaction",
ex, ex,
), ),
) )
@ -152,7 +152,7 @@ private class TransactionElement(
fun release() { fun release() {
val count = referenceCount.decrementAndGet() val count = referenceCount.decrementAndGet()
if (count < 0) { if (count < 0) {
throw IllegalStateException("Transaction was never started or was already released.") throw IllegalStateException("Transaction was never started or was already released")
} else if (count == 0) { } else if (count == 0) {
// Cancel the job that controls the transaction thread, causing it to be released. // Cancel the job that controls the transaction thread, causing it to be released.
transactionThreadControlJob.cancel() transactionThreadControlJob.cancel()

View File

@ -196,7 +196,7 @@ private fun rememberColumnWidthSums(
) { ) {
{ constraints -> { constraints ->
require(constraints.maxWidth != Constraints.Infinity) { require(constraints.maxWidth != Constraints.Infinity) {
"LazyVerticalGrid's width should be bound by parent." "LazyVerticalGrid's width should be bound by parent"
} }
val horizontalPadding = contentPadding.calculateStartPadding(LayoutDirection.Ltr) + val horizontalPadding = contentPadding.calculateStartPadding(LayoutDirection.Ltr) +
contentPadding.calculateEndPadding(LayoutDirection.Ltr) contentPadding.calculateEndPadding(LayoutDirection.Ltr)

View File

@ -69,14 +69,14 @@ class BackupRestoreService : Service() {
*/ */
private lateinit var wakeLock: PowerManager.WakeLock private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var ioScope: CoroutineScope private lateinit var scope: CoroutineScope
private var restorer: BackupRestorer? = null private var restorer: BackupRestorer? = null
private lateinit var notifier: BackupNotifier private lateinit var notifier: BackupNotifier
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
notifier = BackupNotifier(this) notifier = BackupNotifier(this)
wakeLock = acquireWakeLock(javaClass.name) wakeLock = acquireWakeLock(javaClass.name)
@ -95,7 +95,7 @@ class BackupRestoreService : Service() {
private fun destroyJob() { private fun destroyJob() {
restorer?.job?.cancel() restorer?.job?.cancel()
ioScope.cancel() scope.cancel()
if (wakeLock.isHeld) { if (wakeLock.isHeld) {
wakeLock.release() wakeLock.release()
} }
@ -129,7 +129,7 @@ class BackupRestoreService : Service() {
notifier.showRestoreError(exception.message) notifier.showRestoreError(exception.message)
stopSelf(startId) stopSelf(startId)
} }
val job = ioScope.launch(handler) { val job = scope.launch(handler) {
if (restorer?.restoreBackup(uri) == false) { if (restorer?.restoreBackup(uri) == false) {
notifier.showRestoreError(getString(R.string.restoring_backup_canceled)) notifier.showRestoreError(getString(R.string.restoring_backup_canceled))
} }

View File

@ -23,12 +23,12 @@ class TachiyomiImageDecoder(private val resources: ImageSource, private val opti
ImageDecoder.newInstance(it.inputStream()) ImageDecoder.newInstance(it.inputStream())
} }
check(decoder != null && decoder.width > 0 && decoder.height > 0) { "Failed to initialize decoder." } check(decoder != null && decoder.width > 0 && decoder.height > 0) { "Failed to initialize decoder" }
val bitmap = decoder.decode(rgb565 = options.allowRgb565) val bitmap = decoder.decode(rgb565 = options.allowRgb565)
decoder.recycle() decoder.recycle()
check(bitmap != null) { "Failed to decode image." } check(bitmap != null) { "Failed to decode image" }
return DecodeResult( return DecodeResult(
drawable = bitmap.toDrawable(options.context.resources), drawable = bitmap.toDrawable(options.context.resources),

View File

@ -84,12 +84,22 @@ class DownloadManager(
downloader.clearQueue(isNotification) downloader.clearQueue(isNotification)
} }
/**
* Returns the download from queue if the chapter is queued for download
* else it will return null which means that the chapter is not queued for download
*
* @param chapterId the chapter to check.
*/
fun getQueuedDownloadOrNull(chapterId: Long): Download? {
return queue.find { it.chapter.id == chapterId }
}
fun startDownloadNow(chapterId: Long?) { fun startDownloadNow(chapterId: Long?) {
if (chapterId == null) return if (chapterId == null) return
val download = downloader.queue.find { it.chapter.id == chapterId } val download = getQueuedDownloadOrNull(chapterId)
// If not in queue try to start a new download // If not in queue try to start a new download
val toAdd = download ?: runBlocking { Download.fromChapterId(chapterId) } ?: return val toAdd = download ?: runBlocking { Download.fromChapterId(chapterId) } ?: return
val queue = downloader.queue.toMutableList() val queue = queue.toMutableList()
download?.let { queue.remove(it) } download?.let { queue.remove(it) }
queue.add(0, toAdd) queue.add(0, toAdd)
reorderQueue(queue) reorderQueue(queue)
@ -112,13 +122,13 @@ class DownloadManager(
if (downloads.isEmpty()) { if (downloads.isEmpty()) {
DownloadService.stop(context) DownloadService.stop(context)
downloader.queue.clear() queue.clear()
return return
} }
downloader.pause() downloader.pause()
downloader.queue.clear() queue.clear()
downloader.queue.addAll(downloads) queue.addAll(downloads)
if (wasRunning) { if (wasRunning) {
downloader.start() downloader.start()
@ -194,17 +204,6 @@ class DownloadManager(
return cache.isChapterDownloaded(chapterName, chapterScanlator, mangaTitle, sourceId, skipCache) return cache.isChapterDownloaded(chapterName, chapterScanlator, mangaTitle, sourceId, skipCache)
} }
/**
* Returns the download from queue if the chapter is queued for download
* else it will return null which means that the chapter is not queued for download
*
* @param chapter the chapter to check.
*/
fun getChapterDownloadOrNull(chapter: Chapter): Download? {
return downloader.queue
.firstOrNull { it.chapter.id == chapter.id && it.chapter.manga_id == chapter.mangaId }
}
/** /**
* Returns the amount of downloaded chapters. * Returns the amount of downloaded chapters.
*/ */
@ -221,9 +220,8 @@ class DownloadManager(
return cache.getDownloadCount(manga) return cache.getDownloadCount(manga)
} }
fun deletePendingDownloads(downloads: List<Download>) { fun cancelQueuedDownloads(downloads: List<Download>) {
val domainChapters = downloads.map { it.chapter.toDomainChapter()!! } removeFromDownloadQueue(downloads.mapNotNull { it.chapter.toDomainChapter() })
removeFromDownloadQueue(domainChapters)
} }
/** /**
@ -235,7 +233,6 @@ class DownloadManager(
*/ */
fun deleteChapters(chapters: List<Chapter>, manga: Manga, source: Source) { fun deleteChapters(chapters: List<Chapter>, manga: Manga, source: Source) {
val filteredChapters = getChaptersToDelete(chapters, manga) val filteredChapters = getChaptersToDelete(chapters, manga)
if (filteredChapters.isNotEmpty()) { if (filteredChapters.isNotEmpty()) {
launchIO { launchIO {
removeFromDownloadQueue(filteredChapters) removeFromDownloadQueue(filteredChapters)
@ -260,7 +257,7 @@ class DownloadManager(
*/ */
fun deleteManga(manga: Manga, source: Source) { fun deleteManga(manga: Manga, source: Source) {
launchIO { launchIO {
downloader.queue.remove(manga) queue.remove(manga)
provider.findMangaDir(manga.title, source)?.delete() provider.findMangaDir(manga.title, source)?.delete()
cache.removeManga(manga) cache.removeManga(manga)
@ -279,13 +276,13 @@ class DownloadManager(
downloader.pause() downloader.pause()
} }
downloader.queue.remove(chapters) queue.remove(chapters)
if (wasRunning) { if (wasRunning) {
if (downloader.queue.isEmpty()) { if (queue.isEmpty()) {
DownloadService.stop(context) DownloadService.stop(context)
downloader.stop() downloader.stop()
} else if (downloader.queue.isNotEmpty()) { } else if (queue.isNotEmpty()) {
downloader.start() downloader.start()
} }
} }
@ -297,7 +294,7 @@ class DownloadManager(
* @param chapters the list of chapters to delete. * @param chapters the list of chapters to delete.
* @param manga the manga of the chapters. * @param manga the manga of the chapters.
*/ */
fun enqueueDeleteChapters(chapters: List<Chapter>, manga: Manga) { fun enqueueChaptersToDelete(chapters: List<Chapter>, manga: Manga) {
pendingDeleter.addChapters(getChaptersToDelete(chapters, manga), manga) pendingDeleter.addChapters(getChaptersToDelete(chapters, manga), manga)
} }
@ -326,13 +323,13 @@ class DownloadManager(
if (capitalizationChanged) { if (capitalizationChanged) {
val tempName = newName + "_tmp" val tempName = newName + "_tmp"
if (oldFolder.renameTo(tempName).not()) { if (oldFolder.renameTo(tempName).not()) {
logcat(LogPriority.ERROR) { "Failed to rename source download folder: ${oldFolder.name}." } logcat(LogPriority.ERROR) { "Failed to rename source download folder: ${oldFolder.name}" }
return return
} }
} }
if (oldFolder.renameTo(newName).not()) { if (oldFolder.renameTo(newName).not()) {
logcat(LogPriority.ERROR) { "Failed to rename source download folder: ${oldFolder.name}." } logcat(LogPriority.ERROR) { "Failed to rename source download folder: ${oldFolder.name}" }
} }
} }
@ -362,7 +359,7 @@ class DownloadManager(
cache.removeChapter(oldChapter, manga) cache.removeChapter(oldChapter, manga)
cache.addChapter(newName, mangaDir, manga) cache.addChapter(newName, mangaDir, manga)
} else { } else {
logcat(LogPriority.ERROR) { "Could not rename downloaded chapter: ${oldNames.joinToString()}." } logcat(LogPriority.ERROR) { "Could not rename downloaded chapter: ${oldNames.joinToString()}" }
} }
} }

View File

@ -82,11 +82,11 @@ class DownloadService : Service() {
*/ */
private lateinit var wakeLock: PowerManager.WakeLock private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var ioScope: CoroutineScope private lateinit var scope: CoroutineScope
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
startForeground(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS, getPlaceholderNotification()) startForeground(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS, getPlaceholderNotification())
wakeLock = acquireWakeLock(javaClass.name) wakeLock = acquireWakeLock(javaClass.name)
_isRunning.value = true _isRunning.value = true
@ -95,7 +95,7 @@ class DownloadService : Service() {
} }
override fun onDestroy() { override fun onDestroy() {
ioScope?.cancel() scope?.cancel()
_isRunning.value = false _isRunning.value = false
downloadManager.stopDownloads() downloadManager.stopDownloads()
wakeLock.releaseIfHeld() wakeLock.releaseIfHeld()
@ -140,7 +140,7 @@ class DownloadService : Service() {
stopSelf() stopSelf()
} }
} }
.launchIn(ioScope) .launchIn(scope)
} }
/** /**
@ -158,7 +158,7 @@ class DownloadService : Service() {
.catch { .catch {
// Ignore errors // Ignore errors
} }
.launchIn(ioScope) .launchIn(scope)
} }
private fun PowerManager.WakeLock.releaseIfHeld() { private fun PowerManager.WakeLock.releaseIfHeld() {

View File

@ -100,7 +100,7 @@ class LibraryUpdateService(
private lateinit var wakeLock: PowerManager.WakeLock private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var notifier: LibraryUpdateNotifier private lateinit var notifier: LibraryUpdateNotifier
private var ioScope: CoroutineScope? = null private var scope: CoroutineScope? = null
private var mangaToUpdate: List<LibraryManga> = mutableListOf() private var mangaToUpdate: List<LibraryManga> = mutableListOf()
private var updateJob: Job? = null private var updateJob: Job? = null
@ -188,7 +188,7 @@ class LibraryUpdateService(
*/ */
override fun onDestroy() { override fun onDestroy() {
updateJob?.cancel() updateJob?.cancel()
ioScope?.cancel() scope?.cancel()
if (wakeLock.isHeld) { if (wakeLock.isHeld) {
wakeLock.release() wakeLock.release()
} }
@ -220,7 +220,7 @@ class LibraryUpdateService(
// Unsubscribe from any previous subscription if needed // Unsubscribe from any previous subscription if needed
updateJob?.cancel() updateJob?.cancel()
ioScope?.cancel() scope?.cancel()
// If this is a chapter update; set the last update time to now // If this is a chapter update; set the last update time to now
if (target == Target.CHAPTERS) { if (target == Target.CHAPTERS) {
@ -236,8 +236,8 @@ class LibraryUpdateService(
logcat(LogPriority.ERROR, exception) logcat(LogPriority.ERROR, exception)
stopSelf(startId) stopSelf(startId)
} }
ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
updateJob = ioScope?.launch(handler) { updateJob = scope?.launch(handler) {
when (target) { when (target) {
Target.CHAPTERS -> updateChapterList() Target.CHAPTERS -> updateChapterList()
Target.COVERS -> updateCovers() Target.COVERS -> updateCovers()

View File

@ -20,7 +20,7 @@ import java.io.InputStream
class ShizukuInstaller(private val service: Service) : Installer(service) { class ShizukuInstaller(private val service: Service) : Installer(service) {
private val ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private val shizukuDeadListener = Shizuku.OnBinderDeadListener { private val shizukuDeadListener = Shizuku.OnBinderDeadListener {
logcat { "Shizuku was killed prematurely" } logcat { "Shizuku was killed prematurely" }
@ -46,7 +46,7 @@ class ShizukuInstaller(private val service: Service) : Installer(service) {
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
override fun processEntry(entry: Entry) { override fun processEntry(entry: Entry) {
super.processEntry(entry) super.processEntry(entry)
ioScope.launch { scope.launch {
var sessionId: String? = null var sessionId: String? = null
try { try {
val size = service.getUriSize(entry.uri) ?: throw IllegalStateException() val size = service.getUriSize(entry.uri) ?: throw IllegalStateException()
@ -88,7 +88,7 @@ class ShizukuInstaller(private val service: Service) : Installer(service) {
override fun onDestroy() { override fun onDestroy() {
Shizuku.removeBinderDeadListener(shizukuDeadListener) Shizuku.removeBinderDeadListener(shizukuDeadListener)
Shizuku.removeRequestPermissionResultListener(shizukuPermissionListener) Shizuku.removeRequestPermissionResultListener(shizukuPermissionListener)
ioScope.cancel() scope.cancel()
super.onDestroy() super.onDestroy()
} }
@ -116,7 +116,7 @@ class ShizukuInstaller(private val service: Service) : Installer(service) {
false false
} }
} else { } else {
logcat(LogPriority.ERROR) { "Shizuku is not ready to use." } logcat(LogPriority.ERROR) { "Shizuku is not ready to use" }
service.toast(R.string.ext_installer_shizuku_stopped) service.toast(R.string.ext_installer_shizuku_stopped)
service.stopSelf() service.stopSelf()
false false

View File

@ -479,7 +479,7 @@ class DownloadController :
presenter.reorder(selectedSeries + otherSeries) presenter.reorder(selectedSeries + otherSeries)
} }
R.id.cancel_download -> { R.id.cancel_download -> {
presenter.cancelDownloads(listOf(item.download)) presenter.cancel(listOf(item.download))
} }
R.id.cancel_series -> { R.id.cancel_series -> {
val allDownloadsForSeries = adapter?.currentItems val allDownloadsForSeries = adapter?.currentItems
@ -487,7 +487,7 @@ class DownloadController :
?.filter { item.download.manga.id == it.download.manga.id } ?.filter { item.download.manga.id == it.download.manga.id }
?.map(DownloadItem::download) ?.map(DownloadItem::download)
if (!allDownloadsForSeries.isNullOrEmpty()) { if (!allDownloadsForSeries.isNullOrEmpty()) {
presenter.cancelDownloads(allDownloadsForSeries) presenter.cancel(allDownloadsForSeries)
} }
} }
} }

View File

@ -5,7 +5,6 @@ import android.os.Bundle
import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.download.DownloadService import eu.kanade.tachiyomi.data.download.DownloadService
import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.download.model.DownloadQueue
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@ -15,17 +14,12 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import logcat.LogPriority import logcat.LogPriority
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class DownloadPresenter : BasePresenter<DownloadController>() { class DownloadPresenter(
private val downloadManager: DownloadManager = Injekt.get(),
val downloadManager: DownloadManager by injectLazy() ) : BasePresenter<DownloadController>() {
/**
* Property to get the queue from the download manager.
*/
private val downloadQueue: DownloadQueue
get() = downloadManager.queue
private val _state = MutableStateFlow(emptyList<DownloadHeaderItem>()) private val _state = MutableStateFlow(emptyList<DownloadHeaderItem>())
val state = _state.asStateFlow() val state = _state.asStateFlow()
@ -34,7 +28,7 @@ class DownloadPresenter : BasePresenter<DownloadController>() {
super.onCreate(savedState) super.onCreate(savedState)
presenterScope.launch { presenterScope.launch {
downloadQueue.updates downloadManager.queue.updates
.catch { logcat(LogPriority.ERROR, it) } .catch { logcat(LogPriority.ERROR, it) }
.map { downloads -> .map { downloads ->
downloads downloads
@ -49,20 +43,13 @@ class DownloadPresenter : BasePresenter<DownloadController>() {
} }
} }
fun getDownloadStatusFlow() = downloadQueue.statusFlow() fun getDownloadStatusFlow() = downloadManager.queue.statusFlow()
fun getDownloadProgressFlow() = downloadManager.queue.progressFlow()
fun getDownloadProgressFlow() = downloadQueue.progressFlow()
/**
* Pauses the download queue.
*/
fun pauseDownloads() { fun pauseDownloads() {
downloadManager.pauseDownloads() downloadManager.pauseDownloads()
} }
/**
* Clears the download queue.
*/
fun clearQueue(context: Context) { fun clearQueue(context: Context) {
DownloadService.stop(context) DownloadService.stop(context)
downloadManager.clearQueue() downloadManager.clearQueue()
@ -72,7 +59,7 @@ class DownloadPresenter : BasePresenter<DownloadController>() {
downloadManager.reorderQueue(downloads) downloadManager.reorderQueue(downloads)
} }
fun cancelDownloads(downloads: List<Download>) { fun cancel(downloads: List<Download>) {
downloadManager.deletePendingDownloads(downloads) downloadManager.cancelQueuedDownloads(downloads)
} }
} }

View File

@ -496,7 +496,7 @@ class LibraryScreenModel(
mangas.forEach { manga -> mangas.forEach { manga ->
val chapters = getNextChapters.await(manga.id) val chapters = getNextChapters.await(manga.id)
.fastFilterNot { chapter -> .fastFilterNot { chapter ->
downloadManager.queue.any { chapter.id == it.chapter.id } || downloadManager.getQueuedDownloadOrNull(chapter.id) != null ||
downloadManager.isChapterDownloaded( downloadManager.isChapterDownloaded(
chapter.name, chapter.name,
chapter.scanlator, chapter.scanlator,

View File

@ -46,7 +46,6 @@ import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.network.HttpException import eu.kanade.tachiyomi.network.HttpException
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.isLocal
import eu.kanade.tachiyomi.ui.manga.track.TrackItem import eu.kanade.tachiyomi.ui.manga.track.TrackItem
import eu.kanade.tachiyomi.util.chapter.getChapterSort import eu.kanade.tachiyomi.util.chapter.getChapterSort
import eu.kanade.tachiyomi.util.chapter.getNextUnread import eu.kanade.tachiyomi.util.chapter.getNextUnread
@ -502,7 +501,7 @@ class MangaInfoScreenModel(
val activeDownload = if (isLocal) { val activeDownload = if (isLocal) {
null null
} else { } else {
downloadManager.queue.find { chapter.id == it.chapter.id } downloadManager.getQueuedDownloadOrNull(chapter.id)
} }
val downloaded = if (isLocal) { val downloaded = if (isLocal) {
true true
@ -668,8 +667,8 @@ class MangaInfoScreenModel(
} }
fun cancelDownload(chapterId: Long) { fun cancelDownload(chapterId: Long) {
val activeDownload = downloadManager.queue.find { chapterId == it.chapter.id } ?: return val activeDownload = downloadManager.getQueuedDownloadOrNull(chapterId) ?: return
downloadManager.deletePendingDownloads(listOf(activeDownload)) downloadManager.cancelQueuedDownloads(listOf(activeDownload))
updateDownloadState(activeDownload.apply { status = Download.State.NOT_DOWNLOADED }) updateDownloadState(activeDownload.apply { status = Download.State.NOT_DOWNLOADED })
} }

View File

@ -339,7 +339,7 @@ class ReaderPresenter(
newChapters.ref() newChapters.ref()
oldChapters?.unref() oldChapters?.unref()
chapterToDownload = deleteChapterFromDownloadQueue(newChapters.currChapter) chapterToDownload = cancelQueuedDownloads(newChapters.currChapter)
viewerChaptersRelay.call(newChapters) viewerChaptersRelay.call(newChapters)
} }
} }
@ -489,9 +489,9 @@ class ReaderPresenter(
* Removes [currentChapter] from download queue * Removes [currentChapter] from download queue
* if setting is enabled and [currentChapter] is queued for download * if setting is enabled and [currentChapter] is queued for download
*/ */
private fun deleteChapterFromDownloadQueue(currentChapter: ReaderChapter): Download? { private fun cancelQueuedDownloads(currentChapter: ReaderChapter): Download? {
return downloadManager.getChapterDownloadOrNull(currentChapter.chapter.toDomainChapter()!!)?.also { return downloadManager.getQueuedDownloadOrNull(currentChapter.chapter.id!!.toLong())?.also {
downloadManager.deletePendingDownloads(listOf(it)) downloadManager.cancelQueuedDownloads(listOf(it))
} }
} }
@ -875,7 +875,7 @@ class ReaderPresenter(
val manga = manga ?: return val manga = manga ?: return
presenterScope.launchNonCancellable { presenterScope.launchNonCancellable {
downloadManager.enqueueDeleteChapters(listOf(chapter.chapter.toDomainChapter()!!), manga.toDomainManga()!!) downloadManager.enqueueChaptersToDelete(listOf(chapter.chapter.toDomainChapter()!!), manga.toDomainManga()!!)
} }
} }

View File

@ -116,7 +116,7 @@ class UpdatesScreenModel(
private fun List<UpdatesWithRelations>.toUpdateItems(): List<UpdatesItem> { private fun List<UpdatesWithRelations>.toUpdateItems(): List<UpdatesItem> {
return this.map { return this.map {
val activeDownload = downloadManager.queue.find { download -> it.chapterId == download.chapter.id } val activeDownload = downloadManager.getQueuedDownloadOrNull(it.chapterId)
val downloaded = downloadManager.isChapterDownloaded( val downloaded = downloadManager.isChapterDownloaded(
it.chapterName, it.chapterName,
it.scanlator, it.scanlator,
@ -200,8 +200,8 @@ class UpdatesScreenModel(
} }
private fun cancelDownload(chapterId: Long) { private fun cancelDownload(chapterId: Long) {
val activeDownload = downloadManager.queue.find { chapterId == it.chapter.id } ?: return val activeDownload = downloadManager.getQueuedDownloadOrNull(chapterId) ?: return
downloadManager.deletePendingDownloads(listOf(activeDownload)) downloadManager.cancelQueuedDownloads(listOf(activeDownload))
updateDownloadState(activeDownload.apply { status = Download.State.NOT_DOWNLOADED }) updateDownloadState(activeDownload.apply { status = Download.State.NOT_DOWNLOADED })
} }