From 3cd61799b59b58b9a343ac1018be7f47a0ae24b8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 10 Mar 2022 12:12:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/AppConst.kt | 8 ------- .../legado/app/data/dao/KeyboardAssistsDao.kt | 9 ++++++++ .../source/edit/BookSourceEditActivity.kt | 2 +- .../ui/replace/edit/ReplaceEditActivity.kt | 2 +- .../rss/source/edit/RssSourceEditActivity.kt | 2 +- .../legado/app/ui/widget/KeyboardToolPop.kt | 23 ++++++++++++++----- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index fb4ffc6da..87461bb76 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -39,14 +39,6 @@ object AppConst { SimpleDateFormat("yy-MM-dd-HH-mm-ss") } - val keyboardToolChars: List by lazy { - arrayListOf( - "@css:", "", "{{}}", "##", "&&", "%%", "||", "//", "\\", "$.", - "@", ":", "class", "text", "href", "textNodes", "ownText", "all", "html", - "[", "]", "<", ">", "#", "!", ".", "+", "-", "*", "=", "{'webView': true}" - ) - } - const val bookGroupAllId = -1L const val bookGroupLocalId = -2L const val bookGroupAudioId = -3L 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 3d566766c..b6b3406d8 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 @@ -2,6 +2,7 @@ package io.legado.app.data.dao import androidx.room.* import io.legado.app.data.entities.KeyboardAssist +import io.legado.app.help.DefaultData @Dao interface KeyboardAssistsDao { @@ -20,4 +21,12 @@ interface KeyboardAssistsDao { @Delete fun delete(vararg keyboardAssist: KeyboardAssist) + + fun getOrDefault(): List { + return all.ifEmpty { + insert(*DefaultData.keyboardAssists.toTypedArray()) + DefaultData.keyboardAssists + } + } + } \ No newline at end of file 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 1bfd9df64..1759aa04a 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 @@ -66,7 +66,7 @@ class BookSourceEditActivity : } private val mSoftKeyboardTool: PopupWindow by lazy { - KeyboardToolPop(this, this) + KeyboardToolPop(this, this, this) } private var mIsSoftKeyBoardShowing = false 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 4b539edce..58eb714ed 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 @@ -55,7 +55,7 @@ class ReplaceEditActivity : override val viewModel by viewModels() private val mSoftKeyboardTool: PopupWindow by lazy { - KeyboardToolPop(this, this) + KeyboardToolPop(this, this, this) } private var mIsSoftKeyBoardShowing = false 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 c635e6e9b..2fbfc19ff 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 @@ -37,7 +37,7 @@ class RssSourceEditActivity : override val binding by viewBinding(ActivityRssSourceEditBinding::inflate) override val viewModel by viewModels() private val mSoftKeyboardTool: PopupWindow by lazy { - KeyboardToolPop(this, this) + KeyboardToolPop(this, this, this) } private var mIsSoftKeyBoardShowing = false private val adapter by lazy { RssSourceEditAdapter() } diff --git a/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt b/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt index 0d7091e73..acd3ab9a4 100644 --- a/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt +++ b/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt @@ -8,9 +8,14 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter -import io.legado.app.constant.AppConst +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 kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import splitties.systemservices.layoutInflater /** @@ -18,6 +23,7 @@ import splitties.systemservices.layoutInflater */ class KeyboardToolPop( context: Context, + private val scope: CoroutineScope, private val callBack: CallBack ) : PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) { @@ -48,11 +54,16 @@ class KeyboardToolPop( } } } - adapter.setItems(AppConst.charsets) + scope.launch { + val items = withContext(IO) { + appDb.keyboardAssistsDao.getOrDefault() + } + adapter.setItems(items) + } } inner class Adapter(context: Context) : - RecyclerAdapter(context) { + RecyclerAdapter(context) { override fun getViewBinding(parent: ViewGroup): ItemFilletTextBinding { return ItemFilletTextBinding.inflate(inflater, parent, false) @@ -61,11 +72,11 @@ class KeyboardToolPop( override fun convert( holder: ItemViewHolder, binding: ItemFilletTextBinding, - item: String, + item: KeyboardAssist, payloads: MutableList ) { binding.run { - textView.text = item + textView.text = item.key } } @@ -73,7 +84,7 @@ class KeyboardToolPop( holder.itemView.apply { setOnClickListener { getItemByLayoutPosition(holder.layoutPosition)?.let { - callBack.sendText(it) + callBack.sendText(it.value) } } }