feat: 优化代码

pull/123/head
kunfei 5 years ago
parent 6984c6998c
commit ebc1d8d03b
  1. 50
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 7
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 7
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  4. 7
      app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt

@ -238,7 +238,29 @@ object BookHelp {
private var bookOrigin: String? = null
private var replaceRules: List<ReplaceRule> = arrayListOf()
@Synchronized
fun upReplaceRules(name: String? = null, origin: String? = null) {
if (name != null) {
if (bookName != name || bookOrigin != origin) {
replaceRules = if (origin.isNullOrEmpty()) {
App.db.replaceRuleDao().findEnabledByScope(name)
} else {
App.db.replaceRuleDao().findEnabledByScope(name, origin)
}
bookName = name
bookOrigin = origin
}
} else {
val o = bookOrigin
bookName?.let {
replaceRules = if (o.isNullOrEmpty()) {
App.db.replaceRuleDao().findEnabledByScope(it)
} else {
App.db.replaceRuleDao().findEnabledByScope(it, o)
}
}
}
}
fun disposeContent(
title: String,
@ -247,23 +269,17 @@ object BookHelp {
content: String,
enableReplace: Boolean
): String {
synchronized(this) {
if (enableReplace && (bookName != name || bookOrigin != origin)) {
replaceRules = if (origin.isNullOrEmpty()) {
App.db.replaceRuleDao().findEnabledByScope(name)
} else {
App.db.replaceRuleDao().findEnabledByScope(name, origin)
}
}
}
var c = content
for (item in replaceRules) {
item.pattern.let {
if (it.isNotEmpty()) {
c = if (item.isRegex) {
c.replace(it.toRegex(), item.replacement)
} else {
c.replace(it, item.replacement)
if (enableReplace) {
upReplaceRules(name, origin)
for (item in replaceRules) {
item.pattern.let {
if (it.isNotEmpty()) {
c = if (item.isRegex) {
c.replace(it.toRegex(), item.replacement)
} else {
c.replace(it, item.replacement)
}
}
}
}

@ -20,7 +20,9 @@ import io.legado.app.constant.PreferKey
import io.legado.app.constant.Status
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.ReadBookConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.okButton
@ -64,6 +66,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
ChangeSourceDialog.CallBack,
ReadBook.CallBack,
TocRegexDialog.CallBack,
ReplaceEditDialog.CallBack,
ColorPickerDialogListener {
private val requestCodeChapterList = 568
private val requestCodeEditSource = 111
@ -551,6 +554,10 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
}
}
override fun onReplaceRuleSave() {
Coroutine.async { BookHelp.upReplaceRules() }
}
override fun showReadStyle() {
ReadStyleDialog().show(supportFragmentManager, "readStyle")
}

@ -20,7 +20,9 @@ import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.BookHelp
import io.legado.app.help.ItemTouchCallback
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.dialogs.*
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.primaryTextColor
@ -292,6 +294,11 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
}
}
override fun onDestroy() {
super.onDestroy()
Coroutine.async { BookHelp.upReplaceRules() }
}
override fun upCountView() {
select_action_bar.upCountView(adapter.getSelection().size, adapter.getActualItemCount())
}

@ -68,6 +68,7 @@ class ReplaceEditDialog : DialogFragment(),
when (item?.itemId) {
R.id.menu_save -> {
viewModel.save(getReplaceRule()) {
callBack?.onReplaceRuleSave()
dismiss()
}
}
@ -94,4 +95,10 @@ class ReplaceEditDialog : DialogFragment(),
replaceRule.scope = et_scope.text.toString()
return replaceRule
}
val callBack = activity as? CallBack
interface CallBack {
fun onReplaceRuleSave()
}
}

Loading…
Cancel
Save