From c09a842fe1f10e0838a697f0625cc0d175a22e9a Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 22 Jul 2021 19:52:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9C=A8=E7=BA=BF=E5=AF=BC?= =?UTF-8?q?=E5=85=A5txt=E5=B0=8F=E8=AF=B4=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/association/OnLineImportActivity.kt | 25 +++++++++------- .../ui/association/OnLineImportViewModel.kt | 29 ++++++++++++++++++- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/association/OnLineImportActivity.kt b/app/src/main/java/io/legado/app/ui/association/OnLineImportActivity.kt index 45cc57de7..7e44b1251 100644 --- a/app/src/main/java/io/legado/app/ui/association/OnLineImportActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/OnLineImportActivity.kt @@ -3,15 +3,17 @@ package io.legado.app.ui.association import android.os.Bundle import androidx.activity.viewModels import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Theme import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.lib.dialogs.alert import io.legado.app.utils.viewbindingdelegate.viewBinding -class OnLineImportActivity : BaseActivity(theme = Theme.Transparent) { +class OnLineImportActivity : + VMBaseActivity(theme = Theme.Transparent) { override val binding by viewBinding(ActivityTranslucenceBinding::inflate) + override val viewModel by viewModels() override fun onActivityCreated(savedInstanceState: Bundle?) { intent.data?.let { @@ -24,6 +26,7 @@ class OnLineImportActivity : BaseActivity(theme = T "bookSource" -> importBookSource(url) "rssSource" -> importRssSource(url) "replaceRule" -> importReplaceRule(url) + "textTocRule" -> viewModel.importTextTocRule(url, this::finallyDialog) else -> { } } @@ -35,14 +38,14 @@ class OnLineImportActivity : BaseActivity(theme = T binding.rotateLoading.show() viewModel.errorLiveData.observe(this, { binding.rotateLoading.hide() - errorDialog(it) + finallyDialog(getString(R.string.error), it) }) viewModel.successLiveData.observe(this, { binding.rotateLoading.hide() if (it > 0) { ImportBookSourceDialog().show(supportFragmentManager, "bookSource") } else { - errorDialog(getString(R.string.wrong_format)) + finallyDialog(getString(R.string.error), getString(R.string.wrong_format)) } }) viewModel.importSource(url) @@ -53,14 +56,14 @@ class OnLineImportActivity : BaseActivity(theme = T binding.rotateLoading.show() viewModel.errorLiveData.observe(this, { binding.rotateLoading.hide() - errorDialog(it) + finallyDialog(getString(R.string.error), it) }) viewModel.successLiveData.observe(this, { binding.rotateLoading.hide() if (it > 0) { ImportRssSourceDialog().show(supportFragmentManager, "rssSource") } else { - errorDialog(getString(R.string.wrong_format)) + finallyDialog(getString(R.string.error), getString(R.string.wrong_format)) } }) viewModel.importSource(url) @@ -71,22 +74,22 @@ class OnLineImportActivity : BaseActivity(theme = T binding.rotateLoading.show() viewModel.errorLiveData.observe(this, { binding.rotateLoading.hide() - errorDialog(it) + finallyDialog(getString(R.string.error), it) }) viewModel.successLiveData.observe(this, { binding.rotateLoading.hide() if (it > 0) { ImportReplaceRuleDialog().show(supportFragmentManager, "replaceRule") } else { - errorDialog(getString(R.string.wrong_format)) + finallyDialog(getString(R.string.error), getString(R.string.wrong_format)) } }) viewModel.import(url) } - private fun errorDialog(msg: String) { - alert(getString(R.string.error), msg) { - okButton { } + private fun finallyDialog(title: String, msg: String) { + alert(title, msg) { + okButton() onDismiss { finish() } diff --git a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt index 8dfba367e..c3eab090d 100644 --- a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt @@ -1,6 +1,33 @@ package io.legado.app.ui.association import io.legado.app.App +import io.legado.app.R import io.legado.app.base.BaseViewModel +import io.legado.app.data.appDb +import io.legado.app.data.entities.TxtTocRule +import io.legado.app.help.http.newCall +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 OnLineImportViewModel(app: App) : BaseViewModel(app) \ No newline at end of file +class OnLineImportViewModel(app: App) : BaseViewModel(app) { + + fun importTextTocRule(url: String, finally: (title: String, msg: String) -> Unit) { + execute { + okHttpClient.newCall { + url(url) + }.text("utf-8").let { json -> + GSON.fromJsonArray(json)?.let { + appDb.txtTocRuleDao.insert(*it.toTypedArray()) + } + } + }.onSuccess { + finally.invoke(context.getString(R.string.success), "导入Txt规则成功") + }.onError { + finally.invoke(context.getString(R.string.error), it.localizedMessage ?: "未知错误") + } + } + + +} \ No newline at end of file