From f5873d70c640b9deb53a9a62281403b9a657e644 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 14 Nov 2022 22:42:36 -0500 Subject: [PATCH] Don't rely on cache when deleting empty manga folders In case the cache hasn't actually been indexed yet. Maybe fixes #8438. --- .../tachiyomi/data/download/DownloadCache.kt | 8 +- .../data/download/DownloadManager.kt | 34 ++++---- .../data/download/DownloadPendingDeleter.kt | 78 +++++++++---------- .../data/download/DownloadProvider.kt | 6 +- 4 files changed, 61 insertions(+), 65 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index b70dfd2a6b..a02953fb2d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -214,12 +214,8 @@ class DownloadCache( } @Synchronized - fun removeSourceIfEmpty(source: Source) { - val sourceDir = provider.findSourceDir(source) - if (sourceDir?.listFiles()?.isEmpty() == true) { - sourceDir.delete() - rootDownloadsDir.sourceDirs -= source.id - } + fun removeSource(source: Source) { + rootDownloadsDir.sourceDirs -= source.id notifyChanges() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index a2f032b229..02dab3fae4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -247,21 +247,27 @@ class DownloadManager( getChaptersToDelete(chapters, manga) } - launchIO { - removeFromDownloadQueue(filteredChapters) + if (filteredChapters.isNotEmpty()) { + launchIO { + removeFromDownloadQueue(filteredChapters) - val chapterDirs = provider.findChapterDirs(filteredChapters, manga, source) - chapterDirs.forEach { it.delete() } - cache.removeChapters(filteredChapters, manga) + val (mangaDir, chapterDirs) = provider.findChapterDirs(filteredChapters, manga, source) + chapterDirs.forEach { it.delete() } + cache.removeChapters(filteredChapters, manga) - // Delete manga directory if empty - if (cache.getDownloadCount(manga) == 0) { - chapterDirs.firstOrNull()?.parentFile?.delete() - cache.removeManga(manga) + // Delete manga directory if empty + if (mangaDir?.listFiles()?.isEmpty() == true) { + mangaDir.delete() + cache.removeManga(manga) + + // Delete source directory if empty + val sourceDir = provider.findSourceDir(source) + if (sourceDir?.listFiles()?.isEmpty() == true) { + sourceDir.delete() + cache.removeSource(source) + } + } } - - // Delete source directory if empty - cache.removeSourceIfEmpty(source) } return filteredChapters @@ -334,13 +340,13 @@ class DownloadManager( if (capitalizationChanged) { val tempName = newName + "_tmp" if (oldFolder.renameTo(tempName).not()) { - logcat(LogPriority.ERROR) { "Could not 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) { "Could not rename source download folder: ${oldFolder.name}." } + logcat(LogPriority.ERROR) { "Failed to rename source download folder: ${oldFolder.name}." } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt index 859c74e1b6..4ac80ac28c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt @@ -8,16 +8,18 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json -import uy.kohesive.injekt.injectLazy +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get /** * Class used to keep a list of chapters for future deletion. * * @param context the application context. */ -class DownloadPendingDeleter(context: Context) { - - private val json: Json by injectLazy() +class DownloadPendingDeleter( + context: Context, + private val json: Json = Injekt.get(), +) { /** * Preferences used to store the list of chapters to delete. @@ -120,6 +122,36 @@ class DownloadPendingDeleter(context: Context) { return newList } + /** + * Returns a manga entry from a manga model. + */ + private fun Manga.toEntry() = MangaEntry(id, url, title, source) + + /** + * Returns a chapter entry from a chapter model. + */ + private fun Chapter.toEntry() = ChapterEntry(id, url, name, scanlator) + + /** + * Returns a manga model from a manga entry. + */ + private fun MangaEntry.toModel() = Manga.create().copy( + url = url, + title = title, + source = source, + id = id, + ) + + /** + * Returns a chapter model from a chapter entry. + */ + private fun ChapterEntry.toModel() = Chapter.create().copy( + id = id, + url = url, + name = name, + scanlator = scanlator, + ) + /** * Class used to save an entry of chapters with their manga into preferences. */ @@ -150,42 +182,4 @@ class DownloadPendingDeleter(context: Context) { val title: String, val source: Long, ) - - /** - * Returns a manga entry from a manga model. - */ - private fun Manga.toEntry(): MangaEntry { - return MangaEntry(id, url, title, source) - } - - /** - * Returns a chapter entry from a chapter model. - */ - private fun Chapter.toEntry(): ChapterEntry { - return ChapterEntry(id!!, url, name, scanlator) - } - - /** - * Returns a manga model from a manga entry. - */ - private fun MangaEntry.toModel(): Manga { - return Manga.create().copy( - url = url, - title = title, - source = source, - id = id, - ) - } - - /** - * Returns a chapter model from a chapter entry. - */ - private fun ChapterEntry.toModel(): Chapter { - return Chapter.create().copy( - id = id, - url = url, - name = name, - scanlator = scanlator, - ) - } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt index bc0652520b..1b43ad73e2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt @@ -104,9 +104,9 @@ class DownloadProvider( * @param manga the manga of the chapter. * @param source the source of the chapter. */ - fun findChapterDirs(chapters: List, manga: Manga, source: Source): List { - val mangaDir = findMangaDir(manga.title, source) ?: return emptyList() - return chapters.mapNotNull { chapter -> + fun findChapterDirs(chapters: List, manga: Manga, source: Source): Pair> { + val mangaDir = findMangaDir(manga.title, source) ?: return null to emptyList() + return mangaDir to chapters.mapNotNull { chapter -> getValidChapterDirNames(chapter.name, chapter.scanlator).asSequence() .mapNotNull { mangaDir.findFile(it) } .firstOrNull()