Horis 3 лет назад
Родитель
Сommit
a24a97df34

+ 1 - 0
app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt

@@ -19,6 +19,7 @@ import kotlin.coroutines.CoroutineContext
 
 /**
  * 链式协程
+ * 注意:如果协程太快完成,回调会不执行
  */
 @Suppress("unused", "MemberVisibilityCanBePrivate")
 class Coroutine<T>(

+ 25 - 17
app/src/main/java/io/legado/app/model/CacheBook.kt

@@ -18,6 +18,7 @@ import io.legado.app.utils.postEvent
 
 import io.legado.app.utils.startService
 import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.CoroutineStart
 import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.delay
 
@@ -268,6 +269,7 @@ object CacheBook {
                 book,
                 chapter,
                 context = context,
+                start = CoroutineStart.LAZY,
                 executeContext = context
             ).onSuccess { content ->
                 onSuccess(chapter)
@@ -282,7 +284,7 @@ object CacheBook {
                 onCancel(chapterIndex)
             }.onFinally {
                 onFinally()
-            }
+            }.start()
         }
 
         @Synchronized
@@ -297,22 +299,28 @@ object CacheBook {
             postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
             onDownloadSet.add(chapter.index)
             waitDownloadSet.remove(chapter.index)
-            WebBook.getContent(scope, bookSource, book, chapter, executeContext = IO)
-                .onSuccess { content ->
-                    onSuccess(chapter)
-                    ReadBook.downloadedChapters.add(chapter.index)
-                    ReadBook.downloadFailChapters.remove(chapter.index)
-                    downloadFinish(chapter, content, resetPageOffset)
-                }.onError {
-                    onError(chapter, it)
-                    ReadBook.downloadFailChapters[chapter.index] =
-                        (ReadBook.downloadFailChapters[chapter.index] ?: 0) + 1
-                    downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}", resetPageOffset)
-                }.onCancel {
-                    onCancel(chapter.index)
-                }.onFinally {
-                    postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
-                }
+            WebBook.getContent(
+                scope,
+                bookSource,
+                book,
+                chapter,
+                start = CoroutineStart.LAZY,
+                executeContext = IO
+            ).onSuccess { content ->
+                onSuccess(chapter)
+                ReadBook.downloadedChapters.add(chapter.index)
+                ReadBook.downloadFailChapters.remove(chapter.index)
+                downloadFinish(chapter, content, resetPageOffset)
+            }.onError {
+                onError(chapter, it)
+                ReadBook.downloadFailChapters[chapter.index] =
+                    (ReadBook.downloadFailChapters[chapter.index] ?: 0) + 1
+                downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}", resetPageOffset)
+            }.onCancel {
+                onCancel(chapter.index)
+            }.onFinally {
+                postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
+            }.start()
         }
 
         private fun downloadFinish(

+ 3 - 0
app/src/main/java/io/legado/app/model/webBook/SearchModel.kt

@@ -11,6 +11,7 @@ import io.legado.app.help.coroutine.CompositeCoroutine
 import io.legado.app.ui.book.search.SearchScope
 import io.legado.app.utils.getPrefBoolean
 import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.CoroutineStart
 import kotlinx.coroutines.ExecutorCoroutineDispatcher
 import kotlinx.coroutines.asCoroutineDispatcher
 import kotlinx.coroutines.ensureActive
@@ -84,6 +85,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
             searchKey,
             searchPage,
             context = searchPool,
+            start = CoroutineStart.LAZY,
             executeContext = searchPool
         ).timeout(30000L)
             .onSuccess {
@@ -93,6 +95,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
             .onFinally {
                 onFinally(searchId)
             }
+        task.start()
         tasks.add(task)
     }
 

+ 5 - 2
app/src/main/java/io/legado/app/model/webBook/WebBook.kt

@@ -14,6 +14,7 @@ import io.legado.app.model.analyzeRule.AnalyzeRule
 import io.legado.app.model.analyzeRule.AnalyzeUrl
 import io.legado.app.model.analyzeRule.RuleData
 import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.CoroutineStart
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.isActive
 import kotlin.coroutines.CoroutineContext
@@ -30,9 +31,10 @@ object WebBook {
         key: String,
         page: Int? = 1,
         context: CoroutineContext = Dispatchers.IO,
+        start: CoroutineStart = CoroutineStart.DEFAULT,
         executeContext: CoroutineContext = Dispatchers.Main,
     ): Coroutine<ArrayList<SearchBook>> {
-        return Coroutine.async(scope, context, executeContext = executeContext) {
+        return Coroutine.async(scope, context, start = start, executeContext = executeContext) {
             searchBookAwait(bookSource, key, page)
         }
     }
@@ -266,9 +268,10 @@ object WebBook {
         nextChapterUrl: String? = null,
         needSave: Boolean = true,
         context: CoroutineContext = Dispatchers.IO,
+        start: CoroutineStart = CoroutineStart.DEFAULT,
         executeContext: CoroutineContext = Dispatchers.Main,
     ): Coroutine<String> {
-        return Coroutine.async(scope, context, executeContext = executeContext) {
+        return Coroutine.async(scope, context, start = start, executeContext = executeContext) {
             getContentAwait(bookSource, book, bookChapter, nextChapterUrl, needSave)
         }
     }

+ 6 - 1
app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt

@@ -202,7 +202,11 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
         }
         val source = bookSourceList[searchIndex]
         bookSourceList[searchIndex] = emptyBookSource
-        val task = execute(context = searchPool!!, executeContext = searchPool!!) {
+        val task = execute(
+            context = searchPool!!,
+            start = CoroutineStart.LAZY,
+            executeContext = searchPool!!
+        ) {
             val resultBooks = WebBook.searchBookAwait(source, name)
             resultBooks.forEach { searchBook ->
                 if (searchBook.name != name) {
@@ -228,6 +232,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
             .onSuccess {
                 nextSearch()
             }
+        task.start()
         tasks.add(task)
     }