MangaScreenModel: Make download function follow reader preference (#8920)

* Make download function more clearer in manga screen

Maybe resolves #8879

* Minor cleanup

* Minor cleanup 2
This commit is contained in:
zbue 2023-01-22 05:47:22 +08:00 committed by GitHub
parent e4bc8990fb
commit e28b015580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -48,6 +48,7 @@ import eu.kanade.tachiyomi.network.HttpException
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.manga.track.TrackItem 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.getChapterSort
import eu.kanade.tachiyomi.util.chapter.getNextUnread import eu.kanade.tachiyomi.util.chapter.getNextUnread
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
@ -80,6 +81,7 @@ class MangaInfoScreenModel(
private val isFromSource: Boolean, private val isFromSource: Boolean,
private val downloadPreferences: DownloadPreferences = Injekt.get(), private val downloadPreferences: DownloadPreferences = Injekt.get(),
private val libraryPreferences: LibraryPreferences = Injekt.get(), private val libraryPreferences: LibraryPreferences = Injekt.get(),
private val readerPreferences: ReaderPreferences = Injekt.get(),
private val uiPreferences: UiPreferences = Injekt.get(), private val uiPreferences: UiPreferences = Injekt.get(),
private val trackManager: TrackManager = Injekt.get(), private val trackManager: TrackManager = Injekt.get(),
private val downloadManager: DownloadManager = Injekt.get(), private val downloadManager: DownloadManager = Injekt.get(),
@ -112,10 +114,14 @@ class MangaInfoScreenModel(
private val isFavorited: Boolean private val isFavorited: Boolean
get() = manga?.favorite ?: false get() = manga?.favorite ?: false
private val processedChapters: Sequence<ChapterItem>? private val allChapters: List<ChapterItem>?
get() = successState?.chapters
private val filteredChapters: Sequence<ChapterItem>?
get() = successState?.processedChapters get() = successState?.processedChapters
val relativeTime by uiPreferences.relativeTime().asState(coroutineScope) val relativeTime by uiPreferences.relativeTime().asState(coroutineScope)
val skipFiltered by readerPreferences.skipFiltered().asState(coroutineScope)
val dateFormat by mutableStateOf(UiPreferences.dateFormat(uiPreferences.dateFormat().get())) val dateFormat by mutableStateOf(UiPreferences.dateFormat(uiPreferences.dateFormat().get()))
private val selectedPositions: Array<Int> = arrayOf(-1, -1) // first and last selected index in list private val selectedPositions: Array<Int> = 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<ChapterItem> {
return if (skipFiltered) filteredChapters.orEmpty().toList() else allChapters.orEmpty()
}
/** /**
* Returns the next unread chapter or null if everything is read. * Returns the next unread chapter or null if everything is read.
*/ */
@ -526,17 +539,15 @@ class MangaInfoScreenModel(
} }
fun getUnreadChapters(): List<Chapter> { fun getUnreadChapters(): List<Chapter> {
return successState?.processedChapters return getChapterItems()
?.filter { (chapter, dlStatus) -> !chapter.read && dlStatus == Download.State.NOT_DOWNLOADED } .filter { (chapter, dlStatus) -> !chapter.read && dlStatus == Download.State.NOT_DOWNLOADED }
?.map { it.chapter } .map { it.chapter }
?.toList()
?: emptyList()
} }
fun getUnreadChaptersSorted(): List<Chapter> { fun getUnreadChaptersSorted(): List<Chapter> {
val manga = successState?.manga ?: return emptyList() val manga = successState?.manga ?: return emptyList()
val chapters = getUnreadChapters().sortedWith(getChapterSort(manga)) val chaptersSorted = getUnreadChapters().sortedWith(getChapterSort(manga))
return if (manga.sortDescending()) chapters.reversed() else chapters return if (manga.sortDescending()) chaptersSorted.reversed() else chaptersSorted
} }
fun startDownload( fun startDownload(
@ -604,7 +615,7 @@ class MangaInfoScreenModel(
return return
} }
DownloadAction.UNREAD_CHAPTERS -> getUnreadChapters() DownloadAction.UNREAD_CHAPTERS -> getUnreadChapters()
DownloadAction.ALL_CHAPTERS -> successState?.chapters?.map { it.chapter } DownloadAction.ALL_CHAPTERS -> getChapterItems().map { it.chapter }
} }
if (!chaptersToDownload.isNullOrEmpty()) { if (!chaptersToDownload.isNullOrEmpty()) {
startDownload(chaptersToDownload, false) startDownload(chaptersToDownload, false)
@ -619,7 +630,7 @@ class MangaInfoScreenModel(
fun markPreviousChapterRead(pointer: Chapter) { fun markPreviousChapterRead(pointer: Chapter) {
val successState = successState ?: return 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 prevChapters = if (successState.manga.sortDescending()) chapters.asReversed() else chapters
val pointerPos = prevChapters.indexOf(pointer) val pointerPos = prevChapters.indexOf(pointer)
if (pointerPos != -1) markChaptersRead(prevChapters.take(pointerPos), true) if (pointerPos != -1) markChaptersRead(prevChapters.take(pointerPos), true)
@ -917,7 +928,7 @@ class MangaInfoScreenModel(
} }
private fun showDownloadCustomDialog() { private fun showDownloadCustomDialog() {
val max = processedChapters?.count() ?: return val max = getChapterItems().count()
mutableState.update { state -> mutableState.update { state ->
when (state) { when (state) {
MangaScreenState.Loading -> state MangaScreenState.Loading -> state