fix: Don't apply scanlator filter twice

Already handled using SQL
This commit is contained in:
Ahmad Ansori Palembani 2024-06-09 10:59:09 +07:00
parent 41622519e6
commit 44835ecb36
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
9 changed files with 35 additions and 22 deletions

View file

@ -1,11 +1,14 @@
package dev.yokai.domain.chapter.interactor package dev.yokai.domain.chapter.interactor
import dev.yokai.domain.chapter.ChapterRepository import dev.yokai.domain.chapter.ChapterRepository
import eu.kanade.tachiyomi.data.database.models.Manga
class GetChapters( class GetChapters(
private val chapterRepository: ChapterRepository, private val chapterRepository: ChapterRepository,
) { ) {
suspend fun await(mangaId: Long, filterScanlators: Boolean) = chapterRepository.getChapters(mangaId, filterScanlators) suspend fun await(mangaId: Long, filterScanlators: Boolean) = chapterRepository.getChapters(mangaId, filterScanlators)
fun subscribe(mangaId: Long, filterScanlators: Boolean) = chapterRepository.getChaptersAsFlow(mangaId, filterScanlators) suspend fun await(manga: Manga, filterScanlators: Boolean? = null) =
await(manga.id!!, filterScanlators ?: (manga.filtered_scanlators?.isNotEmpty() == true))
fun subscribe(mangaId: Long, filterScanlators: Boolean) = chapterRepository.getChaptersAsFlow(mangaId, filterScanlators)
} }

View file

@ -1136,7 +1136,7 @@ class LibraryPresenter(
/** Returns first unread chapter of a manga */ /** Returns first unread chapter of a manga */
fun getFirstUnread(manga: Manga): Chapter? { fun getFirstUnread(manga: Manga): Chapter? {
val chapters = db.getChapters(manga).executeAsBlocking() val chapters = runBlocking { getChapters.await(manga) }
return ChapterSort(manga, chapterFilter, preferences).getNextUnreadChapter(chapters, false) return ChapterSort(manga, chapterFilter, preferences).getNextUnreadChapter(chapters, false)
} }
@ -1298,7 +1298,7 @@ class LibraryPresenter(
presenterScope.launch { presenterScope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
mangaList.forEach { list -> mangaList.forEach { list ->
val chapters = runBlocking { getChapters.await(list.id!!, true) }.filter { !it.read } val chapters = runBlocking { getChapters.await(list) }.filter { !it.read }
downloadManager.downloadChapters(list, chapters) downloadManager.downloadChapters(list, chapters)
} }
} }

View file

@ -8,6 +8,7 @@ import androidx.core.view.isVisible
import com.bluelinelabs.conductor.Controller import com.bluelinelabs.conductor.Controller
import com.bluelinelabs.conductor.RouterTransaction import com.bluelinelabs.conductor.RouterTransaction
import com.bluelinelabs.conductor.changehandler.SimpleSwapChangeHandler import com.bluelinelabs.conductor.changehandler.SimpleSwapChangeHandler
import dev.yokai.domain.chapter.interactor.GetChapters
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
@ -25,10 +26,13 @@ import eu.kanade.tachiyomi.ui.source.globalsearch.GlobalSearchController
import eu.kanade.tachiyomi.util.chapter.ChapterSort import eu.kanade.tachiyomi.util.chapter.ChapterSort
import eu.kanade.tachiyomi.util.system.extensionIntentForText import eu.kanade.tachiyomi.util.system.extensionIntentForText
import eu.kanade.tachiyomi.util.view.withFadeTransaction import eu.kanade.tachiyomi.util.view.withFadeTransaction
import kotlinx.coroutines.runBlocking
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
class SearchActivity : MainActivity() { class SearchActivity : MainActivity() {
private val getChapters: GetChapters by injectLazy()
private var backToMain = false private var backToMain = false
@ -151,8 +155,8 @@ class SearchActivity : MainActivity() {
val mangaId = extras.getLong(MangaDetailsController.MANGA_EXTRA) val mangaId = extras.getLong(MangaDetailsController.MANGA_EXTRA)
if (mangaId != 0L) { if (mangaId != 0L) {
val db = Injekt.get<DatabaseHelper>() val db = Injekt.get<DatabaseHelper>()
val chapters = db.getChapters(mangaId).executeAsBlocking()
db.getManga(mangaId).executeAsBlocking()?.let { manga -> db.getManga(mangaId).executeAsBlocking()?.let { manga ->
val chapters = runBlocking { getChapters.await(manga) }
val nextUnreadChapter = ChapterSort(manga).getNextUnreadChapter(chapters, false) val nextUnreadChapter = ChapterSort(manga).getNextUnreadChapter(chapters, false)
if (nextUnreadChapter != null) { if (nextUnreadChapter != null) {
val activity = val activity =

View file

@ -25,6 +25,7 @@ class MangaDetailsAdapter(
get() = preferences.shownChapterSwipeTutorial() get() = preferences.shownChapterSwipeTutorial()
var items: List<ChapterItem> = emptyList() var items: List<ChapterItem> = emptyList()
private set
val delegate: MangaDetailsInterface = controller val delegate: MangaDetailsInterface = controller
val presenter = controller.presenter val presenter = controller.presenter
@ -54,10 +55,7 @@ class MangaDetailsAdapter(
updateDataSet(items) updateDataSet(items)
} else { } else {
updateDataSet( updateDataSet(
items.filter { items.filter { it.name.contains(s, true) },
it.name.contains(s, true) ||
it.scanlator?.contains(s, true) == true
},
) )
} }
} }

View file

@ -10,6 +10,7 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import dev.yokai.domain.chapter.interactor.GetChapters
import dev.yokai.domain.download.DownloadPreferences import dev.yokai.domain.download.DownloadPreferences
import dev.yokai.domain.storage.StorageManager import dev.yokai.domain.storage.StorageManager
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
@ -75,6 +76,7 @@ import rx.schedulers.Schedulers
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.io.File import java.io.File
import java.util.Date import java.util.Date
import java.util.concurrent.CancellationException import java.util.concurrent.CancellationException
@ -93,6 +95,7 @@ class ReaderViewModel(
private val storageManager: StorageManager = Injekt.get(), private val storageManager: StorageManager = Injekt.get(),
private val downloadPreferences: DownloadPreferences = Injekt.get(), private val downloadPreferences: DownloadPreferences = Injekt.get(),
) : ViewModel() { ) : ViewModel() {
private val getChapters: GetChapters by injectLazy()
private val mutableState = MutableStateFlow(State()) private val mutableState = MutableStateFlow(State())
val state = mutableState.asStateFlow() val state = mutableState.asStateFlow()
@ -142,7 +145,7 @@ class ReaderViewModel(
*/ */
private val chapterList by lazy { private val chapterList by lazy {
val manga = manga!! val manga = manga!!
val dbChapters = db.getChapters(manga).executeAsBlocking() val dbChapters = runBlocking { getChapters.await(manga) }
val selectedChapter = dbChapters.find { it.id == chapterId } val selectedChapter = dbChapters.find { it.id == chapterId }
?: error("Requested chapter of id $chapterId not found in chapter list") ?: error("Requested chapter of id $chapterId not found in chapter list")
@ -260,7 +263,7 @@ class ReaderViewModel(
val manga = manga ?: return emptyList() val manga = manga ?: return emptyList()
chapterItems = withContext(Dispatchers.IO) { chapterItems = withContext(Dispatchers.IO) {
val chapterSort = ChapterSort(manga, chapterFilter, preferences) val chapterSort = ChapterSort(manga, chapterFilter, preferences)
val dbChapters = db.getChapters(manga).executeAsBlocking() val dbChapters = runBlocking { getChapters.await(manga) }
chapterSort.getChaptersSorted( chapterSort.getChaptersSorted(
dbChapters, dbChapters,
filterForReader = true, filterForReader = true,

View file

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.ui.recents package eu.kanade.tachiyomi.ui.recents
import dev.yokai.domain.chapter.interactor.GetChapters
import dev.yokai.domain.recents.RecentsPreferences import dev.yokai.domain.recents.RecentsPreferences
import dev.yokai.domain.ui.UiPreferences import dev.yokai.domain.ui.UiPreferences
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
@ -32,9 +33,11 @@ import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Calendar import java.util.Calendar
import java.util.Date import java.util.Date
@ -52,6 +55,7 @@ class RecentsPresenter(
val db: DatabaseHelper = Injekt.get(), val db: DatabaseHelper = Injekt.get(),
private val chapterFilter: ChapterFilter = Injekt.get(), private val chapterFilter: ChapterFilter = Injekt.get(),
) : BaseCoroutinePresenter<RecentsController>(), DownloadQueue.DownloadListener { ) : BaseCoroutinePresenter<RecentsController>(), DownloadQueue.DownloadListener {
private val getChapters: GetChapters by injectLazy()
private var recentsJob: Job? = null private var recentsJob: Job? = null
var recentItems = listOf<RecentMangaItem>() var recentItems = listOf<RecentMangaItem>()
@ -452,14 +456,13 @@ class RecentsPresenter(
} }
private fun getNextChapter(manga: Manga): Chapter? { private fun getNextChapter(manga: Manga): Chapter? {
val chapters = db.getChapters(manga).executeAsBlocking() val chapters = runBlocking { getChapters.await(manga) }
return ChapterSort(manga, chapterFilter, preferences).getNextUnreadChapter(chapters, false) return ChapterSort(manga, chapterFilter, preferences).getNextUnreadChapter(chapters, false)
} }
private fun getFirstUpdatedChapter(manga: Manga, chapter: Chapter): Chapter? { private fun getFirstUpdatedChapter(manga: Manga, chapter: Chapter): Chapter? {
val chapters = db.getChapters(manga).executeAsBlocking() val chapters = runBlocking { getChapters.await(manga) }
return chapters return chapters
.filterChaptersByScanlators(manga)
.sortedWith(ChapterSort(manga, chapterFilter, preferences).sortComparator(true)).find { .sortedWith(ChapterSort(manga, chapterFilter, preferences).sortComparator(true)).find {
!it.read && abs(it.date_fetch - chapter.date_fetch) <= TimeUnit.HOURS.toMillis(12) !it.read && abs(it.date_fetch - chapter.date_fetch) <= TimeUnit.HOURS.toMillis(12)
} }

View file

@ -19,7 +19,7 @@ class ChapterFilter(val preferences: PreferencesHelper = Injekt.get(), val downl
val notBookmarkEnabled = manga.bookmarkedFilter(preferences) == Manga.CHAPTER_SHOW_NOT_BOOKMARKED val notBookmarkEnabled = manga.bookmarkedFilter(preferences) == Manga.CHAPTER_SHOW_NOT_BOOKMARKED
// if none of the filters are enabled skip the filtering of them // if none of the filters are enabled skip the filtering of them
val filteredChapters = chapters.filterChaptersByScanlators(manga) val filteredChapters = chapters
return if (readEnabled || unreadEnabled || downloadEnabled || notDownloadEnabled || bookmarkEnabled || notBookmarkEnabled) { return if (readEnabled || unreadEnabled || downloadEnabled || notDownloadEnabled || bookmarkEnabled || notBookmarkEnabled) {
filteredChapters.filter { filteredChapters.filter {
if (readEnabled && it.read.not() || if (readEnabled && it.read.not() ||
@ -40,7 +40,7 @@ class ChapterFilter(val preferences: PreferencesHelper = Injekt.get(), val downl
/** filter chapters for the reader */ /** filter chapters for the reader */
fun <T : Chapter> filterChaptersForReader(chapters: List<T>, manga: Manga, selectedChapter: T? = null): List<T> { fun <T : Chapter> filterChaptersForReader(chapters: List<T>, manga: Manga, selectedChapter: T? = null): List<T> {
var filteredChapters = chapters.filterChaptersByScanlators(manga) var filteredChapters = chapters
// if filter prefs aren't enabled don't even filter // if filter prefs aren't enabled don't even filter
if (!preferences.skipRead() && !preferences.skipFiltered() && !preferences.skipDupe().get()) { if (!preferences.skipRead() && !preferences.skipFiltered() && !preferences.skipDupe().get()) {
return filteredChapters return filteredChapters
@ -84,6 +84,7 @@ class ChapterFilter(val preferences: PreferencesHelper = Injekt.get(), val downl
companion object { companion object {
/** filters chapters for scanlators */ /** filters chapters for scanlators */
@Deprecated("Filter it from SQL instead if possible")
fun <T : Chapter> List<T>.filterChaptersByScanlators(manga: Manga): List<T> { fun <T : Chapter> List<T>.filterChaptersByScanlators(manga: Manga): List<T> {
return manga.filtered_scanlators?.let { filteredScanlatorString -> return manga.filtered_scanlators?.let { filteredScanlatorString ->
val filteredScanlators = ChapterUtil.getScanlators(filteredScanlatorString) val filteredScanlators = ChapterUtil.getScanlators(filteredScanlatorString)

View file

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.util.chapter
import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.chapter.ChapterFilter.Companion.filterChaptersByScanlators
import eu.kanade.tachiyomi.util.lang.compareToCaseInsensitiveNaturalOrder import eu.kanade.tachiyomi.util.lang.compareToCaseInsensitiveNaturalOrder
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -32,7 +31,7 @@ class ChapterSort(val manga: Manga, val chapterFilter: ChapterFilter = Injekt.ge
fun <T : Chapter> getNextUnreadChapter(rawChapters: List<T>, andFiltered: Boolean = true): T? { fun <T : Chapter> getNextUnreadChapter(rawChapters: List<T>, andFiltered: Boolean = true): T? {
val chapters = when { val chapters = when {
andFiltered -> chapterFilter.filterChapters(rawChapters, manga) andFiltered -> chapterFilter.filterChapters(rawChapters, manga)
else -> rawChapters.filterChaptersByScanlators(manga) else -> rawChapters
} }
return chapters.sortedWith(sortComparator(true)).find { !it.read } return chapters.sortedWith(sortComparator(true)).find { !it.read }

View file

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.util.chapter package eu.kanade.tachiyomi.util.chapter
import dev.yokai.domain.chapter.interactor.GetChapters
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
@ -8,8 +9,8 @@ import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.util.chapter.ChapterFilter.Companion.filterChaptersByScanlators import eu.kanade.tachiyomi.util.chapter.ChapterFilter.Companion.filterChaptersByScanlators
import uy.kohesive.injekt.Injekt import kotlinx.coroutines.runBlocking
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy
import java.util.Date import java.util.Date
import java.util.TreeSet import java.util.TreeSet
@ -32,9 +33,10 @@ fun syncChaptersWithSource(
throw Exception("No chapters found") throw Exception("No chapters found")
} }
val downloadManager: DownloadManager = Injekt.get() val downloadManager: DownloadManager by injectLazy()
val getChapters: GetChapters by injectLazy()
// Chapters from db. // Chapters from db.
val dbChapters = db.getChapters(manga).executeAsBlocking() val dbChapters = runBlocking { getChapters.await(manga, false) }
val sourceChapters = rawSourceChapters val sourceChapters = rawSourceChapters
.distinctBy { it.url } .distinctBy { it.url }
@ -159,7 +161,7 @@ fun syncChaptersWithSource(
db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking() db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking()
// Set this manga as updated since chapters were changed // Set this manga as updated since chapters were changed
val newestChapterDate = db.getChapters(manga).executeAsBlocking() val newestChapterDate = runBlocking { getChapters.await(manga, false) }
.maxOfOrNull { it.date_upload } ?: 0L .maxOfOrNull { it.date_upload } ?: 0L
if (newestChapterDate == 0L) { if (newestChapterDate == 0L) {
if (toAdd.isNotEmpty()) { if (toAdd.isNotEmpty()) {