From e28b0155801859f48a18bc1c5aacead54378c77f Mon Sep 17 00:00:00 2001 From: zbue <108109611+zbue@users.noreply.github.com> Date: Sun, 22 Jan 2023 05:47:22 +0800 Subject: [PATCH] MangaScreenModel: Make download function follow reader preference (#8920) * Make download function more clearer in manga screen Maybe resolves #8879 * Minor cleanup * Minor cleanup 2 --- .../tachiyomi/ui/manga/MangaScreenModel.kt | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt index 647ec997a..13f2c16b3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt @@ -48,6 +48,7 @@ import eu.kanade.tachiyomi.network.HttpException import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.ui.manga.track.TrackItem +import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences import eu.kanade.tachiyomi.util.chapter.getChapterSort import eu.kanade.tachiyomi.util.chapter.getNextUnread import eu.kanade.tachiyomi.util.lang.launchIO @@ -80,6 +81,7 @@ class MangaInfoScreenModel( private val isFromSource: Boolean, private val downloadPreferences: DownloadPreferences = Injekt.get(), private val libraryPreferences: LibraryPreferences = Injekt.get(), + private val readerPreferences: ReaderPreferences = Injekt.get(), private val uiPreferences: UiPreferences = Injekt.get(), private val trackManager: TrackManager = Injekt.get(), private val downloadManager: DownloadManager = Injekt.get(), @@ -112,10 +114,14 @@ class MangaInfoScreenModel( private val isFavorited: Boolean get() = manga?.favorite ?: false - private val processedChapters: Sequence? + private val allChapters: List? + get() = successState?.chapters + + private val filteredChapters: Sequence? get() = successState?.processedChapters val relativeTime by uiPreferences.relativeTime().asState(coroutineScope) + val skipFiltered by readerPreferences.skipFiltered().asState(coroutineScope) val dateFormat by mutableStateOf(UiPreferences.dateFormat(uiPreferences.dateFormat().get())) private val selectedPositions: Array = arrayOf(-1, -1) // first and last selected index in list @@ -517,6 +523,13 @@ class MangaInfoScreenModel( } } + /** + * Returns the list of filtered or all chapter items if [skipFiltered] is false. + */ + fun getChapterItems(): List { + return if (skipFiltered) filteredChapters.orEmpty().toList() else allChapters.orEmpty() + } + /** * Returns the next unread chapter or null if everything is read. */ @@ -526,17 +539,15 @@ class MangaInfoScreenModel( } fun getUnreadChapters(): List { - return successState?.processedChapters - ?.filter { (chapter, dlStatus) -> !chapter.read && dlStatus == Download.State.NOT_DOWNLOADED } - ?.map { it.chapter } - ?.toList() - ?: emptyList() + return getChapterItems() + .filter { (chapter, dlStatus) -> !chapter.read && dlStatus == Download.State.NOT_DOWNLOADED } + .map { it.chapter } } fun getUnreadChaptersSorted(): List { val manga = successState?.manga ?: return emptyList() - val chapters = getUnreadChapters().sortedWith(getChapterSort(manga)) - return if (manga.sortDescending()) chapters.reversed() else chapters + val chaptersSorted = getUnreadChapters().sortedWith(getChapterSort(manga)) + return if (manga.sortDescending()) chaptersSorted.reversed() else chaptersSorted } fun startDownload( @@ -604,7 +615,7 @@ class MangaInfoScreenModel( return } DownloadAction.UNREAD_CHAPTERS -> getUnreadChapters() - DownloadAction.ALL_CHAPTERS -> successState?.chapters?.map { it.chapter } + DownloadAction.ALL_CHAPTERS -> getChapterItems().map { it.chapter } } if (!chaptersToDownload.isNullOrEmpty()) { startDownload(chaptersToDownload, false) @@ -619,7 +630,7 @@ class MangaInfoScreenModel( fun markPreviousChapterRead(pointer: Chapter) { val successState = successState ?: return - val chapters = processedChapters.orEmpty().map { it.chapter }.toList() + val chapters = filteredChapters.orEmpty().map { it.chapter }.toList() val prevChapters = if (successState.manga.sortDescending()) chapters.asReversed() else chapters val pointerPos = prevChapters.indexOf(pointer) if (pointerPos != -1) markChaptersRead(prevChapters.take(pointerPos), true) @@ -917,7 +928,7 @@ class MangaInfoScreenModel( } private fun showDownloadCustomDialog() { - val max = processedChapters?.count() ?: return + val max = getChapterItems().count() mutableState.update { state -> when (state) { MangaScreenState.Loading -> state