From 98822a39d9524b08f5954f6bb58ac1d9c704b76d Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 19 Nov 2021 10:50:52 -0500 Subject: [PATCH] Option to clear chapter cache when MainActivity is closed (closes #5651) --- .../kanade/tachiyomi/data/preference/PreferenceKeys.kt | 2 ++ .../tachiyomi/data/preference/PreferencesHelper.kt | 2 ++ .../java/eu/kanade/tachiyomi/ui/main/MainActivity.kt | 9 ++++++++- .../tachiyomi/ui/setting/SettingsAdvancedController.kt | 5 +++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index bb85ce045f..7440d3b5cd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -233,6 +233,8 @@ object PreferenceKeys { const val verboseLogging = "verbose_logging" + const val autoClearChapterCache = "auto_clear_chapter_cache" + fun trackUsername(syncId: Int) = "pref_mangasync_username_$syncId" fun trackPassword(syncId: Int) = "pref_mangasync_password_$syncId" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index d304bb8aef..2d60d4b60d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -334,6 +334,8 @@ class PreferencesHelper(val context: Context) { fun verboseLogging() = prefs.getBoolean(Keys.verboseLogging, false) + fun autoClearChapterCache() = prefs.getBoolean(Keys.autoClearChapterCache, false) + fun setChapterSettingsDefault(manga: Manga) { prefs.edit { putInt(Keys.defaultChapterFilterByRead, manga.readFilter) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 5048fe4131..b972b1243f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -34,6 +34,7 @@ import dev.chrisbanes.insetter.applyInsetter import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.Migrations import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.cache.ChapterCache import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.data.updater.AppUpdateChecker @@ -72,6 +73,7 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.merge import kotlinx.coroutines.flow.onEach import logcat.LogPriority +import uy.kohesive.injekt.injectLazy class MainActivity : BaseViewBindingActivity() { @@ -94,6 +96,8 @@ class MainActivity : BaseViewBindingActivity() { */ private val backstackLiftState = mutableMapOf() + private val chapterCache: ChapterCache by injectLazy() + // To be checked by splash screen. If true then splash screen will be removed. var ready = false @@ -464,7 +468,10 @@ class MainActivity : BaseViewBindingActivity() { // Exit confirmation (resets after 2 seconds) lifecycleScope.launchUI { resetExitConfirmation() } } else if (backstackSize == 1 || !router.handleBack()) { - // Regular back + // Regular back (i.e. closing the app) + if (preferences.autoClearChapterCache()) { + chapterCache.clear() + } super.onBackPressed() } } 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 810a848012..4ab35e05e4 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 @@ -132,6 +132,11 @@ class SettingsAdvancedController : SettingsController() { onClick { clearChapterCache() } } + switchPreference { + key = Keys.autoClearChapterCache + titleRes = R.string.pref_auto_clear_chapter_cache + defaultValue = false + } preference { key = "pref_clear_database" titleRes = R.string.pref_clear_database diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fb1febd2dd..57dc9f0f26 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -458,6 +458,7 @@ Used: %1$s Cache cleared. %1$d files have been deleted An error occurred while clearing cache + Clear chapter cache on app close Clear database Delete history for manga that are not saved in your library Are you sure? Read chapters and progress of non-library manga will be lost