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 index 5b34a04dd..570d65af4 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt @@ -206,6 +206,12 @@ class ImportBookSourceActivity : VMBaseActivity( } else { "新书源" } + + } + } + + override fun registerListener(holder: ItemViewHolder) { + holder.itemView.apply { cb_source_name.setOnCheckedChangeListener { buttonView, isChecked -> if (buttonView.isPressed) { selectStatus[holder.layoutPosition] = isChecked @@ -214,10 +220,6 @@ class ImportBookSourceActivity : VMBaseActivity( } } - override fun registerListener(holder: ItemViewHolder) { - - } - } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index 79dfad343..662d6ff70 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -19,7 +19,7 @@ import java.io.File class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { val errorLiveData = MutableLiveData() - val successLiveData = MutableLiveData>() + val successLiveData = MutableLiveData() val allSources = arrayListOf() val sourceCheckState = arrayListOf() @@ -112,7 +112,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { sourceCheckState.add(has) selectStatus.add(!has) } - successLiveData.postValue(allSources) + successLiveData.postValue(true) } } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt index 2b09dd4d9..c5d757597 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt @@ -32,7 +32,11 @@ class ImportReplaceRuleActivity : VMBaseActivity( }) viewModel.successLiveData.observe(this, Observer { rotate_loading.hide() - successDialog(it) + if (it.size > 0) { + successDialog(it) + } else { + errorDialog("格式不对") + } }) initData() } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt index b976e8cee..bbeed0b2d 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt @@ -1,19 +1,34 @@ package io.legado.app.ui.association +import android.content.Context +import android.content.DialogInterface import android.os.Bundle +import android.util.DisplayMetrics +import android.view.LayoutInflater +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import androidx.appcompat.widget.Toolbar import androidx.lifecycle.Observer +import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R +import io.legado.app.base.BaseDialogFragment import io.legado.app.base.VMBaseActivity +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.constant.Theme import io.legado.app.data.entities.RssSource import io.legado.app.help.IntentDataHelp import io.legado.app.help.SourceHelp import io.legado.app.lib.dialogs.alert -import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.okButton import io.legado.app.utils.applyTint import io.legado.app.utils.getViewModel +import io.legado.app.utils.visible import kotlinx.android.synthetic.main.activity_translucence.* +import kotlinx.android.synthetic.main.dialog_recycler_view.* +import kotlinx.android.synthetic.main.item_source_import.view.* +import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.toast class ImportRssSourceActivity : VMBaseActivity( @@ -32,7 +47,7 @@ class ImportRssSourceActivity : VMBaseActivity( }) viewModel.successLiveData.observe(this, Observer { rotate_loading.hide() - successDialog(it) + successDialog() }) initData() } @@ -78,16 +93,133 @@ class ImportRssSourceActivity : VMBaseActivity( } } - private fun successDialog(allSource: ArrayList) { - alert("解析结果", "共${allSource.size}个订阅源,是否确认导入?") { - okButton { - SourceHelp.insertRssSource(*allSource.toTypedArray()) + private fun successDialog() { + val bundle = Bundle() + val allSourceKey = IntentDataHelp.putData(viewModel.allSources, "source") + bundle.putString("allSourceKey", allSourceKey) + val checkStatusKey = IntentDataHelp.putData(viewModel.sourceCheckState, "check") + bundle.putString("checkStatusKey", checkStatusKey) + val selectStatusKey = IntentDataHelp.putData(viewModel.selectStatus, "select") + bundle.putString("selectStatusKey", selectStatusKey) + SourcesDialog().apply { + arguments = bundle + }.show(supportFragmentManager, "SourceDialog") + } + + class SourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { + + lateinit var adapter: SourcesAdapter + + override fun onStart() { + super.onStart() + val dm = DisplayMetrics() + activity?.windowManager?.defaultDisplay?.getMetrics(dm) + dialog?.window?.setLayout( + (dm.widthPixels * 0.9).toInt(), + ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.dialog_recycler_view, container) + } + + override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { + tool_bar.title = "导入书源" + initMenu() + arguments?.let { + adapter = SourcesAdapter(requireContext()) + val allSources = + IntentDataHelp.getData>(it.getString("allSourceKey")) + adapter.sourceCheckState = + IntentDataHelp.getData>(it.getString("checkStatusKey"))!! + adapter.selectStatus = + IntentDataHelp.getData>(it.getString("selectStatusKey"))!! + + recycler_view.layoutManager = LinearLayoutManager(requireContext()) + recycler_view.adapter = adapter + adapter.setItems(allSources) + tv_cancel.visible() + tv_cancel.onClick { + dismiss() + } + tv_ok.visible() + tv_ok.onClick { + importSelect() + dismiss() + } } - noButton { + } + private fun initMenu() { + tool_bar.setOnMenuItemClickListener(this) + tool_bar.inflateMenu(R.menu.import_source) + } + + override fun onMenuItemClick(item: MenuItem): Boolean { + when (item.itemId) { + R.id.menu_select_all -> { + adapter.selectStatus.forEachIndexed { index, b -> + if (!b) { + adapter.selectStatus[index] = true + } + } + adapter.notifyDataSetChanged() + } } - }.show().applyTint().setOnDismissListener { - finish() + return false + } + + override fun onDismiss(dialog: DialogInterface) { + super.onDismiss(dialog) + activity?.finish() } + + private fun importSelect() { + val selectSource = arrayListOf() + adapter.selectStatus.forEachIndexed { index, b -> + if (b) { + selectSource.add(adapter.getItem(index)!!) + } + } + SourceHelp.insertRssSource(*selectSource.toTypedArray()) + } + + } + + class SourcesAdapter(context: Context) : + SimpleRecyclerAdapter(context, R.layout.item_source_import) { + + lateinit var sourceCheckState: ArrayList + lateinit var selectStatus: ArrayList + + override fun convert(holder: ItemViewHolder, item: RssSource, payloads: MutableList) { + holder.itemView.apply { + cb_source_name.isChecked = selectStatus[holder.layoutPosition] + cb_source_name.text = item.sourceName + tv_source_state.text = if (sourceCheckState[holder.layoutPosition]) { + "已存在" + } else { + "新订阅源" + } + + } + } + + override fun registerListener(holder: ItemViewHolder) { + holder.itemView.apply { + cb_source_name.setOnCheckedChangeListener { buttonView, isChecked -> + if (buttonView.isPressed) { + selectStatus[holder.layoutPosition] = isChecked + } + } + } + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt index 98bacf4da..7a1a82338 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt @@ -5,6 +5,7 @@ import android.net.Uri import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData import com.jayway.jsonpath.JsonPath +import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.RssSource import io.legado.app.help.http.HttpHelper @@ -15,9 +16,12 @@ import java.io.File class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { val errorLiveData = MutableLiveData() - val successLiveData = MutableLiveData>() + val successLiveData = MutableLiveData() + + val allSources = arrayListOf() + val sourceCheckState = arrayListOf() + val selectStatus = arrayListOf() - private val allSources = arrayListOf() fun importSourceFromFilePath(path: String) { execute { @@ -39,7 +43,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { } } }.onSuccess { - successLiveData.postValue(allSources) + comparisonSource() } } @@ -77,7 +81,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { }.onError { errorLiveData.postValue("ImportError:${it.localizedMessage}") }.onSuccess { - successLiveData.postValue(allSources) + comparisonSource() } } @@ -93,4 +97,15 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { } } + private fun comparisonSource() { + execute { + allSources.forEach { + val has = App.db.rssSourceDao().getByKey(it.sourceUrl) != null + sourceCheckState.add(has) + selectStatus.add(!has) + } + successLiveData.postValue(true) + } + } + } \ No newline at end of file diff --git a/app/src/main/res/layout/item_source_import.xml b/app/src/main/res/layout/item_source_import.xml index c1d474f29..a4bf3fbd6 100644 --- a/app/src/main/res/layout/item_source_import.xml +++ b/app/src/main/res/layout/item_source_import.xml @@ -15,7 +15,7 @@ \ No newline at end of file