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 18b385627..a1e1badca 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 @@ -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") - } - } \ No newline at end of file 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 49439f76b..d01b49f26 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 @@ -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) { - alert("解析结果", "共${allSource.size}个替换规则,是否确认导入?") { - okButton { - appDb.replaceRuleDao.insert(*allSource.toTypedArray()) - } - noButton() - onDismiss { - finish() - } - }.show() - } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt index 03cda5066..3f6b14dbf 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt @@ -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() + private val viewModel by activityViewModels() 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 ) { - 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() + } + } + } } } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt index 4dba9c35d..0fc20ca89 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt @@ -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() - val successLiveData = MutableLiveData>() + val successLiveData = MutableLiveData() val allRules = arrayListOf() - val checkRules = arrayListOf() - val selectStatus = arrayListOf() + val checkRules = arrayListOf() + val selectStatus = arrayListOf() + + 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() + 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) } } } \ No newline at end of file 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 0ee8a4126..7a2cfe9d9 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 @@ -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") - } - } \ No newline at end of file