mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
优化
This commit is contained in:
parent
03f8437270
commit
7d978ebd6e
@ -35,6 +35,8 @@ data class TextChapter(
|
|||||||
|
|
||||||
private var layout: TextChapterLayout? = null
|
private var layout: TextChapterLayout? = null
|
||||||
|
|
||||||
|
val layoutChannel get() = layout!!.channel
|
||||||
|
|
||||||
fun getPage(index: Int): TextPage? {
|
fun getPage(index: Int): TextPage? {
|
||||||
return pages.getOrNull(index)
|
return pages.getOrNull(index)
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ import io.legado.app.utils.postEvent
|
|||||||
import io.legado.app.utils.spToPx
|
import io.legado.app.utils.spToPx
|
||||||
import io.legado.app.utils.splitNotBlank
|
import io.legado.app.utils.splitNotBlank
|
||||||
import io.legado.app.utils.textHeight
|
import io.legado.app.utils.textHeight
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import java.util.LinkedList
|
import java.util.LinkedList
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
@ -306,6 +307,8 @@ object ChapterProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getTextChapterAsync(
|
fun getTextChapterAsync(
|
||||||
|
scope: CoroutineScope,
|
||||||
|
book: Book,
|
||||||
bookChapter: BookChapter,
|
bookChapter: BookChapter,
|
||||||
displayTitle: String,
|
displayTitle: String,
|
||||||
bookContent: BookContent,
|
bookContent: BookContent,
|
||||||
@ -320,7 +323,9 @@ object ChapterProvider {
|
|||||||
bookChapter.isVip,
|
bookChapter.isVip,
|
||||||
bookChapter.isPay,
|
bookChapter.isPay,
|
||||||
bookContent.effectiveReplaceRules
|
bookContent.effectiveReplaceRules
|
||||||
)
|
).apply {
|
||||||
|
createLayout(scope, book, bookContent)
|
||||||
|
}
|
||||||
|
|
||||||
return textChapter
|
return textChapter
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ import io.legado.app.utils.fastSum
|
|||||||
import io.legado.app.utils.splitNotBlank
|
import io.legado.app.utils.splitNotBlank
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.ensureActive
|
import kotlinx.coroutines.ensureActive
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.util.LinkedList
|
import java.util.LinkedList
|
||||||
@ -78,6 +79,8 @@ class TextChapterLayout(
|
|||||||
|
|
||||||
var exception: Throwable? = null
|
var exception: Throwable? = null
|
||||||
|
|
||||||
|
var channel = Channel<TextPage>(Int.MAX_VALUE)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
job = Coroutine.async(scope) {
|
job = Coroutine.async(scope) {
|
||||||
launch {
|
launch {
|
||||||
@ -124,6 +127,7 @@ class TextChapterLayout(
|
|||||||
textPage.isCompleted = true
|
textPage.isCompleted = true
|
||||||
textPage.textChapter = textChapter
|
textPage.textChapter = textChapter
|
||||||
textPage.upLinesPosition()
|
textPage.upLinesPosition()
|
||||||
|
channel.trySend(textPage)
|
||||||
try {
|
try {
|
||||||
listener?.onLayoutPageCompleted(textPages.lastIndex, textPage)
|
listener?.onLayoutPageCompleted(textPages.lastIndex, textPage)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@ -133,6 +137,7 @@ class TextChapterLayout(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun onCompleted() {
|
private fun onCompleted() {
|
||||||
|
channel.close()
|
||||||
try {
|
try {
|
||||||
listener?.onLayoutCompleted()
|
listener?.onLayoutCompleted()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@ -144,6 +149,7 @@ class TextChapterLayout(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun onException(e: Throwable) {
|
private fun onException(e: Throwable) {
|
||||||
|
channel.close(e)
|
||||||
if (e is CancellationException) {
|
if (e is CancellationException) {
|
||||||
listener = null
|
listener = null
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user