feat: 优化代码

pull/198/head
kunfei 5 years ago
parent 544e64b415
commit b5bdea83eb
  1. 41
      app/src/main/java/io/legado/app/help/BookHelp.kt

@ -11,6 +11,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.apache.commons.text.similarity.JaccardSimilarity import org.apache.commons.text.similarity.JaccardSimilarity
@ -242,24 +243,16 @@ object BookHelp {
private var replaceRules: List<ReplaceRule> = arrayListOf() private var replaceRules: List<ReplaceRule> = arrayListOf()
@Synchronized @Synchronized
fun upReplaceRules(name: String? = null, origin: String? = null) { suspend fun upReplaceRules() {
if (name != null) { withContext(IO) {
if (bookName != name || bookOrigin != origin) { synchronized(this) {
replaceRules = if (origin.isNullOrEmpty()) { val o = bookOrigin
App.db.replaceRuleDao().findEnabledByScope(name) bookName?.let {
} else { replaceRules = if (o.isNullOrEmpty()) {
App.db.replaceRuleDao().findEnabledByScope(name, origin) App.db.replaceRuleDao().findEnabledByScope(it)
} } else {
bookName = name App.db.replaceRuleDao().findEnabledByScope(it, o)
bookOrigin = origin }
}
} else {
val o = bookOrigin
bookName?.let {
replaceRules = if (o.isNullOrEmpty()) {
App.db.replaceRuleDao().findEnabledByScope(it)
} else {
App.db.replaceRuleDao().findEnabledByScope(it, o)
} }
} }
} }
@ -274,7 +267,17 @@ object BookHelp {
): String { ): String {
var c = content var c = content
if (enableReplace) { if (enableReplace) {
upReplaceRules(name, origin) synchronized(this) {
if (bookName != name || bookOrigin != origin) {
bookName = name
bookOrigin = origin
replaceRules = if (origin.isNullOrEmpty()) {
App.db.replaceRuleDao().findEnabledByScope(name)
} else {
App.db.replaceRuleDao().findEnabledByScope(name, origin)
}
}
}
replaceRules.forEach { item -> replaceRules.forEach { item ->
item.pattern.let { item.pattern.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {

Loading…
Cancel
Save