优化
This commit is contained in:
@@ -12,24 +12,21 @@ import io.legado.app.data.appDb
|
|||||||
import io.legado.app.data.entities.TxtTocRule
|
import io.legado.app.data.entities.TxtTocRule
|
||||||
import io.legado.app.databinding.ActivityTxtTocRuleBinding
|
import io.legado.app.databinding.ActivityTxtTocRuleBinding
|
||||||
import io.legado.app.databinding.DialogEditTextBinding
|
import io.legado.app.databinding.DialogEditTextBinding
|
||||||
import io.legado.app.databinding.DialogTocRegexEditBinding
|
|
||||||
import io.legado.app.lib.dialogs.alert
|
import io.legado.app.lib.dialogs.alert
|
||||||
import io.legado.app.lib.theme.primaryColor
|
import io.legado.app.lib.theme.primaryColor
|
||||||
import io.legado.app.ui.widget.SelectActionBar
|
import io.legado.app.ui.widget.SelectActionBar
|
||||||
import io.legado.app.ui.widget.recycler.DragSelectTouchHelper
|
import io.legado.app.ui.widget.recycler.DragSelectTouchHelper
|
||||||
import io.legado.app.ui.widget.recycler.ItemTouchCallback
|
import io.legado.app.ui.widget.recycler.ItemTouchCallback
|
||||||
import io.legado.app.ui.widget.recycler.VerticalDivider
|
import io.legado.app.ui.widget.recycler.VerticalDivider
|
||||||
import io.legado.app.utils.ACache
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.setEdgeEffectColor
|
|
||||||
import io.legado.app.utils.splitNotBlank
|
|
||||||
import io.legado.app.utils.toastOnUi
|
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
import kotlinx.coroutines.flow.conflate
|
import kotlinx.coroutines.flow.conflate
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class TxtTocRuleActivity : VMBaseActivity<ActivityTxtTocRuleBinding, TxtTocRuleViewModel>(),
|
class TxtTocRuleActivity : VMBaseActivity<ActivityTxtTocRuleBinding, TxtTocRuleViewModel>(),
|
||||||
TxtTocRuleAdapter.CallBack,
|
TxtTocRuleAdapter.CallBack,
|
||||||
SelectActionBar.CallBack {
|
SelectActionBar.CallBack,
|
||||||
|
TxtTocRuleEditDialog.Callback {
|
||||||
|
|
||||||
override val viewModel: TxtTocRuleViewModel by viewModels()
|
override val viewModel: TxtTocRuleViewModel by viewModels()
|
||||||
override val binding: ActivityTxtTocRuleBinding by viewBinding(ActivityTxtTocRuleBinding::inflate)
|
override val binding: ActivityTxtTocRuleBinding by viewBinding(ActivityTxtTocRuleBinding::inflate)
|
||||||
@@ -80,7 +77,7 @@ class TxtTocRuleActivity : VMBaseActivity<ActivityTxtTocRuleBinding, TxtTocRuleV
|
|||||||
|
|
||||||
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
when (item.itemId) {
|
when (item.itemId) {
|
||||||
R.id.menu_add -> edit(TxtTocRule())
|
R.id.menu_add -> showDialogFragment(TxtTocRuleEditDialog())
|
||||||
R.id.menu_default -> viewModel.importDefault()
|
R.id.menu_default -> viewModel.importDefault()
|
||||||
R.id.menu_import -> showImportDialog()
|
R.id.menu_import -> showImportDialog()
|
||||||
}
|
}
|
||||||
@@ -98,24 +95,7 @@ class TxtTocRuleActivity : VMBaseActivity<ActivityTxtTocRuleBinding, TxtTocRuleV
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun edit(source: TxtTocRule) {
|
override fun edit(source: TxtTocRule) {
|
||||||
alert(titleResource = R.string.txt_toc_regex) {
|
showDialogFragment(TxtTocRuleEditDialog(source.id))
|
||||||
val alertBinding = DialogTocRegexEditBinding.inflate(layoutInflater)
|
|
||||||
alertBinding.apply {
|
|
||||||
tvRuleName.setText(source.name)
|
|
||||||
tvRuleRegex.setText(source.rule)
|
|
||||||
tvRuleExample.setText(source.example)
|
|
||||||
}
|
|
||||||
customView { alertBinding.root }
|
|
||||||
okButton {
|
|
||||||
alertBinding.apply {
|
|
||||||
source.name = tvRuleName.text.toString()
|
|
||||||
source.rule = tvRuleRegex.text.toString()
|
|
||||||
source.example = tvRuleExample.text.toString()
|
|
||||||
viewModel.save(source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cancelButton()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClickSelectBarMainAction() {
|
override fun onClickSelectBarMainAction() {
|
||||||
@@ -134,6 +114,10 @@ class TxtTocRuleActivity : VMBaseActivity<ActivityTxtTocRuleBinding, TxtTocRuleV
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun saveTxtTocRule(txtTocRule: TxtTocRule) {
|
||||||
|
viewModel.save(txtTocRule)
|
||||||
|
}
|
||||||
|
|
||||||
override fun update(vararg source: TxtTocRule) {
|
override fun update(vararg source: TxtTocRule) {
|
||||||
viewModel.update(*source)
|
viewModel.update(*source)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import io.legado.app.data.appDb
|
|||||||
import io.legado.app.data.entities.TxtTocRule
|
import io.legado.app.data.entities.TxtTocRule
|
||||||
import io.legado.app.databinding.DialogEditTextBinding
|
import io.legado.app.databinding.DialogEditTextBinding
|
||||||
import io.legado.app.databinding.DialogTocRegexBinding
|
import io.legado.app.databinding.DialogTocRegexBinding
|
||||||
import io.legado.app.databinding.DialogTocRegexEditBinding
|
|
||||||
import io.legado.app.databinding.ItemTocRegexBinding
|
import io.legado.app.databinding.ItemTocRegexBinding
|
||||||
import io.legado.app.lib.dialogs.alert
|
import io.legado.app.lib.dialogs.alert
|
||||||
import io.legado.app.lib.theme.backgroundColor
|
import io.legado.app.lib.theme.backgroundColor
|
||||||
@@ -37,7 +36,8 @@ import kotlinx.coroutines.launch
|
|||||||
* txt目录规则
|
* txt目录规则
|
||||||
*/
|
*/
|
||||||
class TxtTocRuleDialog() : BaseDialogFragment(R.layout.dialog_toc_regex),
|
class TxtTocRuleDialog() : BaseDialogFragment(R.layout.dialog_toc_regex),
|
||||||
Toolbar.OnMenuItemClickListener {
|
Toolbar.OnMenuItemClickListener,
|
||||||
|
TxtTocRuleEditDialog.Callback {
|
||||||
|
|
||||||
constructor(tocRegex: String?) : this() {
|
constructor(tocRegex: String?) : this() {
|
||||||
arguments = Bundle().apply {
|
arguments = Bundle().apply {
|
||||||
@@ -116,7 +116,7 @@ class TxtTocRuleDialog() : BaseDialogFragment(R.layout.dialog_toc_regex),
|
|||||||
|
|
||||||
override fun onMenuItemClick(item: MenuItem?): Boolean {
|
override fun onMenuItemClick(item: MenuItem?): Boolean {
|
||||||
when (item?.itemId) {
|
when (item?.itemId) {
|
||||||
R.id.menu_add -> editRule()
|
R.id.menu_add -> showDialogFragment(TxtTocRuleEditDialog())
|
||||||
R.id.menu_default -> viewModel.importDefault()
|
R.id.menu_default -> viewModel.importDefault()
|
||||||
R.id.menu_import -> showImportDialog()
|
R.id.menu_import -> showImportDialog()
|
||||||
R.id.menu_split_long_chapter -> {
|
R.id.menu_split_long_chapter -> {
|
||||||
@@ -128,6 +128,10 @@ class TxtTocRuleDialog() : BaseDialogFragment(R.layout.dialog_toc_regex),
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun saveTxtTocRule(txtTocRule: TxtTocRule) {
|
||||||
|
viewModel.save(txtTocRule)
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressLint("InflateParams")
|
@SuppressLint("InflateParams")
|
||||||
private fun showImportDialog() {
|
private fun showImportDialog() {
|
||||||
val aCache = ACache.get(cacheDir = false)
|
val aCache = ACache.get(cacheDir = false)
|
||||||
@@ -164,29 +168,6 @@ class TxtTocRuleDialog() : BaseDialogFragment(R.layout.dialog_toc_regex),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("InflateParams")
|
|
||||||
private fun editRule(rule: TxtTocRule? = null) {
|
|
||||||
val tocRule = rule?.copy() ?: TxtTocRule()
|
|
||||||
requireContext().alert(titleResource = R.string.txt_toc_regex) {
|
|
||||||
val alertBinding = DialogTocRegexEditBinding.inflate(layoutInflater)
|
|
||||||
alertBinding.apply {
|
|
||||||
tvRuleName.setText(tocRule.name)
|
|
||||||
tvRuleRegex.setText(tocRule.rule)
|
|
||||||
tvRuleExample.setText(tocRule.example)
|
|
||||||
}
|
|
||||||
customView { alertBinding.root }
|
|
||||||
okButton {
|
|
||||||
alertBinding.apply {
|
|
||||||
tocRule.name = tvRuleName.text.toString()
|
|
||||||
tocRule.rule = tvRuleRegex.text.toString()
|
|
||||||
tocRule.example = tvRuleExample.text.toString()
|
|
||||||
viewModel.save(tocRule)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cancelButton()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inner class TocRegexAdapter(context: Context) :
|
inner class TocRegexAdapter(context: Context) :
|
||||||
RecyclerAdapter<TxtTocRule, ItemTocRegexBinding>(context),
|
RecyclerAdapter<TxtTocRule, ItemTocRegexBinding>(context),
|
||||||
ItemTouchCallback.Callback {
|
ItemTouchCallback.Callback {
|
||||||
@@ -233,7 +214,7 @@ class TxtTocRuleDialog() : BaseDialogFragment(R.layout.dialog_toc_regex),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ivEdit.setOnClickListener {
|
ivEdit.setOnClickListener {
|
||||||
editRule(getItem(holder.layoutPosition))
|
showDialogFragment(TxtTocRuleEditDialog(getItem(holder.layoutPosition)?.id))
|
||||||
}
|
}
|
||||||
ivDelete.setOnClickListener {
|
ivDelete.setOnClickListener {
|
||||||
getItem(holder.layoutPosition)?.let { item ->
|
getItem(holder.layoutPosition)?.let { item ->
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package io.legado.app.ui.book.toc.rule
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.MenuItem
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.appcompat.widget.Toolbar
|
||||||
|
import androidx.fragment.app.viewModels
|
||||||
|
import io.legado.app.R
|
||||||
|
import io.legado.app.base.BaseDialogFragment
|
||||||
|
import io.legado.app.base.BaseViewModel
|
||||||
|
import io.legado.app.data.appDb
|
||||||
|
import io.legado.app.data.entities.TxtTocRule
|
||||||
|
import io.legado.app.databinding.DialogTocRegexEditBinding
|
||||||
|
import io.legado.app.lib.theme.primaryColor
|
||||||
|
import io.legado.app.utils.GSON
|
||||||
|
import io.legado.app.utils.applyTint
|
||||||
|
import io.legado.app.utils.sendToClip
|
||||||
|
import io.legado.app.utils.setLayout
|
||||||
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
|
|
||||||
|
class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit, true),
|
||||||
|
Toolbar.OnMenuItemClickListener {
|
||||||
|
|
||||||
|
constructor(id: Long?) : this() {
|
||||||
|
id ?: return
|
||||||
|
arguments = Bundle().apply {
|
||||||
|
putLong("id", id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val binding by viewBinding(DialogTocRegexEditBinding::bind)
|
||||||
|
private val viewModel by viewModels<ViewModel>()
|
||||||
|
private val callback get() = (parentFragment as? Callback) ?: activity as? Callback
|
||||||
|
|
||||||
|
override fun onStart() {
|
||||||
|
super.onStart()
|
||||||
|
setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
binding.toolBar.setBackgroundColor(primaryColor)
|
||||||
|
initMenu()
|
||||||
|
viewModel.initData(arguments?.getLong("id")) {
|
||||||
|
binding.tvRuleName.setText(it?.name)
|
||||||
|
binding.tvRuleRegex.setText(it?.rule)
|
||||||
|
binding.tvRuleExample.setText(it?.example)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initMenu() {
|
||||||
|
binding.toolBar.inflateMenu(R.menu.txt_toc_rule_edit)
|
||||||
|
binding.toolBar.menu.applyTint(requireContext())
|
||||||
|
binding.toolBar.setOnMenuItemClickListener(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onMenuItemClick(item: MenuItem?): Boolean {
|
||||||
|
when (item?.itemId) {
|
||||||
|
R.id.menu_save -> {
|
||||||
|
callback?.saveTxtTocRule(getRuleFromView())
|
||||||
|
dismissAllowingStateLoss()
|
||||||
|
}
|
||||||
|
R.id.menu_copy_rule -> context?.sendToClip(GSON.toJson(getRuleFromView()))
|
||||||
|
R.id.menu_paste_rule -> {}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getRuleFromView(): TxtTocRule {
|
||||||
|
val tocRule = viewModel.tocRule ?: TxtTocRule().apply {
|
||||||
|
viewModel.tocRule = this
|
||||||
|
}
|
||||||
|
binding.run {
|
||||||
|
tocRule.name = tvRuleName.text.toString()
|
||||||
|
tocRule.rule = tvRuleRegex.text.toString()
|
||||||
|
tocRule.example = tvRuleExample.text.toString()
|
||||||
|
}
|
||||||
|
return tocRule
|
||||||
|
}
|
||||||
|
|
||||||
|
class ViewModel(application: Application) : BaseViewModel(application) {
|
||||||
|
|
||||||
|
var tocRule: TxtTocRule? = null
|
||||||
|
|
||||||
|
fun initData(id: Long?, finally: (tocRule: TxtTocRule?) -> Unit) {
|
||||||
|
execute {
|
||||||
|
tocRule?.let {
|
||||||
|
return@execute
|
||||||
|
}
|
||||||
|
if (id == null) {
|
||||||
|
return@execute
|
||||||
|
}
|
||||||
|
tocRule = appDb.txtTocRuleDao.get(id)
|
||||||
|
}.onFinally {
|
||||||
|
finally.invoke(tocRule)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Callback {
|
||||||
|
|
||||||
|
fun saveTxtTocRule(txtTocRule: TxtTocRule)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,12 +1,41 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:orientation="vertical"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:paddingTop="10dp"
|
android:padding="16dp">
|
||||||
android:paddingStart="10dp"
|
|
||||||
android:paddingEnd="10dp">
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:background="@drawable/shape_card_view"
|
||||||
|
android:orientation="vertical"
|
||||||
|
tools:ignore="UselessParent">
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.Toolbar
|
||||||
|
android:id="@+id/tool_bar"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@color/background_menu"
|
||||||
|
android:elevation="5dp"
|
||||||
|
android:theme="?attr/actionBarStyle"
|
||||||
|
app:displayHomeAsUp="false"
|
||||||
|
app:fitStatusBar="false"
|
||||||
|
app:popupTheme="@style/AppTheme.PopupOverlay"
|
||||||
|
app:title="@string/txt_toc_regex"
|
||||||
|
app:titleTextAppearance="@style/ToolbarTitle" />
|
||||||
|
|
||||||
|
<androidx.core.widget.NestedScrollView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:overScrollMode="ifContentScrolls">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
<io.legado.app.ui.widget.text.TextInputLayout
|
<io.legado.app.ui.widget.text.TextInputLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
@@ -48,3 +77,9 @@
|
|||||||
</io.legado.app.ui.widget.text.TextInputLayout>
|
</io.legado.app.ui.widget.text.TextInputLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
</androidx.core.widget.NestedScrollView>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</FrameLayout>
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_save"
|
||||||
|
android:title="@string/action_save"
|
||||||
|
android:icon="@drawable/ic_save"
|
||||||
|
app:showAsAction="always" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_copy"
|
||||||
|
android:title="@string/copy_rule" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_paste_rule"
|
||||||
|
android:title="@string/paste_rule" />
|
||||||
|
|
||||||
|
</menu>
|
||||||
Reference in New Issue
Block a user