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
@@ -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()}" }
}
}
@@ -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() {