|
@@ -1,14 +1,9 @@
|
|
|
package eu.kanade.tachiyomi.widget.materialdialogs
|
|
|
|
|
|
-import android.view.View
|
|
|
+import android.view.LayoutInflater
|
|
|
import android.view.ViewGroup
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
-import com.afollestad.materialdialogs.MaterialDialog
|
|
|
-import com.afollestad.materialdialogs.internal.list.DialogAdapter
|
|
|
-import com.afollestad.materialdialogs.list.getItemSelector
|
|
|
-import com.afollestad.materialdialogs.utils.MDUtil.inflate
|
|
|
-import com.afollestad.materialdialogs.utils.MDUtil.maybeSetTextColor
|
|
|
-import eu.kanade.tachiyomi.R
|
|
|
+import eu.kanade.tachiyomi.databinding.DialogQuadstatemultichoiceItemBinding
|
|
|
|
|
|
private object CheckPayload
|
|
|
private object InverseCheckPayload
|
|
@@ -17,15 +12,13 @@ private object UncheckPayload
|
|
|
typealias QuadStateMultiChoiceListener = (indices: IntArray) -> Unit
|
|
|
|
|
|
internal class QuadStateMultiChoiceDialogAdapter(
|
|
|
- private var dialog: MaterialDialog,
|
|
|
internal var items: List<CharSequence>,
|
|
|
disabledItems: IntArray?,
|
|
|
initialSelected: IntArray,
|
|
|
- internal var selection: QuadStateMultiChoiceListener
|
|
|
-) : RecyclerView.Adapter<QuadStateMultiChoiceViewHolder>(),
|
|
|
- DialogAdapter<CharSequence, QuadStateMultiChoiceListener> {
|
|
|
+ internal var listener: QuadStateMultiChoiceListener
|
|
|
+) : RecyclerView.Adapter<QuadStateMultiChoiceViewHolder>() {
|
|
|
|
|
|
- private val states = QuadStateCheckBox.State.values()
|
|
|
+ private val states = QuadStateTextView.State.values()
|
|
|
|
|
|
private var currentSelection: IntArray = initialSelected
|
|
|
set(value) {
|
|
@@ -34,15 +27,15 @@ internal class QuadStateMultiChoiceDialogAdapter(
|
|
|
previousSelection.forEachIndexed { index, previous ->
|
|
|
val current = value[index]
|
|
|
when {
|
|
|
- current == QuadStateCheckBox.State.CHECKED.ordinal && previous != QuadStateCheckBox.State.CHECKED.ordinal -> {
|
|
|
+ current == QuadStateTextView.State.CHECKED.ordinal && previous != QuadStateTextView.State.CHECKED.ordinal -> {
|
|
|
// This value was selected
|
|
|
notifyItemChanged(index, CheckPayload)
|
|
|
}
|
|
|
- current == QuadStateCheckBox.State.INVERSED.ordinal && previous != QuadStateCheckBox.State.INVERSED.ordinal -> {
|
|
|
+ current == QuadStateTextView.State.INVERSED.ordinal && previous != QuadStateTextView.State.INVERSED.ordinal -> {
|
|
|
// This value was inverse selected
|
|
|
notifyItemChanged(index, InverseCheckPayload)
|
|
|
}
|
|
|
- current == QuadStateCheckBox.State.UNCHECKED.ordinal && previous != QuadStateCheckBox.State.UNCHECKED.ordinal -> {
|
|
|
+ current == QuadStateTextView.State.UNCHECKED.ordinal && previous != QuadStateTextView.State.UNCHECKED.ordinal -> {
|
|
|
// This value was unselected
|
|
|
notifyItemChanged(index, UncheckPayload)
|
|
|
}
|
|
@@ -54,26 +47,24 @@ internal class QuadStateMultiChoiceDialogAdapter(
|
|
|
internal fun itemClicked(index: Int) {
|
|
|
val newSelection = this.currentSelection.toMutableList()
|
|
|
newSelection[index] = when (currentSelection[index]) {
|
|
|
- QuadStateCheckBox.State.CHECKED.ordinal -> QuadStateCheckBox.State.INVERSED.ordinal
|
|
|
- QuadStateCheckBox.State.INVERSED.ordinal -> QuadStateCheckBox.State.UNCHECKED.ordinal
|
|
|
+ QuadStateTextView.State.CHECKED.ordinal -> QuadStateTextView.State.INVERSED.ordinal
|
|
|
+ QuadStateTextView.State.INVERSED.ordinal -> QuadStateTextView.State.UNCHECKED.ordinal
|
|
|
// INDETERMINATE or UNCHECKED
|
|
|
- else -> QuadStateCheckBox.State.CHECKED.ordinal
|
|
|
+ else -> QuadStateTextView.State.CHECKED.ordinal
|
|
|
}
|
|
|
this.currentSelection = newSelection.toIntArray()
|
|
|
+ listener(currentSelection)
|
|
|
}
|
|
|
|
|
|
override fun onCreateViewHolder(
|
|
|
parent: ViewGroup,
|
|
|
viewType: Int
|
|
|
): QuadStateMultiChoiceViewHolder {
|
|
|
- val listItemView: View = parent.inflate(dialog.windowContext, R.layout.md_listitem_quadstatemultichoice)
|
|
|
- val viewHolder = QuadStateMultiChoiceViewHolder(
|
|
|
- itemView = listItemView,
|
|
|
+ return QuadStateMultiChoiceViewHolder(
|
|
|
+ itemBinding = DialogQuadstatemultichoiceItemBinding
|
|
|
+ .inflate(LayoutInflater.from(parent.context), parent, false),
|
|
|
adapter = this
|
|
|
)
|
|
|
- viewHolder.titleView.maybeSetTextColor(dialog.windowContext, R.attr.md_color_content)
|
|
|
-
|
|
|
- return viewHolder
|
|
|
}
|
|
|
|
|
|
override fun getItemCount() = items.size
|
|
@@ -83,14 +74,8 @@ internal class QuadStateMultiChoiceDialogAdapter(
|
|
|
position: Int
|
|
|
) {
|
|
|
holder.isEnabled = !disabledIndices.contains(position)
|
|
|
-
|
|
|
holder.controlView.state = states[currentSelection[position]]
|
|
|
- holder.titleView.text = items[position]
|
|
|
- holder.itemView.background = dialog.getItemSelector()
|
|
|
-
|
|
|
- if (dialog.bodyFont != null) {
|
|
|
- holder.titleView.typeface = dialog.bodyFont
|
|
|
- }
|
|
|
+ holder.controlView.text = items[position]
|
|
|
}
|
|
|
|
|
|
override fun onBindViewHolder(
|
|
@@ -100,88 +85,18 @@ internal class QuadStateMultiChoiceDialogAdapter(
|
|
|
) {
|
|
|
when (payloads.firstOrNull()) {
|
|
|
CheckPayload -> {
|
|
|
- holder.controlView.state = QuadStateCheckBox.State.CHECKED
|
|
|
+ holder.controlView.state = QuadStateTextView.State.CHECKED
|
|
|
return
|
|
|
}
|
|
|
InverseCheckPayload -> {
|
|
|
- holder.controlView.state = QuadStateCheckBox.State.INVERSED
|
|
|
+ holder.controlView.state = QuadStateTextView.State.INVERSED
|
|
|
return
|
|
|
}
|
|
|
UncheckPayload -> {
|
|
|
- holder.controlView.state = QuadStateCheckBox.State.UNCHECKED
|
|
|
+ holder.controlView.state = QuadStateTextView.State.UNCHECKED
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
super.onBindViewHolder(holder, position, payloads)
|
|
|
}
|
|
|
-
|
|
|
- override fun positiveButtonClicked() {
|
|
|
- selection.invoke(currentSelection)
|
|
|
- }
|
|
|
-
|
|
|
- override fun replaceItems(
|
|
|
- items: List<CharSequence>,
|
|
|
- listener: QuadStateMultiChoiceListener?
|
|
|
- ) {
|
|
|
- this.items = items
|
|
|
- if (listener != null) {
|
|
|
- this.selection = listener
|
|
|
- }
|
|
|
- this.notifyDataSetChanged()
|
|
|
- }
|
|
|
-
|
|
|
- override fun disableItems(indices: IntArray) {
|
|
|
- this.disabledIndices = indices
|
|
|
- notifyDataSetChanged()
|
|
|
- }
|
|
|
-
|
|
|
- override fun checkItems(indices: IntArray) {
|
|
|
- val newSelection = this.currentSelection.toMutableList()
|
|
|
- for (index in indices) {
|
|
|
- newSelection[index] = QuadStateCheckBox.State.CHECKED.ordinal
|
|
|
- }
|
|
|
- this.currentSelection = newSelection.toIntArray()
|
|
|
- }
|
|
|
-
|
|
|
- override fun uncheckItems(indices: IntArray) {
|
|
|
- val newSelection = this.currentSelection.toMutableList()
|
|
|
- for (index in indices) {
|
|
|
- newSelection[index] = QuadStateCheckBox.State.UNCHECKED.ordinal
|
|
|
- }
|
|
|
- this.currentSelection = newSelection.toIntArray()
|
|
|
- }
|
|
|
-
|
|
|
- override fun toggleItems(indices: IntArray) {
|
|
|
- val newSelection = this.currentSelection.toMutableList()
|
|
|
- for (index in indices) {
|
|
|
- if (this.disabledIndices.contains(index)) {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- if (this.currentSelection[index] != QuadStateCheckBox.State.CHECKED.ordinal) {
|
|
|
- newSelection[index] = QuadStateCheckBox.State.CHECKED.ordinal
|
|
|
- } else {
|
|
|
- newSelection[index] = QuadStateCheckBox.State.UNCHECKED.ordinal
|
|
|
- }
|
|
|
- }
|
|
|
- this.currentSelection = newSelection.toIntArray()
|
|
|
- }
|
|
|
-
|
|
|
- override fun checkAllItems() {
|
|
|
- this.currentSelection = IntArray(itemCount) { QuadStateCheckBox.State.CHECKED.ordinal }
|
|
|
- }
|
|
|
-
|
|
|
- override fun uncheckAllItems() {
|
|
|
- this.currentSelection = IntArray(itemCount) { QuadStateCheckBox.State.UNCHECKED.ordinal }
|
|
|
- }
|
|
|
-
|
|
|
- override fun toggleAllChecked() {
|
|
|
- if (this.currentSelection.any { it != QuadStateCheckBox.State.CHECKED.ordinal }) {
|
|
|
- checkAllItems()
|
|
|
- } else {
|
|
|
- uncheckAllItems()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- override fun isItemChecked(index: Int) = this.currentSelection[index] == QuadStateCheckBox.State.CHECKED.ordinal
|
|
|
}
|