Skip to content

Commit

Permalink
Adjust filtered chapter logic to be consistent between library and ma…
Browse files Browse the repository at this point in the history
…nga details
  • Loading branch information
nonproto committed Nov 26, 2024
1 parent 12d5e04 commit a3a4189
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 62 deletions.
54 changes: 27 additions & 27 deletions app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.util.chapter

import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.scanlatorList
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import org.nekomanga.constants.MdConstants
Expand Down Expand Up @@ -32,7 +31,8 @@ class ChapterFilter(
manga.bookmarkedFilter(mangaDetailsPreferences) == Manga.CHAPTER_SHOW_NOT_BOOKMARKED

// if none of the filters are enabled skip the filtering of them
val filteredChapters = filterChaptersByScanlators(chapters, manga)
val filteredChapters = filterChaptersByScanlators(chapters, manga, preferences)

return if (
readEnabled ||
unreadEnabled ||
Expand Down Expand Up @@ -60,22 +60,7 @@ class ChapterFilter(
manga: Manga,
selectedChapter: T? = null,
): List<T> {
var filteredChapters = filterChaptersByScanlators(chapters, manga)

val blockedScanlator = preferences.blockedScanlators().get()

if (blockedScanlator.isNotEmpty()) {
filteredChapters =
filteredChapters.filter { chp ->
chp.scanlatorList().none { it in blockedScanlator }
}
}

// filter out unsupported official scanlators
filteredChapters =
filteredChapters.filter { chp ->
!MdConstants.UnsupportedOfficialScanlators.contains(chp.scanlator)
}
var filteredChapters = filterChaptersByScanlators(chapters, manga, preferences)

// if filter preferences are not enabled don't even filter
if (
Expand Down Expand Up @@ -128,15 +113,30 @@ class ChapterFilter(
return filteredChapters
}

/** filters chapters for scanlators */
fun <T : Chapter> filterChaptersByScanlators(chapters: List<T>, manga: Manga): List<T> {
return manga.filtered_scanlators?.let { filteredScanlatorString ->
val filteredScanlators = ChapterUtil.getScanlators(filteredScanlatorString)
chapters.filter {
ChapterUtil.getScanlators(it.scanlator).none { group ->
filteredScanlators.contains(group)
}
/**
* filters chapters for scanlators, excludes globally blocked, unsupported and manga specific
* filtered
*/
fun <T : Chapter> filterChaptersByScanlators(
chapters: List<T>,
manga: Manga,
preferences: PreferencesHelper,
): List<T> {
val blockedGroups = preferences.blockedScanlators().get()
val filteredGroupList = ChapterUtil.getScanlators(manga.filtered_scanlators)

return chapters.filter {
val groups = ChapterUtil.getScanlators(it.scanlator)
groups.none { group ->
val inBlocked = group in blockedGroups
val inFiltered =
when (filteredGroupList.isEmpty()) {
true -> false
false -> filteredGroupList.contains(group)
}
val unsupported = group in MdConstants.UnsupportedOfficialScanlators
inBlocked || inFiltered || unsupported
}
} ?: chapters
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package eu.kanade.tachiyomi.util.chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import kotlin.collections.contains
import org.nekomanga.constants.MdConstants
import org.nekomanga.domain.chapter.ChapterItem
import org.nekomanga.domain.details.MangaDetailsPreferences
import org.nekomanga.domain.reader.ReaderPreferences
Expand Down Expand Up @@ -30,8 +32,7 @@ class ChapterItemFilter(
manga.bookmarkedFilter(mangaDetailsPreferences) == Manga.CHAPTER_SHOW_NOT_BOOKMARKED

// if none of the filters are enabled skip the filtering of them
val filteredChapters =
filterChaptersByLanguage(filterChaptersByScanlators(chapters, manga), manga)
val filteredChapters = filterChaptersByScanlatorsAndLanguage(chapters, manga, preferences)
return if (
readEnabled ||
unreadEnabled ||
Expand All @@ -42,19 +43,14 @@ class ChapterItemFilter(
) {
filteredChapters.filter { chapterItem ->
val chapter = chapterItem.chapter
if (
readEnabled && !chapter.read ||
(unreadEnabled && chapter.read) ||
(bookmarkEnabled && !chapter.bookmark) ||
(notBookmarkEnabled && chapter.bookmark) ||
(downloadEnabled &&
!downloadManager.isChapterDownloaded(chapter.toDbChapter(), manga)) ||
(notDownloadEnabled &&
downloadManager.isChapterDownloaded(chapter.toDbChapter(), manga))
) {
return@filter false
}
return@filter true
return@filter !(readEnabled && !chapter.read ||
(unreadEnabled && chapter.read) ||
(bookmarkEnabled && !chapter.bookmark) ||
(notBookmarkEnabled && chapter.bookmark) ||
(downloadEnabled &&
!downloadManager.isChapterDownloaded(chapter.toDbChapter(), manga)) ||
(notDownloadEnabled &&
downloadManager.isChapterDownloaded(chapter.toDbChapter(), manga)))
}
} else {
filteredChapters
Expand All @@ -67,7 +63,7 @@ class ChapterItemFilter(
manga: Manga,
selectedChapter: T? = null,
): List<T> {
var filteredChapters = filterChaptersByScanlators(chapters, manga)
var filteredChapters = filterChaptersByScanlatorsAndLanguage(chapters, manga, preferences)
// if neither preference is enabled don't even filter
if (!readerPreferences.skipRead().get() && !readerPreferences.skipFiltered().get()) {
return filteredChapters
Expand All @@ -93,26 +89,33 @@ class ChapterItemFilter(
}

/** filters chapters for scanlators */
fun <T : ChapterItem> filterChaptersByScanlators(chapters: List<T>, manga: Manga): List<T> {
return manga.filtered_scanlators?.let { filteredScanlatorString ->
val filteredScanlators = ChapterUtil.getScanlators(filteredScanlatorString)
chapters.filter {
ChapterUtil.getScanlators(it.chapter.scanlator).none { group ->
filteredScanlators.contains(group)
}
}
} ?: chapters
}
fun <T : ChapterItem> filterChaptersByScanlatorsAndLanguage(
chapters: List<T>,
manga: Manga,
preferences: PreferencesHelper,
): List<T> {

val blockedGroups = preferences.blockedScanlators().get()
val filteredGroupList = ChapterUtil.getScanlators(manga.filtered_scanlators)
val filteredLanguages = ChapterUtil.getLanguages(manga.filtered_language)

/** filters chapters for language */
fun <T : ChapterItem> filterChaptersByLanguage(chapters: List<T>, manga: Manga): List<T> {
return manga.filtered_language?.let { filteredLanguageString ->
val filteredLanguages = ChapterUtil.getLanguages(filteredLanguageString)
chapters.filter {
ChapterUtil.getLanguages(it.chapter.language).none { lang ->
filteredLanguages.contains(lang)
return chapters.filter {
val groups = ChapterUtil.getScanlators(it.chapter.scanlator)
val languages = ChapterUtil.getLanguages(it.chapter.language)
val foundLanguage = languages.none { language -> language in filteredLanguages }
val foundGroup =
groups.none { group ->
val inBlocked = group in blockedGroups
val inFiltered =
when (filteredGroupList.isEmpty()) {
true -> false
false -> filteredGroupList.contains(group)
}
val unsupported = group in MdConstants.UnsupportedOfficialScanlators
inBlocked || inFiltered || unsupported
}
}
} ?: chapters

foundLanguage || foundGroup
}
}
}

0 comments on commit a3a4189

Please sign in to comment.