mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
abc043f38f
commit
b920558e20
@ -1,6 +1,5 @@
|
||||
package io.legado.app.help.coroutine
|
||||
|
||||
import io.legado.app.BuildConfig
|
||||
import kotlinx.coroutines.*
|
||||
import timber.log.Timber
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
@ -138,21 +137,20 @@ class Coroutine<T>(
|
||||
return scope.plus(Dispatchers.Main).launch {
|
||||
try {
|
||||
start?.let { dispatchVoidCallback(this, it) }
|
||||
ensureActive()
|
||||
val value = executeBlock(scope, context, timeMillis ?: 0L, block)
|
||||
if (isActive) {
|
||||
success?.let { dispatchCallback(this, value, it) }
|
||||
}
|
||||
ensureActive()
|
||||
success?.let { dispatchCallback(this, value, it) }
|
||||
} catch (e: CancellationException) {
|
||||
Timber.e("任务取消")
|
||||
} catch (e: Throwable) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Timber.e(e)
|
||||
}
|
||||
Timber.e(e)
|
||||
val consume: Boolean = errorReturn?.value?.let { value ->
|
||||
if (isActive) {
|
||||
success?.let { dispatchCallback(this, value, it) }
|
||||
}
|
||||
true
|
||||
} ?: false
|
||||
|
||||
if (!consume && isActive) {
|
||||
error?.let { dispatchCallback(this, e, it) }
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import io.legado.app.data.entities.BookSource
|
||||
import io.legado.app.help.AppConfig
|
||||
import io.legado.app.help.BookHelp
|
||||
import io.legado.app.help.ContentProcessor
|
||||
import io.legado.app.help.coroutine.Coroutine
|
||||
import io.legado.app.help.storage.AppWebDav
|
||||
import io.legado.app.model.NoStackTraceException
|
||||
import io.legado.app.model.ReadAloud
|
||||
@ -25,10 +26,12 @@ import io.legado.app.utils.msg
|
||||
import io.legado.app.utils.postEvent
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.ensureActive
|
||||
|
||||
class ReadBookViewModel(application: Application) : BaseViewModel(application) {
|
||||
var isInitFinish = false
|
||||
var searchContentQuery = ""
|
||||
var changeSourceCoroutine: Coroutine<*>? = null
|
||||
|
||||
fun initData(intent: Intent) {
|
||||
execute {
|
||||
@ -165,12 +168,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
|
||||
fun changeTo(source: BookSource, book: Book) {
|
||||
execute {
|
||||
changeSourceCoroutine?.cancel()
|
||||
changeSourceCoroutine = execute {
|
||||
ReadBook.upMsg(context.getString(R.string.loading))
|
||||
if (book.tocUrl.isEmpty()) {
|
||||
WebBook.getBookInfoAwait(this, source, book)
|
||||
}
|
||||
ensureActive()
|
||||
val chapters = WebBook.getChapterListAwait(this, source, book)
|
||||
ensureActive()
|
||||
val oldBook = ReadBook.book!!
|
||||
book.durChapterIndex = BookHelp.getDurChapter(
|
||||
oldBook.durChapterIndex,
|
||||
|
Loading…
Reference in New Issue
Block a user