From d8abfbe3644e87e3ea875ee85822240adcbb99ea Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 5 Mar 2020 21:16:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/TxtTocRuleDao.kt | 3 + .../app/ui/book/read/config/TocRegexDialog.kt | 71 +++++++++++++------ .../ui/book/read/config/TocRegexViewModel.kt | 39 ++++++++++ .../book/source/manage/BookSourceActivity.kt | 3 +- .../ui/widget/text/AutoCompleteTextView.kt | 6 +- app/src/main/res/menu/txt_toc_regex.xml | 5 ++ app/src/main/res/values/strings.xml | 2 +- 7 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt diff --git a/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt index 5812b419c..1bd502a09 100644 --- a/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt @@ -16,6 +16,9 @@ interface TxtTocRuleDao { @get:Query("select * from txtTocRules where enable = 1 order by serialNumber") val enabled: List + @get:Query("select ifNull(max(serialNumber), 0) from txtTocRules") + val lastOrderNum: Int + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg rule: TxtTocRule) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt index af5d1ed65..7e9afbf83 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt @@ -15,6 +15,7 @@ import androidx.lifecycle.Observer import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.snackbar.Snackbar import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -27,9 +28,10 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.cancelButton import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.okButton -import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.ui.widget.recycler.VerticalDivider -import io.legado.app.utils.applyTint +import io.legado.app.ui.widget.text.AutoCompleteTextView +import io.legado.app.utils.* +import kotlinx.android.synthetic.main.dialog_edit_text.* import kotlinx.android.synthetic.main.dialog_toc_regex.* import kotlinx.android.synthetic.main.dialog_toc_regex_edit.view.* import kotlinx.android.synthetic.main.item_toc_regex.view.* @@ -40,11 +42,12 @@ import java.util.* class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { - + private val importTocRuleKey = "tocRuleUrl" private lateinit var adapter: TocRegexAdapter private var tocRegexLiveData: LiveData>? = null var selectedName: String? = null private var durRegex: String? = null + lateinit var viewModel: TocRegexViewModel override fun onStart() { super.onStart() @@ -58,6 +61,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { container: ViewGroup?, savedInstanceState: Bundle? ): View? { + viewModel = getViewModel(TocRegexViewModel::class.java) return inflater.inflate(R.layout.dialog_toc_regex, container) } @@ -121,17 +125,51 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { override fun onMenuItemClick(item: MenuItem?): Boolean { when (item?.itemId) { R.id.menu_add -> editRule() - R.id.menu_default -> importDefault() + R.id.menu_default -> viewModel.importDefault() + R.id.menu_import -> showImportDialog() } return false } - private fun importDefault() { - launch(IO) { - AnalyzeTxtFile.getDefaultRules().let { - App.db.txtTocRule().insert(*it.toTypedArray()) - } + @SuppressLint("InflateParams") + private fun showImportDialog() { + val aCache = ACache.get(requireContext(), cacheDir = false) + val defaultUrl = "https://gitee.com/fisher52/YueDuJson/raw/master/myTxtChapterRule.json" + val cacheUrls: MutableList = aCache + .getAsString(importTocRuleKey) + ?.splitNotBlank(",") + ?.toMutableList() + ?: mutableListOf() + if (cacheUrls.contains(defaultUrl)) { + cacheUrls.add(0, defaultUrl) } + requireContext().alert(titleResource = R.string.import_book_source_on_line) { + var editText: AutoCompleteTextView? = null + customView { + layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { + editText = edit_view + edit_view.setFilterValues(cacheUrls) + edit_view.delCallBack = { + cacheUrls.remove(it) + aCache.put(importTocRuleKey, cacheUrls.joinToString(",")) + } + } + } + okButton { + val text = editText?.text?.toString() + text?.let { + if (!cacheUrls.contains(it)) { + cacheUrls.add(0, it) + aCache.put(importTocRuleKey, cacheUrls.joinToString(",")) + } + Snackbar.make(tool_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() + viewModel.importOnLine(it) { msg -> + tool_bar.snackbar(msg) + } + } + } + cancelButton() + }.show().applyTint() } @SuppressLint("InflateParams") @@ -151,25 +189,13 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { rootView?.apply { tocRule.name = tv_rule_name.text.toString() tocRule.rule = tv_rule_regex.text.toString() - saveRule(tocRule, rule) + viewModel.saveRule(tocRule, rule) } } cancelButton() }.show().applyTint() } - private fun saveRule(rule: TxtTocRule, oldRule: TxtTocRule? = null) { - launch(IO) { - if (rule.serialNumber < 0) { - rule.serialNumber = adapter.getItems().lastOrNull()?.serialNumber ?: 0 + 1 - } - oldRule?.let { - App.db.txtTocRule().delete(oldRule) - } - App.db.txtTocRule().insert(rule) - } - } - inner class TocRegexAdapter(context: Context) : SimpleRecyclerAdapter(context, R.layout.item_toc_regex), ItemTouchCallback.OnItemTouchCallbackListener { @@ -240,7 +266,6 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { } } - companion object { fun show(fragmentManager: FragmentManager, tocRegex: String? = null) { val dialog = TocRegexDialog() diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt new file mode 100644 index 000000000..afecf211d --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt @@ -0,0 +1,39 @@ +package io.legado.app.ui.book.read.config + +import android.app.Application +import io.legado.app.App +import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.TxtTocRule +import io.legado.app.model.localBook.AnalyzeTxtFile + +class TocRegexViewModel(application: Application) : BaseViewModel(application) { + + fun saveRule(rule: TxtTocRule, oldRule: TxtTocRule? = null) { + execute { + if (rule.serialNumber < 0) { + rule.serialNumber = App.db.txtTocRule().lastOrderNum + 1 + } + oldRule?.let { + App.db.txtTocRule().delete(oldRule) + } + App.db.txtTocRule().insert(rule) + } + } + + fun importDefault() { + execute { + AnalyzeTxtFile.getDefaultRules().let { + App.db.txtTocRule().insert(*it.toTypedArray()) + } + } + } + + fun importOnLine(url: String, finally: (msg: String) -> Unit) { + execute { + + }.onFinally { + + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index c05294965..31b1c2028 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -214,7 +214,8 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity customView { layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { editText = edit_view - edit_view.setFilterValues(cacheUrls) { + edit_view.setFilterValues(cacheUrls) + edit_view.delCallBack = { cacheUrls.remove(it) aCache.put(importRecordKey, cacheUrls.joinToString(",")) } diff --git a/app/src/main/java/io/legado/app/ui/widget/text/AutoCompleteTextView.kt b/app/src/main/java/io/legado/app/ui/widget/text/AutoCompleteTextView.kt index ac0a5a8ac..1319aac5e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/text/AutoCompleteTextView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/text/AutoCompleteTextView.kt @@ -42,15 +42,13 @@ class AutoCompleteTextView : AppCompatAutoCompleteTextView { return super.onTouchEvent(event) } - fun setFilterValues(values: List?, delCallBack: ((value: String) -> Unit)? = null) { - this.delCallBack = delCallBack + fun setFilterValues(values: List?) { values?.let { setAdapter(MyAdapter(context, values)) } } - fun setFilterValues(vararg value: String, delCallBack: ((value: String) -> Unit)? = null) { - this.delCallBack = delCallBack + fun setFilterValues(vararg value: String) { setAdapter(MyAdapter(context, value.toMutableList())) } diff --git a/app/src/main/res/menu/txt_toc_regex.xml b/app/src/main/res/menu/txt_toc_regex.xml index 38a50e8cc..2eb4a2b4c 100644 --- a/app/src/main/res/menu/txt_toc_regex.xml +++ b/app/src/main/res/menu/txt_toc_regex.xml @@ -15,4 +15,9 @@ android:title="@string/import_default_rule" app:showAsAction="never" /> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 68f3af591..74aa0d74d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -226,7 +226,7 @@ 打开外部书籍 来源:%s 本地导入 - 网络导入 + 导入在线规则 检查更新间隔 按阅读时间 按更新时间