pull/1391/head
gedoor 3 years ago
parent abc043f38f
commit b920558e20
  1. 10
      app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt
  2. 8
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -1,6 +1,5 @@
package io.legado.app.help.coroutine package io.legado.app.help.coroutine
import io.legado.app.BuildConfig
import kotlinx.coroutines.* import kotlinx.coroutines.*
import timber.log.Timber import timber.log.Timber
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -138,21 +137,20 @@ class Coroutine<T>(
return scope.plus(Dispatchers.Main).launch { return scope.plus(Dispatchers.Main).launch {
try { try {
start?.let { dispatchVoidCallback(this, it) } start?.let { dispatchVoidCallback(this, it) }
ensureActive()
val value = executeBlock(scope, context, timeMillis ?: 0L, block) val value = executeBlock(scope, context, timeMillis ?: 0L, block)
if (isActive) { ensureActive()
success?.let { dispatchCallback(this, value, it) } success?.let { dispatchCallback(this, value, it) }
} } catch (e: CancellationException) {
Timber.e("任务取消")
} catch (e: Throwable) { } catch (e: Throwable) {
if (BuildConfig.DEBUG) {
Timber.e(e) Timber.e(e)
}
val consume: Boolean = errorReturn?.value?.let { value -> val consume: Boolean = errorReturn?.value?.let { value ->
if (isActive) { if (isActive) {
success?.let { dispatchCallback(this, value, it) } success?.let { dispatchCallback(this, value, it) }
} }
true true
} ?: false } ?: false
if (!consume && isActive) { if (!consume && isActive) {
error?.let { dispatchCallback(this, e, it) } 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.AppConfig
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.ContentProcessor import io.legado.app.help.ContentProcessor
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.storage.AppWebDav import io.legado.app.help.storage.AppWebDav
import io.legado.app.model.NoStackTraceException import io.legado.app.model.NoStackTraceException
import io.legado.app.model.ReadAloud 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.postEvent
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.ensureActive
class ReadBookViewModel(application: Application) : BaseViewModel(application) { class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var isInitFinish = false var isInitFinish = false
var searchContentQuery = "" var searchContentQuery = ""
var changeSourceCoroutine: Coroutine<*>? = null
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
@ -165,12 +168,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
fun changeTo(source: BookSource, book: Book) { fun changeTo(source: BookSource, book: Book) {
execute { changeSourceCoroutine?.cancel()
changeSourceCoroutine = execute {
ReadBook.upMsg(context.getString(R.string.loading)) ReadBook.upMsg(context.getString(R.string.loading))
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
WebBook.getBookInfoAwait(this, source, book) WebBook.getBookInfoAwait(this, source, book)
} }
ensureActive()
val chapters = WebBook.getChapterListAwait(this, source, book) val chapters = WebBook.getChapterListAwait(this, source, book)
ensureActive()
val oldBook = ReadBook.book!! val oldBook = ReadBook.book!!
book.durChapterIndex = BookHelp.getDurChapter( book.durChapterIndex = BookHelp.getDurChapter(
oldBook.durChapterIndex, oldBook.durChapterIndex,

Loading…
Cancel
Save