This commit is contained in:
Horis 2023-11-18 21:28:32 +08:00
parent 951a0c91a5
commit 43d3fdcf61
5 changed files with 40 additions and 20 deletions

View File

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

View File

@ -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(

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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)
}