diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 5b8d94322..b12306619 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -385,12 +385,15 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } } - override fun getSelectedText(): String { - return page_view.selectedText - } + override val selectedText: String get() = page_view.selectedText override fun onMenuItemSelected(item: MenuItemImpl): Boolean { - + when (item.itemId) { + R.id.menu_replace -> ReplaceEditDialog.show( + supportFragmentManager, + pattern = selectedText + ) + } return false } diff --git a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt index 506be8653..c86624526 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt @@ -68,13 +68,13 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac private fun onMenuItemSelected(item: MenuItemImpl) { when (item.itemId) { - R.id.menu_copy -> context.sendToClip(callBack.getSelectedText()) + R.id.menu_copy -> context.sendToClip(callBack.selectedText) } } interface CallBack { fun onMenuItemSelected(item: MenuItemImpl): Boolean - fun getSelectedText(): String + val selectedText: String } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index 4bcaa7945..9474ad4d1 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -306,9 +306,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi } override fun edit(rule: ReplaceRule) { - ReplaceEditDialog - .newInstance(rule.id) - .show(supportFragmentManager, "editReplace") + ReplaceEditDialog.show(supportFragmentManager, rule.id) } override fun toTop(rule: ReplaceRule) { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt b/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt index 5470b4c5f..ba7d3a168 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt @@ -9,6 +9,7 @@ import android.view.ViewGroup import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import androidx.appcompat.widget.Toolbar import androidx.fragment.app.DialogFragment +import androidx.fragment.app.FragmentManager import androidx.lifecycle.Observer import io.legado.app.R import io.legado.app.constant.Theme @@ -22,14 +23,15 @@ class ReplaceEditDialog : DialogFragment(), companion object { - fun newInstance(id: Long? = null): ReplaceEditDialog { + fun show(fragmentManager: FragmentManager, id: Long? = null, pattern: String? = null) { val dialog = ReplaceEditDialog() id?.let { val bundle = Bundle() bundle.putLong("id", id) + bundle.putString("pattern", pattern) dialog.arguments = bundle } - return dialog + dialog.show(fragmentManager, "editReplace") } } @@ -56,7 +58,7 @@ class ReplaceEditDialog : DialogFragment(), tool_bar.inflateMenu(R.menu.replace_edit) tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.setOnMenuItemClickListener(this) - viewModel.replaceRuleData.observe(this, Observer { + viewModel.replaceRuleData.observe(viewLifecycleOwner, Observer { upReplaceView(it) }) arguments?.let { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditViewModel.kt index 281a0ac2d..f326c88c3 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditViewModel.kt @@ -19,6 +19,12 @@ class ReplaceEditViewModel(application: Application) : BaseViewModel(application App.db.replaceRuleDao().findById(id)?.let { replaceRuleData.postValue(it) } + } else { + bundle.getString("pattern")?.let { pattern -> + replaceRuleData.postValue( + ReplaceRule(pattern = pattern) + ) + } } } }