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 574221e680..9c01afa1ba 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 @@ -4,6 +4,7 @@ import android.content.Context import com.hippo.unifile.UniFile import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.download.model.Download @@ -15,6 +16,8 @@ import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.util.lang.launchIO import rx.Observable import timber.log.Timber +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy /** @@ -24,7 +27,10 @@ import uy.kohesive.injekt.injectLazy * * @param context the application context. */ -class DownloadManager(private val context: Context) { +class DownloadManager( + private val context: Context, + private val db: DatabaseHelper = Injekt.get() +) { private val sourceManager: SourceManager by injectLazy() private val preferences: PreferencesHelper by injectLazy() @@ -241,7 +247,7 @@ class DownloadManager(private val context: Context) { val filteredChapters = if (isCancelling) { chapters } else { - getChaptersToDelete(chapters) + getChaptersToDelete(chapters, manga) } launchIO { @@ -296,7 +302,7 @@ class DownloadManager(private val context: Context) { * @param manga the manga of the chapters. */ fun enqueueDeleteChapters(chapters: List, manga: Manga) { - pendingDeleter.addChapters(getChaptersToDelete(chapters), manga) + pendingDeleter.addChapters(getChaptersToDelete(chapters, manga), manga) } /** @@ -336,8 +342,19 @@ class DownloadManager(private val context: Context) { } } - private fun getChaptersToDelete(chapters: List): List { - return if (!preferences.removeBookmarkedChapters()) { + private fun getChaptersToDelete(chapters: List, manga: Manga): List { + // Retrieve the categories that are set to exclude from being deleted on read + val categoriesToExclude = preferences.removeExcludeCategories().get().map(String::toInt) + val categoriesForManga = + manga.let { it -> + db.getCategoriesForManga(it).executeAsBlocking() + .mapNotNull { it.id } + .takeUnless { it.isEmpty() } + } ?: listOf(0) + + return if (categoriesForManga.intersect(categoriesToExclude).isNotEmpty()) { + chapters.filterNot { it.read } + } else if (!preferences.removeBookmarkedChapters()) { chapters.filterNot { it.bookmark } } else { chapters 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 9a4784caa6..02ddf2fab8 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 @@ -179,6 +179,7 @@ object PreferenceKeys { const val downloadNewCategories = "download_new_categories" const val downloadNewCategoriesExclude = "download_new_categories_exclude" + const val removeExcludeCategories = "remove_exclude_categories" const val libraryDisplayMode = "pref_display_mode_library" 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 9a77030f8c..ffb26b7bed 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 @@ -232,6 +232,8 @@ class PreferencesHelper(val context: Context) { fun removeBookmarkedChapters() = prefs.getBoolean(Keys.removeBookmarkedChapters, false) + fun removeExcludeCategories() = flowPrefs.getStringSet(Keys.removeExcludeCategories, emptySet()) + fun libraryUpdateInterval() = flowPrefs.getInt(Keys.libraryUpdateInterval, 24) fun libraryUpdateRestriction() = flowPrefs.getStringSet(Keys.libraryUpdateRestriction, setOf(UNMETERED_NETWORK)) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index 91f1c673c2..07b37c5e36 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -409,6 +409,7 @@ class ReaderPresenter( val currentChapterPosition = chapterList.indexOf(currentChapter) val removeAfterReadSlots = preferences.removeAfterReadSlots() val chapterToDelete = chapterList.getOrNull(currentChapterPosition - removeAfterReadSlots) + // Check if deleting option is enabled and chapter exists if (removeAfterReadSlots != -1 && chapterToDelete != null) { enqueueDeleteReadChapters(chapterToDelete) 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 c9a367b634..03df452ef4 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 @@ -21,6 +21,7 @@ import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.util.preference.defaultValue import eu.kanade.tachiyomi.util.preference.entriesRes import eu.kanade.tachiyomi.util.preference.intListPreference +import eu.kanade.tachiyomi.util.preference.multiSelectListPreference import eu.kanade.tachiyomi.util.preference.onClick import eu.kanade.tachiyomi.util.preference.preference import eu.kanade.tachiyomi.util.preference.preferenceCategory @@ -44,6 +45,9 @@ class SettingsDownloadController : SettingsController() { override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply { titleRes = R.string.pref_category_downloads + val dbCategories = db.getCategories().executeAsBlocking() + val categories = listOf(Category.createDefault(context)) + dbCategories + preference { key = Keys.downloadsDirectory titleRes = R.string.pref_download_directory @@ -93,10 +97,26 @@ class SettingsDownloadController : SettingsController() { titleRes = R.string.pref_remove_bookmarked_chapters defaultValue = false } - } + multiSelectListPreference { + key = Keys.removeExcludeCategories + titleRes = R.string.pref_remove_exclude_categories + entries = categories.map { it.name }.toTypedArray() + entryValues = categories.map { it.id.toString() }.toTypedArray() - val dbCategories = db.getCategories().executeAsBlocking() - val categories = listOf(Category.createDefault(context)) + dbCategories + preferences.removeExcludeCategories().asFlow() + .onEach { mutable -> + val selected = mutable + .mapNotNull { id -> categories.find { it.id == id.toInt() } } + .sortedBy { it.order } + + summary = if (selected.isEmpty()) { + resources?.getString(R.string.none) + } else { + selected.joinToString { it.name } + } + }.launchIn(viewScope) + } + } preferenceCategory { titleRes = R.string.pref_category_auto_download diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8f947d868c..59b5741670 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -382,6 +382,7 @@ After marked as read Automatically after reading Allow deleting bookmarked chapters + Categories to exclude in deleting Custom location Disabled Last read chapter