This commit is contained in:
Horis
2025-11-05 22:01:32 +08:00
parent 4e7f21d245
commit 147db05c19
3 changed files with 42 additions and 18 deletions
@@ -33,8 +33,11 @@ import io.legado.app.ui.book.read.page.provider.LayoutProgressListener
import io.legado.app.utils.postEvent
import io.legado.app.utils.stackTraceStr
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.SupervisorJob
@@ -681,11 +684,11 @@ object ReadBook : CoroutineScope by MainScope() {
canceled: Boolean = false,
success: (() -> Unit)? = null
) {
removeLoading(chapter.index)
if (canceled || chapter.index !in durChapterIndex - 1..durChapterIndex + 1) {
removeLoading(chapter.index)
return
}
Coroutine.async {
Coroutine.async(start = CoroutineStart.LAZY) {
val contentProcessor = ContentProcessor.get(book.name, book.origin)
val displayTitle = chapter.getDisplayTitle(
contentProcessor.getTitleReplaceRules(),
@@ -699,7 +702,9 @@ object ReadBook : CoroutineScope by MainScope() {
when (val offset = chapter.index - durChapterIndex) {
0 -> {
curTextChapter?.cancelLayout()
curTextChapter = textChapter
withContext(Main) {
curTextChapter = textChapter
}
callBack?.upMenuView()
var available = false
for (page in textChapter.layoutChannel) {
@@ -724,14 +729,18 @@ object ReadBook : CoroutineScope by MainScope() {
-1 -> {
prevTextChapter?.cancelLayout()
prevTextChapter = textChapter
withContext(Main) {
prevTextChapter = textChapter
}
textChapter.layoutChannel.receiveAsFlow().collect()
if (upContent) callBack?.upContent(offset, resetPageOffset)
}
1 -> {
nextTextChapter?.cancelLayout()
nextTextChapter = textChapter
withContext(Main) {
nextTextChapter = textChapter
}
for (page in textChapter.layoutChannel) {
if (page.index > 1) {
continue
@@ -743,11 +752,16 @@ object ReadBook : CoroutineScope by MainScope() {
return@async
}.onError {
if (it is CancellationException) {
return@onError
}
AppLog.put("ChapterProvider ERROR", it)
appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}")
}.onSuccess {
success?.invoke()
}
}.onFinally {
removeLoading(chapter.index)
}.start()
}
suspend fun contentLoadFinishAwait(
@@ -757,8 +771,8 @@ object ReadBook : CoroutineScope by MainScope() {
upContent: Boolean = true,
resetPageOffset: Boolean
) {
removeLoading(chapter.index)
if (chapter.index !in durChapterIndex - 1..durChapterIndex + 1) {
removeLoading(chapter.index)
return
}
kotlin.runCatching {
@@ -775,7 +789,9 @@ object ReadBook : CoroutineScope by MainScope() {
when (val offset = chapter.index - durChapterIndex) {
0 -> {
curTextChapter?.cancelLayout()
curTextChapter = textChapter
withContext(Main) {
curTextChapter = textChapter
}
callBack?.upMenuView()
var available = false
for (page in textChapter.layoutChannel) {
@@ -800,14 +816,18 @@ object ReadBook : CoroutineScope by MainScope() {
-1 -> {
prevTextChapter?.cancelLayout()
prevTextChapter = textChapter
withContext(Main) {
prevTextChapter = textChapter
}
textChapter.layoutChannel.receiveAsFlow().collect()
if (upContent) callBack?.upContent(offset, resetPageOffset)
}
1 -> {
nextTextChapter?.cancelLayout()
nextTextChapter = textChapter
withContext(Main) {
nextTextChapter = textChapter
}
for (page in textChapter.layoutChannel) {
if (page.index > 1) {
continue
@@ -816,12 +836,14 @@ object ReadBook : CoroutineScope by MainScope() {
}
}
}
return
}.onFailure {
if (it is CancellationException) {
return@onFailure
}
AppLog.put("ChapterProvider ERROR", it)
appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}")
}
removeLoading(chapter.index)
}
@Synchronized
@@ -19,7 +19,7 @@ import java.security.MessageDigest
* 仅当applyBinarization为true时有效。
*/
class EpaperTransformation(
@IntRange(0, 255) private val threshold: Int = 128,
@param:IntRange(0, 255) private val threshold: Int = 128,
) : BitmapTransformation() {
private val ID =
@@ -30,10 +30,10 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import java.util.LinkedList
import kotlin.coroutines.coroutineContext
import kotlin.math.roundToInt
class TextChapterLayout(
@@ -109,6 +109,8 @@ class TextChapterLayout(
}.onError {
exception = it
onException(it)
}.onCancel {
channel.cancel()
}.onFinally {
isCompleted = true
}
@@ -227,7 +229,7 @@ class TextChapterLayout(
val sb = StringBuffer()
var isSetTypedImage = false
contents.forEach { content ->
coroutineContext.ensureActive()
currentCoroutineContext().ensureActive()
if (isTextImageStyle) {
//图片样式为文字嵌入类型
var text = content.replace(ChapterProvider.srcReplaceChar, "")
@@ -260,7 +262,7 @@ class TextChapterLayout(
if (content.contains("<img")) {
val matcher = AppPattern.imgPattern.matcher(content)
while (matcher.find()) {
coroutineContext.ensureActive()
currentCoroutineContext().ensureActive()
val text = content.substring(start, matcher.start())
if (text.isNotBlank()) {
setTypeText(
@@ -314,7 +316,7 @@ class TextChapterLayout(
textPage.height += endPadding
}
textPage.text = stringBuilder.toString()
coroutineContext.ensureActive()
currentCoroutineContext().ensureActive()
onPageCompleted()
onCompleted()
}
@@ -773,7 +775,7 @@ class TextChapterLayout(
textPage.leftLineSize = textPage.lineSize
}
textPage.text = stringBuilder.toString()
coroutineContext.ensureActive()
currentCoroutineContext().ensureActive()
onPageCompleted()
//新建页面
pendingTextPage = TextPage()