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.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<ChapterItem>?
private val allChapters: List<ChapterItem>?
get() = successState?.chapters
private val filteredChapters: Sequence<ChapterItem>?
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<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.
*/
@ -526,17 +539,15 @@ class MangaInfoScreenModel(
}
fun getUnreadChapters(): List<Chapter> {
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<Chapter> {
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