diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index 5650b1b80..d7125640a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -690,7 +690,7 @@ class MangaController : fun onNextChapters(chapters: List) { // If the list is empty and it hasn't requested previously, fetch chapters from source // We use presenter chapters instead because they are always unfiltered - if (!presenter.hasRequested && presenter.chapters.isEmpty()) { + if (!presenter.hasRequested && presenter.allChapters.isEmpty()) { fetchChaptersFromSource() } @@ -1029,8 +1029,8 @@ class MangaController : showCustomDownloadDialog() return } - R.id.download_unread -> presenter.chapters.filter { !it.read } - R.id.download_all -> presenter.chapters + R.id.download_unread -> presenter.allChapters.filter { !it.read } + R.id.download_all -> presenter.allChapters else -> emptyList() } if (chaptersToDownload.isNotEmpty()) { @@ -1042,7 +1042,7 @@ class MangaController : private fun showCustomDownloadDialog() { DownloadCustomChaptersDialog( this, - presenter.chapters.size + presenter.allChapters.size ).showDialog(router) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index 40f79fe10..cffd365f2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt @@ -66,10 +66,9 @@ class MangaPresenter( */ private var fetchMangaJob: Job? = null - /** - * List of chapters of the manga. It's always unfiltered and unsorted. - */ - var chapters: List = emptyList() + var allChapters: List = emptyList() + private set + var filteredAndSortedChapters: List = emptyList() private set /** @@ -125,7 +124,13 @@ class MangaPresenter( // Prepare the relay. chaptersRelay.flatMap { applyChapterFilters(it) } .observeOn(AndroidSchedulers.mainThread()) - .subscribeLatestCache(MangaController::onNextChapters) { _, error -> Timber.e(error) } + .subscribeLatestCache( + { _, chapters -> + filteredAndSortedChapters = chapters + view?.onNextChapters(chapters) + }, + { _, error -> Timber.e(error) } + ) // Manga info - end @@ -144,7 +149,7 @@ class MangaPresenter( setDownloadedChapters(chapters) // Store the last emission - this.chapters = chapters + this.allChapters = chapters // Listen for download status changes observeDownloads() @@ -402,7 +407,7 @@ class MangaPresenter( * Updates the UI after applying the filters. */ private fun refreshChapters() { - chaptersRelay.call(chapters) + chaptersRelay.call(allChapters) } /** @@ -444,7 +449,7 @@ class MangaPresenter( private fun onDownloadStatusChange(download: Download) { // Assign the download to the model object. if (download.status == Download.State.QUEUE) { - chapters.find { it.id == download.chapter.id }?.let { + allChapters.find { it.id == download.chapter.id }?.let { if (it.download == null) { it.download = download } @@ -461,16 +466,15 @@ class MangaPresenter( * Returns the next unread chapter or null if everything is read. */ fun getNextUnreadChapter(): ChapterItem? { - val chapters = chapters.sortedWith(getChapterSort(manga)) return if (sortDescending()) { - return chapters.findLast { !it.read } + return filteredAndSortedChapters.findLast { !it.read } } else { - chapters.find { !it.read } + filteredAndSortedChapters.find { !it.read } } } fun getUnreadChaptersSorted(): List { - val chapters = chapters + val chapters = allChapters .sortedWith(getChapterSort(manga)) .filter { !it.read && it.status == Download.State.NOT_DOWNLOADED } .distinctBy { it.name } @@ -708,7 +712,7 @@ class MangaPresenter( db.insertTrack(track).executeAsBlocking() if (it.service is UnattendedTrackService) { - syncChaptersWithTrackServiceTwoWay(db, chapters, track, it.service) + syncChaptersWithTrackServiceTwoWay(db, allChapters, track, it.service) } } } @@ -743,7 +747,7 @@ class MangaPresenter( db.insertTrack(item).executeAsBlocking() if (service is UnattendedTrackService) { - syncChaptersWithTrackServiceTwoWay(db, chapters, item, service) + syncChaptersWithTrackServiceTwoWay(db, allChapters, item, service) } } catch (e: Throwable) { withUIContext { view?.applicationContext?.toast(e.message) }