Allow deleting bookmarked chapters option

Some minor tweaks to this:
* tapping delete download on manga details/recents WILL delete the chapter, regardless of bookmark status
* Tapping Remove all downloads in manga details will remove all downloads still, since there's an option to remove all but bookmarks anyway (library like upstream still deletes all)

closes #1470

Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com>
This commit is contained in:
Jays2Kings 2023-02-14 13:55:33 -05:00
parent 3fecc0bedb
commit 15a16048ae
6 changed files with 33 additions and 10 deletions

View file

@ -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
@ -31,6 +32,8 @@ class DownloadManager(val context: Context) {
*/
private val sourceManager by injectLazy<SourceManager>()
private val preferences by injectLazy<PreferencesHelper>()
/**
* Downloads provider, used to retrieve the folders where the chapters are or should be stored.
*/
@ -249,16 +252,17 @@ class DownloadManager(val context: Context) {
* @param manga the manga of the chapters.
* @param source the source of the chapters.
*/
fun deleteChapters(chapters: List<Chapter>, manga: Manga, source: Source) {
fun deleteChapters(chapters: List<Chapter>, manga: Manga, source: Source, force: Boolean = false) {
val filteredChapters = if (force) chapters else getChaptersToDelete(chapters, manga)
GlobalScope.launch(Dispatchers.IO) {
val wasPaused = isPaused()
if (chapters.isEmpty()) {
if (filteredChapters.isEmpty()) {
DownloadService.stop(context)
downloader.queue.clear()
return@launch
}
downloader.pause()
downloader.queue.remove(chapters)
downloader.queue.remove(filteredChapters)
if (!wasPaused && downloader.queue.isNotEmpty()) {
downloader.start()
DownloadService.callListeners(true)
@ -268,15 +272,15 @@ class DownloadManager(val context: Context) {
DownloadService.callListeners(false)
downloader.stop()
}
queue.remove(chapters)
queue.remove(filteredChapters)
val chapterDirs =
provider.findChapterDirs(chapters, manga, source) + provider.findTempChapterDirs(
chapters,
provider.findChapterDirs(filteredChapters, manga, source) + provider.findTempChapterDirs(
filteredChapters,
manga,
source,
)
chapterDirs.forEach { it.delete() }
cache.removeChapters(chapters, manga)
cache.removeChapters(filteredChapters, manga)
if (cache.getDownloadCount(manga, true) == 0) { // Delete manga directory if empty
chapterDirs.firstOrNull()?.parentFile?.delete()
}
@ -356,7 +360,7 @@ class DownloadManager(val context: Context) {
* @param manga the manga of the chapters.
*/
fun enqueueDeleteChapters(chapters: List<Chapter>, manga: Manga) {
pendingDeleter.addChapters(chapters, manga)
pendingDeleter.addChapters(getChaptersToDelete(chapters, manga), manga)
}
/**
@ -398,4 +402,13 @@ class DownloadManager(val context: Context) {
fun addListener(listener: DownloadQueue.DownloadListener) = queue.addListener(listener)
fun removeListener(listener: DownloadQueue.DownloadListener) = queue.removeListener(listener)
private fun getChaptersToDelete(chapters: List<Chapter>, manga: Manga): List<Chapter> {
// Retrieve the categories that are set to exclude from being deleted on read
return if (!preferences.removeBookmarkedChapters().get()) {
chapters.filterNot { it.bookmark }
} else {
chapters
}
}
}

View file

@ -393,6 +393,8 @@ class PreferencesHelper(val context: Context) {
fun deleteRemovedChapters() = flowPrefs.getInt(Keys.deleteRemovedChapters, 0)
fun removeBookmarkedChapters() = flowPrefs.getBoolean("pref_remove_bookmarked", false)
fun showAllCategories() = flowPrefs.getBoolean("show_all_categories", true)
fun showAllCategoriesWhenSearchingSingleCategory() = flowPrefs.getBoolean("show_all_categories_when_searching_single_category", false)

View file

@ -280,8 +280,9 @@ class MangaDetailsPresenter(
* @param chapter the chapter to delete.
*/
fun deleteChapter(chapter: ChapterItem) {
downloadManager.deleteChapters(listOf(chapter), manga, source)
downloadManager.deleteChapters(listOf(chapter), manga, source, true)
this.chapters.find { it.id == chapter.id }?.apply {
if (chapter.chapter.bookmark && !preferences.removeBookmarkedChapters().get()) return@apply
status = Download.State.QUEUE
download = null
}
@ -303,6 +304,7 @@ class MangaDetailsPresenter(
}
chapters.forEach { chapter ->
this.chapters.find { it.id == chapter.id }?.apply {
if (chapter.chapter.bookmark && !preferences.removeBookmarkedChapters().get() && !isEverything) return@apply
status = Download.State.QUEUE
download = null
}

View file

@ -425,11 +425,12 @@ class RecentsPresenter(
fun deleteChapter(chapter: Chapter, manga: Manga, update: Boolean = true) {
val source = Injekt.get<SourceManager>().getOrStub(manga.source)
launchIO {
downloadManager.deleteChapters(listOf(chapter), manga, source)
downloadManager.deleteChapters(listOf(chapter), manga, source, true)
}
if (update) {
val item = recentItems.find { it.chapter.id == chapter.id } ?: return
item.apply {
if (chapter.bookmark && !preferences.removeBookmarkedChapters().get()) return@apply
status = Download.State.NOT_DOWNLOADED
download = null
}

View file

@ -79,6 +79,10 @@ class SettingsDownloadController : SettingsController() {
entryRange = -1..4
defaultValue = -1
}
switchPreference {
bindTo(preferences.removeBookmarkedChapters())
titleRes = R.string.allow_deleting_bookmarked_chapters
}
}
val dbCategories = db.getCategories().executeAsBlocking()

View file

@ -955,6 +955,7 @@
<string name="download_location">Download location</string>
<string name="only_download_over_wifi">Only download over Wi-Fi</string>
<string name="remove_when_marked_as_read">Remove when marked as read</string>
<string name="allow_deleting_bookmarked_chapters">Allow deleting bookmarked chapters</string>
<string name="remove_after_read">Remove after read</string>
<string name="custom_location">Custom location</string>
<string name="last_read_chapter">Last read chapter</string>