Horis hace 2 años
padre
commit
aabeef4e27

+ 15 - 12
app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt

@@ -13,6 +13,7 @@ import io.legado.app.help.book.BookHelp
 import io.legado.app.help.config.AppConfig
 import io.legado.app.help.config.ReadBookConfig
 import io.legado.app.help.coroutine.Coroutine
+import io.legado.app.help.globalExecutor
 import io.legado.app.model.ImageProvider
 import io.legado.app.model.ReadBook
 import io.legado.app.ui.book.read.page.entities.TextChapter
@@ -68,7 +69,7 @@ class TextChapterLayout(
     private val stringBuilder = StringBuilder()
 
     private var isCompleted = false
-    private val job: Coroutine<*>
+    private var job: Coroutine<*>? = null
     private val bookChapter inline get() = textChapter.chapter
     private val displayTitle inline get() = textChapter.title
     private val chaptersSize inline get() = textChapter.chaptersSize
@@ -76,17 +77,19 @@ class TextChapterLayout(
     var exception: Throwable? = null
 
     init {
-        job = Coroutine.async(scope) {
-            launch {
-                val bookSource = book.getBookSource() ?: return@launch
-                BookHelp.saveImages(bookSource, book, bookChapter, bookContent.toString())
+        globalExecutor.submit {
+            job = Coroutine.async(scope) {
+                launch {
+                    val bookSource = book.getBookSource() ?: return@launch
+                    BookHelp.saveImages(bookSource, book, bookChapter, bookContent.toString())
+                }
+                getTextChapter(book, bookChapter, displayTitle, bookContent)
+            }.onError {
+                exception = it
+                onException(it)
+            }.onFinally {
+                isCompleted = true
             }
-            getTextChapter(book, bookChapter, displayTitle, bookContent)
-        }.onError {
-            exception = it
-            onException(it)
-        }.onFinally {
-            isCompleted = true
         }
     }
 
@@ -106,7 +109,7 @@ class TextChapterLayout(
     }
 
     fun cancel() {
-        job.cancel()
+        job?.cancel()
     }
 
     private fun onPageCompleted() {