From 43d3fdcf616fbaf58da8ad915b4e40a7e4229c8f Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Sat, 18 Nov 2023 21:28:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/coroutine/Coroutine.kt | 1 + .../java/io/legado/app/model/CacheBook.kt | 42 +++++++++++-------- .../legado/app/model/webBook/SearchModel.kt | 3 ++ .../io/legado/app/model/webBook/WebBook.kt | 7 +++- .../changesource/ChangeBookSourceViewModel.kt | 7 +++- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt index 4610ae334..6bb8b846e 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt @@ -19,6 +19,7 @@ import kotlin.coroutines.CoroutineContext /** * 链式协程 + * 注意:如果协程太快完成,回调会不执行 */ @Suppress("unused", "MemberVisibilityCanBePrivate") class Coroutine( diff --git a/app/src/main/java/io/legado/app/model/CacheBook.kt b/app/src/main/java/io/legado/app/model/CacheBook.kt index 7bba3acc0..a22778624 100644 --- a/app/src/main/java/io/legado/app/model/CacheBook.kt +++ b/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( diff --git a/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt b/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt index 1ac0e0c06..66a399461 100644 --- a/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt +++ b/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) } diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index 0592a50ef..53cb00113 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/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> { - 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 { - return Coroutine.async(scope, context, executeContext = executeContext) { + return Coroutine.async(scope, context, start = start, executeContext = executeContext) { getContentAwait(bookSource, book, bookChapter, nextChapterUrl, needSave) } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt index b35eadb82..0fdc4d4f9 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt +++ b/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) }