优化替换编辑界面

pull/418/head
gedoor 4 years ago
parent a59ecb190a
commit a2712e904e
  1. 10
      app/src/main/AndroidManifest.xml
  2. 1
      app/src/main/java/io/legado/app/constant/EventBus.kt
  3. 2
      app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt
  4. 2
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  5. 16
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  6. 2
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt
  7. 2
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt
  8. 2
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt
  9. 2
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt
  10. 2
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt
  11. 2
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListViewModel.kt
  12. 2
      app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt
  13. 2
      app/src/main/java/io/legado/app/ui/replace/DiffCallBack.kt
  14. 2
      app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt
  15. 8
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt
  16. 2
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleAdapter.kt
  17. 2
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleViewModel.kt
  18. 166
      app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt
  19. 14
      app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt
  20. 158
      app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt
  21. 14
      app/src/main/res/layout/activity_replace_edit.xml
  22. 2
      app/src/main/res/layout/activity_replace_rule.xml

@ -181,6 +181,12 @@
android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize|stateHidden" />
<!-- 书源编辑 -->
<activity
android:name=".ui.replace.edit.ReplaceEditActivity"
android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize|stateHidden" />
<!-- 配置界面 -->
<activity
android:name=".ui.config.ConfigActivity"
@ -207,7 +213,7 @@
android:launchMode="singleTop" />
<!-- 替换规则界面 -->
<activity
android:name=".ui.replacerule.ReplaceRuleActivity"
android:name=".ui.replace.ReplaceRuleActivity"
android:launchMode="singleTop">
</activity>
<!-- 书籍管理 -->
@ -220,7 +226,7 @@
android:launchMode="singleTop" />
<!-- 目录 -->
<activity
android:name=".ui.book.chapterlist.ChapterListActivity"
android:name=".ui.book.toc.ChapterListActivity"
android:launchMode="singleTop"
android:screenOrientation="behind" />
<activity

@ -22,4 +22,5 @@ object EventBus {
const val SAVE_CONTENT = "saveContent"
const val CHECK_SOURCE = "checkSource"
const val CHECK_SOURCE_DONE = "checkSourceDone"
const val REPLACE_RULE_SAVE = "replaceRuleSave"
}

@ -25,7 +25,7 @@ import io.legado.app.lib.dialogs.okButton
import io.legado.app.service.AudioPlayService
import io.legado.app.service.help.AudioPlay
import io.legado.app.ui.book.changesource.ChangeSourceDialog
import io.legado.app.ui.book.chapterlist.ChapterListActivity
import io.legado.app.ui.book.toc.ChapterListActivity
import io.legado.app.ui.widget.image.CoverImageView
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_audio_play.*

@ -28,12 +28,12 @@ import io.legado.app.lib.theme.getPrimaryTextColor
import io.legado.app.ui.audio.AudioPlayActivity
import io.legado.app.ui.book.changecover.ChangeCoverDialog
import io.legado.app.ui.book.changesource.ChangeSourceDialog
import io.legado.app.ui.book.chapterlist.ChapterListActivity
import io.legado.app.ui.book.group.GroupSelectDialog
import io.legado.app.ui.book.info.edit.BookInfoEditActivity
import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.book.search.SearchActivity
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.book.toc.ChapterListActivity
import io.legado.app.ui.widget.image.CoverImageView
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_book_info.*

@ -38,7 +38,6 @@ import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.help.ReadAloud
import io.legado.app.service.help.ReadBook
import io.legado.app.ui.book.changesource.ChangeSourceDialog
import io.legado.app.ui.book.chapterlist.ChapterListActivity
import io.legado.app.ui.book.info.BookInfoActivity
import io.legado.app.ui.book.read.config.*
import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.BG_COLOR
@ -49,9 +48,10 @@ import io.legado.app.ui.book.read.page.TextPageFactory
import io.legado.app.ui.book.read.page.delegate.PageDelegate
import io.legado.app.ui.book.searchContent.SearchContentActivity
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.book.toc.ChapterListActivity
import io.legado.app.ui.login.SourceLogin
import io.legado.app.ui.replacerule.ReplaceRuleActivity
import io.legado.app.ui.replacerule.edit.ReplaceEditDialog
import io.legado.app.ui.replace.ReplaceRuleActivity
import io.legado.app.ui.replace.edit.ReplaceEditActivity
import io.legado.app.ui.widget.dialog.TextDialog
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_book_read.*
@ -78,7 +78,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(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<ReadBookViewModel>(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<ReadBookViewModel>(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<ReadBookViewModel>(R.layout.activity_boo
observeEvent<String>(PreferKey.showBrightnessView) {
read_menu.upBrightnessState()
}
observeEvent<String>(EventBus.REPLACE_RULE_SAVE) {
onReplaceRuleSave()
}
}
private fun upScreenTimeOut() {

@ -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

@ -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

@ -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

@ -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

@ -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

@ -1,4 +1,4 @@
package io.legado.app.ui.book.chapterlist
package io.legado.app.ui.book.toc
import android.app.Application

@ -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

@ -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

@ -1,4 +1,4 @@
package io.legado.app.ui.replacerule
package io.legado.app.ui.replace
import android.annotation.SuppressLint
import android.content.Context

@ -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) {

@ -1,4 +1,4 @@
package io.legado.app.ui.replacerule
package io.legado.app.ui.replace
import android.content.Context
import android.os.Bundle

@ -1,4 +1,4 @@
package io.legado.app.ui.replacerule
package io.legado.app.ui.replace
import android.app.Application
import android.text.TextUtils

@ -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<ReplaceEditViewModel>(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()
}
}
}
}

@ -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<ReplaceRule>()
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,

@ -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()
}
}

@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tool_bar"
<io.legado.app.ui.widget.TitleBar
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_menu"
android:theme="?attr/actionBarStyle"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/replace_rule_edit"
app:titleTextAppearance="@style/ToolbarTitle" />
app:contentInsetStartWithNavigation="0dp"
app:displayHomeAsUp="true"
app:fitStatusBar="false"
app:title="@string/replace_rule_edit" />
<ScrollView
android:layout_width="match_parent"

@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.replacerule.ReplaceRuleActivity">
tools:context=".ui.replace.ReplaceRuleActivity">
<io.legado.app.ui.widget.TitleBar
android:id="@+id/title_bar"

Loading…
Cancel
Save