From 3df45dec469b01222f0006b91f719f13e923be67 Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Wed, 31 Jul 2019 15:13:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/help/coroutine/CompositeCoroutine.kt | 85 +++++++++++++++++++ .../app/help/coroutine/CoroutineContainer.kt | 15 ++++ .../legado/app/model/webbook/SourceDebug.kt | 8 +- 3 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt create mode 100644 app/src/main/java/io/legado/app/help/coroutine/CoroutineContainer.kt diff --git a/app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt new file mode 100644 index 000000000..99c2df223 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt @@ -0,0 +1,85 @@ +package io.legado.app.help.coroutine + +class CompositeCoroutine : CoroutineContainer { + + private var resources: HashSet>? = null + + val size: Int + get() = resources?.size ?: 0 + + val isEmpty: Boolean + get() = size == 0 + + constructor() + + constructor(vararg coroutines: Coroutine<*>) { + this.resources = hashSetOf(*coroutines) + } + + constructor(coroutines: Iterable>) { + this.resources = hashSetOf() + for (d in coroutines) { + this.resources?.add(d) + } + } + + override fun add(coroutine: Coroutine<*>): Boolean { + synchronized(this) { + var set: HashSet>? = resources + if (resources == null) { + set = hashSetOf() + resources = set + } + return set!!.add(coroutine) + } + } + + override fun addAll(vararg coroutines: Coroutine<*>): Boolean { + synchronized(this) { + var set: HashSet>? = resources + if (resources == null) { + set = hashSetOf() + resources = set + } + for (coroutine in coroutines) { + val add = set!!.add(coroutine) + if (!add) { + return false + } + } + } + return true + } + + override fun remove(coroutine: Coroutine<*>): Boolean { + if (delete(coroutine)) { + coroutine.cancel() + return true + } + return false + } + + override fun delete(coroutine: Coroutine<*>): Boolean { + synchronized(this) { + val set = resources + if (set == null || !set.remove(coroutine)) { + return false + } + } + return true + } + + override fun clear() { + val set: HashSet>? + synchronized(this) { + set = resources + resources = null + } + + set?.forEach { + runCatching { + it.cancel() + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/coroutine/CoroutineContainer.kt b/app/src/main/java/io/legado/app/help/coroutine/CoroutineContainer.kt new file mode 100644 index 000000000..d98a2b8da --- /dev/null +++ b/app/src/main/java/io/legado/app/help/coroutine/CoroutineContainer.kt @@ -0,0 +1,15 @@ +package io.legado.app.help.coroutine + +interface CoroutineContainer { + + fun add(coroutine: Coroutine<*>): Boolean + + fun addAll(vararg coroutines: Coroutine<*>): Boolean + + fun remove(coroutine: Coroutine<*>): Boolean + + fun delete(coroutine: Coroutine<*>): Boolean + + fun clear() + +} diff --git a/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt b/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt index 03e9d0446..919be87a6 100644 --- a/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt +++ b/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp +import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook import io.legado.app.utils.htmlFormat @@ -16,7 +17,7 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) { companion object { private var debugSource: String? = null private var callback: Callback? = null - private val tasks: MutableList> = mutableListOf() + private val tasks: CompositeCoroutine = CompositeCoroutine() @SuppressLint("ConstantLocale") private val DEBUG_TIME_FORMAT = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) @@ -34,11 +35,6 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) { } fun cancelDebug(destroy: Boolean = false) { - tasks.forEach { - if (!it.isCancelled) { - it.cancel() - } - } tasks.clear() if (destroy) {