feat: 优化代码

pull/133/head
kunfei 5 years ago
parent 36683d5734
commit 6678e9a939
  1. 14
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  2. 17
      app/src/main/java/io/legado/app/help/BookHelp.kt

@ -33,14 +33,20 @@ interface ReplaceRuleDao {
fun findByIds(vararg ids: Long): List<ReplaceRule> fun findByIds(vararg ids: Long): List<ReplaceRule>
@Query( @Query(
"""SELECT * FROM replace_rules WHERE isEnabled = 1 """
AND (scope LIKE '%' || :scope || '%' or scope is null or scope = '')""" SELECT * FROM replace_rules WHERE isEnabled = 1
AND (scope LIKE '%' || :scope || '%' or scope is null or scope = '')
order by sortOrder
"""
) )
fun findEnabledByScope(scope: String): List<ReplaceRule> fun findEnabledByScope(scope: String): List<ReplaceRule>
@Query( @Query(
"""SELECT * FROM replace_rules WHERE isEnabled = 1 """
AND (scope LIKE '%' || :name || '%' or scope LIKE '%' || :origin || '%' or scope is null or scope = '')""" SELECT * FROM replace_rules WHERE isEnabled = 1
AND (scope LIKE '%' || :name || '%' or scope LIKE '%' || :origin || '%' or scope is null or scope = '')
order by sortOrder
"""
) )
fun findEnabledByScope(name: String, origin: String): List<ReplaceRule> fun findEnabledByScope(name: String, origin: String): List<ReplaceRule>

@ -11,7 +11,10 @@ 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.Main
import kotlinx.coroutines.withContext
import org.apache.commons.text.similarity.JaccardSimilarity import org.apache.commons.text.similarity.JaccardSimilarity
import org.jetbrains.anko.toast
import java.io.File import java.io.File
import kotlin.math.min import kotlin.math.min
@ -262,7 +265,7 @@ object BookHelp {
} }
} }
fun disposeContent( suspend fun disposeContent(
title: String, title: String,
name: String, name: String,
origin: String?, origin: String?,
@ -272,15 +275,19 @@ object BookHelp {
var c = content var c = content
if (enableReplace) { if (enableReplace) {
upReplaceRules(name, origin) upReplaceRules(name, origin)
kotlin.runCatching { replaceRules.forEach { item ->
replaceRules.forEach { item -> item.pattern.let {
item.pattern.let { if (it.isNotEmpty()) {
if (it.isNotEmpty()) { try {
c = if (item.isRegex) { c = if (item.isRegex) {
c.replace(it.toRegex(), item.replacement) c.replace(it.toRegex(), item.replacement)
} else { } else {
c.replace(it, item.replacement) c.replace(it, item.replacement)
} }
} catch (e: Exception) {
withContext(Main) {
App.INSTANCE.toast("${item.name}替换出错")
}
} }
} }
} }

Loading…
Cancel
Save