优化替换规则导入

pull/1155/head
gedoor 3 years ago
parent 87e4ee063d
commit d1cf6c459a
  1. 6
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt
  2. 17
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt
  3. 67
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt
  4. 49
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt
  5. 6
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt

@ -30,7 +30,7 @@ class ImportBookSourceActivity :
viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide()
if (it > 0) {
successDialog()
ImportBookSourceDialog().show(supportFragmentManager, "SourceDialog")
} else {
errorDialog(getString(R.string.wrong_format))
}
@ -72,8 +72,4 @@ class ImportBookSourceActivity :
}.show()
}
private fun successDialog() {
ImportBookSourceDialog().show(supportFragmentManager, "SourceDialog")
}
}

@ -4,8 +4,6 @@ import android.os.Bundle
import androidx.activity.viewModels
import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.Theme
import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.databinding.ActivityTranslucenceBinding
import io.legado.app.help.IntentDataHelp
import io.legado.app.lib.dialogs.alert
@ -28,8 +26,8 @@ class ImportReplaceRuleActivity :
})
viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide()
if (it.size > 0) {
successDialog(it)
if (it > 0) {
ImportReplaceRuleDialog().show(supportFragmentManager, "importReplaceRule")
} else {
errorDialog("格式不对")
}
@ -71,15 +69,4 @@ class ImportReplaceRuleActivity :
}.show()
}
private fun successDialog(allSource: ArrayList<ReplaceRule>) {
alert("解析结果", "${allSource.size}个替换规则,是否确认导入?") {
okButton {
appDb.replaceRuleDao.insert(*allSource.toTypedArray())
}
noButton()
onDismiss {
finish()
}
}.show()
}
}

@ -1,11 +1,12 @@
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.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
@ -14,12 +15,14 @@ import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.databinding.DialogRecyclerViewBinding
import io.legado.app.databinding.ItemSourceImportBinding
import io.legado.app.ui.widget.dialog.WaitDialog
import io.legado.app.utils.viewbindingdelegate.viewBinding
import io.legado.app.utils.visible
class ImportReplaceRuleDialog : BaseDialogFragment() {
private val binding by viewBinding(DialogRecyclerViewBinding::bind)
private val viewModel by viewModels<ImportReplaceRuleViewModel>()
private val viewModel by activityViewModels<ImportReplaceRuleViewModel>()
lateinit var adapter: SourcesAdapter
override fun onStart() {
@ -44,6 +47,52 @@ class ImportReplaceRuleDialog : BaseDialogFragment() {
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
binding.recyclerView.adapter = adapter
adapter.setItems(viewModel.allRules)
binding.tvCancel.visible()
binding.tvCancel.setOnClickListener {
dismissAllowingStateLoss()
}
binding.tvOk.visible()
binding.tvOk.setOnClickListener {
val waitDialog = WaitDialog(requireContext())
waitDialog.show()
viewModel.importSelect {
waitDialog.dismiss()
dismissAllowingStateLoss()
}
}
upSelectText()
binding.tvFooterLeft.visible()
binding.tvFooterLeft.setOnClickListener {
val selectAll = viewModel.isSelectAll()
viewModel.selectStatus.forEachIndexed { index, b ->
if (b != !selectAll) {
viewModel.selectStatus[index] = !selectAll
}
}
adapter.notifyDataSetChanged()
upSelectText()
}
}
private fun upSelectText() {
if (viewModel.isSelectAll()) {
binding.tvFooterLeft.text = getString(
R.string.select_cancel_count,
viewModel.selectCount(),
viewModel.allRules.size
)
} else {
binding.tvFooterLeft.text = getString(
R.string.select_all_count,
viewModel.selectCount(),
viewModel.allRules.size
)
}
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
activity?.finish()
}
inner class SourcesAdapter(context: Context) :
@ -59,11 +108,21 @@ class ImportReplaceRuleDialog : BaseDialogFragment() {
item: ReplaceRule,
payloads: MutableList<Any>
) {
TODO("Not yet implemented")
binding.run {
cbSourceName.isChecked = viewModel.selectStatus[holder.layoutPosition]
cbSourceName.text = item.name
}
}
override fun registerListener(holder: ItemViewHolder, binding: ItemSourceImportBinding) {
TODO("Not yet implemented")
binding.run {
cbSourceName.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed) {
viewModel.selectStatus[holder.layoutPosition] = isChecked
upSelectText()
}
}
}
}
}

@ -3,7 +3,9 @@ package io.legado.app.ui.association
import android.app.Application
import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.AppConfig
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
@ -12,11 +14,30 @@ import io.legado.app.utils.isAbsUrl
class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
val errorLiveData = MutableLiveData<String>()
val successLiveData = MutableLiveData<ArrayList<ReplaceRule>>()
val successLiveData = MutableLiveData<Int>()
val allRules = arrayListOf<ReplaceRule>()
val checkRules = arrayListOf<ReplaceRule>()
val selectStatus = arrayListOf<ReplaceRule>()
val checkRules = arrayListOf<ReplaceRule?>()
val selectStatus = arrayListOf<Boolean>()
fun isSelectAll(): Boolean {
selectStatus.forEach {
if (!it) {
return false
}
}
return true
}
fun selectCount(): Int {
var count = 0
selectStatus.forEach {
if (it) {
count++
}
}
return count
}
fun import(text: String) {
execute {
@ -35,15 +56,33 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
errorLiveData.postValue(it.localizedMessage ?: "ERROR")
}.onSuccess {
comparisonSource()
successLiveData.postValue(allRules)
}
}
fun importSelect(finally: () -> Unit) {
execute {
val keepName = AppConfig.importKeepName
val selectRules = arrayListOf<ReplaceRule>()
selectStatus.forEachIndexed { index, b ->
if (b) {
val rule = allRules[index]
selectRules.add(rule)
}
}
appDb.replaceRuleDao.insert(*selectRules.toTypedArray())
}.onFinally {
finally.invoke()
}
}
private fun comparisonSource() {
execute {
allRules.forEach {
checkRules.add(null)
selectStatus.add(false)
}
}.onSuccess {
successLiveData.postValue(allRules.size)
}
}
}

@ -29,7 +29,7 @@ class ImportRssSourceActivity :
viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide()
if (it > 0) {
successDialog()
ImportRssSourceDialog().show(supportFragmentManager, "SourceDialog")
} else {
errorDialog(getString(R.string.wrong_format))
}
@ -71,8 +71,4 @@ class ImportRssSourceActivity :
}.show()
}
private fun successDialog() {
ImportRssSourceDialog().show(supportFragmentManager, "SourceDialog")
}
}
Loading…
Cancel
Save