diff --git a/app/src/main/java/io/legado/app/data/dao/KeyboardAssistsDao.kt b/app/src/main/java/io/legado/app/data/dao/KeyboardAssistsDao.kt index b6b3406d8..1292c219b 100644 --- a/app/src/main/java/io/legado/app/data/dao/KeyboardAssistsDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/KeyboardAssistsDao.kt @@ -3,6 +3,7 @@ package io.legado.app.data.dao import androidx.room.* import io.legado.app.data.entities.KeyboardAssist import io.legado.app.help.DefaultData +import kotlinx.coroutines.flow.Flow @Dao interface KeyboardAssistsDao { @@ -13,6 +14,15 @@ interface KeyboardAssistsDao { @Query("select * from keyboardAssists where type = :type") fun getByType(type: Int): List + @get:Query("select * from keyboardAssists") + val flowAll: Flow> + + @Query("select * from keyboardAssists where type = :type") + fun flowByType(type: Int): Flow> + + @get:Query("select max(serialNo) from keyboardAssists") + val maxSerialNo: Int + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg keyboardAssist: KeyboardAssist) diff --git a/app/src/main/java/io/legado/app/data/entities/KeyboardAssist.kt b/app/src/main/java/io/legado/app/data/entities/KeyboardAssist.kt index 3677967b3..e3765e641 100644 --- a/app/src/main/java/io/legado/app/data/entities/KeyboardAssist.kt +++ b/app/src/main/java/io/legado/app/data/entities/KeyboardAssist.kt @@ -6,11 +6,11 @@ import androidx.room.Entity @Entity(tableName = "keyboardAssists", primaryKeys = ["type", "key"]) data class KeyboardAssist( @ColumnInfo(defaultValue = "0") - val type: Int = 0, + var type: Int = 0, @ColumnInfo(defaultValue = "") - val key: String, + var key: String, @ColumnInfo(defaultValue = "") - val value: String, + var value: String, @ColumnInfo(defaultValue = "0") - val serialNo: Int + var serialNo: Int = 0 ) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt index f4b0a41f1..4f9775886 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt @@ -46,7 +46,7 @@ class GroupManageDialog : BaseDialogFragment(R.layout.dialog_recycler_view), override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { binding.toolBar.setBackgroundColor(primaryColor) - binding.toolBar.title = getString(R.string.group_manage) + binding.toolBar.setTitle(R.string.group_manage) initView() initData() initMenu() diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 7c1a3d84b..b5f472eb8 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -17,16 +17,16 @@ import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.* import io.legado.app.databinding.ActivityBookSourceEditBinding import io.legado.app.help.config.LocalConfig +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert -import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.primaryColor import io.legado.app.ui.book.source.debug.BookSourceDebugActivity import io.legado.app.ui.document.HandleFileContract import io.legado.app.ui.login.SourceLoginActivity import io.legado.app.ui.qrcode.QrCodeResult -import io.legado.app.ui.widget.KeyboardToolPop import io.legado.app.ui.widget.dialog.TextDialog +import io.legado.app.ui.widget.keyboard.KeyboardToolPop import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -435,17 +435,24 @@ class BookSourceEditActivity : return true } - override fun keyboardHelp() { - val items = arrayListOf("插入URL参数", "书源教程", "js教程", "正则教程", "选择文件") - selector(getString(R.string.help), items) { _, index -> - when (index) { - 0 -> sendText(AppConst.urlOption) - 1 -> showHelp("ruleHelp") - 2 -> showHelp("jsHelp") - 3 -> showHelp("regexHelp") - 4 -> selectDoc.launch { - mode = HandleFileContract.FILE - } + override fun helpActions(): List> { + return arrayListOf( + SelectItem("插入URL参数", "urlOption"), + SelectItem("书源教程", "ruleHelp"), + SelectItem("js教程", "jsHelp"), + SelectItem("正则教程", "regexHelp"), + SelectItem("选择文件", "selectFile"), + ) + } + + override fun onHelpActionSelect(action: String) { + when (action) { + "urlOption" -> sendText(AppConst.urlOption) + "ruleHelp" -> showHelp("ruleHelp") + "jsHelp" -> showHelp("jsHelp") + "regexHelp" -> showHelp("regexHelp") + "selectFile" -> selectDoc.launch { + mode = HandleFileContract.FILE } } } 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 index 7201f0405..9c8044075 100644 --- 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 @@ -11,9 +11,9 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.ReplaceRule import io.legado.app.databinding.ActivityReplaceEditBinding -import io.legado.app.lib.dialogs.selector -import io.legado.app.ui.widget.KeyboardToolPop +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.ui.widget.dialog.TextDialog +import io.legado.app.ui.widget.keyboard.KeyboardToolPop import io.legado.app.utils.showDialogFragment import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -57,7 +57,7 @@ class ReplaceEditActivity : upReplaceView(it) } binding.ivHelp.setOnClickListener { - showRegexHelp() + showHelp("regexHelp") } } @@ -112,12 +112,15 @@ class ReplaceEditActivity : return replaceRule } - override fun keyboardHelp() { - val items = arrayListOf("正则教程") - selector(getString(R.string.help), items) { _, index -> - when (index) { - 0 -> showRegexHelp() - } + override fun helpActions(): List> { + return arrayListOf( + SelectItem("正则教程", "regexHelp") + ) + } + + override fun onHelpActionSelect(action: String) { + when (action) { + "regexHelp" -> showHelp("regexHelp") } } @@ -138,8 +141,10 @@ class ReplaceEditActivity : } } - private fun showRegexHelp() { - val mdText = String(assets.open("help/regexHelp.md").readBytes()) + @Suppress("SameParameterValue") + private fun showHelp(fileName: String) { + //显示目录help下的帮助文档 + val mdText = String(assets.open("help/${fileName}.md").readBytes()) showDialogFragment(TextDialog(mdText, TextDialog.Mode.MD)) } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt index 9379fd463..08961c6aa 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt @@ -13,14 +13,15 @@ import io.legado.app.constant.AppConst import io.legado.app.data.entities.RssSource import io.legado.app.databinding.ActivityRssSourceEditBinding import io.legado.app.help.config.LocalConfig +import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert -import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.primaryColor +import io.legado.app.ui.document.HandleFileContract import io.legado.app.ui.login.SourceLoginActivity import io.legado.app.ui.qrcode.QrCodeResult import io.legado.app.ui.rss.source.debug.RssSourceDebugActivity -import io.legado.app.ui.widget.KeyboardToolPop import io.legado.app.ui.widget.dialog.TextDialog +import io.legado.app.ui.widget.keyboard.KeyboardToolPop import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -35,6 +36,15 @@ class RssSourceEditActivity : } private val adapter by lazy { RssSourceEditAdapter() } private val sourceEntities: ArrayList = ArrayList() + private val selectDoc = registerForActivityResult(HandleFileContract()) { + it.uri?.let { uri -> + if (uri.isContentScheme()) { + sendText(uri.toString()) + } else { + sendText(uri.path.toString()) + } + } + } private val qrCodeResult = registerForActivityResult(QrCodeResult()) { it?.let { viewModel.importSource(it) { source: RssSource -> @@ -54,7 +64,7 @@ class RssSourceEditActivity : override fun onPostCreate(savedInstanceState: Bundle?) { super.onPostCreate(savedInstanceState) if (!LocalConfig.ruleHelpVersionIsLast) { - showRuleHelp() + showHelp("ruleHelp") } } @@ -130,7 +140,7 @@ class RssSourceEditActivity : getString(R.string.share_rss_source), ErrorCorrectionLevel.L ) - R.id.menu_help -> showRuleHelp() + R.id.menu_help -> showHelp("ruleHelp") } return super.onCompatOptionsItemSelected(item) } @@ -225,13 +235,24 @@ class RssSourceEditActivity : return true } - override fun keyboardHelp() { - val items = arrayListOf("插入URL参数", "订阅源教程", "正则教程") - selector(getString(R.string.help), items) { _, index -> - when (index) { - 0 -> sendText(AppConst.urlOption) - 1 -> showRuleHelp() - 2 -> showRegexHelp() + override fun helpActions(): List> { + return arrayListOf( + SelectItem("插入URL参数", "urlOption"), + SelectItem("订阅源教程", "ruleHelp"), + SelectItem("js教程", "jsHelp"), + SelectItem("正则教程", "regexHelp"), + SelectItem("选择文件", "selectFile"), + ) + } + + override fun onHelpActionSelect(action: String) { + when (action) { + "urlOption" -> sendText(AppConst.urlOption) + "ruleHelp" -> showHelp("ruleHelp") + "jsHelp" -> showHelp("jsHelp") + "regexHelp" -> showHelp("regexHelp") + "selectFile" -> selectDoc.launch { + mode = HandleFileContract.FILE } } } @@ -251,13 +272,9 @@ class RssSourceEditActivity : } } - private fun showRuleHelp() { - val mdText = String(assets.open("help/ruleHelp.md").readBytes()) - showDialogFragment(TextDialog(mdText, TextDialog.Mode.MD)) - } - - private fun showRegexHelp() { - val mdText = String(assets.open("help/regexHelp.md").readBytes()) + private fun showHelp(fileName: String) { + //显示目录help下的帮助文档 + val mdText = String(assets.open("help/${fileName}.md").readBytes()) showDialogFragment(TextDialog(mdText, TextDialog.Mode.MD)) } diff --git a/app/src/main/java/io/legado/app/ui/widget/keyboard/KeyboardAssistsConfig.kt b/app/src/main/java/io/legado/app/ui/widget/keyboard/KeyboardAssistsConfig.kt new file mode 100644 index 000000000..2af55b0ca --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/keyboard/KeyboardAssistsConfig.kt @@ -0,0 +1,156 @@ +package io.legado.app.ui.widget.keyboard + +import android.content.Context +import android.os.Bundle +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import androidx.appcompat.widget.Toolbar +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import io.legado.app.R +import io.legado.app.base.BaseDialogFragment +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.RecyclerAdapter +import io.legado.app.data.appDb +import io.legado.app.data.entities.KeyboardAssist +import io.legado.app.databinding.DialogMultipleEditTextBinding +import io.legado.app.databinding.DialogRecyclerViewBinding +import io.legado.app.databinding.Item1lineTextAndDelBinding +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.theme.primaryColor +import io.legado.app.ui.widget.recycler.ItemTouchCallback +import io.legado.app.ui.widget.recycler.VerticalDivider +import io.legado.app.utils.applyTint +import io.legado.app.utils.setLayout +import io.legado.app.utils.viewbindingdelegate.viewBinding +import io.legado.app.utils.visible +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch + +class KeyboardAssistsConfig : BaseDialogFragment(R.layout.dialog_recycler_view), + Toolbar.OnMenuItemClickListener { + + private val binding by viewBinding(DialogRecyclerViewBinding::bind) + private val adapter by lazy { KeyAdapter(requireContext()) } + + override fun onStart() { + super.onStart() + setLayout(0.9f, 0.9f) + } + + override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { + binding.toolBar.setBackgroundColor(primaryColor) + binding.toolBar.setTitle(R.string.assists_key_config) + initView() + initMenu() + initData() + } + + private fun initView() { + binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) + binding.recyclerView.addItemDecoration(VerticalDivider(requireContext())) + binding.recyclerView.adapter = adapter + val itemTouchCallback = ItemTouchCallback(adapter) + itemTouchCallback.isCanDrag = true + ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recyclerView) + + } + + private fun initMenu() { + binding.toolBar.setOnMenuItemClickListener(this) + binding.toolBar.inflateMenu(R.menu.keyboard_assists_config) + binding.toolBar.menu.applyTint(requireContext()) + } + + private fun initData() { + launch { + appDb.keyboardAssistsDao.flowAll.collect { + adapter.setItems(it) + } + } + } + + override fun onMenuItemClick(item: MenuItem?): Boolean { + when (item?.itemId) { + R.id.menu_add -> editKey(null) + } + return false + } + + private fun editKey(keyboardAssist: KeyboardAssist?) { + alert { + setTitle("辅助按键") + val alertBinding = DialogMultipleEditTextBinding.inflate(layoutInflater).apply { + layout1.hint = "key" + layout2.hint = "value" + layout2.visible() + } + setCustomView(alertBinding.root) + cancelButton() + okButton { + launch(IO) { + val newKeyboardAssist = KeyboardAssist( + key = alertBinding.edit1.text.toString(), + value = alertBinding.edit2.text.toString() + ) + if (keyboardAssist == null) { + newKeyboardAssist.serialNo = appDb.keyboardAssistsDao.maxSerialNo + 1 + appDb.keyboardAssistsDao.insert(newKeyboardAssist) + } else { + newKeyboardAssist.serialNo = keyboardAssist.serialNo + appDb.keyboardAssistsDao.delete(keyboardAssist) + appDb.keyboardAssistsDao.insert(newKeyboardAssist) + } + } + } + } + } + + private inner class KeyAdapter(context: Context) : + RecyclerAdapter(context), + ItemTouchCallback.Callback { + + private var isMoved = false + + override fun getViewBinding(parent: ViewGroup): Item1lineTextAndDelBinding { + return Item1lineTextAndDelBinding.inflate(inflater, parent, false) + } + + override fun convert( + holder: ItemViewHolder, + binding: Item1lineTextAndDelBinding, + item: KeyboardAssist, + payloads: MutableList + ) { + binding.textView.text = item.key + } + + override fun registerListener(holder: ItemViewHolder, binding: Item1lineTextAndDelBinding) { + binding.ivDelete.setOnClickListener { + getItemByLayoutPosition(holder.layoutPosition)?.let { keyboardAssist -> + editKey(keyboardAssist) + } + } + } + + override fun swap(srcPosition: Int, targetPosition: Int): Boolean { + swapItem(srcPosition, targetPosition) + isMoved = true + return true + } + + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + if (isMoved) { + for ((index, item) in getItems().withIndex()) { + item.serialNo = index + 1 + } + launch(IO) { + appDb.keyboardAssistsDao.update(*getItems().toTypedArray()) + } + } + isMoved = false + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt b/app/src/main/java/io/legado/app/ui/widget/keyboard/KeyboardToolPop.kt similarity index 79% rename from app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt rename to app/src/main/java/io/legado/app/ui/widget/keyboard/KeyboardToolPop.kt index 103e8d259..86bc3ffe5 100644 --- a/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt +++ b/app/src/main/java/io/legado/app/ui/widget/keyboard/KeyboardToolPop.kt @@ -1,20 +1,23 @@ -package io.legado.app.ui.widget +package io.legado.app.ui.widget.keyboard import android.content.Context import android.graphics.Rect import android.view.* import android.widget.PopupWindow +import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.data.appDb import io.legado.app.data.entities.KeyboardAssist import io.legado.app.databinding.ItemFilletTextBinding import io.legado.app.databinding.PopupKeyboardToolBinding +import io.legado.app.lib.dialogs.SelectItem +import io.legado.app.lib.dialogs.selector +import io.legado.app.utils.activity +import io.legado.app.utils.showDialogFragment import io.legado.app.utils.windowSize import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import splitties.systemservices.layoutInflater import splitties.systemservices.windowManager import kotlin.math.abs @@ -83,7 +86,7 @@ class KeyboardToolPop( ItemFilletTextBinding.inflate(context.layoutInflater, it, false).apply { textView.text = helpChar root.setOnClickListener { - callBack.keyboardHelp() + helpAlert() } } } @@ -92,13 +95,29 @@ class KeyboardToolPop( @Suppress("MemberVisibilityCanBePrivate") fun upAdapterData() { scope.launch { - val items = withContext(IO) { - appDb.keyboardAssistsDao.getOrDefault() + appDb.keyboardAssistsDao.flowByType(0).collect { + adapter.setItems(it) } - adapter.setItems(items) } } + private fun helpAlert() { + val items = arrayListOf( + SelectItem(context.getString(R.string.assists_key_config), "keyConfig") + ) + items.addAll(callBack.helpActions()) + context.selector(context.getString(R.string.help), items) { _, selectItem, i -> + when (selectItem.value) { + "keyConfig" -> config() + else -> callBack.onHelpActionSelect(selectItem.value) + } + } + } + + private fun config() { + contentView.activity?.showDialogFragment() + } + inner class Adapter(context: Context) : RecyclerAdapter(context) { @@ -130,7 +149,9 @@ class KeyboardToolPop( interface CallBack { - fun keyboardHelp() + fun helpActions(): List> = arrayListOf() + + fun onHelpActionSelect(action: String) fun sendText(text: String) diff --git a/app/src/main/res/layout/dialog_multiple_edit_text.xml b/app/src/main/res/layout/dialog_multiple_edit_text.xml new file mode 100644 index 000000000..b8b8957c8 --- /dev/null +++ b/app/src/main/res/layout/dialog_multiple_edit_text.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/keyboard_assists_config.xml b/app/src/main/res/menu/keyboard_assists_config.xml new file mode 100644 index 000000000..52bf86d0d --- /dev/null +++ b/app/src/main/res/menu/keyboard_assists_config.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-es-rES/strings.xml b/app/src/main/res/values-es-rES/strings.xml index 0b0f18d1f..d28a2ae20 100644 --- a/app/src/main/res/values-es-rES/strings.xml +++ b/app/src/main/res/values-es-rES/strings.xml @@ -955,5 +955,6 @@ 显示图标 显示默认书籍图标 缓存/导出 + 辅助按键配置 diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index 00a7c5267..bb37d654e 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -958,5 +958,6 @@ 显示图标 显示默认书籍图标 缓存/导出 + 辅助按键配置 diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 1df5c61fa..2c7c7dc97 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -958,5 +958,6 @@ 显示图标 显示默认书籍图标 缓存/导出 + 辅助按键配置 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 421f66332..c9035c69c 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -955,5 +955,6 @@ 显示图标 显示默认书籍图标 缓存/导出 + 辅助按键配置 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 19d3cc2ab..71711a07c 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -957,5 +957,6 @@ 显示图标 显示默认书籍图标 缓存/导出 + 辅助按键配置 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 761f90dbb..4360d7aba 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -957,5 +957,6 @@ 显示图标 显示默认书籍图标 缓存/导出 + 辅助按键配置 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ed3b63383..6b89b531d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -958,5 +958,6 @@ 显示图标 显示默认书籍图标 缓存/导出 + 辅助按键配置