优化导入功能

pull/473/head
gedoor 4 years ago
parent bc1b10b441
commit 53099f7251
  1. 56
      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. 45
      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.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
@ -30,14 +29,14 @@ 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)
get() = getViewModelOfActivity(ImportBookSourceViewModel::class.java)
lateinit var adapter: SourcesAdapter
private var _groupName: String? = null
override fun onStart() {
super.onStart()
@ -60,21 +59,18 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
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)
adapter.setItems(viewModel.allSources)
tv_cancel.visible()
tv_cancel.onClick {
dismiss()
}
tv_ok.visible()
tv_ok.onClick {
importSelect()
dismiss()
viewModel.importSelect {
dismiss()
}
}
}
@ -96,25 +92,25 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
}
okButton {
editText?.text?.toString()?.let { group ->
_groupName = group
item.title = getString(R.string.diy_edit_source_group_title, _groupName)
viewModel.groupName = group
item.title = getString(R.string.diy_edit_source_group_title, group)
}
}
noButton { }
}.show().applyTint()
}
R.id.menu_select_all -> {
adapter.selectStatus.forEachIndexed { index, b ->
viewModel.selectStatus.forEachIndexed { index, b ->
if (!b) {
adapter.selectStatus[index] = true
viewModel.selectStatus[index] = true
}
}
adapter.notifyDataSetChanged()
}
R.id.menu_un_select_all -> {
adapter.selectStatus.forEachIndexed { index, b ->
viewModel.selectStatus.forEachIndexed { index, b ->
if (b) {
adapter.selectStatus[index] = false
viewModel.selectStatus[index] = false
}
}
adapter.notifyDataSetChanged()
@ -128,31 +124,15 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
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) {
lateinit var sourceCheckState: ArrayList<Boolean>
lateinit var selectStatus: ArrayList<Boolean>
override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) {
holder.itemView.apply {
cb_source_name.isChecked = selectStatus[holder.layoutPosition]
cb_source_name.isChecked = viewModel.selectStatus[holder.layoutPosition]
cb_source_name.text = item.bookSourceName
tv_source_state.text = if (sourceCheckState[holder.layoutPosition]) {
tv_source_state.text = if (viewModel.sourceCheckState[holder.layoutPosition]) {
"已存在"
} else {
"新书源"
@ -165,7 +145,7 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
holder.itemView.apply {
cb_source_name.setOnCheckedChangeListener { buttonView, isChecked ->
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.base.BaseViewModel
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.storage.OldRule
import io.legado.app.help.storage.Restore
@ -16,7 +17,7 @@ import io.legado.app.utils.*
import java.io.File
class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
var groupName: String? = null
val errorLiveData = MutableLiveData<String>()
val successLiveData = MutableLiveData<Int>()
@ -24,6 +25,25 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
val sourceCheckState = 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) {
execute {
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.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
@ -22,7 +21,9 @@ import kotlinx.android.synthetic.main.dialog_recycler_view.*
import kotlinx.android.synthetic.main.item_source_import.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
/**
* 导入rss源弹出窗口
*/
class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
val viewModel: ImportRssSourceViewModel
@ -51,20 +52,18 @@ class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
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)
adapter.setItems(viewModel.allSources)
tv_cancel.visible()
tv_cancel.onClick {
dismiss()
}
tv_ok.visible()
tv_ok.onClick {
importSelect()
dismiss()
viewModel.importSelect {
dismiss()
}
}
}
@ -76,17 +75,17 @@ class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
override fun onMenuItemClick(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_select_all -> {
adapter.selectStatus.forEachIndexed { index, b ->
viewModel.selectStatus.forEachIndexed { index, b ->
if (!b) {
adapter.selectStatus[index] = true
viewModel.selectStatus[index] = true
}
}
adapter.notifyDataSetChanged()
}
R.id.menu_un_select_all -> {
adapter.selectStatus.forEachIndexed { index, b ->
viewModel.selectStatus.forEachIndexed { index, b ->
if (b) {
adapter.selectStatus[index] = false
viewModel.selectStatus[index] = false
}
}
adapter.notifyDataSetChanged()
@ -100,28 +99,14 @@ class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
activity?.finish()
}
private fun importSelect() {
val selectSource = arrayListOf<RssSource>()
adapter.selectStatus.forEachIndexed { index, b ->
if (b) {
selectSource.add(adapter.getItem(index)!!)
}
}
SourceHelp.insertRssSource(*selectSource.toTypedArray())
}
class SourcesAdapter(context: Context) :
inner class SourcesAdapter(context: Context) :
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>) {
holder.itemView.apply {
cb_source_name.isChecked = selectStatus[holder.layoutPosition]
cb_source_name.isChecked = viewModel.selectStatus[holder.layoutPosition]
cb_source_name.text = item.sourceName
tv_source_state.text = if (sourceCheckState[holder.layoutPosition]) {
tv_source_state.text = if (viewModel.sourceCheckState[holder.layoutPosition]) {
"已存在"
} else {
"新订阅源"
@ -134,7 +119,7 @@ class ImportRssSourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
holder.itemView.apply {
cb_source_name.setOnCheckedChangeListener { buttonView, isChecked ->
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.base.BaseViewModel
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.storage.Restore
import io.legado.app.utils.*
@ -24,6 +25,20 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
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) {
execute {
val content = if (path.isContentScheme()) {

Loading…
Cancel
Save