diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt index ef0863907..55259b746 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt @@ -23,6 +23,10 @@ class FileAssociationActivity : binding.rotateLoading.hide() ImportBookSourceDialog.start(supportFragmentManager, it) } + viewModel.importRssSourceLive.observe(this) { + binding.rotateLoading.hide() + ImportRssSourceDialog.start(supportFragmentManager, it) + } viewModel.importReplaceRuleLive.observe(this) { binding.rotateLoading.hide() ImportReplaceRuleDialog.start(supportFragmentManager, it) diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt index 55c1a1ace..26e099e52 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt @@ -16,6 +16,7 @@ import java.io.File class FileAssociationViewModel(application: Application) : BaseViewModel(application) { val importBookSourceLive = MutableLiveData() + val importRssSourceLive = MutableLiveData() val importReplaceRuleLive = MutableLiveData() val successLiveData = MutableLiveData() val errorLiveData = MutableLiveData() @@ -39,15 +40,7 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica return@execute } content.contains("sourceUrl") -> { - successLiveData.postValue( - Intent( - context, - ImportRssSourceActivity::class.java - ).apply { - val dataKey = IntentDataHelp.putData(content) - putExtra("dataKey", dataKey) - } - ) + importRssSourceLive.postValue(IntentDataHelp.putData(content)) return@execute } content.contains("pattern") -> { diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt index 67cf3b348..a874d4259 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt @@ -9,7 +9,8 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar -import androidx.fragment.app.activityViewModels +import androidx.fragment.app.FragmentManager +import androidx.fragment.app.viewModels import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -23,21 +24,29 @@ import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.databinding.DialogRecyclerViewBinding import io.legado.app.databinding.ItemSourceImportBinding import io.legado.app.help.AppConfig +import io.legado.app.help.IntentDataHelp import io.legado.app.lib.dialogs.alert import io.legado.app.ui.widget.dialog.WaitDialog -import io.legado.app.utils.dp -import io.legado.app.utils.putPrefBoolean -import io.legado.app.utils.splitNotBlank +import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding -import io.legado.app.utils.visible /** * 导入rss源弹出窗口 */ class ImportRssSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { + companion object { + fun start(fragmentManager: FragmentManager, source: String) { + ImportRssSourceDialog().apply { + arguments = Bundle().apply { + putString("source", source) + } + }.show(fragmentManager, "importRssSource") + } + } + private val binding by viewBinding(DialogRecyclerViewBinding::bind) - private val viewModel by activityViewModels() + private val viewModel by viewModels() lateinit var adapter: SourcesAdapter override fun onStart() { @@ -58,6 +67,7 @@ class ImportRssSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListe override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { binding.toolBar.setTitle(R.string.import_rss_source) + binding.rotateLoading.show() initMenu() adapter = SourcesAdapter(requireContext()) binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) @@ -88,6 +98,37 @@ class ImportRssSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListe adapter.notifyDataSetChanged() upSelectText() } + viewModel.errorLiveData.observe(this, { + binding.rotateLoading.hide() + binding.tvMsg.apply { + text = it + visible() + } + }) + viewModel.successLiveData.observe(this, { + binding.rotateLoading.hide() + if (it > 0) { + adapter.setItems(viewModel.allSources) + } else { + binding.tvMsg.apply { + setText(R.string.wrong_format) + visible() + } + } + }) + val source = arguments?.getString("source") + if (source.isNullOrEmpty()) { + dismiss() + return + } + if (source.isAbsUrl()) { + viewModel.importSource(source) + } else { + IntentDataHelp.getData(source)?.let { + viewModel.importSource(it) + return + } + } } private fun upSelectText() { 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 03455a4ed..234b2915a 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 @@ -2,7 +2,6 @@ package io.legado.app.ui.association import android.os.Bundle import androidx.activity.viewModels -import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Theme import io.legado.app.databinding.ActivityTranslucenceBinding @@ -29,7 +28,7 @@ class OnLineImportActivity : } when (it.path) { "/bookSource" -> ImportBookSourceDialog.start(supportFragmentManager, url) - "/rssSource" -> importRssSource(url) + "/rssSource" -> ImportRssSourceDialog.start(supportFragmentManager, url) "/replaceRule" -> ImportReplaceRuleDialog.start(supportFragmentManager, url) "/textTocRule" -> viewModel.importTextTocRule(url, this::finallyDialog) "/httpTTS" -> viewModel.importHttpTTS(url, this::finallyDialog) @@ -37,7 +36,7 @@ class OnLineImportActivity : "/readConfig" -> viewModel.importReadConfig(url, this::finallyDialog) "/importonline" -> when (it.host) { "booksource" -> ImportBookSourceDialog.start(supportFragmentManager, url) - "rsssource" -> importRssSource(url) + "rsssource" -> ImportRssSourceDialog.start(supportFragmentManager, url) "replace" -> ImportReplaceRuleDialog.start(supportFragmentManager, url) else -> { toastOnUi("url error") @@ -48,42 +47,6 @@ class OnLineImportActivity : } } - private fun importRssSource(url: String) { - val viewModel by viewModels() - binding.rotateLoading.show() - viewModel.errorLiveData.observe(this, { - binding.rotateLoading.hide() - finallyDialog(getString(R.string.error), it) - }) - viewModel.successLiveData.observe(this, { - binding.rotateLoading.hide() - if (it > 0) { - ImportRssSourceDialog().show(supportFragmentManager, "rssSource") - } else { - finallyDialog(getString(R.string.error), getString(R.string.wrong_format)) - } - }) - viewModel.importSource(url) - } - - private fun importReplaceRule(url: String) { - val viewModel by viewModels() - binding.rotateLoading.show() - viewModel.errorLiveData.observe(this, { - binding.rotateLoading.hide() - finallyDialog(getString(R.string.error), it) - }) - viewModel.successLiveData.observe(this, { - binding.rotateLoading.hide() - if (it > 0) { - ImportReplaceRuleDialog().show(supportFragmentManager, "replaceRule") - } else { - finallyDialog(getString(R.string.error), getString(R.string.wrong_format)) - } - }) - viewModel.import(url) - } - private fun finallyDialog(title: String, msg: String) { alert(title, msg) { okButton() diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 889acc8c8..257e0a5ac 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -23,7 +23,7 @@ import io.legado.app.help.IntentDataHelp import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor -import io.legado.app.ui.association.ImportRssSourceActivity +import io.legado.app.ui.association.ImportRssSourceDialog import io.legado.app.ui.document.FilePicker import io.legado.app.ui.document.FilePickerParam import io.legado.app.ui.qrcode.QrCodeResult @@ -57,17 +57,13 @@ class RssSourceActivity : VMBaseActivity { - putExtra("source", it) - } + ImportRssSourceDialog.start(supportFragmentManager, it) } private val importDoc = registerForActivityResult(FilePicker()) { uri -> kotlin.runCatching { uri?.readText(this)?.let { val dataKey = IntentDataHelp.putData(it) - startActivity { - putExtra("dataKey", dataKey) - } + ImportRssSourceDialog.start(supportFragmentManager, dataKey) } }.onFailure { toastOnUi("readTextError:${it.localizedMessage}") @@ -284,9 +280,7 @@ class RssSourceActivity : VMBaseActivity { - putExtra("source", it) - } + ImportRssSourceDialog.start(supportFragmentManager, it) } } cancelButton() diff --git a/app/src/main/java/io/legado/app/ui/rss/subscription/RuleSubActivity.kt b/app/src/main/java/io/legado/app/ui/rss/subscription/RuleSubActivity.kt index 8c1fcc0e3..de0f10c39 100644 --- a/app/src/main/java/io/legado/app/ui/rss/subscription/RuleSubActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/subscription/RuleSubActivity.kt @@ -15,9 +15,8 @@ import io.legado.app.databinding.DialogRuleSubEditBinding import io.legado.app.lib.dialogs.alert import io.legado.app.ui.association.ImportBookSourceDialog import io.legado.app.ui.association.ImportReplaceRuleDialog -import io.legado.app.ui.association.ImportRssSourceActivity +import io.legado.app.ui.association.ImportRssSourceDialog import io.legado.app.ui.widget.recycler.ItemTouchCallback -import io.legado.app.utils.startActivity import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO @@ -77,9 +76,7 @@ class RuleSubActivity : BaseActivity(), ImportBookSourceDialog.start(supportFragmentManager, ruleSub.url) } 1 -> { - startActivity { - putExtra("source", ruleSub.url) - } + ImportRssSourceDialog.start(supportFragmentManager, ruleSub.url) } 2 -> { ImportReplaceRuleDialog.start(supportFragmentManager, ruleSub.url)