优化
This commit is contained in:
@@ -19,6 +19,7 @@ import kotlin.coroutines.CoroutineContext
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 链式协程
|
* 链式协程
|
||||||
|
* 注意:如果协程太快完成,回调会不执行
|
||||||
*/
|
*/
|
||||||
@Suppress("unused", "MemberVisibilityCanBePrivate")
|
@Suppress("unused", "MemberVisibilityCanBePrivate")
|
||||||
class Coroutine<T>(
|
class Coroutine<T>(
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import io.legado.app.utils.postEvent
|
|||||||
|
|
||||||
import io.legado.app.utils.startService
|
import io.legado.app.utils.startService
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.CoroutineStart
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
@@ -268,6 +269,7 @@ object CacheBook {
|
|||||||
book,
|
book,
|
||||||
chapter,
|
chapter,
|
||||||
context = context,
|
context = context,
|
||||||
|
start = CoroutineStart.LAZY,
|
||||||
executeContext = context
|
executeContext = context
|
||||||
).onSuccess { content ->
|
).onSuccess { content ->
|
||||||
onSuccess(chapter)
|
onSuccess(chapter)
|
||||||
@@ -282,7 +284,7 @@ object CacheBook {
|
|||||||
onCancel(chapterIndex)
|
onCancel(chapterIndex)
|
||||||
}.onFinally {
|
}.onFinally {
|
||||||
onFinally()
|
onFinally()
|
||||||
}
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@@ -297,22 +299,28 @@ object CacheBook {
|
|||||||
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
||||||
onDownloadSet.add(chapter.index)
|
onDownloadSet.add(chapter.index)
|
||||||
waitDownloadSet.remove(chapter.index)
|
waitDownloadSet.remove(chapter.index)
|
||||||
WebBook.getContent(scope, bookSource, book, chapter, executeContext = IO)
|
WebBook.getContent(
|
||||||
.onSuccess { content ->
|
scope,
|
||||||
onSuccess(chapter)
|
bookSource,
|
||||||
ReadBook.downloadedChapters.add(chapter.index)
|
book,
|
||||||
ReadBook.downloadFailChapters.remove(chapter.index)
|
chapter,
|
||||||
downloadFinish(chapter, content, resetPageOffset)
|
start = CoroutineStart.LAZY,
|
||||||
}.onError {
|
executeContext = IO
|
||||||
onError(chapter, it)
|
).onSuccess { content ->
|
||||||
ReadBook.downloadFailChapters[chapter.index] =
|
onSuccess(chapter)
|
||||||
(ReadBook.downloadFailChapters[chapter.index] ?: 0) + 1
|
ReadBook.downloadedChapters.add(chapter.index)
|
||||||
downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}", resetPageOffset)
|
ReadBook.downloadFailChapters.remove(chapter.index)
|
||||||
}.onCancel {
|
downloadFinish(chapter, content, resetPageOffset)
|
||||||
onCancel(chapter.index)
|
}.onError {
|
||||||
}.onFinally {
|
onError(chapter, it)
|
||||||
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
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(
|
private fun downloadFinish(
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import io.legado.app.help.coroutine.CompositeCoroutine
|
|||||||
import io.legado.app.ui.book.search.SearchScope
|
import io.legado.app.ui.book.search.SearchScope
|
||||||
import io.legado.app.utils.getPrefBoolean
|
import io.legado.app.utils.getPrefBoolean
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.CoroutineStart
|
||||||
import kotlinx.coroutines.ExecutorCoroutineDispatcher
|
import kotlinx.coroutines.ExecutorCoroutineDispatcher
|
||||||
import kotlinx.coroutines.asCoroutineDispatcher
|
import kotlinx.coroutines.asCoroutineDispatcher
|
||||||
import kotlinx.coroutines.ensureActive
|
import kotlinx.coroutines.ensureActive
|
||||||
@@ -84,6 +85,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
|
|||||||
searchKey,
|
searchKey,
|
||||||
searchPage,
|
searchPage,
|
||||||
context = searchPool,
|
context = searchPool,
|
||||||
|
start = CoroutineStart.LAZY,
|
||||||
executeContext = searchPool
|
executeContext = searchPool
|
||||||
).timeout(30000L)
|
).timeout(30000L)
|
||||||
.onSuccess {
|
.onSuccess {
|
||||||
@@ -93,6 +95,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
|
|||||||
.onFinally {
|
.onFinally {
|
||||||
onFinally(searchId)
|
onFinally(searchId)
|
||||||
}
|
}
|
||||||
|
task.start()
|
||||||
tasks.add(task)
|
tasks.add(task)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import io.legado.app.model.analyzeRule.AnalyzeRule
|
|||||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||||
import io.legado.app.model.analyzeRule.RuleData
|
import io.legado.app.model.analyzeRule.RuleData
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.CoroutineStart
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.isActive
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
@@ -30,9 +31,10 @@ object WebBook {
|
|||||||
key: String,
|
key: String,
|
||||||
page: Int? = 1,
|
page: Int? = 1,
|
||||||
context: CoroutineContext = Dispatchers.IO,
|
context: CoroutineContext = Dispatchers.IO,
|
||||||
|
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||||
executeContext: CoroutineContext = Dispatchers.Main,
|
executeContext: CoroutineContext = Dispatchers.Main,
|
||||||
): Coroutine<ArrayList<SearchBook>> {
|
): Coroutine<ArrayList<SearchBook>> {
|
||||||
return Coroutine.async(scope, context, executeContext = executeContext) {
|
return Coroutine.async(scope, context, start = start, executeContext = executeContext) {
|
||||||
searchBookAwait(bookSource, key, page)
|
searchBookAwait(bookSource, key, page)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -266,9 +268,10 @@ object WebBook {
|
|||||||
nextChapterUrl: String? = null,
|
nextChapterUrl: String? = null,
|
||||||
needSave: Boolean = true,
|
needSave: Boolean = true,
|
||||||
context: CoroutineContext = Dispatchers.IO,
|
context: CoroutineContext = Dispatchers.IO,
|
||||||
|
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||||
executeContext: CoroutineContext = Dispatchers.Main,
|
executeContext: CoroutineContext = Dispatchers.Main,
|
||||||
): Coroutine<String> {
|
): Coroutine<String> {
|
||||||
return Coroutine.async(scope, context, executeContext = executeContext) {
|
return Coroutine.async(scope, context, start = start, executeContext = executeContext) {
|
||||||
getContentAwait(bookSource, book, bookChapter, nextChapterUrl, needSave)
|
getContentAwait(bookSource, book, bookChapter, nextChapterUrl, needSave)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -202,7 +202,11 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
|
|||||||
}
|
}
|
||||||
val source = bookSourceList[searchIndex]
|
val source = bookSourceList[searchIndex]
|
||||||
bookSourceList[searchIndex] = emptyBookSource
|
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)
|
val resultBooks = WebBook.searchBookAwait(source, name)
|
||||||
resultBooks.forEach { searchBook ->
|
resultBooks.forEach { searchBook ->
|
||||||
if (searchBook.name != name) {
|
if (searchBook.name != name) {
|
||||||
@@ -228,6 +232,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
|
|||||||
.onSuccess {
|
.onSuccess {
|
||||||
nextSearch()
|
nextSearch()
|
||||||
}
|
}
|
||||||
|
task.start()
|
||||||
tasks.add(task)
|
tasks.add(task)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user