Improve handling of adjacent chapters

This commit is contained in:
Syer10
2022-12-30 17:32:15 -05:00
parent a88740f14c
commit 5cd7f63485

View File

@@ -43,7 +43,9 @@ import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
@@ -252,22 +254,15 @@ class ReaderMenuViewModel @Inject constructor(
fromMenuButton: Boolean = true,
) {
//resetValues()
val chapter = if (viewerChapters.currChapter.value == null) {
ReaderChapter(
getChapter.asFlow(mangaId, chapterIndex)
.take(1)
.catch {
_state.value = ReaderChapter.State.Error(it)
log.warn(it) { "Error getting chapter" }
}
.singleOrNull() ?: return
)
} else {
viewerChapters.currChapter.value!!
}
val (chapter, pages) = coroutineScope {
val getCurrentChapter = async {
val chapter = getReaderChapter(chapterIndex) ?: return@async null
val pages = loader.loadChapter(chapter)
viewerChapters.currChapter.value = chapter
chapter to pages
}
val getAdjacentChapters = async {
val chapters = getChapters.asFlow(mangaId)
.take(1)
.catch {
@@ -277,26 +272,33 @@ class ReaderMenuViewModel @Inject constructor(
}
.single()
val nextChapter = async {
if (viewerChapters.nextChapter.value == null) {
val nextChapter = chapters.find { it.index == chapterIndex + 1 }
if (nextChapter != null) {
viewerChapters.nextChapter.value = ReaderChapter(
nextChapter
)
viewerChapters.nextChapter.value = getReaderChapter(nextChapter.index)
} else {
viewerChapters.nextChapter.value = null
}
}
}
val prevChapter = async {
if (viewerChapters.prevChapter.value == null) {
val prevChapter = chapters.find { it.index == chapterIndex - 1 }
if (prevChapter != null) {
viewerChapters.prevChapter.value = ReaderChapter(
prevChapter
)
viewerChapters.prevChapter.value = getReaderChapter(prevChapter.index)
} else {
viewerChapters.prevChapter.value = null
}
}
}
nextChapter.await()
prevChapter.await()
}
getAdjacentChapters.await()
getCurrentChapter.await()
} ?: return
chapter.stateObserver
.onEach {
@@ -308,7 +310,7 @@ class ReaderMenuViewModel @Inject constructor(
.onEach { (pageList) ->
val prevSeparator = ReaderPageSeparator(viewerChapters.prevChapter.value, chapter)
val nextSeparator = ReaderPageSeparator(chapter, viewerChapters.nextChapter.value)
_pages.value = (_pages.value.ifEmpty { listOf(prevSeparator) } + pageList + nextSeparator).toImmutableList()
_pages.value = (listOf(prevSeparator) + pageList + nextSeparator).toImmutableList()
if (fromMenuButton) {
val lastPageReadOffset = chapter.chapter.meta.juiPageOffset
@@ -337,6 +339,18 @@ class ReaderMenuViewModel @Inject constructor(
.launchIn(chapter.scope)
}
private suspend fun getReaderChapter(chapterIndex: Int): ReaderChapter? {
return ReaderChapter(
getChapter.asFlow(params.mangaId, chapterIndex)
.take(1)
.catch {
_state.value = ReaderChapter.State.Error(it)
log.warn(it) { "Error getting chapter $chapterIndex" }
}
.singleOrNull() ?: return null
)
}
private fun markChapterRead(chapter: ReaderChapter) {
scope.launch {
updateChapterRead.await(chapter.chapter, read = true, onError = { toast(it.message.orEmpty()) })