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