From 1c10b4e7fe9a1d3dfa8e970eb62351b44a8150e2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 24 Jul 2021 12:38:53 +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 --- app/src/main/AndroidManifest.xml | 5 -- .../ui/association/FileAssociationActivity.kt | 4 ++ .../association/FileAssociationViewModel.kt | 12 +--- .../association/ImportBookSourceActivity.kt | 62 ------------------ .../ui/association/ImportBookSourceDialog.kt | 64 +++++++++++++++---- .../book/source/manage/BookSourceActivity.kt | 14 ++-- .../ui/rss/subscription/RuleSubActivity.kt | 6 +- .../main/res/layout/dialog_recycler_view.xml | 9 +++ 8 files changed, 72 insertions(+), 104 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 522dd2c43..4960d2873 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -335,11 +335,6 @@ - - () val successLiveData = MutableLiveData() val errorLiveData = MutableLiveData() @@ -34,15 +34,7 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica //暂时根据文件内容判断属于什么 when { content.contains("bookSourceUrl") -> { - successLiveData.postValue( - Intent( - context, - ImportBookSourceActivity::class.java - ).apply { - val dataKey = IntentDataHelp.putData(content) - putExtra("dataKey", dataKey) - } - ) + importBookSourceLive.postValue(IntentDataHelp.putData(content)) return@execute } content.contains("sourceUrl") -> { diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt deleted file mode 100644 index f2c09bd88..000000000 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt +++ /dev/null @@ -1,62 +0,0 @@ -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 -import io.legado.app.help.IntentDataHelp -import io.legado.app.lib.dialogs.alert -import io.legado.app.utils.viewbindingdelegate.viewBinding - - -class ImportBookSourceActivity : - VMBaseActivity( - theme = Theme.Transparent - ) { - - override val binding by viewBinding(ActivityTranslucenceBinding::inflate) - override val viewModel by viewModels() - - override fun onActivityCreated(savedInstanceState: Bundle?) { - binding.rotateLoading.show() - viewModel.errorLiveData.observe(this, { - binding.rotateLoading.hide() - errorDialog(it) - }) - viewModel.successLiveData.observe(this, { - binding.rotateLoading.hide() - if (it > 0) { - ImportBookSourceDialog().show(supportFragmentManager, "SourceDialog") - } else { - errorDialog(getString(R.string.wrong_format)) - } - }) - initData() - } - - private fun initData() { - intent.getStringExtra("dataKey")?.let { - IntentDataHelp.getData(it)?.let { source -> - viewModel.importSource(source) - return - } - } - intent.getStringExtra("source")?.let { - viewModel.importSource(it) - return - } - finish() - } - - private fun errorDialog(msg: String) { - alert(getString(R.string.error), msg) { - okButton { } - onDismiss { - finish() - } - }.show() - } - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt index 038df02d5..0db0970dc 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt @@ -2,14 +2,14 @@ package io.legado.app.ui.association import android.annotation.SuppressLint import android.content.Context -import android.content.DialogInterface import android.os.Bundle import android.view.LayoutInflater 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,13 +23,11 @@ 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 /** @@ -37,10 +35,21 @@ import io.legado.app.utils.visible */ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { - private val binding by viewBinding(DialogRecyclerViewBinding::bind) + companion object { + + fun start(fragmentManager: FragmentManager, source: String) { + ImportBookSourceDialog().apply { + arguments = Bundle().apply { + putString("source", source) + } + }.show(fragmentManager, "importBookSource") + } - val viewModel: ImportBookSourceViewModel by activityViewModels() - lateinit var adapter: SourcesAdapter + } + + private val binding by viewBinding(DialogRecyclerViewBinding::bind) + private val viewModel by viewModels() + private lateinit var adapter: SourcesAdapter override fun onStart() { super.onStart() @@ -60,11 +69,11 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { binding.toolBar.setTitle(R.string.import_book_source) + binding.rotateLoading.show() initMenu() adapter = SourcesAdapter(requireContext()) binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) binding.recyclerView.adapter = adapter - adapter.setItems(viewModel.allSources) binding.tvCancel.visible() binding.tvCancel.setOnClickListener { dismissAllowingStateLoss() @@ -90,6 +99,31 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList adapter.notifyDataSetChanged() upSelectText() } + viewModel.errorLiveData.observe(this, { + binding.rotateLoading.hide() + errorDialog(it) + }) + viewModel.successLiveData.observe(this, { + binding.rotateLoading.hide() + if (it > 0) { + adapter.setItems(viewModel.allSources) + } else { + errorDialog(getString(R.string.wrong_format)) + } + }) + 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() { @@ -148,9 +182,13 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList return false } - override fun onDismiss(dialog: DialogInterface) { - super.onDismiss(dialog) - activity?.finish() + private fun errorDialog(msg: String) { + alert(getString(R.string.error), msg) { + okButton { } + onDismiss { + + } + }.show() } inner class SourcesAdapter(context: Context) : diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 1b36fed1c..f59a8ded5 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -27,7 +27,7 @@ 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.service.help.CheckSource -import io.legado.app.ui.association.ImportBookSourceActivity +import io.legado.app.ui.association.ImportBookSourceDialog import io.legado.app.ui.book.source.debug.BookSourceDebugActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.document.FilePicker @@ -63,18 +63,14 @@ class BookSourceActivity : VMBaseActivity { - putExtra("source", it) - } + ImportBookSourceDialog.start(supportFragmentManager, it) } private val importDoc = registerForActivityResult(FilePicker()) { uri -> uri ?: return@registerForActivityResult try { uri.readText(this)?.let { val dataKey = IntentDataHelp.putData(it) - startActivity { - putExtra("dataKey", dataKey) - } + ImportBookSourceDialog.start(supportFragmentManager, dataKey) } } catch (e: Exception) { toastOnUi("readTextError:${e.localizedMessage}") @@ -421,9 +417,7 @@ class BookSourceActivity : VMBaseActivity { - putExtra("source", it) - } + ImportBookSourceDialog.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 a54fef5ed..f7c2ed3b3 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 @@ -13,7 +13,7 @@ import io.legado.app.data.entities.RuleSub import io.legado.app.databinding.ActivityRuleSubBinding import io.legado.app.databinding.DialogRuleSubEditBinding import io.legado.app.lib.dialogs.alert -import io.legado.app.ui.association.ImportBookSourceActivity +import io.legado.app.ui.association.ImportBookSourceDialog import io.legado.app.ui.association.ImportReplaceRuleActivity import io.legado.app.ui.association.ImportRssSourceActivity import io.legado.app.ui.widget.recycler.ItemTouchCallback @@ -74,9 +74,7 @@ class RuleSubActivity : BaseActivity(), override fun openSubscription(ruleSub: RuleSub) { when (ruleSub.type) { 0 -> { - startActivity { - putExtra("source", ruleSub.url) - } + ImportBookSourceDialog.start(supportFragmentManager, ruleSub.url) } 1 -> { startActivity { diff --git a/app/src/main/res/layout/dialog_recycler_view.xml b/app/src/main/res/layout/dialog_recycler_view.xml index f368f646d..2afcbca96 100644 --- a/app/src/main/res/layout/dialog_recycler_view.xml +++ b/app/src/main/res/layout/dialog_recycler_view.xml @@ -25,6 +25,15 @@ android:layout_height="match_parent" android:scrollbars="none" /> + +