From bc1b10b44173a448c8567b9c8466659a00aee3e8 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 8 Nov 2020 15:25:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=BC=E5=85=A5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../association/ImportBookSourceActivity.kt | 179 +----------------- .../ui/association/ImportBookSourceDialog.kt | 176 +++++++++++++++++ .../ui/association/ImportRssSourceActivity.kt | 152 +-------------- .../ui/association/ImportRssSourceDialog.kt | 145 ++++++++++++++ app/src/main/res/menu/import_source.xml | 5 + 5 files changed, 328 insertions(+), 329 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt create mode 100644 app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt 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 666916c7e..c023bf189 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 @@ -1,38 +1,15 @@ 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.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.BookSource 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.customView -import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.okButton -import io.legado.app.ui.widget.text.AutoCompleteTextView import io.legado.app.utils.applyTint -import io.legado.app.utils.getSize 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_edit_text.view.* -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 @@ -103,161 +80,7 @@ class ImportBookSourceActivity : VMBaseActivity( } 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 - private var _groupName: String? = null - - override fun onStart() { - super.onStart() - val dm = requireActivity().getSize() - 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.setTitle(R.string.import_book_source) - 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() - } - } - } - - private fun initMenu() { - tool_bar.setOnMenuItemClickListener(this) - tool_bar.inflateMenu(R.menu.import_source) - } - - @SuppressLint("InflateParams") - override fun onMenuItemClick(item: MenuItem): Boolean { - when (item.itemId) { - R.id.menu_new_group -> { - alert(R.string.diy_edit_source_group) { - var editText: AutoCompleteTextView? = null - customView { - layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { - editText = edit_view - } - } - okButton { - editText?.text?.toString()?.let { group -> - _groupName = group - item.title = getString(R.string.diy_edit_source_group_title, _groupName) - } - } - noButton { } - }.show().applyTint() - } - R.id.menu_select_all -> { - adapter.selectStatus.forEachIndexed { index, b -> - if (!b) { - adapter.selectStatus[index] = true - } - } - adapter.notifyDataSetChanged() - } - R.id.menu_un_select_all -> { - adapter.selectStatus.forEachIndexed { index, b -> - if (b) { - adapter.selectStatus[index] = false - } - } - adapter.notifyDataSetChanged() - } - } - return false - } - - override fun onDismiss(dialog: DialogInterface) { - super.onDismiss(dialog) - activity?.finish() - } - - private fun importSelect() { - val selectSource = arrayListOf() - adapter.selectStatus.forEachIndexed { index, b -> - if (_groupName != null) { - adapter.getItem(index)!!.bookSourceGroup = _groupName - } - if (b) { - selectSource.add(adapter.getItem(index)!!) - } - } - SourceHelp.insertBookSource(*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: BookSource, payloads: MutableList) { - holder.itemView.apply { - cb_source_name.isChecked = selectStatus[holder.layoutPosition] - cb_source_name.text = item.bookSourceName - 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 - } - } - } - } - + ImportBookSourceDialog().show(supportFragmentManager, "SourceDialog") } } \ 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 new file mode 100644 index 000000000..f1fffad61 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt @@ -0,0 +1,176 @@ +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.recyclerview.widget.LinearLayoutManager +import io.legado.app.R +import io.legado.app.base.BaseDialogFragment +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.data.entities.BookSource +import io.legado.app.help.SourceHelp +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.customView +import io.legado.app.lib.dialogs.noButton +import io.legado.app.lib.dialogs.okButton +import io.legado.app.ui.widget.text.AutoCompleteTextView +import io.legado.app.utils.applyTint +import io.legado.app.utils.getSize +import io.legado.app.utils.getViewModelOfActivity +import io.legado.app.utils.visible +import kotlinx.android.synthetic.main.dialog_edit_text.view.* +import kotlinx.android.synthetic.main.dialog_recycler_view.* +import kotlinx.android.synthetic.main.item_source_import.view.* +import org.jetbrains.anko.sdk27.listeners.onClick + + +class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { + + val viewModel: ImportBookSourceViewModel + get() = + getViewModelOfActivity(ImportBookSourceViewModel::class.java) + lateinit var adapter: SourcesAdapter + private var _groupName: String? = null + + override fun onStart() { + super.onStart() + val dm = requireActivity().getSize() + 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.setTitle(R.string.import_book_source) + initMenu() + adapter = SourcesAdapter(requireContext()) + val allSources = viewModel.allSources + adapter.sourceCheckState = viewModel.sourceCheckState + adapter.selectStatus = viewModel.selectStatus + + 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() + } + } + + private fun initMenu() { + tool_bar.setOnMenuItemClickListener(this) + tool_bar.inflateMenu(R.menu.import_source) + } + + @SuppressLint("InflateParams") + override fun onMenuItemClick(item: MenuItem): Boolean { + when (item.itemId) { + R.id.menu_new_group -> { + alert(R.string.diy_edit_source_group) { + var editText: AutoCompleteTextView? = null + customView { + layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { + editText = edit_view + } + } + okButton { + editText?.text?.toString()?.let { group -> + _groupName = group + item.title = getString(R.string.diy_edit_source_group_title, _groupName) + } + } + noButton { } + }.show().applyTint() + } + R.id.menu_select_all -> { + adapter.selectStatus.forEachIndexed { index, b -> + if (!b) { + adapter.selectStatus[index] = true + } + } + adapter.notifyDataSetChanged() + } + R.id.menu_un_select_all -> { + adapter.selectStatus.forEachIndexed { index, b -> + if (b) { + adapter.selectStatus[index] = false + } + } + adapter.notifyDataSetChanged() + } + } + return false + } + + override fun onDismiss(dialog: DialogInterface) { + super.onDismiss(dialog) + activity?.finish() + } + + private fun importSelect() { + val selectSource = arrayListOf() + adapter.selectStatus.forEachIndexed { index, b -> + if (_groupName != null) { + adapter.getItem(index)!!.bookSourceGroup = _groupName + } + if (b) { + selectSource.add(adapter.getItem(index)!!) + } + } + SourceHelp.insertBookSource(*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: BookSource, payloads: MutableList) { + holder.itemView.apply { + cb_source_name.isChecked = selectStatus[holder.layoutPosition] + cb_source_name.text = item.bookSourceName + 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 + } + } + } + } + + } + +} 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 06c9d0155..8e91a46c5 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,33 +1,15 @@ package io.legado.app.ui.association -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.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.okButton import io.legado.app.utils.applyTint -import io.legado.app.utils.getSize 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( @@ -97,139 +79,7 @@ class ImportRssSourceActivity : VMBaseActivity( } 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 = requireActivity().getSize() - 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 = getString(R.string.import_rss_source) - 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() - } - } - } - - 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() - } - R.id.menu_un_select_all -> { - adapter.selectStatus.forEachIndexed { index, b -> - if (b) { - adapter.selectStatus[index] = false - } - } - adapter.notifyDataSetChanged() - } - } - 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 - } - } - } - } - + ImportRssSourcesDialog().show(supportFragmentManager, "SourceDialog") } } \ No newline at end of file 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 new file mode 100644 index 000000000..c295546d8 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt @@ -0,0 +1,145 @@ +package io.legado.app.ui.association + +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.recyclerview.widget.LinearLayoutManager +import io.legado.app.R +import io.legado.app.base.BaseDialogFragment +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.data.entities.RssSource +import io.legado.app.help.SourceHelp +import io.legado.app.utils.getSize +import io.legado.app.utils.getViewModelOfActivity +import io.legado.app.utils.visible +import kotlinx.android.synthetic.main.dialog_recycler_view.* +import kotlinx.android.synthetic.main.item_source_import.view.* +import org.jetbrains.anko.sdk27.listeners.onClick + + +class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { + + val viewModel: ImportRssSourceViewModel + get() = + getViewModelOfActivity(ImportRssSourceViewModel::class.java) + lateinit var adapter: SourcesAdapter + + override fun onStart() { + super.onStart() + val dm = requireActivity().getSize() + 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 = getString(R.string.import_rss_source) + initMenu() + adapter = SourcesAdapter(requireContext()) + val allSources = viewModel.allSources + adapter.sourceCheckState = viewModel.sourceCheckState + adapter.selectStatus = viewModel.selectStatus + 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() + } + } + + 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() + } + R.id.menu_un_select_all -> { + adapter.selectStatus.forEachIndexed { index, b -> + if (b) { + adapter.selectStatus[index] = false + } + } + adapter.notifyDataSetChanged() + } + } + 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 + } + } + } + } + } + + +} diff --git a/app/src/main/res/menu/import_source.xml b/app/src/main/res/menu/import_source.xml index 8a2971e3f..b4abd1d0e 100644 --- a/app/src/main/res/menu/import_source.xml +++ b/app/src/main/res/menu/import_source.xml @@ -20,4 +20,9 @@ android:title="@string/un_select_all" app:showAsAction="never" /> + + \ No newline at end of file