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