diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchScopeDialog.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchScopeDialog.kt index 15cb4aa78..935ce36f7 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchScopeDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchScopeDialog.kt @@ -1,46 +1,183 @@ package io.legado.app.ui.book.search +import android.annotation.SuppressLint import android.os.Bundle import android.view.View import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.BaseDialogFragment +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.data.appDb +import io.legado.app.data.entities.BookSource import io.legado.app.databinding.DialogSearchScopeBinding +import io.legado.app.databinding.ItemCheckBoxBinding +import io.legado.app.databinding.ItemRadioButtonBinding import io.legado.app.lib.theme.primaryColor import io.legado.app.utils.setLayout import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class SearchScopeDialog : BaseDialogFragment(R.layout.dialog_search_scope) { private val binding by viewBinding(DialogSearchScopeBinding::bind) - val callback: Callback - get() { - return parentFragment as? Callback ?: activity as Callback - } + val callback: Callback get() = parentFragment as? Callback ?: activity as Callback + var groups: List? = null + var sources: List? = null + + val adapter by lazy { + RecyclerAdapter() + } override fun onStart() { super.onStart() - setLayout(0.9f, ViewGroup.LayoutParams.WRAP_CONTENT) + setLayout(0.9f, 0.8f) } override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { binding.toolBar.setBackgroundColor(primaryColor) + binding.recyclerView.adapter = adapter initOtherView() + initData() + upData() } private fun initOtherView() { + binding.rgScope.setOnCheckedChangeListener { group, checkedId -> + upData() + } binding.tvCancel.setOnClickListener { dismiss() } binding.tvAllSource.setOnClickListener { callback.onSearchScopeOk(SearchScope("")) + dismiss() } binding.tvOk.setOnClickListener { - callback.onSearchScopeOk(SearchScope("")) + if (binding.rbGroup.isChecked) { + callback.onSearchScopeOk(SearchScope(adapter.selectGroups)) + } else { + val selectSource = adapter.selectSource + if (selectSource != null) { + callback.onSearchScopeOk(SearchScope(selectSource)) + } else { + callback.onSearchScopeOk(SearchScope("")) + } + } + dismiss() + } + } + + private fun initData() { + launch { + withContext(IO) { + groups = appDb.bookSourceDao.allGroups + sources = appDb.bookSourceDao.allEnabled + } + upData() + } + } + + @SuppressLint("NotifyDataSetChanged") + private fun upData() { + launch { + adapter.notifyDataSetChanged() } } + inner class RecyclerAdapter : RecyclerView.Adapter() { + + val selectGroups = arrayListOf() + var selectSource: BookSource? = null + + override fun getItemViewType(position: Int): Int { + return if (binding.rbSource.isChecked) { + 1 + } else { + 0 + } + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder { + return if (viewType == 1) { + ItemViewHolder(ItemRadioButtonBinding.inflate(layoutInflater, parent, false)) + } else { + ItemViewHolder(ItemCheckBoxBinding.inflate(layoutInflater, parent, false)) + } + } + + override fun onBindViewHolder( + holder: ItemViewHolder, + position: Int, + payloads: MutableList + ) { + if (payloads.isEmpty()) { + super.onBindViewHolder(holder, position, payloads) + } else { + when (holder.binding) { + is ItemCheckBoxBinding -> { + groups?.get(position)?.let { + holder.binding.checkBox.isChecked = selectGroups.contains(it) + holder.binding.checkBox.text = it + } + } + is ItemRadioButtonBinding -> { + sources?.get(position)?.let { + holder.binding.radioButton.isChecked = selectSource == it + holder.binding.radioButton.text = it.bookSourceName + } + } + } + } + } + + override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { + when (holder.binding) { + is ItemCheckBoxBinding -> { + groups?.get(position)?.let { + holder.binding.checkBox.isChecked = selectGroups.contains(it) + holder.binding.checkBox.text = it + holder.binding.checkBox.setOnCheckedChangeListener { buttonView, isChecked -> + if (buttonView.isPressed) { + if (isChecked) { + selectGroups.add(it) + } else { + selectGroups.remove(it) + } + notifyItemRangeChanged(0, itemCount, "up") + } + } + } + } + is ItemRadioButtonBinding -> { + sources?.get(position)?.let { + holder.binding.radioButton.isChecked = selectSource == it + holder.binding.radioButton.text = it.bookSourceName + holder.binding.radioButton.setOnCheckedChangeListener { buttonView, isChecked -> + if (buttonView.isPressed) { + if (isChecked) { + selectSource = it + } + notifyItemRangeChanged(0, itemCount, "up") + } + } + } + } + } + } + + override fun getItemCount(): Int { + return if (binding.rbSource.isChecked) { + sources?.size ?: 0 + } else { + groups?.size ?: 0 + } + } + + } interface Callback { diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt index 1e04585e9..ad3899b0d 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt @@ -75,7 +75,6 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment startActivity() R.id.menu_search -> startActivity() R.id.menu_update_toc -> activityViewModel.upToc(books) diff --git a/app/src/main/res/layout/item_check_box.xml b/app/src/main/res/layout/item_check_box.xml new file mode 100644 index 000000000..af9a14b14 --- /dev/null +++ b/app/src/main/res/layout/item_check_box.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_radio_button.xml b/app/src/main/res/layout/item_radio_button.xml new file mode 100644 index 000000000..e681b4cf5 --- /dev/null +++ b/app/src/main/res/layout/item_radio_button.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file