优化导入功能

pull/473/head
gedoor 4 years ago
parent bc1b10b441
commit 53099f7251
  1. 54
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt
  2. 22
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt
  3. 43
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt
  4. 15
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt

@ -15,7 +15,6 @@ import io.legado.app.base.BaseDialogFragment
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.BookSource 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.alert
import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.customView
import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.noButton
@ -30,14 +29,14 @@ import kotlinx.android.synthetic.main.dialog_recycler_view.*
import kotlinx.android.synthetic.main.item_source_import.view.* import kotlinx.android.synthetic.main.item_source_import.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
/**
* 导入书源弹出窗口
*/
class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
val viewModel: ImportBookSourceViewModel val viewModel: ImportBookSourceViewModel
get() = get() = getViewModelOfActivity(ImportBookSourceViewModel::class.java)
getViewModelOfActivity(ImportBookSourceViewModel::class.java)
lateinit var adapter: SourcesAdapter lateinit var adapter: SourcesAdapter
private var _groupName: String? = null
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -60,23 +59,20 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
tool_bar.setTitle(R.string.import_book_source) tool_bar.setTitle(R.string.import_book_source)
initMenu() initMenu()
adapter = SourcesAdapter(requireContext()) adapter = SourcesAdapter(requireContext())
val allSources = viewModel.allSources
adapter.sourceCheckState = viewModel.sourceCheckState
adapter.selectStatus = viewModel.selectStatus
recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.adapter = adapter recycler_view.adapter = adapter
adapter.setItems(allSources) adapter.setItems(viewModel.allSources)
tv_cancel.visible() tv_cancel.visible()
tv_cancel.onClick { tv_cancel.onClick {
dismiss() dismiss()
} }
tv_ok.visible() tv_ok.visible()
tv_ok.onClick { tv_ok.onClick {
importSelect() viewModel.importSelect {
dismiss() dismiss()
} }
} }
}
private fun initMenu() { private fun initMenu() {
tool_bar.setOnMenuItemClickListener(this) tool_bar.setOnMenuItemClickListener(this)
@ -96,25 +92,25 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
} }
okButton { okButton {
editText?.text?.toString()?.let { group -> editText?.text?.toString()?.let { group ->
_groupName = group viewModel.groupName = group
item.title = getString(R.string.diy_edit_source_group_title, _groupName) item.title = getString(R.string.diy_edit_source_group_title, group)
} }
} }
noButton { } noButton { }
}.show().applyTint() }.show().applyTint()
} }
R.id.menu_select_all -> { R.id.menu_select_all -> {
adapter.selectStatus.forEachIndexed { index, b -> viewModel.selectStatus.forEachIndexed { index, b ->
if (!b) { if (!b) {
adapter.selectStatus[index] = true viewModel.selectStatus[index] = true
} }
} }
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
} }
R.id.menu_un_select_all -> { R.id.menu_un_select_all -> {
adapter.selectStatus.forEachIndexed { index, b -> viewModel.selectStatus.forEachIndexed { index, b ->
if (b) { if (b) {
adapter.selectStatus[index] = false viewModel.selectStatus[index] = false
} }
} }
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
@ -128,31 +124,15 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
activity?.finish() activity?.finish()
} }
private fun importSelect() {
val selectSource = arrayListOf<BookSource>()
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) : inner class SourcesAdapter(context: Context) :
SimpleRecyclerAdapter<BookSource>(context, R.layout.item_source_import) { SimpleRecyclerAdapter<BookSource>(context, R.layout.item_source_import) {
lateinit var sourceCheckState: ArrayList<Boolean>
lateinit var selectStatus: ArrayList<Boolean>
override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) {
holder.itemView.apply { holder.itemView.apply {
cb_source_name.isChecked = selectStatus[holder.layoutPosition] cb_source_name.isChecked = viewModel.selectStatus[holder.layoutPosition]
cb_source_name.text = item.bookSourceName cb_source_name.text = item.bookSourceName
tv_source_state.text = if (sourceCheckState[holder.layoutPosition]) { tv_source_state.text = if (viewModel.sourceCheckState[holder.layoutPosition]) {
"已存在" "已存在"
} else { } else {
"新书源" "新书源"
@ -165,7 +145,7 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
holder.itemView.apply { holder.itemView.apply {
cb_source_name.setOnCheckedChangeListener { buttonView, isChecked -> cb_source_name.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed) { if (buttonView.isPressed) {
selectStatus[holder.layoutPosition] = isChecked viewModel.selectStatus[holder.layoutPosition] = isChecked
} }
} }
} }

@ -9,6 +9,7 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.SourceHelp
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.help.storage.OldRule import io.legado.app.help.storage.OldRule
import io.legado.app.help.storage.Restore import io.legado.app.help.storage.Restore
@ -16,7 +17,7 @@ import io.legado.app.utils.*
import java.io.File import java.io.File
class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
var groupName: String? = null
val errorLiveData = MutableLiveData<String>() val errorLiveData = MutableLiveData<String>()
val successLiveData = MutableLiveData<Int>() val successLiveData = MutableLiveData<Int>()
@ -24,6 +25,25 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
val sourceCheckState = arrayListOf<Boolean>() val sourceCheckState = arrayListOf<Boolean>()
val selectStatus = arrayListOf<Boolean>() val selectStatus = arrayListOf<Boolean>()
fun importSelect(finally: () -> Unit) {
execute {
val selectSource = arrayListOf<BookSource>()
selectStatus.forEachIndexed { index, b ->
if (groupName != null) {
allSources[index].bookSourceGroup = groupName
}
if (b) {
selectSource.add(allSources[index])
}
}
SourceHelp.insertBookSource(*selectSource.toTypedArray())
}.onFinally {
finally.invoke()
}
}
fun importSourceFromFilePath(path: String) { fun importSourceFromFilePath(path: String) {
execute { execute {
val content = if (path.isContentScheme()) { val content = if (path.isContentScheme()) {

@ -14,7 +14,6 @@ import io.legado.app.base.BaseDialogFragment
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.SourceHelp
import io.legado.app.utils.getSize import io.legado.app.utils.getSize
import io.legado.app.utils.getViewModelOfActivity import io.legado.app.utils.getViewModelOfActivity
import io.legado.app.utils.visible import io.legado.app.utils.visible
@ -22,7 +21,9 @@ import kotlinx.android.synthetic.main.dialog_recycler_view.*
import kotlinx.android.synthetic.main.item_source_import.view.* import kotlinx.android.synthetic.main.item_source_import.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
/**
* 导入rss源弹出窗口
*/
class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
val viewModel: ImportRssSourceViewModel val viewModel: ImportRssSourceViewModel
@ -51,22 +52,20 @@ class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
tool_bar.title = getString(R.string.import_rss_source) tool_bar.title = getString(R.string.import_rss_source)
initMenu() initMenu()
adapter = SourcesAdapter(requireContext()) adapter = SourcesAdapter(requireContext())
val allSources = viewModel.allSources
adapter.sourceCheckState = viewModel.sourceCheckState
adapter.selectStatus = viewModel.selectStatus
recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.adapter = adapter recycler_view.adapter = adapter
adapter.setItems(allSources) adapter.setItems(viewModel.allSources)
tv_cancel.visible() tv_cancel.visible()
tv_cancel.onClick { tv_cancel.onClick {
dismiss() dismiss()
} }
tv_ok.visible() tv_ok.visible()
tv_ok.onClick { tv_ok.onClick {
importSelect() viewModel.importSelect {
dismiss() dismiss()
} }
} }
}
private fun initMenu() { private fun initMenu() {
tool_bar.setOnMenuItemClickListener(this) tool_bar.setOnMenuItemClickListener(this)
@ -76,17 +75,17 @@ class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
override fun onMenuItemClick(item: MenuItem): Boolean { override fun onMenuItemClick(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.menu_select_all -> { R.id.menu_select_all -> {
adapter.selectStatus.forEachIndexed { index, b -> viewModel.selectStatus.forEachIndexed { index, b ->
if (!b) { if (!b) {
adapter.selectStatus[index] = true viewModel.selectStatus[index] = true
} }
} }
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
} }
R.id.menu_un_select_all -> { R.id.menu_un_select_all -> {
adapter.selectStatus.forEachIndexed { index, b -> viewModel.selectStatus.forEachIndexed { index, b ->
if (b) { if (b) {
adapter.selectStatus[index] = false viewModel.selectStatus[index] = false
} }
} }
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
@ -100,28 +99,14 @@ class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
activity?.finish() activity?.finish()
} }
private fun importSelect() { inner class SourcesAdapter(context: Context) :
val selectSource = arrayListOf<RssSource>()
adapter.selectStatus.forEachIndexed { index, b ->
if (b) {
selectSource.add(adapter.getItem(index)!!)
}
}
SourceHelp.insertRssSource(*selectSource.toTypedArray())
}
class SourcesAdapter(context: Context) :
SimpleRecyclerAdapter<RssSource>(context, R.layout.item_source_import) { SimpleRecyclerAdapter<RssSource>(context, R.layout.item_source_import) {
lateinit var sourceCheckState: ArrayList<Boolean>
lateinit var selectStatus: ArrayList<Boolean>
override fun convert(holder: ItemViewHolder, item: RssSource, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: RssSource, payloads: MutableList<Any>) {
holder.itemView.apply { holder.itemView.apply {
cb_source_name.isChecked = selectStatus[holder.layoutPosition] cb_source_name.isChecked = viewModel.selectStatus[holder.layoutPosition]
cb_source_name.text = item.sourceName cb_source_name.text = item.sourceName
tv_source_state.text = if (sourceCheckState[holder.layoutPosition]) { tv_source_state.text = if (viewModel.sourceCheckState[holder.layoutPosition]) {
"已存在" "已存在"
} else { } else {
"新订阅源" "新订阅源"
@ -134,7 +119,7 @@ class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
holder.itemView.apply { holder.itemView.apply {
cb_source_name.setOnCheckedChangeListener { buttonView, isChecked -> cb_source_name.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed) { if (buttonView.isPressed) {
selectStatus[holder.layoutPosition] = isChecked viewModel.selectStatus[holder.layoutPosition] = isChecked
} }
} }
} }

@ -9,6 +9,7 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.SourceHelp
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.help.storage.Restore import io.legado.app.help.storage.Restore
import io.legado.app.utils.* import io.legado.app.utils.*
@ -24,6 +25,20 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
val selectStatus = arrayListOf<Boolean>() val selectStatus = arrayListOf<Boolean>()
fun importSelect(finally: () -> Unit) {
execute {
val selectSource = arrayListOf<RssSource>()
selectStatus.forEachIndexed { index, b ->
if (b) {
selectSource.add(allSources[index])
}
}
SourceHelp.insertRssSource(*selectSource.toTypedArray())
}.onFinally {
finally.invoke()
}
}
fun importSourceFromFilePath(path: String) { fun importSourceFromFilePath(path: String) {
execute { execute {
val content = if (path.isContentScheme()) { val content = if (path.isContentScheme()) {

Loading…
Cancel
Save