Don't rely on cache when deleting empty manga folders

In case the cache hasn't actually been indexed yet. Maybe fixes #8438.

(cherry picked from commit f5873d70c640b9deb53a9a62281403b9a657e644)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt
#	app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt
This commit is contained in:
arkon 2022-11-14 22:42:36 -05:00 committed by Jobobby04
parent 1202c36a98
commit a1accd3ed3
4 changed files with 63 additions and 27 deletions

View File

@ -229,12 +229,8 @@ class DownloadCache(
}
@Synchronized
fun removeSourceIfEmpty(source: Source) {
val sourceDir = provider.findSourceDir(source)
if (sourceDir?.listFiles()?.isEmpty() == true) {
sourceDir.delete()
fun removeSource(source: Source) {
rootDownloadsDir.sourceDirs -= source.id
}
notifyChanges()
}

View File

@ -250,21 +250,27 @@ class DownloadManager(
getChaptersToDelete(chapters, manga)
}
if (filteredChapters.isNotEmpty()) {
launchIO {
removeFromDownloadQueue(filteredChapters)
val chapterDirs = provider.findChapterDirs(filteredChapters, manga, source)
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()
if (mangaDir?.listFiles()?.isEmpty() == true) {
mangaDir.delete()
cache.removeManga(manga)
}
// Delete source directory if empty
cache.removeSourceIfEmpty(source)
val sourceDir = provider.findSourceDir(source)
if (sourceDir?.listFiles()?.isEmpty() == true) {
sourceDir.delete()
cache.removeSource(source)
}
}
}
}
return filteredChapters
@ -389,13 +395,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}." }
}
}

View File

@ -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,38 @@ class DownloadPendingDeleter(context: Context) {
return newList
}
/**
* Returns a manga entry from a manga model.
*/
private fun Manga.toEntry() = MangaEntry(id, url, /* SY --> */ ogTitle /* SY <-- */, 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,
// SY -->
ogTitle = title,
// SY <--
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.
*/

View File

@ -104,9 +104,9 @@ class DownloadProvider(
* @param manga the manga of the chapter.
* @param source the source of the chapter.
*/
fun findChapterDirs(chapters: List<Chapter>, manga: Manga, source: Source): List<UniFile> {
val mangaDir = findMangaDir(/* SY --> */ manga.ogTitle /* SY <-- */, source) ?: return emptyList()
return chapters.mapNotNull { chapter ->
fun findChapterDirs(chapters: List<Chapter>, manga: Manga, source: Source): Pair<UniFile?, List<UniFile>> {
val mangaDir = findMangaDir(/* SY --> */ manga.ogTitle /* SY <-- */, source) ?: return null to emptyList()
return mangaDir to chapters.mapNotNull { chapter ->
getValidChapterDirNames(chapter.name, chapter.scanlator).asSequence()
.mapNotNull { mangaDir.findFile(it) }
.firstOrNull()