This commit is contained in:
Horis
2025-11-05 22:42:37 +08:00
parent 147db05c19
commit 476d2a04fd
@@ -47,7 +47,9 @@ import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import splitties.init.appCtx import splitties.init.appCtx
import kotlin.math.max import kotlin.math.max
@@ -68,6 +70,12 @@ object ReadBook : CoroutineScope by MainScope() {
var prevTextChapter: TextChapter? = null var prevTextChapter: TextChapter? = null
var curTextChapter: TextChapter? = null var curTextChapter: TextChapter? = null
var nextTextChapter: TextChapter? = null var nextTextChapter: TextChapter? = null
var prevChapterLoadingJob: Coroutine<*>? = null
var curChapterLoadingJob: Coroutine<*>? = null
var nextChapterLoadingJob: Coroutine<*>? = null
var prevChapterLoadingLock = Mutex()
var curChapterLoadingLock = Mutex()
var nextChapterLoadingLock = Mutex()
var bookSource: BookSource? = null var bookSource: BookSource? = null
var msg: String? = null var msg: String? = null
private val loadingChapters = arrayListOf<Int>() private val loadingChapters = arrayListOf<Int>()
@@ -675,6 +683,7 @@ object ReadBook : CoroutineScope by MainScope() {
/** /**
* 内容加载完成 * 内容加载完成
*/ */
@Synchronized
fun contentLoadFinish( fun contentLoadFinish(
book: Book, book: Book,
chapter: BookChapter, chapter: BookChapter,
@@ -684,11 +693,17 @@ object ReadBook : CoroutineScope by MainScope() {
canceled: Boolean = false, canceled: Boolean = false,
success: (() -> Unit)? = null success: (() -> Unit)? = null
) { ) {
removeLoading(chapter.index)
if (canceled || chapter.index !in durChapterIndex - 1..durChapterIndex + 1) { if (canceled || chapter.index !in durChapterIndex - 1..durChapterIndex + 1) {
removeLoading(chapter.index)
return return
} }
Coroutine.async(start = CoroutineStart.LAZY) { val offset = chapter.index - durChapterIndex
when (offset) {
0 -> curChapterLoadingJob?.cancel()
-1 -> prevChapterLoadingJob?.cancel()
1 -> nextChapterLoadingJob?.cancel()
}
val job = Coroutine.async(this, start = CoroutineStart.LAZY) {
val contentProcessor = ContentProcessor.get(book.name, book.origin) val contentProcessor = ContentProcessor.get(book.name, book.origin)
val displayTitle = chapter.getDisplayTitle( val displayTitle = chapter.getDisplayTitle(
contentProcessor.getTitleReplaceRules(), contentProcessor.getTitleReplaceRules(),
@@ -696,12 +711,12 @@ object ReadBook : CoroutineScope by MainScope() {
) )
val contents = contentProcessor val contents = contentProcessor
.getContent(book, chapter, content, includeTitle = false) .getContent(book, chapter, content, includeTitle = false)
ensureActive()
val textChapter = ChapterProvider.getTextChapterAsync( val textChapter = ChapterProvider.getTextChapterAsync(
this@ReadBook, book, chapter, displayTitle, contents, simulatedChapterSize this, book, chapter, displayTitle, contents, simulatedChapterSize
) )
when (val offset = chapter.index - durChapterIndex) { when (offset) {
0 -> { 0 -> curChapterLoadingLock.withLock {
curTextChapter?.cancelLayout()
withContext(Main) { withContext(Main) {
curTextChapter = textChapter curTextChapter = textChapter
} }
@@ -727,8 +742,7 @@ object ReadBook : CoroutineScope by MainScope() {
callBack?.contentLoadFinish() callBack?.contentLoadFinish()
} }
-1 -> { -1 -> prevChapterLoadingLock.withLock {
prevTextChapter?.cancelLayout()
withContext(Main) { withContext(Main) {
prevTextChapter = textChapter prevTextChapter = textChapter
} }
@@ -736,8 +750,7 @@ object ReadBook : CoroutineScope by MainScope() {
if (upContent) callBack?.upContent(offset, resetPageOffset) if (upContent) callBack?.upContent(offset, resetPageOffset)
} }
1 -> { 1 -> nextChapterLoadingLock.withLock {
nextTextChapter?.cancelLayout()
withContext(Main) { withContext(Main) {
nextTextChapter = textChapter nextTextChapter = textChapter
} }
@@ -759,9 +772,13 @@ object ReadBook : CoroutineScope by MainScope() {
appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}") appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}")
}.onSuccess { }.onSuccess {
success?.invoke() success?.invoke()
}.onFinally { }
removeLoading(chapter.index) when (offset) {
}.start() 0 -> curChapterLoadingJob = job
-1 -> prevChapterLoadingJob = job
1 -> nextChapterLoadingJob = job
}
job.start()
} }
suspend fun contentLoadFinishAwait( suspend fun contentLoadFinishAwait(
@@ -771,8 +788,8 @@ object ReadBook : CoroutineScope by MainScope() {
upContent: Boolean = true, upContent: Boolean = true,
resetPageOffset: Boolean resetPageOffset: Boolean
) { ) {
removeLoading(chapter.index)
if (chapter.index !in durChapterIndex - 1..durChapterIndex + 1) { if (chapter.index !in durChapterIndex - 1..durChapterIndex + 1) {
removeLoading(chapter.index)
return return
} }
kotlin.runCatching { kotlin.runCatching {
@@ -843,7 +860,6 @@ object ReadBook : CoroutineScope by MainScope() {
AppLog.put("ChapterProvider ERROR", it) AppLog.put("ChapterProvider ERROR", it)
appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}") appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}")
} }
removeLoading(chapter.index)
} }
@Synchronized @Synchronized