Merge remote-tracking branch 'origin/master'

pull/32/head
kunfei 5 years ago
commit cfd51d959d
  1. 85
      app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt
  2. 15
      app/src/main/java/io/legado/app/help/coroutine/CoroutineContainer.kt
  3. 8
      app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt

@ -0,0 +1,85 @@
package io.legado.app.help.coroutine
class CompositeCoroutine : CoroutineContainer {
private var resources: HashSet<Coroutine<*>>? = 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<Coroutine<*>>) {
this.resources = hashSetOf()
for (d in coroutines) {
this.resources?.add(d)
}
}
override fun add(coroutine: Coroutine<*>): Boolean {
synchronized(this) {
var set: HashSet<Coroutine<*>>? = resources
if (resources == null) {
set = hashSetOf()
resources = set
}
return set!!.add(coroutine)
}
}
override fun addAll(vararg coroutines: Coroutine<*>): Boolean {
synchronized(this) {
var set: HashSet<Coroutine<*>>? = 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<Coroutine<*>>?
synchronized(this) {
set = resources
resources = null
}
set?.forEach {
runCatching {
it.cancel()
}
}
}
}

@ -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()
}

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.utils.htmlFormat import io.legado.app.utils.htmlFormat
@ -16,7 +17,7 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) {
companion object { companion object {
private var debugSource: String? = null private var debugSource: String? = null
private var callback: Callback? = null private var callback: Callback? = null
private val tasks: MutableList<Coroutine<*>> = mutableListOf() private val tasks: CompositeCoroutine = CompositeCoroutine()
@SuppressLint("ConstantLocale") @SuppressLint("ConstantLocale")
private val DEBUG_TIME_FORMAT = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) 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) { fun cancelDebug(destroy: Boolean = false) {
tasks.forEach {
if (!it.isCancelled) {
it.cancel()
}
}
tasks.clear() tasks.clear()
if (destroy) { if (destroy) {

Loading…
Cancel
Save