Browse Source

Check items fix (#1771)

aiqency 6 years ago
parent
commit
c86ebb91d0

+ 15 - 2
core/src/main/java/com/afollestad/materialdialogs/internal/list/MultiChoiceDialogAdapter.kt

@@ -182,12 +182,19 @@ internal class MultiChoiceDialogAdapter(
     val existingSelection = this.currentSelection
     val indicesToAdd = indices.filter { !existingSelection.contains(it) }
     this.currentSelection = this.currentSelection.appendAll(indicesToAdd)
+    if (existingSelection.isEmpty()) {
+      dialog.setActionButtonEnabled(POSITIVE, true)
+    }
   }
 
   override fun uncheckItems(indices: IntArray) {
     val existingSelection = this.currentSelection
     val indicesToAdd = indices.filter { existingSelection.contains(it) }
-    this.currentSelection = this.currentSelection.removeAll(indicesToAdd)
+    this.currentSelection = this.currentSelection.removeAll(indicesToAdd).also {
+      if (it.isEmpty()) {
+        dialog.setActionButtonEnabled(POSITIVE, allowEmptySelection)
+      }
+    }
   }
 
   override fun toggleItems(indices: IntArray) {
@@ -200,7 +207,9 @@ internal class MultiChoiceDialogAdapter(
         newSelection.add(target)
       }
     }
-    this.currentSelection = newSelection.toIntArray()
+    this.currentSelection = newSelection.toIntArray().also {
+      dialog.setActionButtonEnabled(POSITIVE, if (it.isEmpty()) allowEmptySelection else true)
+    }
   }
 
   override fun checkAllItems() {
@@ -208,10 +217,14 @@ internal class MultiChoiceDialogAdapter(
     val wholeRange = IntArray(itemCount) { it }
     val indicesToAdd = wholeRange.filter { !existingSelection.contains(it) }
     this.currentSelection = this.currentSelection.appendAll(indicesToAdd)
+    if (existingSelection.isEmpty()) {
+      dialog.setActionButtonEnabled(POSITIVE, true)
+    }
   }
 
   override fun uncheckAllItems() {
     this.currentSelection = intArrayOf()
+    dialog.setActionButtonEnabled(POSITIVE, allowEmptySelection)
   }
 
   override fun toggleAllChecked() {