feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 80a587f8c4
commit 9384ad3aa6
  1. 1
      app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt
  2. 9
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  3. 31
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt

@ -24,4 +24,5 @@ interface TxtTocRuleDao {
@Delete @Delete
fun delete(vararg rule: TxtTocRule) fun delete(vararg rule: TxtTocRule)
} }

@ -226,12 +226,19 @@ object AnalyzeTxtFile {
private fun getTocRules(): List<TxtTocRule> { private fun getTocRules(): List<TxtTocRule> {
val rules = App.db.txtTocRule().all val rules = App.db.txtTocRule().all
if (rules.isEmpty()) {
return getDefaultRules()
}
return rules
}
fun getDefaultRules(): List<TxtTocRule> {
App.INSTANCE.assets.open("txtTocRule.json").readBytes().let { byteArray -> App.INSTANCE.assets.open("txtTocRule.json").readBytes().let { byteArray ->
GSON.fromJsonArray<TxtTocRule>(String(byteArray))?.let { GSON.fromJsonArray<TxtTocRule>(String(byteArray))?.let {
App.db.txtTocRule().insert(*it.toTypedArray()) App.db.txtTocRule().insert(*it.toTypedArray())
return it return it
} }
} }
return rules return emptyList()
} }
} }

@ -21,12 +21,14 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.constant.Theme import io.legado.app.constant.Theme
import io.legado.app.data.entities.TxtTocRule import io.legado.app.data.entities.TxtTocRule
import io.legado.app.help.ItemTouchCallback import io.legado.app.help.ItemTouchCallback
import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.utils.applyTint import io.legado.app.utils.applyTint
import io.legado.app.utils.getVerticalDivider import io.legado.app.utils.getVerticalDivider
import kotlinx.android.synthetic.main.dialog_toc_regex.* import kotlinx.android.synthetic.main.dialog_toc_regex.*
import kotlinx.android.synthetic.main.item_toc_regex.view.* import kotlinx.android.synthetic.main.item_toc_regex.view.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.* import java.util.*
@ -98,14 +100,23 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) { when (item?.itemId) {
R.id.menu_add -> { R.id.menu_add -> editRule()
R.id.menu_default -> importDefault()
} }
R.id.menu_default -> { return false
} }
private fun importDefault() {
launch(IO) {
AnalyzeTxtFile.getDefaultRules().let {
App.db.txtTocRule().insert(*it.toTypedArray())
} }
return false
} }
}
private fun editRule(rule: TxtTocRule? = null) {
}
inner class TocRegexAdapter(context: Context) : inner class TocRegexAdapter(context: Context) :
SimpleRecyclerAdapter<TxtTocRule>(context, R.layout.item_toc_regex), SimpleRecyclerAdapter<TxtTocRule>(context, R.layout.item_toc_regex),
@ -123,12 +134,24 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
} }
override fun registerListener(holder: ItemViewHolder) { override fun registerListener(holder: ItemViewHolder) {
holder.itemView.rb_regex_name.setOnCheckedChangeListener { buttonView, isChecked -> holder.itemView.apply {
rb_regex_name.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed && isChecked) { if (buttonView.isPressed && isChecked) {
selectedName = getItem(holder.layoutPosition)?.name selectedName = getItem(holder.layoutPosition)?.name
updateItems(0, itemCount - 1, true) updateItems(0, itemCount - 1, true)
} }
} }
tv_edit.onClick {
editRule(getItem(holder.layoutPosition))
}
tv_del.onClick {
getItem(holder.layoutPosition)?.let { item ->
launch(IO) {
App.db.txtTocRule().delete(item)
}
}
}
}
} }
private var isMoved = false private var isMoved = false

Loading…
Cancel
Save