From 696ffff40115fe8abfd299639c573681bfed3d80 Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 9 Aug 2020 20:10:33 -0400 Subject: [PATCH] Added option to clean cache of online covers in advanced --- .../kanade/tachiyomi/data/cache/CoverCache.kt | 33 +++++++++++++++++++ .../ui/setting/SettingsAdvancedController.kt | 9 +++++ app/src/main/res/values/strings.xml | 4 +++ 3 files changed, 46 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 4bed1dc957..8e11469190 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 @@ -60,6 +60,10 @@ class CoverCache(val context: Context) { return Formatter.formatFileSize(context, DiskUtil.getDirectorySize(cacheDir)) } + fun getOnlineCoverCacheSize(): String { + return Formatter.formatFileSize(context, DiskUtil.getDirectorySize(onlineCoverDirectory)) + } + fun deleteOldCovers() { GlobalScope.launch(Dispatchers.Default) { val db = Injekt.get() @@ -86,6 +90,35 @@ class CoverCache(val context: Context) { } } + /** + * Clear out online covers until its under a certain size + */ + fun deleteAllCachedCovers() { + GlobalScope.launch(Dispatchers.IO) { + val directory = onlineCoverDirectory + val size = DiskUtil.getDirectorySize(directory) + if (size <= maxOnlineCacheSize) { + return@launch + } + var deletedSize = 0L + val files = + directory.listFiles()?.sortedBy { it.lastModified() }?.iterator() ?: return@launch + while (files.hasNext()) { + val file = files.next() + deletedSize += file.length() + file.delete() + } + withContext(Dispatchers.Main) { + context.toast( + context.getString( + R.string.deleted_, Formatter.formatFileSize(context, deletedSize) + ) + ) + } + } + lastClean = System.currentTimeMillis() + } + /** * Clear out online covers until its under a certain size */ 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 289c532338..976aeb6d07 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 @@ -75,6 +75,15 @@ class SettingsAdvancedController : SettingsController() { coverCache.deleteOldCovers() } } + preference { + titleRes = R.string.clean_up_cached_covers_non_library + summary = context.getString(R.string.delete_all_covers__not_in_library_used_, coverCache.getOnlineCoverCacheSize()) + + onClick { + context.toast(R.string.starting_cleanup) + coverCache.deleteAllCachedCovers() + } + } preference { titleRes = R.string.clear_cookies diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9765ac7308..7b709bed17 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -562,6 +562,10 @@ Clean up cached covers Delete old and unused cached covers of manga in your library that has been updated.\nCurrently using: %1$s + Clean up cached covers not in + library + Delete all covers cached that are + not in your library \nCurrently using: %1$s Global search