Allow exclusion on Delete After Read per category (#5857)

* Added the exclude category from delete after being read

* Stopped it from adding a wildcard to the import

* Placed the remove after read to the download manager
This commit is contained in:
tobinstultiens 2021-09-12 00:09:24 +02:00 committed by GitHub
parent 7b3c18bb97
commit a051079c6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 8 deletions

View file

@ -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<Chapter>, 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<Chapter>): List<Chapter> {
return if (!preferences.removeBookmarkedChapters()) {
private fun getChaptersToDelete(chapters: List<Chapter>, manga: Manga): List<Chapter> {
// 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

View file

@ -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"

View file

@ -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))

View file

@ -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)

View file

@ -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

View file

@ -382,6 +382,7 @@
<string name="pref_remove_after_marked_as_read">After marked as read</string>
<string name="pref_remove_after_read">Automatically after reading</string>
<string name="pref_remove_bookmarked_chapters">Allow deleting bookmarked chapters</string>
<string name="pref_remove_exclude_categories">Categories to exclude in deleting</string>
<string name="custom_dir">Custom location</string>
<string name="disabled">Disabled</string>
<string name="last_read_chapter">Last read chapter</string>