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,51 +254,51 @@ 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
val pages = loader.loadChapter(chapter)
viewerChapters.currChapter.value = chapter
chapter to pages
}
val getAdjacentChapters = async {
val chapters = getChapters.asFlow(mangaId)
.take(1) .take(1)
.catch { .catch {
_state.value = ReaderChapter.State.Error(it) log.warn(it) { "Error getting chapter list" }
log.warn(it) { "Error getting chapter" } // TODO: 2022-07-01 Error toast
emit(emptyList())
} }
.singleOrNull() ?: return .single()
)
} else {
viewerChapters.currChapter.value!!
}
val pages = loader.loadChapter(chapter)
viewerChapters.currChapter.value = chapter
val chapters = getChapters.asFlow(mangaId) val nextChapter = async {
.take(1) if (viewerChapters.nextChapter.value == null) {
.catch { val nextChapter = chapters.find { it.index == chapterIndex + 1 }
log.warn(it) { "Error getting chapter list" } if (nextChapter != null) {
// TODO: 2022-07-01 Error toast viewerChapters.nextChapter.value = getReaderChapter(nextChapter.index)
emit(emptyList()) } 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 = getReaderChapter(prevChapter.index)
} else {
viewerChapters.prevChapter.value = null
}
}
}
nextChapter.await()
prevChapter.await()
} }
.single()
if (viewerChapters.nextChapter.value == null) { getAdjacentChapters.await()
val nextChapter = chapters.find { it.index == chapterIndex + 1 } getCurrentChapter.await()
if (nextChapter != null) { } ?: return
viewerChapters.nextChapter.value = ReaderChapter(
nextChapter
)
} else {
viewerChapters.nextChapter.value = null
}
}
if (viewerChapters.prevChapter.value == null) {
val prevChapter = chapters.find { it.index == chapterIndex - 1 }
if (prevChapter != null) {
viewerChapters.prevChapter.value = ReaderChapter(
prevChapter
)
} else {
viewerChapters.prevChapter.value = null
}
}
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()) })