diff --git a/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt index fea5818fa..5812b419c 100644 --- a/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt @@ -24,4 +24,5 @@ interface TxtTocRuleDao { @Delete fun delete(vararg rule: TxtTocRule) + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt index 93e1c8e4a..86e64bb02 100644 --- a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt @@ -226,12 +226,19 @@ object AnalyzeTxtFile { private fun getTocRules(): List { val rules = App.db.txtTocRule().all + if (rules.isEmpty()) { + return getDefaultRules() + } + return rules + } + + fun getDefaultRules(): List { App.INSTANCE.assets.open("txtTocRule.json").readBytes().let { byteArray -> GSON.fromJsonArray(String(byteArray))?.let { App.db.txtTocRule().insert(*it.toTypedArray()) return it } } - return rules + return emptyList() } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt index 04185bcdb..74272fd01 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt @@ -21,12 +21,14 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.constant.Theme import io.legado.app.data.entities.TxtTocRule import io.legado.app.help.ItemTouchCallback +import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.utils.applyTint import io.legado.app.utils.getVerticalDivider import kotlinx.android.synthetic.main.dialog_toc_regex.* import kotlinx.android.synthetic.main.item_toc_regex.view.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch +import org.jetbrains.anko.sdk27.listeners.onClick import java.util.* @@ -98,14 +100,23 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { override fun onMenuItemClick(item: MenuItem?): Boolean { when (item?.itemId) { - R.id.menu_add -> { - } - R.id.menu_default -> { - } + R.id.menu_add -> editRule() + R.id.menu_default -> importDefault() } return false } + private fun importDefault() { + launch(IO) { + AnalyzeTxtFile.getDefaultRules().let { + App.db.txtTocRule().insert(*it.toTypedArray()) + } + } + } + + private fun editRule(rule: TxtTocRule? = null) { + + } inner class TocRegexAdapter(context: Context) : SimpleRecyclerAdapter(context, R.layout.item_toc_regex), @@ -123,10 +134,22 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { } override fun registerListener(holder: ItemViewHolder) { - holder.itemView.rb_regex_name.setOnCheckedChangeListener { buttonView, isChecked -> - if (buttonView.isPressed && isChecked) { - selectedName = getItem(holder.layoutPosition)?.name - updateItems(0, itemCount - 1, true) + holder.itemView.apply { + rb_regex_name.setOnCheckedChangeListener { buttonView, isChecked -> + if (buttonView.isPressed && isChecked) { + selectedName = getItem(holder.layoutPosition)?.name + 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) + } + } } } }