From 50405887d2de6fcf8491fbfefbd2a49924ab3385 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 9 May 2020 15:01:04 -0400 Subject: [PATCH] Option to delete old cached covers --- .../kanade/tachiyomi/data/cache/CoverCache.kt | 39 +++++++++++++++++++ .../ui/setting/SettingsAdvancedController.kt | 14 +++++++ app/src/main/res/values/strings.xml | 4 ++ 3 files changed, 57 insertions(+) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt index 2fa227146a..0fb232cc94 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt @@ -1,7 +1,18 @@ package eu.kanade.tachiyomi.data.cache import android.content.Context +import android.text.format.Formatter +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.util.storage.DiskUtil +import eu.kanade.tachiyomi.util.system.executeOnIO +import eu.kanade.tachiyomi.util.system.toast +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import java.io.File import java.io.IOException import java.io.InputStream @@ -23,6 +34,34 @@ class CoverCache(private val context: Context) { private val cacheDir = context.getExternalFilesDir("covers") ?: File(context.filesDir, "covers").also { it.mkdirs() } + fun deleteOldCovers() { + GlobalScope.launch(Dispatchers.Default) { + val db = Injekt.get() + var deletedSize = 0L + val urls = db.getLibraryMangas().executeOnIO().mapNotNull { + it.thumbnail_url?.let { url -> + return@mapNotNull DiskUtil.hashKeyForDisk(url) + } + null + } + val files = cacheDir.listFiles()?.iterator() ?: return@launch + while (files.hasNext()) { + val file = files.next() + if (file.name !in urls) { + deletedSize += file.length() + file.delete() + } + } + withContext(Dispatchers.Main) { + context.toast( + context.getString( + R.string.deleted_, Formatter.formatFileSize(context, deletedSize) + ) + ) + } + } + } + /** * Returns the cover from cache. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt index f7c427b0ad..37fc0bb6a9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt @@ -14,6 +14,7 @@ import androidx.preference.PreferenceScreen import com.afollestad.materialdialogs.MaterialDialog import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.cache.ChapterCache +import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.library.LibraryUpdateService @@ -43,6 +44,8 @@ class SettingsAdvancedController : SettingsController() { private val db: DatabaseHelper by injectLazy() + private val coverCache: CoverCache by injectLazy() + @SuppressLint("BatteryLife") override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { titleRes = R.string.advanced @@ -97,6 +100,17 @@ class SettingsAdvancedController : SettingsController() { onClick { cleanupDownloads() } } + + preference { + titleRes = R.string.clean_up_cached_covers + + summaryRes = R.string.delete_old_covers_in_library + + onClick { + context.toast(R.string.starting_cleanup) + coverCache.deleteOldCovers() + } + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { val pm = context.getSystemService(Context.POWER_SERVICE) as? PowerManager? if (pm != null) preference { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 06f011666c..afee6452d5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -537,6 +537,9 @@ Cleanup done. Removed %d folder Cleanup done. Removed %d folders + Clean up cached covers + Delete old and unused cached covers of + manga in your library that has been updated Version @@ -653,6 +656,7 @@ Create Default Delete + Deleted: %1$s Display Drag handle Edit