pull/2477/head
parent
167c73a526
commit
3e5a84f2bd
@ -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,50 +1,85 @@ |
||||
<?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" |
||||
android:orientation="vertical" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:paddingTop="10dp" |
||||
android:paddingStart="10dp" |
||||
android:paddingEnd="10dp"> |
||||
android:padding="16dp"> |
||||
|
||||
<io.legado.app.ui.widget.text.TextInputLayout |
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:hint="@string/name"> |
||||
android:layout_gravity="center" |
||||
android:background="@drawable/shape_card_view" |
||||
android:orientation="vertical" |
||||
tools:ignore="UselessParent"> |
||||
|
||||
<io.legado.app.lib.theme.view.ThemeEditText |
||||
android:id="@+id/tv_rule_name" |
||||
<androidx.appcompat.widget.Toolbar |
||||
android:id="@+id/tool_bar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" /> |
||||
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" /> |
||||
|
||||
</io.legado.app.ui.widget.text.TextInputLayout> |
||||
|
||||
<io.legado.app.ui.widget.text.TextInputLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:hint="@string/regex"> |
||||
|
||||
<io.legado.app.lib.theme.view.ThemeEditText |
||||
android:id="@+id/tv_rule_regex" |
||||
<androidx.core.widget.NestedScrollView |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" /> |
||||
android:overScrollMode="ifContentScrolls"> |
||||
|
||||
</io.legado.app.ui.widget.text.TextInputLayout> |
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="vertical"> |
||||
|
||||
<io.legado.app.ui.widget.text.TextInputLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:hint="@string/example"> |
||||
<io.legado.app.ui.widget.text.TextInputLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:hint="@string/name"> |
||||
|
||||
<io.legado.app.lib.theme.view.ThemeEditText |
||||
android:id="@+id/tv_rule_example" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" /> |
||||
<io.legado.app.lib.theme.view.ThemeEditText |
||||
android:id="@+id/tv_rule_name" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" /> |
||||
|
||||
</io.legado.app.ui.widget.text.TextInputLayout> |
||||
|
||||
<io.legado.app.ui.widget.text.TextInputLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:hint="@string/regex"> |
||||
|
||||
<io.legado.app.lib.theme.view.ThemeEditText |
||||
android:id="@+id/tv_rule_regex" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" /> |
||||
|
||||
</io.legado.app.ui.widget.text.TextInputLayout> |
||||
|
||||
<io.legado.app.ui.widget.text.TextInputLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:hint="@string/example"> |
||||
|
||||
<io.legado.app.lib.theme.view.ThemeEditText |
||||
android:id="@+id/tv_rule_example" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" /> |
||||
|
||||
</io.legado.app.ui.widget.text.TextInputLayout> |
||||
|
||||
</LinearLayout> |
||||
|
||||
</androidx.core.widget.NestedScrollView> |
||||
|
||||
</io.legado.app.ui.widget.text.TextInputLayout> |
||||
</LinearLayout> |
||||
|
||||
</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> |
Loading…
Reference in new issue