添加在线导入txt小说规则

pull/1155/head
gedoor 3 years ago
parent 82b5bd0685
commit c09a842fe1
  1. 25
      app/src/main/java/io/legado/app/ui/association/OnLineImportActivity.kt
  2. 29
      app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt

@ -3,15 +3,17 @@ package io.legado.app.ui.association
import android.os.Bundle import android.os.Bundle
import androidx.activity.viewModels import androidx.activity.viewModels
import io.legado.app.R 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.constant.Theme
import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.databinding.ActivityTranslucenceBinding
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = Theme.Transparent) { class OnLineImportActivity :
VMBaseActivity<ActivityTranslucenceBinding, OnLineImportViewModel>(theme = Theme.Transparent) {
override val binding by viewBinding(ActivityTranslucenceBinding::inflate) override val binding by viewBinding(ActivityTranslucenceBinding::inflate)
override val viewModel by viewModels<OnLineImportViewModel>()
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
intent.data?.let { intent.data?.let {
@ -24,6 +26,7 @@ class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = T
"bookSource" -> importBookSource(url) "bookSource" -> importBookSource(url)
"rssSource" -> importRssSource(url) "rssSource" -> importRssSource(url)
"replaceRule" -> importReplaceRule(url) "replaceRule" -> importReplaceRule(url)
"textTocRule" -> viewModel.importTextTocRule(url, this::finallyDialog)
else -> { else -> {
} }
} }
@ -35,14 +38,14 @@ class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = T
binding.rotateLoading.show() binding.rotateLoading.show()
viewModel.errorLiveData.observe(this, { viewModel.errorLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
errorDialog(it) finallyDialog(getString(R.string.error), it)
}) })
viewModel.successLiveData.observe(this, { viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
if (it > 0) { if (it > 0) {
ImportBookSourceDialog().show(supportFragmentManager, "bookSource") ImportBookSourceDialog().show(supportFragmentManager, "bookSource")
} else { } else {
errorDialog(getString(R.string.wrong_format)) finallyDialog(getString(R.string.error), getString(R.string.wrong_format))
} }
}) })
viewModel.importSource(url) viewModel.importSource(url)
@ -53,14 +56,14 @@ class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = T
binding.rotateLoading.show() binding.rotateLoading.show()
viewModel.errorLiveData.observe(this, { viewModel.errorLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
errorDialog(it) finallyDialog(getString(R.string.error), it)
}) })
viewModel.successLiveData.observe(this, { viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
if (it > 0) { if (it > 0) {
ImportRssSourceDialog().show(supportFragmentManager, "rssSource") ImportRssSourceDialog().show(supportFragmentManager, "rssSource")
} else { } else {
errorDialog(getString(R.string.wrong_format)) finallyDialog(getString(R.string.error), getString(R.string.wrong_format))
} }
}) })
viewModel.importSource(url) viewModel.importSource(url)
@ -71,22 +74,22 @@ class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = T
binding.rotateLoading.show() binding.rotateLoading.show()
viewModel.errorLiveData.observe(this, { viewModel.errorLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
errorDialog(it) finallyDialog(getString(R.string.error), it)
}) })
viewModel.successLiveData.observe(this, { viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
if (it > 0) { if (it > 0) {
ImportReplaceRuleDialog().show(supportFragmentManager, "replaceRule") ImportReplaceRuleDialog().show(supportFragmentManager, "replaceRule")
} else { } else {
errorDialog(getString(R.string.wrong_format)) finallyDialog(getString(R.string.error), getString(R.string.wrong_format))
} }
}) })
viewModel.import(url) viewModel.import(url)
} }
private fun errorDialog(msg: String) { private fun finallyDialog(title: String, msg: String) {
alert(getString(R.string.error), msg) { alert(title, msg) {
okButton { } okButton()
onDismiss { onDismiss {
finish() finish()
} }

@ -1,6 +1,33 @@
package io.legado.app.ui.association package io.legado.app.ui.association
import io.legado.app.App import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseViewModel 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) 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<TxtTocRule>(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 ?: "未知错误")
}
}
}
Loading…
Cancel
Save