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

@@ -69,14 +69,14 @@ class BackupRestoreService : Service() {
*/
private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var ioScope: CoroutineScope
private lateinit var scope: CoroutineScope
private var restorer: BackupRestorer? = null
private lateinit var notifier: BackupNotifier
override fun onCreate() {
super.onCreate()
ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
notifier = BackupNotifier(this)
wakeLock = acquireWakeLock(javaClass.name)
@@ -95,7 +95,7 @@ class BackupRestoreService : Service() {
private fun destroyJob() {
restorer?.job?.cancel()
ioScope.cancel()
scope.cancel()
if (wakeLock.isHeld) {
wakeLock.release()
}
@@ -129,7 +129,7 @@ class BackupRestoreService : Service() {
notifier.showRestoreError(exception.message)
stopSelf(startId)
}
val job = ioScope.launch(handler) {
val job = scope.launch(handler) {
if (restorer?.restoreBackup(uri) == false) {
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())
}
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)
decoder.recycle()
check(bitmap != null) { "Failed to decode image." }
check(bitmap != null) { "Failed to decode image" }
return DecodeResult(
drawable = bitmap.toDrawable(options.context.resources),

View File

@@ -84,12 +84,22 @@ class DownloadManager(
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?) {
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
val toAdd = download ?: runBlocking { Download.fromChapterId(chapterId) } ?: return
val queue = downloader.queue.toMutableList()
val queue = queue.toMutableList()
download?.let { queue.remove(it) }
queue.add(0, toAdd)
reorderQueue(queue)
@@ -112,13 +122,13 @@ class DownloadManager(
if (downloads.isEmpty()) {
DownloadService.stop(context)
downloader.queue.clear()
queue.clear()
return
}
downloader.pause()
downloader.queue.clear()
downloader.queue.addAll(downloads)
queue.clear()
queue.addAll(downloads)
if (wasRunning) {
downloader.start()
@@ -194,17 +204,6 @@ class DownloadManager(
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.
*/
@@ -221,9 +220,8 @@ class DownloadManager(
return cache.getDownloadCount(manga)
}
fun deletePendingDownloads(downloads: List<Download>) {
val domainChapters = downloads.map { it.chapter.toDomainChapter()!! }
removeFromDownloadQueue(domainChapters)
fun cancelQueuedDownloads(downloads: List<Download>) {
removeFromDownloadQueue(downloads.mapNotNull { it.chapter.toDomainChapter() })
}
/**
@@ -235,7 +233,6 @@ class DownloadManager(
*/
fun deleteChapters(chapters: List<Chapter>, manga: Manga, source: Source) {
val filteredChapters = getChaptersToDelete(chapters, manga)
if (filteredChapters.isNotEmpty()) {
launchIO {
removeFromDownloadQueue(filteredChapters)
@@ -260,7 +257,7 @@ class DownloadManager(
*/
fun deleteManga(manga: Manga, source: Source) {
launchIO {
downloader.queue.remove(manga)
queue.remove(manga)
provider.findMangaDir(manga.title, source)?.delete()
cache.removeManga(manga)
@@ -279,13 +276,13 @@ class DownloadManager(
downloader.pause()
}
downloader.queue.remove(chapters)
queue.remove(chapters)
if (wasRunning) {
if (downloader.queue.isEmpty()) {
if (queue.isEmpty()) {
DownloadService.stop(context)
downloader.stop()
} else if (downloader.queue.isNotEmpty()) {
} else if (queue.isNotEmpty()) {
downloader.start()
}
}
@@ -297,7 +294,7 @@ class DownloadManager(
* @param chapters the list of chapters to delete.
* @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)
}
@@ -326,13 +323,13 @@ class DownloadManager(
if (capitalizationChanged) {
val tempName = newName + "_tmp"
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
}
}
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.addChapter(newName, mangaDir, manga)
} 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 ioScope: CoroutineScope
private lateinit var scope: CoroutineScope
override fun onCreate() {
super.onCreate()
ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
startForeground(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS, getPlaceholderNotification())
wakeLock = acquireWakeLock(javaClass.name)
_isRunning.value = true
@@ -95,7 +95,7 @@ class DownloadService : Service() {
}
override fun onDestroy() {
ioScope?.cancel()
scope?.cancel()
_isRunning.value = false
downloadManager.stopDownloads()
wakeLock.releaseIfHeld()
@@ -140,7 +140,7 @@ class DownloadService : Service() {
stopSelf()
}
}
.launchIn(ioScope)
.launchIn(scope)
}
/**
@@ -158,7 +158,7 @@ class DownloadService : Service() {
.catch {
// Ignore errors
}
.launchIn(ioScope)
.launchIn(scope)
}
private fun PowerManager.WakeLock.releaseIfHeld() {

View File

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