Add option to skip downloading duplicate read chapters (#1125)

* Add query to get chapter count by manga and chapter number

* Add functions to get chapter count by manga and chapter number

* Only count read chapters

* Add interactor

* Savepoint

* Extract new chapter logic to separate function

* Update javadocs

* Add preference to toggle new functionality

* Add todo

* Add debug logcat

* Use string resource instead of hardcoding title

* Add temporary logcat for debugging

* Fix detekt issues

* Update javadocs

* Update download unread chapters preference

* Remove debug logcat calls

* Update javadocs

* Resolve issue where read chapters were still being downloaded during manual manga fetch

* Apply code review changes

* Apply code review changes

* Revert "Apply code review changes"

This reverts commit 1a2dce78acc66a7c529ce5b572bdaf94804b1a30.

* Revert "Apply code review changes"

This reverts commit ac2a77829313967ad39ce3cb0c0231083b9d640d.

* Group download chapter logic inside the interactor GetChaptersToDownload

* Update javadocs

* Apply code review

* Apply code review

* Apply code review

* Update CHANGELOG.md to include the new feature

* Run spotless

* Update domain/src/main/java/mihon/domain/chapter/interactor/FilterChaptersForDownload.kt

---------

Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com>
This commit is contained in:
Dani 2024-08-23 11:43:46 +02:00 committed by GitHub
parent 379d587826
commit ca968f162e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 101 additions and 45 deletions

View file

@ -0,0 +1,77 @@
package mihon.domain.chapter.interactor
import tachiyomi.domain.category.interactor.GetCategories
import tachiyomi.domain.chapter.interactor.GetChaptersByMangaId
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.manga.model.Manga
/**
* Interactor responsible for determining which chapters of a manga should be downloaded.
*
* @property getChaptersByMangaId Interactor for retrieving chapters by manga ID.
* @property downloadPreferences User preferences related to chapter downloads.
* @property getCategories Interactor for retrieving categories associated with a manga.
*/
class FilterChaptersForDownload(
private val getChaptersByMangaId: GetChaptersByMangaId,
private val downloadPreferences: DownloadPreferences,
private val getCategories: GetCategories,
) {
/**
* Determines which chapters of a manga should be downloaded based on user preferences.
*
* @param manga The manga for which chapters may be downloaded.
* @param newChapters The list of new chapters available for the manga.
* @return A list of chapters that should be downloaded
*/
suspend fun await(manga: Manga, newChapters: List<Chapter>): List<Chapter> {
if (
newChapters.isEmpty() ||
!downloadPreferences.downloadNewChapters().get() ||
!manga.shouldDownloadNewChapters()
) {
return emptyList()
}
if (!downloadPreferences.downloadNewUnreadChaptersOnly().get()) return newChapters
val readChapterNumbers = getChaptersByMangaId.await(manga.id)
.asSequence()
.filter { it.read && it.isRecognizedNumber }
.map { it.chapterNumber }
.toSet()
return newChapters.filterNot { it.chapterNumber in readChapterNumbers }
}
/**
* Determines whether new chapters should be downloaded for the manga based on user preferences and the
* categories to which the manga belongs.
*
* @return `true` if chapters of the manga should be downloaded
*/
private suspend fun Manga.shouldDownloadNewChapters(): Boolean {
if (!favorite) return false
val categories = getCategories.await(id).map { it.id }.ifEmpty { listOf(DEFAULT_CATEGORY_ID) }
val includedCategories = downloadPreferences.downloadNewChapterCategories().get().map { it.toLong() }
val excludedCategories = downloadPreferences.downloadNewChapterCategoriesExclude().get().map { it.toLong() }
return when {
// Default Download from all categories
includedCategories.isEmpty() && excludedCategories.isEmpty() -> true
// In excluded category
categories.any { it in excludedCategories } -> false
// Included category not selected
includedCategories.isEmpty() -> true
// In included category
else -> categories.any { it in includedCategories }
}
}
companion object {
private const val DEFAULT_CATEGORY_ID = 0L
}
}

View file

@ -42,4 +42,6 @@ class DownloadPreferences(
"download_new_categories_exclude",
emptySet(),
)
fun downloadNewUnreadChaptersOnly() = preferenceStore.getBoolean("download_new_unread_chapters_only", false)
}