821938089 4 anni fa
parent
commit
0500eab76a

+ 4 - 1
app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt

@@ -40,6 +40,8 @@ class Coroutine<T>(
     private var timeMillis: Long? = null
     private var timeMillis: Long? = null
     private var errorReturn: Result<T>? = null
     private var errorReturn: Result<T>? = null
 
 
+    private var isCancelCalled = false
+
     val isCancelled: Boolean
     val isCancelled: Boolean
         get() = job.isCancelled
         get() = job.isCancelled
 
 
@@ -115,6 +117,7 @@ class Coroutine<T>(
 
 
     //取消当前任务
     //取消当前任务
     fun cancel(cause: CancellationException? = null) {
     fun cancel(cause: CancellationException? = null) {
+        isCancelCalled = true
         job.cancel(cause)
         job.cancel(cause)
         cancel?.let {
         cancel?.let {
             MainScope().launch {
             MainScope().launch {
@@ -146,8 +149,8 @@ class Coroutine<T>(
                 success?.let { dispatchCallback(this, value, it) }
                 success?.let { dispatchCallback(this, value, it) }
             } catch (e: Throwable) {
             } catch (e: Throwable) {
                 e.printOnDebug()
                 e.printOnDebug()
+                if (e is CancellationException && !isCancelCalled) this@Coroutine.cancel()
                 if (e is CancellationException && e !is TimeoutCancellationException) {
                 if (e is CancellationException && e !is TimeoutCancellationException) {
-                    this@Coroutine.cancel()
                     return@launch
                     return@launch
                 }
                 }
                 val consume: Boolean = errorReturn?.value?.let { value ->
                 val consume: Boolean = errorReturn?.value?.let { value ->