made ReaderChapterFilter not a singleton

This commit is contained in:
Jay 2020-05-23 21:32:20 -04:00
parent 1d5fe4f775
commit 2e96a2179a
3 changed files with 21 additions and 25 deletions

View File

@ -12,7 +12,6 @@ import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.extension.ExtensionManager import eu.kanade.tachiyomi.extension.ExtensionManager
import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.reader.ReaderChapterFilter
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import uy.kohesive.injekt.api.InjektModule import uy.kohesive.injekt.api.InjektModule
@ -49,8 +48,6 @@ class AppModule(val app: Application) : InjektModule {
addSingletonFactory { Gson() } addSingletonFactory { Gson() }
addSingletonFactory { ReaderChapterFilter() }
// Asynchronously init expensive components for a faster cold start // Asynchronously init expensive components for a faster cold start
GlobalScope.launch { get<PreferencesHelper>() } GlobalScope.launch { get<PreferencesHelper>() }

View File

@ -4,25 +4,22 @@ 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.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
/** /**
* This class filters chapters for the reader based on the user enabled preferences and filters * This class filters chapters for the reader based on the user enabled preferences and filters
*/ */
class ReaderChapterFilter( class ReaderChapterFilter(
private val downloadManager: DownloadManager = Injekt.get(), private val downloadManager: DownloadManager,
private val preferences: PreferencesHelper = Injekt.get() private val preferences: PreferencesHelper
) { ) {
fun filterChapter( fun filterChapter(
dbChapters: List<Chapter>, dbChapters: List<Chapter>,
manga: Manga, manga: Manga,
chapterId: Long, selectedChapter: Chapter? = null
selectedChapter: Chapter?
): List<Chapter> { ): List<Chapter> {
// if neither preference is enabled dont even filter // if neither preference is enabled don't even filter
if (!preferences.skipRead() && !preferences.skipFiltered()) { if (!preferences.skipRead() && !preferences.skipFiltered()) {
return dbChapters return dbChapters
} }
@ -30,15 +27,6 @@ class ReaderChapterFilter(
var filteredChapters = dbChapters var filteredChapters = dbChapters
if (preferences.skipRead()) { if (preferences.skipRead()) {
filteredChapters = filteredChapters.filter { !it.read } filteredChapters = filteredChapters.filter { !it.read }
// add the selected chapter to the list in case it was read and user clicked it
if (chapterId != -1L) {
val find = filteredChapters.find { it.id == chapterId }
if (find == null) {
val mutableList = filteredChapters.toMutableList()
selectedChapter?.let { mutableList.add(it) }
filteredChapters = mutableList.toList()
}
}
} }
if (preferences.skipFiltered()) { if (preferences.skipFiltered()) {
val readEnabled = manga.readFilter == Manga.SHOW_READ val readEnabled = manga.readFilter == Manga.SHOW_READ
@ -61,6 +49,16 @@ class ReaderChapterFilter(
} }
} }
} }
// add the selected chapter to the list in case it was filtered out
if (selectedChapter?.id != null) {
val find = filteredChapters.find { it.id == selectedChapter.id }
if (find == null) {
val mutableList = filteredChapters.toMutableList()
mutableList.add(selectedChapter)
filteredChapters = mutableList.toList()
}
}
return filteredChapters return filteredChapters
} }
} }

View File

@ -51,10 +51,11 @@ class ReaderPresenter(
private val sourceManager: SourceManager = Injekt.get(), private val sourceManager: SourceManager = Injekt.get(),
private val downloadManager: DownloadManager = Injekt.get(), private val downloadManager: DownloadManager = Injekt.get(),
private val coverCache: CoverCache = Injekt.get(), private val coverCache: CoverCache = Injekt.get(),
private val preferences: PreferencesHelper = Injekt.get(), private val preferences: PreferencesHelper = Injekt.get()
private val readerChapterFilter: ReaderChapterFilter = Injekt.get()
) : BasePresenter<ReaderActivity>() { ) : BasePresenter<ReaderActivity>() {
private val readerChapterFilter = ReaderChapterFilter(downloadManager, preferences)
/** /**
* The manga loaded in the reader. It can be null when instantiated for a short time. * The manga loaded in the reader. It can be null when instantiated for a short time.
*/ */
@ -97,8 +98,8 @@ class ReaderPresenter(
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")
val chaptersForReader = readerChapterFilter val chaptersForReader =
.filterChapter(dbChapters, manga, chapterId, selectedChapter) readerChapterFilter.filterChapter(dbChapters, manga, selectedChapter)
when (manga.sorting) { when (manga.sorting) {
Manga.SORTING_SOURCE -> ChapterLoadBySource().get(chaptersForReader) Manga.SORTING_SOURCE -> ChapterLoadBySource().get(chaptersForReader)
@ -183,8 +184,8 @@ class ReaderPresenter(
val manga = manga ?: return emptyList() val manga = manga ?: return emptyList()
chapterItems = withContext(Dispatchers.IO) { chapterItems = withContext(Dispatchers.IO) {
val dbChapters = db.getChapters(manga).executeAsBlocking() val dbChapters = db.getChapters(manga).executeAsBlocking()
val list = readerChapterFilter val list =
.filterChapter(dbChapters, manga, -1L, null) readerChapterFilter.filterChapter(dbChapters, manga, getCurrentChapter()?.chapter)
.sortedBy { .sortedBy {
when (manga.sorting) { when (manga.sorting) {
Manga.SORTING_NUMBER -> it.chapter_number Manga.SORTING_NUMBER -> it.chapter_number