pull/1866/head
parent
f1ad8cb94d
commit
75f5cd1178
@ -0,0 +1,179 @@ |
||||
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.View |
||||
import android.view.ViewGroup |
||||
import androidx.fragment.app.viewModels |
||||
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.RecyclerAdapter |
||||
import io.legado.app.databinding.DialogRecyclerViewBinding |
||||
import io.legado.app.databinding.ItemSourceImportBinding |
||||
import io.legado.app.help.config.ThemeConfig |
||||
import io.legado.app.lib.theme.primaryColor |
||||
import io.legado.app.ui.widget.dialog.CodeDialog |
||||
import io.legado.app.ui.widget.dialog.WaitDialog |
||||
import io.legado.app.utils.GSON |
||||
import io.legado.app.utils.setLayout |
||||
import io.legado.app.utils.showDialogFragment |
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding |
||||
import io.legado.app.utils.visible |
||||
import splitties.views.onClick |
||||
|
||||
class ImportThemeDialog() : BaseDialogFragment(R.layout.dialog_recycler_view) { |
||||
|
||||
constructor(source: String, finishOnDismiss: Boolean = false) : this() { |
||||
arguments = Bundle().apply { |
||||
putString("source", source) |
||||
putBoolean("finishOnDismiss", finishOnDismiss) |
||||
} |
||||
} |
||||
|
||||
private val binding by viewBinding(DialogRecyclerViewBinding::bind) |
||||
private val viewModel by viewModels<ImportThemeViewModel>() |
||||
private val adapter by lazy { SourcesAdapter(requireContext()) } |
||||
|
||||
override fun onStart() { |
||||
super.onStart() |
||||
setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) |
||||
} |
||||
|
||||
override fun onDismiss(dialog: DialogInterface) { |
||||
super.onDismiss(dialog) |
||||
if (arguments?.getBoolean("finishOnDismiss") == true) { |
||||
activity?.finish() |
||||
} |
||||
} |
||||
|
||||
@SuppressLint("NotifyDataSetChanged") |
||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { |
||||
binding.toolBar.setBackgroundColor(primaryColor) |
||||
binding.toolBar.setTitle(R.string.import_book_source) |
||||
binding.rotateLoading.show() |
||||
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) |
||||
binding.recyclerView.adapter = adapter |
||||
binding.tvCancel.visible() |
||||
binding.tvCancel.setOnClickListener { |
||||
dismissAllowingStateLoss() |
||||
} |
||||
binding.tvOk.visible() |
||||
binding.tvOk.setOnClickListener { |
||||
val waitDialog = WaitDialog(requireContext()) |
||||
waitDialog.show() |
||||
viewModel.importSelect { |
||||
waitDialog.dismiss() |
||||
dismissAllowingStateLoss() |
||||
} |
||||
} |
||||
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() |
||||
} |
||||
viewModel.errorLiveData.observe(this) { |
||||
binding.rotateLoading.hide() |
||||
binding.tvMsg.apply { |
||||
text = it |
||||
visible() |
||||
} |
||||
} |
||||
viewModel.successLiveData.observe(this) { |
||||
binding.rotateLoading.hide() |
||||
if (it > 0) { |
||||
adapter.setItems(viewModel.allSources) |
||||
upSelectText() |
||||
} else { |
||||
binding.tvMsg.apply { |
||||
setText(R.string.wrong_format) |
||||
visible() |
||||
} |
||||
} |
||||
} |
||||
val source = arguments?.getString("source") |
||||
if (source.isNullOrEmpty()) { |
||||
dismiss() |
||||
return |
||||
} |
||||
viewModel.importSource(source) |
||||
} |
||||
|
||||
private fun upSelectText() { |
||||
if (viewModel.isSelectAll) { |
||||
binding.tvFooterLeft.text = getString( |
||||
R.string.select_cancel_count, |
||||
viewModel.selectCount, |
||||
viewModel.allSources.size |
||||
) |
||||
} else { |
||||
binding.tvFooterLeft.text = getString( |
||||
R.string.select_all_count, |
||||
viewModel.selectCount, |
||||
viewModel.allSources.size |
||||
) |
||||
} |
||||
} |
||||
|
||||
inner class SourcesAdapter(context: Context) : |
||||
RecyclerAdapter<ThemeConfig.Config, ItemSourceImportBinding>(context) { |
||||
|
||||
override fun getViewBinding(parent: ViewGroup): ItemSourceImportBinding { |
||||
return ItemSourceImportBinding.inflate(inflater, parent, false) |
||||
} |
||||
|
||||
override fun convert( |
||||
holder: ItemViewHolder, |
||||
binding: ItemSourceImportBinding, |
||||
item: ThemeConfig.Config, |
||||
payloads: MutableList<Any> |
||||
) { |
||||
binding.apply { |
||||
cbSourceName.isChecked = viewModel.selectStatus[holder.layoutPosition] |
||||
cbSourceName.text = item.themeName |
||||
val localSource = viewModel.checkSources[holder.layoutPosition] |
||||
tvSourceState.text = when { |
||||
localSource == null -> "新增" |
||||
localSource != item -> "更新" |
||||
else -> "已有" |
||||
} |
||||
} |
||||
} |
||||
|
||||
override fun registerListener(holder: ItemViewHolder, binding: ItemSourceImportBinding) { |
||||
binding.apply { |
||||
cbSourceName.setOnCheckedChangeListener { buttonView, isChecked -> |
||||
if (buttonView.isPressed) { |
||||
viewModel.selectStatus[holder.layoutPosition] = isChecked |
||||
upSelectText() |
||||
} |
||||
} |
||||
root.onClick { |
||||
cbSourceName.isChecked = !cbSourceName.isChecked |
||||
viewModel.selectStatus[holder.layoutPosition] = cbSourceName.isChecked |
||||
upSelectText() |
||||
} |
||||
tvOpen.setOnClickListener { |
||||
val source = viewModel.allSources[holder.layoutPosition] |
||||
showDialogFragment( |
||||
CodeDialog( |
||||
GSON.toJson(source), |
||||
disableEdit = false, |
||||
requestId = holder.layoutPosition.toString() |
||||
) |
||||
) |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,106 @@ |
||||
package io.legado.app.ui.association |
||||
|
||||
import android.app.Application |
||||
import androidx.lifecycle.MutableLiveData |
||||
import io.legado.app.R |
||||
import io.legado.app.base.BaseViewModel |
||||
import io.legado.app.exception.NoStackTraceException |
||||
import io.legado.app.help.config.ThemeConfig |
||||
import io.legado.app.help.http.newCallResponseBody |
||||
import io.legado.app.help.http.okHttpClient |
||||
import io.legado.app.help.http.text |
||||
import io.legado.app.utils.* |
||||
|
||||
class ImportThemeViewModel(app: Application) : BaseViewModel(app) { |
||||
|
||||
val errorLiveData = MutableLiveData<String>() |
||||
val successLiveData = MutableLiveData<Int>() |
||||
|
||||
val allSources = arrayListOf<ThemeConfig.Config>() |
||||
val checkSources = arrayListOf<ThemeConfig.Config?>() |
||||
val selectStatus = arrayListOf<Boolean>() |
||||
|
||||
val isSelectAll: Boolean |
||||
get() { |
||||
selectStatus.forEach { |
||||
if (!it) { |
||||
return false |
||||
} |
||||
} |
||||
return true |
||||
} |
||||
|
||||
val selectCount: Int |
||||
get() { |
||||
var count = 0 |
||||
selectStatus.forEach { |
||||
if (it) { |
||||
count++ |
||||
} |
||||
} |
||||
return count |
||||
} |
||||
|
||||
fun importSelect(finally: () -> Unit) { |
||||
execute { |
||||
selectStatus.forEachIndexed { index, b -> |
||||
if (b) { |
||||
ThemeConfig.addConfig(allSources[index]) |
||||
} |
||||
} |
||||
}.onFinally { |
||||
finally.invoke() |
||||
} |
||||
} |
||||
|
||||
fun importSource(text: String) { |
||||
execute { |
||||
importSourceAwait(text.trim()) |
||||
}.onError { |
||||
it.printOnDebug() |
||||
errorLiveData.postValue(it.localizedMessage ?: "") |
||||
}.onSuccess { |
||||
comparisonSource() |
||||
} |
||||
} |
||||
|
||||
private suspend fun importSourceAwait(text: String) { |
||||
when { |
||||
text.isJsonObject() -> { |
||||
GSON.fromJsonObject<ThemeConfig.Config>(text).getOrThrow()?.let { |
||||
allSources.add(it) |
||||
} |
||||
} |
||||
text.isJsonArray() -> GSON.fromJsonArray<ThemeConfig.Config>(text).getOrThrow() |
||||
?.let { items -> |
||||
allSources.addAll(items) |
||||
} |
||||
text.isAbsUrl() -> { |
||||
importSourceUrl(text) |
||||
} |
||||
else -> throw NoStackTraceException(context.getString(R.string.wrong_format)) |
||||
} |
||||
} |
||||
|
||||
private suspend fun importSourceUrl(url: String) { |
||||
okHttpClient.newCallResponseBody { |
||||
url(url) |
||||
}.text().let { |
||||
importSourceAwait(it) |
||||
} |
||||
} |
||||
|
||||
private fun comparisonSource() { |
||||
execute { |
||||
allSources.forEach { config -> |
||||
val source = ThemeConfig.configList.find { |
||||
it.themeName == config.themeName |
||||
} |
||||
checkSources.add(source) |
||||
selectStatus.add(source == null || source != config) |
||||
} |
||||
successLiveData.postValue(allSources.size) |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,179 @@ |
||||
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.View |
||||
import android.view.ViewGroup |
||||
import androidx.fragment.app.viewModels |
||||
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.RecyclerAdapter |
||||
import io.legado.app.data.entities.TxtTocRule |
||||
import io.legado.app.databinding.DialogRecyclerViewBinding |
||||
import io.legado.app.databinding.ItemSourceImportBinding |
||||
import io.legado.app.lib.theme.primaryColor |
||||
import io.legado.app.ui.widget.dialog.CodeDialog |
||||
import io.legado.app.ui.widget.dialog.WaitDialog |
||||
import io.legado.app.utils.GSON |
||||
import io.legado.app.utils.setLayout |
||||
import io.legado.app.utils.showDialogFragment |
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding |
||||
import io.legado.app.utils.visible |
||||
import splitties.views.onClick |
||||
|
||||
class ImportTxtRuleDialog() : BaseDialogFragment(R.layout.dialog_recycler_view) { |
||||
|
||||
constructor(source: String, finishOnDismiss: Boolean = false) : this() { |
||||
arguments = Bundle().apply { |
||||
putString("source", source) |
||||
putBoolean("finishOnDismiss", finishOnDismiss) |
||||
} |
||||
} |
||||
|
||||
private val binding by viewBinding(DialogRecyclerViewBinding::bind) |
||||
private val viewModel by viewModels<ImportTxtRuleViewModel>() |
||||
private val adapter by lazy { SourcesAdapter(requireContext()) } |
||||
|
||||
override fun onStart() { |
||||
super.onStart() |
||||
setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) |
||||
} |
||||
|
||||
override fun onDismiss(dialog: DialogInterface) { |
||||
super.onDismiss(dialog) |
||||
if (arguments?.getBoolean("finishOnDismiss") == true) { |
||||
activity?.finish() |
||||
} |
||||
} |
||||
|
||||
@SuppressLint("NotifyDataSetChanged") |
||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { |
||||
binding.toolBar.setBackgroundColor(primaryColor) |
||||
binding.toolBar.setTitle(R.string.import_book_source) |
||||
binding.rotateLoading.show() |
||||
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) |
||||
binding.recyclerView.adapter = adapter |
||||
binding.tvCancel.visible() |
||||
binding.tvCancel.setOnClickListener { |
||||
dismissAllowingStateLoss() |
||||
} |
||||
binding.tvOk.visible() |
||||
binding.tvOk.setOnClickListener { |
||||
val waitDialog = WaitDialog(requireContext()) |
||||
waitDialog.show() |
||||
viewModel.importSelect { |
||||
waitDialog.dismiss() |
||||
dismissAllowingStateLoss() |
||||
} |
||||
} |
||||
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() |
||||
} |
||||
viewModel.errorLiveData.observe(this) { |
||||
binding.rotateLoading.hide() |
||||
binding.tvMsg.apply { |
||||
text = it |
||||
visible() |
||||
} |
||||
} |
||||
viewModel.successLiveData.observe(this) { |
||||
binding.rotateLoading.hide() |
||||
if (it > 0) { |
||||
adapter.setItems(viewModel.allSources) |
||||
upSelectText() |
||||
} else { |
||||
binding.tvMsg.apply { |
||||
setText(R.string.wrong_format) |
||||
visible() |
||||
} |
||||
} |
||||
} |
||||
val source = arguments?.getString("source") |
||||
if (source.isNullOrEmpty()) { |
||||
dismiss() |
||||
return |
||||
} |
||||
viewModel.importSource(source) |
||||
} |
||||
|
||||
private fun upSelectText() { |
||||
if (viewModel.isSelectAll) { |
||||
binding.tvFooterLeft.text = getString( |
||||
R.string.select_cancel_count, |
||||
viewModel.selectCount, |
||||
viewModel.allSources.size |
||||
) |
||||
} else { |
||||
binding.tvFooterLeft.text = getString( |
||||
R.string.select_all_count, |
||||
viewModel.selectCount, |
||||
viewModel.allSources.size |
||||
) |
||||
} |
||||
} |
||||
|
||||
inner class SourcesAdapter(context: Context) : |
||||
RecyclerAdapter<TxtTocRule, ItemSourceImportBinding>(context) { |
||||
|
||||
override fun getViewBinding(parent: ViewGroup): ItemSourceImportBinding { |
||||
return ItemSourceImportBinding.inflate(inflater, parent, false) |
||||
} |
||||
|
||||
override fun convert( |
||||
holder: ItemViewHolder, |
||||
binding: ItemSourceImportBinding, |
||||
item: TxtTocRule, |
||||
payloads: MutableList<Any> |
||||
) { |
||||
binding.apply { |
||||
cbSourceName.isChecked = viewModel.selectStatus[holder.layoutPosition] |
||||
cbSourceName.text = item.name |
||||
val localSource = viewModel.checkSources[holder.layoutPosition] |
||||
tvSourceState.text = when { |
||||
localSource == null -> "新增" |
||||
item != localSource -> "更新" |
||||
else -> "已有" |
||||
} |
||||
} |
||||
} |
||||
|
||||
override fun registerListener(holder: ItemViewHolder, binding: ItemSourceImportBinding) { |
||||
binding.apply { |
||||
cbSourceName.setOnCheckedChangeListener { buttonView, isChecked -> |
||||
if (buttonView.isPressed) { |
||||
viewModel.selectStatus[holder.layoutPosition] = isChecked |
||||
upSelectText() |
||||
} |
||||
} |
||||
root.onClick { |
||||
cbSourceName.isChecked = !cbSourceName.isChecked |
||||
viewModel.selectStatus[holder.layoutPosition] = cbSourceName.isChecked |
||||
upSelectText() |
||||
} |
||||
tvOpen.setOnClickListener { |
||||
val source = viewModel.allSources[holder.layoutPosition] |
||||
showDialogFragment( |
||||
CodeDialog( |
||||
GSON.toJson(source), |
||||
disableEdit = false, |
||||
requestId = holder.layoutPosition.toString() |
||||
) |
||||
) |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,107 @@ |
||||
package io.legado.app.ui.association |
||||
|
||||
import android.app.Application |
||||
import androidx.lifecycle.MutableLiveData |
||||
import io.legado.app.R |
||||
import io.legado.app.base.BaseViewModel |
||||
import io.legado.app.data.appDb |
||||
import io.legado.app.data.entities.TxtTocRule |
||||
import io.legado.app.exception.NoStackTraceException |
||||
import io.legado.app.help.http.newCallResponseBody |
||||
import io.legado.app.help.http.okHttpClient |
||||
import io.legado.app.help.http.text |
||||
import io.legado.app.utils.* |
||||
|
||||
class ImportTxtRuleViewModel(app: Application) : BaseViewModel(app) { |
||||
|
||||
val errorLiveData = MutableLiveData<String>() |
||||
val successLiveData = MutableLiveData<Int>() |
||||
|
||||
val allSources = arrayListOf<TxtTocRule>() |
||||
val checkSources = arrayListOf<TxtTocRule?>() |
||||
val selectStatus = arrayListOf<Boolean>() |
||||
|
||||
val isSelectAll: Boolean |
||||
get() { |
||||
selectStatus.forEach { |
||||
if (!it) { |
||||
return false |
||||
} |
||||
} |
||||
return true |
||||
} |
||||
|
||||
val selectCount: Int |
||||
get() { |
||||
var count = 0 |
||||
selectStatus.forEach { |
||||
if (it) { |
||||
count++ |
||||
} |
||||
} |
||||
return count |
||||
} |
||||
|
||||
fun importSelect(finally: () -> Unit) { |
||||
execute { |
||||
val selectSource = arrayListOf<TxtTocRule>() |
||||
selectStatus.forEachIndexed { index, b -> |
||||
if (b) { |
||||
selectSource.add(allSources[index]) |
||||
} |
||||
} |
||||
appDb.txtTocRuleDao.insert(*selectSource.toTypedArray()) |
||||
}.onFinally { |
||||
finally.invoke() |
||||
} |
||||
} |
||||
|
||||
fun importSource(text: String) { |
||||
execute { |
||||
importSourceAwait(text.trim()) |
||||
}.onError { |
||||
it.printOnDebug() |
||||
errorLiveData.postValue(it.localizedMessage ?: "") |
||||
}.onSuccess { |
||||
comparisonSource() |
||||
} |
||||
} |
||||
|
||||
private suspend fun importSourceAwait(text: String) { |
||||
when { |
||||
text.isJsonObject() -> { |
||||
GSON.fromJsonObject<TxtTocRule>(text).getOrThrow()?.let { |
||||
allSources.add(it) |
||||
} |
||||
} |
||||
text.isJsonArray() -> GSON.fromJsonArray<TxtTocRule>(text).getOrThrow() |
||||
?.let { items -> |
||||
allSources.addAll(items) |
||||
} |
||||
text.isAbsUrl() -> { |
||||
importSourceUrl(text) |
||||
} |
||||
else -> throw NoStackTraceException(context.getString(R.string.wrong_format)) |
||||
} |
||||
} |
||||
|
||||
private suspend fun importSourceUrl(url: String) { |
||||
okHttpClient.newCallResponseBody { |
||||
url(url) |
||||
}.text().let { |
||||
importSourceAwait(it) |
||||
} |
||||
} |
||||
|
||||
private fun comparisonSource() { |
||||
execute { |
||||
allSources.forEach { |
||||
val source = appDb.txtTocRuleDao.get(it.id) |
||||
checkSources.add(source) |
||||
selectStatus.add(source == null || it != source) |
||||
} |
||||
successLiveData.postValue(allSources.size) |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue