From a2712e904eca7f4af4c4b4d3e1e79cecbd2892c3 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 13 Oct 2020 23:38:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9B=BF=E6=8D=A2=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 10 +- .../java/io/legado/app/constant/EventBus.kt | 1 + .../legado/app/ui/audio/AudioPlayActivity.kt | 2 +- .../app/ui/book/info/BookInfoActivity.kt | 2 +- .../app/ui/book/read/ReadBookActivity.kt | 16 +- .../{chapterlist => toc}/BookmarkAdapter.kt | 2 +- .../{chapterlist => toc}/BookmarkFragment.kt | 2 +- .../ChapterListActivity.kt | 2 +- .../ChapterListAdapter.kt | 2 +- .../ChapterListFragment.kt | 2 +- .../ChapterListViewModel.kt | 2 +- .../io/legado/app/ui/main/my/MyFragment.kt | 2 +- .../{replacerule => replace}/DiffCallBack.kt | 2 +- .../GroupManageDialog.kt | 2 +- .../ReplaceRuleActivity.kt | 8 +- .../ReplaceRuleAdapter.kt | 2 +- .../ReplaceRuleViewModel.kt | 2 +- .../ui/replace/edit/ReplaceEditActivity.kt | 166 ++++++++++++++++++ .../edit/ReplaceEditViewModel.kt | 14 +- .../ui/replacerule/edit/ReplaceEditDialog.kt | 158 ----------------- ...ace_edit.xml => activity_replace_edit.xml} | 14 +- .../main/res/layout/activity_replace_rule.xml | 2 +- 22 files changed, 216 insertions(+), 199 deletions(-) rename app/src/main/java/io/legado/app/ui/book/{chapterlist => toc}/BookmarkAdapter.kt (98%) rename app/src/main/java/io/legado/app/ui/book/{chapterlist => toc}/BookmarkFragment.kt (99%) rename app/src/main/java/io/legado/app/ui/book/{chapterlist => toc}/ChapterListActivity.kt (98%) rename app/src/main/java/io/legado/app/ui/book/{chapterlist => toc}/ChapterListAdapter.kt (98%) rename app/src/main/java/io/legado/app/ui/book/{chapterlist => toc}/ChapterListFragment.kt (99%) rename app/src/main/java/io/legado/app/ui/book/{chapterlist => toc}/ChapterListViewModel.kt (95%) rename app/src/main/java/io/legado/app/ui/{replacerule => replace}/DiffCallBack.kt (97%) rename app/src/main/java/io/legado/app/ui/{replacerule => replace}/GroupManageDialog.kt (99%) rename app/src/main/java/io/legado/app/ui/{replacerule => replace}/ReplaceRuleActivity.kt (98%) rename app/src/main/java/io/legado/app/ui/{replacerule => replace}/ReplaceRuleAdapter.kt (99%) rename app/src/main/java/io/legado/app/ui/{replacerule => replace}/ReplaceRuleViewModel.kt (99%) create mode 100644 app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt rename app/src/main/java/io/legado/app/ui/{replacerule => replace}/edit/ReplaceEditViewModel.kt (77%) delete mode 100644 app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt rename app/src/main/res/layout/{dialog_replace_edit.xml => activity_replace_edit.xml} (91%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d67e2db53..8a50fc718 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -181,6 +181,12 @@ android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize|stateHidden" /> + + @@ -220,7 +226,7 @@ android:launchMode="singleTop" /> (R.layout.activity_boo ReadBook.CallBack, AutoReadDialog.CallBack, TocRegexDialog.CallBack, - ReplaceEditDialog.CallBack, ColorPickerDialogListener { private val requestCodeChapterList = 568 private val requestCodeEditSource = 111 @@ -487,8 +486,8 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo ReadBook.bookSource?.bookSourceUrl?.let { scopes.add(it) } - ReplaceEditDialog.show( - supportFragmentManager, + ReplaceEditActivity.show( + this, pattern = selectedText, scope = scopes.joinToString(";") ) @@ -696,7 +695,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo /** * 替换规则变化 */ - override fun onReplaceRuleSave() { + private fun onReplaceRuleSave() { Coroutine.async { BookHelp.upReplaceRules() ReadBook.loadContent(resetPageOffset = false) @@ -918,6 +917,9 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo observeEvent(PreferKey.showBrightnessView) { read_menu.upBrightnessState() } + observeEvent(EventBus.REPLACE_RULE_SAVE) { + onReplaceRuleSave() + } } private fun upScreenTimeOut() { diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkAdapter.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt similarity index 98% rename from app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkAdapter.kt rename to app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt index e9c7384ea..64f004aed 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.book.chapterlist +package io.legado.app.ui.book.toc import android.view.LayoutInflater import android.view.View diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt similarity index 99% rename from app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkFragment.kt rename to app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt index b24d438e8..0f507640c 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/BookmarkFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.book.chapterlist +package io.legado.app.ui.book.toc import android.annotation.SuppressLint import android.app.Activity diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListActivity.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt similarity index 98% rename from app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListActivity.kt rename to app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt index 58f19a6b4..b595bacbe 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.book.chapterlist +package io.legado.app.ui.book.toc import android.os.Bundle import android.view.Menu diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListAdapter.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt similarity index 98% rename from app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListAdapter.kt rename to app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt index 5d4b3621b..9a994f7ca 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.book.chapterlist +package io.legado.app.ui.book.toc import android.content.Context import android.view.View diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt similarity index 99% rename from app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListFragment.kt rename to app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt index d4339bb23..679b53238 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.book.chapterlist +package io.legado.app.ui.book.toc import android.annotation.SuppressLint import android.app.Activity.RESULT_OK diff --git a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListViewModel.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListViewModel.kt similarity index 95% rename from app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListViewModel.kt rename to app/src/main/java/io/legado/app/ui/book/toc/ChapterListViewModel.kt index 7671facc9..2fed31e10 100644 --- a/app/src/main/java/io/legado/app/ui/book/chapterlist/ChapterListViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListViewModel.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.book.chapterlist +package io.legado.app.ui.book.toc import android.app.Application diff --git a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt index 8ceb76b48..20b36c065 100644 --- a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt @@ -24,7 +24,7 @@ import io.legado.app.ui.config.BackupRestoreUi import io.legado.app.ui.config.ConfigActivity import io.legado.app.ui.config.ConfigViewModel import io.legado.app.ui.filechooser.FileChooserDialog -import io.legado.app.ui.replacerule.ReplaceRuleActivity +import io.legado.app.ui.replace.ReplaceRuleActivity import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.ui.widget.prefs.NameListPreference import io.legado.app.ui.widget.prefs.PreferenceCategory diff --git a/app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt b/app/src/main/java/io/legado/app/ui/replace/DiffCallBack.kt similarity index 97% rename from app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt rename to app/src/main/java/io/legado/app/ui/replace/DiffCallBack.kt index 62fe0cec3..0a5200116 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt +++ b/app/src/main/java/io/legado/app/ui/replace/DiffCallBack.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.replacerule +package io.legado.app.ui.replace import android.os.Bundle import androidx.recyclerview.widget.DiffUtil diff --git a/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt similarity index 99% rename from app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt rename to app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt index bf9cb5409..b18c15fc1 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.replacerule +package io.legado.app.ui.replace import android.annotation.SuppressLint import android.content.Context diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt similarity index 98% rename from app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt rename to app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt index 6e0ae230d..302c59b6d 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.replacerule +package io.legado.app.ui.replace import android.annotation.SuppressLint import android.app.Activity @@ -28,7 +28,7 @@ import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.association.ImportReplaceRuleActivity import io.legado.app.ui.filechooser.FileChooserDialog import io.legado.app.ui.filechooser.FilePicker -import io.legado.app.ui.replacerule.edit.ReplaceEditDialog +import io.legado.app.ui.replace.edit.ReplaceEditActivity import io.legado.app.ui.widget.SelectActionBar import io.legado.app.ui.widget.recycler.DragSelectTouchHelper import io.legado.app.ui.widget.recycler.ItemTouchCallback @@ -169,7 +169,7 @@ class ReplaceRuleActivity : override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_add_replace_rule -> - ReplaceEditDialog().show(supportFragmentManager, "replaceNew") + ReplaceEditActivity.show(this) R.id.menu_group_manage -> GroupManageDialog().show(supportFragmentManager, "groupManage") @@ -306,7 +306,7 @@ class ReplaceRuleActivity : override fun edit(rule: ReplaceRule) { setResult(Activity.RESULT_OK) - ReplaceEditDialog.show(supportFragmentManager, rule.id) + ReplaceEditActivity.show(this, rule.id) } override fun toTop(rule: ReplaceRule) { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleAdapter.kt similarity index 99% rename from app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt rename to app/src/main/java/io/legado/app/ui/replace/ReplaceRuleAdapter.kt index bcb29e87d..14c42568a 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleAdapter.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.replacerule +package io.legado.app.ui.replace import android.content.Context import android.os.Bundle diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleViewModel.kt similarity index 99% rename from app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt rename to app/src/main/java/io/legado/app/ui/replace/ReplaceRuleViewModel.kt index afbbcdcf6..a14647c82 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleViewModel.kt @@ -1,4 +1,4 @@ -package io.legado.app.ui.replacerule +package io.legado.app.ui.replace import android.app.Application import android.text.TextUtils diff --git a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt new file mode 100644 index 000000000..90e1fcaa0 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt @@ -0,0 +1,166 @@ +package io.legado.app.ui.replace.edit + +import android.content.Context +import android.content.Intent +import android.graphics.Rect +import android.os.Bundle +import android.view.Gravity +import android.view.Menu +import android.view.MenuItem +import android.view.ViewTreeObserver +import android.widget.EditText +import android.widget.PopupWindow +import io.legado.app.R +import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.AppConst +import io.legado.app.constant.EventBus +import io.legado.app.data.entities.ReplaceRule +import io.legado.app.ui.widget.KeyboardToolPop +import io.legado.app.utils.getViewModel +import io.legado.app.utils.postEvent +import kotlinx.android.synthetic.main.activity_replace_edit.* +import org.jetbrains.anko.displayMetrics +import org.jetbrains.anko.toast +import kotlin.math.abs + +/** + * 编辑替换规则 + */ +class ReplaceEditActivity : + VMBaseActivity(R.layout.activity_replace_edit, false), + ViewTreeObserver.OnGlobalLayoutListener, + KeyboardToolPop.CallBack { + + companion object { + + fun show( + context: Context, + id: Long = -1, + pattern: String? = null, + isRegex: Boolean = false, + scope: String? = null + ) { + val intent = Intent(context, ReplaceEditActivity::class.java) + intent.putExtra("id", id) + intent.putExtra("pattern", pattern) + intent.putExtra("isRegex", isRegex) + intent.putExtra("scope", scope) + context.startActivity(intent) + } + } + + override val viewModel: ReplaceEditViewModel + get() = getViewModel(ReplaceEditViewModel::class.java) + + private var mSoftKeyboardTool: PopupWindow? = null + private var mIsSoftKeyBoardShowing = false + + override fun onActivityCreated(savedInstanceState: Bundle?) { + mSoftKeyboardTool = KeyboardToolPop(this, AppConst.keyboardToolChars, this) + window.decorView.viewTreeObserver.addOnGlobalLayoutListener(this) + viewModel.replaceRuleData.observe(this, { + upReplaceView(it) + }) + viewModel.initData(intent) + } + + override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.replace_edit, menu) + return super.onCompatCreateOptionsMenu(menu) + } + + override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.menu_save -> { + val rule = getReplaceRule() + if (!rule.isValid()) { + toast(R.string.replace_rule_invalid) + } else { + viewModel.save(rule) { + postEvent(EventBus.REPLACE_RULE_SAVE, "") + finish() + } + } + } + } + return true + } + + private fun upReplaceView(replaceRule: ReplaceRule) { + et_name.setText(replaceRule.name) + et_group.setText(replaceRule.group) + et_replace_rule.setText(replaceRule.pattern) + cb_use_regex.isChecked = replaceRule.isRegex + et_replace_to.setText(replaceRule.replacement) + et_scope.setText(replaceRule.scope) + } + + private fun getReplaceRule(): ReplaceRule { + val replaceRule: ReplaceRule = viewModel.replaceRuleData.value ?: ReplaceRule() + replaceRule.name = et_name.text.toString() + replaceRule.group = et_group.text.toString() + replaceRule.pattern = et_replace_rule.text.toString() + replaceRule.isRegex = cb_use_regex.isChecked + replaceRule.replacement = et_replace_to.text.toString() + replaceRule.scope = et_scope.text.toString() + return replaceRule + } + + private fun insertText(text: String) { + if (text.isBlank()) return + val view = window?.decorView?.findFocus() + if (view is EditText) { + val start = view.selectionStart + val end = view.selectionEnd + val edit = view.editableText//获取EditText的文字 + if (start < 0 || start >= edit.length) { + edit.append(text) + } else { + edit.replace(start, end, text)//光标所在位置插入文字 + } + } + } + + override fun sendText(text: String) { + if (text == AppConst.keyboardToolChars[0]) { + val view = window?.decorView?.findFocus() + view?.clearFocus() + } else { + insertText(text) + } + } + + private fun showKeyboardTopPopupWindow() { + mSoftKeyboardTool?.let { + if (it.isShowing) return + if (!isFinishing) { + it.showAtLocation(ll_content, Gravity.BOTTOM, 0, 0) + } + } + } + + private fun closePopupWindow() { + mSoftKeyboardTool?.dismiss() + } + + override fun onGlobalLayout() { + val rect = Rect() + // 获取当前页面窗口的显示范围 + window.decorView.getWindowVisibleDisplayFrame(rect) + val screenHeight = this.displayMetrics.heightPixels + val keyboardHeight = screenHeight - rect.bottom // 输入法的高度 + val preShowing = mIsSoftKeyBoardShowing + if (abs(keyboardHeight) > screenHeight / 5) { + mIsSoftKeyBoardShowing = true // 超过屏幕五分之一则表示弹出了输入法 + root_view.setPadding(0, 0, 0, 100) + showKeyboardTopPopupWindow() + } else { + mIsSoftKeyBoardShowing = false + root_view.setPadding(0, 0, 0, 0) + if (preShowing) { + closePopupWindow() + } + } + } + +} diff --git a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt similarity index 77% rename from app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditViewModel.kt rename to app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt index 1308b64ec..12df23446 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt @@ -1,7 +1,7 @@ -package io.legado.app.ui.replacerule.edit +package io.legado.app.ui.replace.edit import android.app.Application -import android.os.Bundle +import android.content.Intent import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel @@ -11,18 +11,18 @@ class ReplaceEditViewModel(application: Application) : BaseViewModel(application val replaceRuleData = MutableLiveData() - fun initData(bundle: Bundle) { + fun initData(intent: Intent) { execute { replaceRuleData.value ?: let { - val id = bundle.getLong("id") + val id = intent.getLongExtra("id", -1) if (id > 0) { App.db.replaceRuleDao().findById(id)?.let { replaceRuleData.postValue(it) } } else { - val pattern = bundle.getString("pattern") ?: "" - val isRegex = bundle.getBoolean("isRegex") - val scope = bundle.getString("scope") + val pattern = intent.getStringExtra("pattern") ?: "" + val isRegex = intent.getBooleanExtra("isRegex", false) + val scope = intent.getStringExtra("scope") val rule = ReplaceRule( name = pattern, pattern = pattern, 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 deleted file mode 100644 index fd922296b..000000000 --- a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt +++ /dev/null @@ -1,158 +0,0 @@ -package io.legado.app.ui.replacerule.edit - -import android.os.Bundle -import android.view.LayoutInflater -import android.view.MenuItem -import android.view.View -import android.view.ViewGroup -import android.view.ViewGroup.LayoutParams.MATCH_PARENT -import android.view.ViewGroup.LayoutParams.WRAP_CONTENT -import android.widget.EditText -import android.widget.PopupWindow -import androidx.appcompat.widget.Toolbar -import androidx.fragment.app.FragmentManager -import io.legado.app.R -import io.legado.app.base.BaseDialogFragment -import io.legado.app.constant.AppConst -import io.legado.app.data.entities.ReplaceRule -import io.legado.app.lib.theme.primaryColor -import io.legado.app.ui.widget.KeyboardToolPop -import io.legado.app.utils.applyTint -import io.legado.app.utils.getViewModel -import io.legado.app.utils.toast -import kotlinx.android.synthetic.main.dialog_replace_edit.* -import org.jetbrains.anko.sdk27.listeners.onFocusChange - -/** - * 编辑替换规则 - */ -class ReplaceEditDialog : BaseDialogFragment(), - Toolbar.OnMenuItemClickListener, - KeyboardToolPop.CallBack { - - companion object { - - fun show( - fragmentManager: FragmentManager, - id: Long = -1, - pattern: String? = null, - isRegex: Boolean = false, - scope: String? = null - ) { - val dialog = ReplaceEditDialog() - val bundle = Bundle() - bundle.putLong("id", id) - bundle.putString("pattern", pattern) - bundle.putBoolean("isRegex", isRegex) - bundle.putString("scope", scope) - dialog.arguments = bundle - dialog.show(fragmentManager, this::class.simpleName) - } - } - - private lateinit var viewModel: ReplaceEditViewModel - private lateinit var mSoftKeyboardTool: PopupWindow - - override fun onStart() { - super.onStart() - dialog?.window?.setLayout(MATCH_PARENT, WRAP_CONTENT) - } - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - viewModel = getViewModel(ReplaceEditViewModel::class.java) - return inflater.inflate(R.layout.dialog_replace_edit, container) - } - - override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { - tool_bar.setBackgroundColor(primaryColor) - mSoftKeyboardTool = KeyboardToolPop(requireContext(), AppConst.keyboardToolChars, this) - tool_bar.inflateMenu(R.menu.replace_edit) - tool_bar.menu.applyTint(requireContext()) - tool_bar.setOnMenuItemClickListener(this) - viewModel.replaceRuleData.observe(viewLifecycleOwner, { - upReplaceView(it) - }) - arguments?.let { - viewModel.initData(it) - } - et_replace_rule.onFocusChange { v, hasFocus -> - if (hasFocus) { - mSoftKeyboardTool.width = v.width - mSoftKeyboardTool.showAsDropDown(v) - } else { - mSoftKeyboardTool.dismiss() - } - } - } - - override fun onMenuItemClick(item: MenuItem?): Boolean { - when (item?.itemId) { - R.id.menu_save -> { - val rule = getReplaceRule() - if (!rule.isValid()){ - toast(R.string.replace_rule_invalid) - } else{ - viewModel.save(rule) { - callBack?.onReplaceRuleSave() - dismiss() - } - } - } - } - return true - } - - private fun upReplaceView(replaceRule: ReplaceRule) { - et_name.setText(replaceRule.name) - et_group.setText(replaceRule.group) - et_replace_rule.setText(replaceRule.pattern) - cb_use_regex.isChecked = replaceRule.isRegex - et_replace_to.setText(replaceRule.replacement) - et_scope.setText(replaceRule.scope) - } - - private fun getReplaceRule(): ReplaceRule { - val replaceRule: ReplaceRule = viewModel.replaceRuleData.value ?: ReplaceRule() - replaceRule.name = et_name.text.toString() - replaceRule.group = et_group.text.toString() - replaceRule.pattern = et_replace_rule.text.toString() - replaceRule.isRegex = cb_use_regex.isChecked - replaceRule.replacement = et_replace_to.text.toString() - replaceRule.scope = et_scope.text.toString() - return replaceRule - } - - val callBack get() = activity as? CallBack - - private fun insertText(text: String) { - if (text.isBlank()) return - val view = dialog?.window?.decorView?.findFocus() - if (view is EditText) { - val start = view.selectionStart - val end = view.selectionEnd - val edit = view.editableText//获取EditText的文字 - if (start < 0 || start >= edit.length) { - edit.append(text) - } else { - edit.replace(start, end, text)//光标所在位置插入文字 - } - } - } - - override fun sendText(text: String) { - if (text == AppConst.keyboardToolChars[0]) { - val view = dialog?.window?.decorView?.findFocus() - view?.clearFocus() - } else { - insertText(text) - } - } - - interface CallBack { - fun onReplaceRuleSave() - } -} diff --git a/app/src/main/res/layout/dialog_replace_edit.xml b/app/src/main/res/layout/activity_replace_edit.xml similarity index 91% rename from app/src/main/res/layout/dialog_replace_edit.xml rename to app/src/main/res/layout/activity_replace_edit.xml index 91af590fe..c6b80619d 100644 --- a/app/src/main/res/layout/dialog_replace_edit.xml +++ b/app/src/main/res/layout/activity_replace_edit.xml @@ -1,19 +1,19 @@ - + app:contentInsetStartWithNavigation="0dp" + app:displayHomeAsUp="true" + app:fitStatusBar="false" + app:title="@string/replace_rule_edit" /> + tools:context=".ui.replace.ReplaceRuleActivity">