From f617f4b6bfee500df29d8e7b775df23a5d16f3f2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 16 Nov 2021 21:10: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 --- .../ui/book/local/rule/TxtTocRuleActivity.kt | 86 ++++++++++++++++++- .../ui/book/local/rule/TxtTocRuleAdapter.kt | 11 +++ .../ui/book/local/rule/TxtTocRuleViewModel.kt | 38 +++++++- 3 files changed, 131 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt index 151753cb3..0b09d970c 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt @@ -1,10 +1,21 @@ package io.legado.app.ui.book.local.rule +import android.annotation.SuppressLint import android.os.Bundle +import android.view.Menu +import android.view.MenuItem import androidx.activity.viewModels +import com.google.android.material.snackbar.Snackbar +import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.TxtTocRule import io.legado.app.databinding.ActivityTxtTocRuleBinding +import io.legado.app.databinding.DialogEditTextBinding +import io.legado.app.databinding.DialogTocRegexEditBinding +import io.legado.app.lib.dialogs.alert +import io.legado.app.utils.ACache +import io.legado.app.utils.snackbar +import io.legado.app.utils.splitNotBlank import io.legado.app.utils.viewbindingdelegate.viewBinding class TxtTocRuleActivity : VMBaseActivity(), @@ -15,6 +26,7 @@ class TxtTocRuleActivity : VMBaseActivity edit(TxtTocRule()) + R.id.menu_default -> viewModel.importDefault() + R.id.menu_import -> showImportDialog() + } + return super.onCompatOptionsItemSelected(item) + } + override fun del(source: TxtTocRule) { viewModel.del(source) } override fun edit(source: TxtTocRule) { - + alert(titleResource = R.string.txt_toc_regex) { + val alertBinding = DialogTocRegexEditBinding.inflate(layoutInflater) + alertBinding.apply { + tvRuleName.setText(source.name) + tvRuleRegex.setText(source.rule) + } + customView { alertBinding.root } + okButton { + alertBinding.apply { + source.name = tvRuleName.text.toString() + source.rule = tvRuleRegex.text.toString() + viewModel.save(source) + } + } + cancelButton() + } } override fun update(vararg source: TxtTocRule) { - + viewModel.update(*source) } override fun toTop(source: TxtTocRule) { @@ -50,7 +91,48 @@ class TxtTocRuleActivity : VMBaseActivity = aCache + .getAsString(importTocRuleKey) + ?.splitNotBlank(",") + ?.toMutableList() + ?: mutableListOf() + if (!cacheUrls.contains(defaultUrl)) { + cacheUrls.add(0, defaultUrl) + } + alert(titleResource = R.string.import_on_line) { + val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { + editView.hint = "url" + editView.setFilterValues(cacheUrls) + editView.delCallBack = { + cacheUrls.remove(it) + aCache.put(importTocRuleKey, cacheUrls.joinToString(",")) + } + } + customView { alertBinding.root } + okButton { + val text = alertBinding.editView.text?.toString() + text?.let { + if (!cacheUrls.contains(it)) { + cacheUrls.add(0, it) + aCache.put(importTocRuleKey, cacheUrls.joinToString(",")) + } + Snackbar.make(binding.root, R.string.importing, Snackbar.LENGTH_INDEFINITE) + .show() + viewModel.importOnLine(it) { msg -> + binding.root.snackbar(msg) + } + } + } + cancelButton() + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleAdapter.kt index 5b18f3566..c222843fc 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleAdapter.kt @@ -12,6 +12,17 @@ class TxtTocRuleAdapter(context: Context, private val callback: Callback) : private val selected = linkedSetOf() + val selection: List + get() { + val selection = arrayListOf() + getItems().map { + if (selected.contains(it)) { + selection.add(it) + } + } + return selection.sortedBy { it.serialNumber } + } + override fun getViewBinding(parent: ViewGroup): ItemTxtTocRuleBinding { return ItemTxtTocRuleBinding.inflate(inflater, parent, false) } diff --git a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleViewModel.kt index b3b6f8dad..d9760a2cd 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleViewModel.kt @@ -4,18 +4,52 @@ import android.app.Application import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.TxtTocRule +import io.legado.app.help.DefaultData +import io.legado.app.help.http.newCallResponseBody +import io.legado.app.help.http.okHttpClient +import io.legado.app.help.http.text +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonArray class TxtTocRuleViewModel(app: Application) : BaseViewModel(app) { + fun save(txtTocRule: TxtTocRule) { + execute { + appDb.txtTocRuleDao.insert(txtTocRule) + } + } + fun del(txtTocRule: TxtTocRule) { execute { appDb.txtTocRuleDao.delete(txtTocRule) } } - fun update(txtTocRule: TxtTocRule) { + fun update(vararg txtTocRule: TxtTocRule) { + execute { + appDb.txtTocRuleDao.update(*txtTocRule) + } + } + + fun importDefault() { + execute { + DefaultData.importDefaultTocRules() + } + } + + fun importOnLine(url: String, finally: (msg: String) -> Unit) { execute { - appDb.txtTocRuleDao.update(txtTocRule) + okHttpClient.newCallResponseBody { + url(url) + }.text("utf-8").let { json -> + GSON.fromJsonArray(json)?.let { + appDb.txtTocRuleDao.insert(*it.toTypedArray()) + } + } + }.onSuccess { + finally("导入成功") + }.onError { + finally("导入失败") } }