From 5a320d87e80e84814e92c1f727201eaadaff294e Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Sat, 3 Sep 2022 19:18:19 +0600 Subject: [PATCH] Remove download source folder when empty (#7928) --- .../eu/kanade/tachiyomi/data/download/DownloadCache.kt | 10 ++++++++++ .../kanade/tachiyomi/data/download/DownloadManager.kt | 4 ++++ 2 files changed, 14 insertions(+) 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 dd0e3fe1ef..9c1e090816 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 @@ -6,6 +6,7 @@ import com.hippo.unifile.UniFile import eu.kanade.domain.manga.model.Manga import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceManager import kotlinx.coroutines.flow.onEach import uy.kohesive.injekt.Injekt @@ -113,6 +114,15 @@ class DownloadCache( return 0 } + /** + * Returns true if source has download + * + * @param source the source to check. + */ + fun sourceHasDownload(source: Source): Boolean { + return rootDir.files[source.id]?.files?.size?.let { it > 0 } ?: false + } + /** * Checks if the cache needs a renewal and performs it if needed. */ 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 b30933c23f..97d9f28a37 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 @@ -281,6 +281,10 @@ class DownloadManager( cache.removeChapters(filteredChapters, manga) if (cache.getDownloadCount(manga) == 0) { // Delete manga directory if empty chapterDirs.firstOrNull()?.parentFile?.delete() + cache.removeManga(manga) + } + if (!cache.sourceHasDownload(source)) { // Delete source directory if empty + provider.findSourceDir(source)?.delete() } } return filteredChapters