From dbaac69fada009fdf92500303e89f864ac3fdf48 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 9 Aug 2020 12:18:05 -0400 Subject: [PATCH] Add option to prevent deleting bookmarked chapters (closes #2082) --- .../tachiyomi/data/download/DownloadManager.kt | 18 ++++++++++++------ .../data/preference/PreferenceKeys.kt | 2 ++ .../data/preference/PreferencesHelper.kt | 2 ++ .../tachiyomi/ui/manga/MangaPresenter.kt | 10 ++++++++-- .../ui/setting/SettingsDownloadController.kt | 5 +++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 30 insertions(+), 8 deletions(-) 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 240dbc2c75..d8a8368ac0 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 @@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.download.model.DownloadQueue +import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.model.Page @@ -24,10 +25,8 @@ import uy.kohesive.injekt.injectLazy */ class DownloadManager(private val context: Context) { - /** - * The sources manager. - */ - private val sourceManager by injectLazy() + private val sourceManager: SourceManager by injectLazy() + private val preferences: PreferencesHelper by injectLazy() /** * Downloads provider, used to retrieve the folders where the chapters are or should be stored. @@ -201,9 +200,16 @@ class DownloadManager(private val context: Context) { */ fun deleteChapters(chapters: List, manga: Manga, source: Source) { queue.remove(chapters) - val chapterDirs = provider.findChapterDirs(chapters, manga, source) + + val filteredChapters = if (!preferences.removeBookmarkedChapters()) { + chapters.filterNot { it.bookmark } + } else { + chapters + } + + val chapterDirs = provider.findChapterDirs(filteredChapters, manga, source) chapterDirs.forEach { it.delete() } - cache.removeChapters(chapters, manga) + cache.removeChapters(filteredChapters, manga) if (cache.getDownloadCount(manga) == 0) { // Delete manga directory if empty chapterDirs.firstOrNull()?.parentFile?.delete() } 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 818c6c7183..2c7f66dcf1 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 @@ -97,6 +97,8 @@ object PreferenceKeys { const val removeAfterMarkedAsRead = "pref_remove_after_marked_as_read_key" + const val removeBookmarkedChapters = "pref_remove_bookmarked" + const val libraryUpdateInterval = "pref_library_update_interval_key" const val libraryUpdateRestriction = "library_update_restriction" 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 f7a0ecd59a..11bffb1863 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 @@ -188,6 +188,8 @@ class PreferencesHelper(val context: Context) { fun removeAfterMarkedAsRead() = prefs.getBoolean(Keys.removeAfterMarkedAsRead, false) + fun removeBookmarkedChapters() = prefs.getBoolean(Keys.removeBookmarkedChapters, false) + fun libraryUpdateInterval() = flowPrefs.getInt(Keys.libraryUpdateInterval, 24) fun libraryUpdateRestriction() = prefs.getStringSet(Keys.libraryUpdateRestriction, setOf("wifi")) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index 196f26efa5..a3ea29a820 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt @@ -494,8 +494,14 @@ class MangaPresenter( * @param chapters the chapters to delete. */ private fun deleteChaptersInternal(chapters: List) { - downloadManager.deleteChapters(chapters, manga, source) - chapters.forEach { + val filteredChapters = if (!preferences.removeBookmarkedChapters()) { + chapters.filterNot { it.bookmark } + } else { + chapters + } + + downloadManager.deleteChapters(filteredChapters, manga, source) + filteredChapters.forEach { it.status = Download.NOT_DOWNLOADED it.download = null } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt index 3916725b36..82c055a0fc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt @@ -84,6 +84,11 @@ class SettingsDownloadController : SettingsController() { defaultValue = "-1" summary = "%s" } + switchPreference { + key = Keys.removeBookmarkedChapters + titleRes = R.string.pref_remove_bookmarked_chapters + defaultValue = false + } } val dbCategories = db.getCategories().executeAsBlocking() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c6f6a9dfae..98b69261f8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -314,6 +314,7 @@ Delete chapters After manually marked as read After reading + Delete bookmarked chapters Custom location Disabled Last read chapter