|
|
@@ -20,7 +20,6 @@ import io.legado.app.model.localBook.TextFile
|
|
|
import io.legado.app.model.webBook.WebBook
|
|
|
import io.legado.app.service.BaseReadAloudService
|
|
|
import io.legado.app.ui.book.read.page.entities.TextChapter
|
|
|
-import io.legado.app.ui.book.read.page.entities.TextPage
|
|
|
import io.legado.app.ui.book.read.page.provider.ChapterProvider
|
|
|
import io.legado.app.ui.book.read.page.provider.LayoutProgressListener
|
|
|
import io.legado.app.utils.stackTraceStr
|
|
|
@@ -31,6 +30,8 @@ import kotlinx.coroutines.MainScope
|
|
|
import kotlinx.coroutines.SupervisorJob
|
|
|
import kotlinx.coroutines.cancelChildren
|
|
|
import kotlinx.coroutines.delay
|
|
|
+import kotlinx.coroutines.flow.collect
|
|
|
+import kotlinx.coroutines.flow.receiveAsFlow
|
|
|
import kotlinx.coroutines.launch
|
|
|
import splitties.init.appCtx
|
|
|
import kotlin.math.max
|
|
|
@@ -533,89 +534,58 @@ object ReadBook : CoroutineScope by MainScope() {
|
|
|
)
|
|
|
val contents = contentProcessor
|
|
|
.getContent(book, chapter, content, includeTitle = false)
|
|
|
- val textChapter =
|
|
|
- ChapterProvider.getTextChapterAsync(chapter, displayTitle, contents, chapterSize)
|
|
|
+ val textChapter = ChapterProvider.getTextChapterAsync(
|
|
|
+ this@ReadBook, book, chapter, displayTitle, contents, chapterSize
|
|
|
+ )
|
|
|
when (val offset = chapter.index - durChapterIndex) {
|
|
|
0 -> {
|
|
|
curTextChapter?.cancelLayout()
|
|
|
curTextChapter = textChapter
|
|
|
callBack?.upMenuView()
|
|
|
- textChapter.setProgressListener(object : LayoutProgressListener {
|
|
|
- var available = false
|
|
|
-
|
|
|
- override fun onLayoutPageCompleted(index: Int, page: TextPage) {
|
|
|
- if (!available && page.containPos(durChapterPos)) {
|
|
|
- if (upContent) {
|
|
|
- callBack?.upContent(offset, resetPageOffset)
|
|
|
- }
|
|
|
- curPageChanged()
|
|
|
- callBack?.contentLoadFinish()
|
|
|
- available = true
|
|
|
- }
|
|
|
- if (upContent && isScroll) {
|
|
|
- if (max(index - 3, 0) < durPageIndex) {
|
|
|
- callBack?.upContent(offset, resetPageOffset)
|
|
|
- }
|
|
|
+ var available = false
|
|
|
+ for (page in textChapter.layoutChannel) {
|
|
|
+ val index = page.index
|
|
|
+ if (!available && page.containPos(durChapterPos)) {
|
|
|
+ if (upContent) {
|
|
|
+ callBack?.upContent(offset, resetPageOffset)
|
|
|
}
|
|
|
- callBack?.onLayoutPageCompleted(index, page)
|
|
|
+ curPageChanged()
|
|
|
+ callBack?.contentLoadFinish()
|
|
|
+ available = true
|
|
|
}
|
|
|
-
|
|
|
- override fun onLayoutCompleted() {
|
|
|
- if (upContent) callBack?.upContent(offset, resetPageOffset)
|
|
|
- success?.invoke()
|
|
|
- callBack?.onLayoutCompleted()
|
|
|
- }
|
|
|
-
|
|
|
- override fun onLayoutException(e: Throwable) {
|
|
|
- AppLog.put("ChapterProvider ERROR", e)
|
|
|
- appCtx.toastOnUi("ChapterProvider ERROR:\n${e.stackTraceStr}")
|
|
|
- callBack?.onLayoutException(e)
|
|
|
+ if (upContent && isScroll) {
|
|
|
+ if (max(index - 3, 0) < durPageIndex) {
|
|
|
+ callBack?.upContent(offset, false)
|
|
|
+ }
|
|
|
}
|
|
|
- })
|
|
|
+ callBack?.onLayoutPageCompleted(index, page)
|
|
|
+ }
|
|
|
+ if (upContent) callBack?.upContent(offset, false)
|
|
|
}
|
|
|
|
|
|
-1 -> {
|
|
|
prevTextChapter?.cancelLayout()
|
|
|
prevTextChapter = textChapter
|
|
|
- textChapter.setProgressListener(object : LayoutProgressListener {
|
|
|
- override fun onLayoutCompleted() {
|
|
|
- if (upContent) callBack?.upContent(offset, resetPageOffset)
|
|
|
- success?.invoke()
|
|
|
- }
|
|
|
-
|
|
|
- override fun onLayoutException(e: Throwable) {
|
|
|
- AppLog.put("ChapterProvider ERROR", e)
|
|
|
- appCtx.toastOnUi("ChapterProvider ERROR:\n${e.stackTraceStr}")
|
|
|
- }
|
|
|
- })
|
|
|
+ textChapter.layoutChannel.receiveAsFlow().collect()
|
|
|
+ if (upContent) callBack?.upContent(offset, resetPageOffset)
|
|
|
}
|
|
|
|
|
|
1 -> {
|
|
|
nextTextChapter?.cancelLayout()
|
|
|
nextTextChapter = textChapter
|
|
|
- textChapter.setProgressListener(object : LayoutProgressListener {
|
|
|
- override fun onLayoutPageCompleted(index: Int, page: TextPage) {
|
|
|
- if (index > 1) {
|
|
|
- return
|
|
|
- }
|
|
|
- if (upContent) callBack?.upContent(offset, resetPageOffset)
|
|
|
- }
|
|
|
-
|
|
|
- override fun onLayoutCompleted() {
|
|
|
- success?.invoke()
|
|
|
+ for (page in textChapter.layoutChannel) {
|
|
|
+ if (page.index > 1) {
|
|
|
+ continue
|
|
|
}
|
|
|
-
|
|
|
- override fun onLayoutException(e: Throwable) {
|
|
|
- AppLog.put("ChapterProvider ERROR", e)
|
|
|
- appCtx.toastOnUi("ChapterProvider ERROR:\n${e.stackTraceStr}")
|
|
|
- }
|
|
|
- })
|
|
|
+ if (upContent) callBack?.upContent(offset, resetPageOffset)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- textChapter.createLayout(this@ReadBook, book, contents)
|
|
|
}.onError {
|
|
|
AppLog.put("ChapterProvider ERROR", it)
|
|
|
appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}")
|
|
|
+ }.onSuccess {
|
|
|
+ success?.invoke()
|
|
|
}
|
|
|
}
|
|
|
|