Browse Source

ButtonCallback is now deprecated in favor of single callbacks.

Aidan Follestad 9 years ago
parent
commit
5b53a8903f

+ 29 - 37
README.md

@@ -230,46 +230,40 @@ new MaterialDialog.Builder(this)
 
 # Callbacks
 
-To know when the user selects an action button, you set a callback. To do this, use the `ButtonCallback`
-class and override its `onPositive()`, `onNegative()`, or `onNeutral()` methods as needed. The advantage
-to this is that you can override button functionality *À la carte*, so no need to stub empty methods.
+To know when the user selects an action button, you set callbacks:
 
 ```java
 new MaterialDialog.Builder(this)
-        .callback(new MaterialDialog.ButtonCallback() {
-            @Override
-            public void onPositive(MaterialDialog dialog) {
-            }
-        });
-
-new MaterialDialog.Builder(this)
-        .callback(new MaterialDialog.ButtonCallback() {
-            @Override
-            public void onPositive(MaterialDialog dialog) {
-            }
-
-            @Override
-            public void onNegative(MaterialDialog dialog) {
-            }
-        });
-
-new MaterialDialog.Builder(this)
-        .callback(new MaterialDialog.ButtonCallback() {
-            @Override
-            public void onPositive(MaterialDialog dialog) {
-            }
-
-            @Override
-            public void onNegative(MaterialDialog dialog) {
-            }
-
-            @Override
-            public void onNeutral(MaterialDialog dialog) {
-            }
-        });
+    .onPositive(new MaterialDialog.SingleButtonCallback() {
+        @Override
+        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
+            // TODO
+        }
+    })
+    .onPositive(new MaterialDialog.SingleButtonCallback() {
+        @Override
+        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
+            // TODO
+        }
+    })
+    .onPositive(new MaterialDialog.SingleButtonCallback() {
+        @Override
+        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
+            // TODO
+        }
+    })
+    .onAny(new MaterialDialog.SingleButtonCallback() {
+        @Override
+        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
+            // TODO
+        }
+    });
 ```
 
-Alternatively, you can use the `onPositive()`, `onNegative()`, `onNetural()` and `onAny()` methods on the builder, which each take a `SingleButtonCallback`. This is especially useful if you use [retrolambda](https://github.com/orfjackal/retrolambda) in your project, as these methods are compatible with lambda arguments.
+If you are listening for all three action buttons, you could just use `onAny()`. The `which` (`DialogAction`)
+ parameter will tell you which button was pressed.
+
+These methods are compatible with lambda arguments.
 
 ```java
 new MaterialDialog.Builder(this)
@@ -279,8 +273,6 @@ new MaterialDialog.Builder(this)
         .onAny(dialog -> {});
 ```
 
-Both forms of callbacks may be defined, in which case both will be called when applicable. The `ButtonCallback` methods will be called before the `SingleButtonCallback` methods.
-
 If `autoDismiss` is turned off, then you must manually dismiss the dialog in these callbacks. Auto dismiss is on by default.
 
 ---

+ 12 - 6
core/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -343,7 +343,7 @@ public class MaterialDialog extends DialogBase implements
                     mBuilder.callback.onPositive(this);
                 }
                 if (mBuilder.onPositiveCallback != null)
-                    mBuilder.onPositiveCallback.onClick(this, DialogAction.POSITIVE);
+                    mBuilder.onPositiveCallback.onClick(this, tag);
                 if (mBuilder.listCallbackSingleChoice != null)
                     sendSingleChoiceCallback(v);
                 if (mBuilder.listCallbackMultiChoice != null)
@@ -359,7 +359,7 @@ public class MaterialDialog extends DialogBase implements
                     mBuilder.callback.onNegative(this);
                 }
                 if (mBuilder.onNegativeCallback != null)
-                    mBuilder.onNegativeCallback.onClick(this, DialogAction.NEGATIVE);
+                    mBuilder.onNegativeCallback.onClick(this, tag);
                 if (mBuilder.autoDismiss) dismiss();
                 break;
             }
@@ -369,14 +369,13 @@ public class MaterialDialog extends DialogBase implements
                     mBuilder.callback.onNeutral(this);
                 }
                 if (mBuilder.onNeutralCallback != null)
-                    mBuilder.onNeutralCallback.onClick(this, DialogAction.NEUTRAL);
+                    mBuilder.onNeutralCallback.onClick(this, tag);
                 if (mBuilder.autoDismiss) dismiss();
                 break;
             }
         }
-
         if (mBuilder.onAnyCallback != null)
-            mBuilder.onAnyCallback.onClick(this, null);
+            mBuilder.onAnyCallback.onClick(this, tag);
     }
 
     /**
@@ -1790,18 +1789,25 @@ public class MaterialDialog extends DialogBase implements
 
     /**
      * Override these as needed, so no needing to sub empty methods from an interface
+     *
+     * @deprecated Use the individual onPositive, onNegative, onNeutral, or onAny Builder methods instead.
      */
+    @Deprecated
     public static abstract class ButtonCallback {
 
+        @Deprecated
         public void onAny(MaterialDialog dialog) {
         }
 
+        @Deprecated
         public void onPositive(MaterialDialog dialog) {
         }
 
+        @Deprecated
         public void onNegative(MaterialDialog dialog) {
         }
 
+        @Deprecated
         public void onNeutral(MaterialDialog dialog) {
         }
 
@@ -1842,7 +1848,7 @@ public class MaterialDialog extends DialogBase implements
      */
     public interface SingleButtonCallback {
 
-        void onClick(@NonNull MaterialDialog dialog, @Nullable DialogAction which);
+        void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which);
     }
 
     public interface InputCallback {

+ 7 - 27
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -114,12 +114,6 @@ public class MainActivity extends AppCompatActivity implements
                 .content(R.string.shareLocationPrompt)
                 .positiveText(R.string.agree)
                 .negativeText(R.string.disagree)
-                .onPositive(new MaterialDialog.SingleButtonCallback() {
-                    @Override
-                    public void onClick(MaterialDialog dialog) {
-
-                    }
-                })
                 .show();
     }
 
@@ -186,20 +180,10 @@ public class MainActivity extends AppCompatActivity implements
                 .positiveText(R.string.agree)
                 .negativeText(R.string.disagree)
                 .neutralText(R.string.more_info)
-                .callback(new MaterialDialog.ButtonCallback() {
-                    @Override
-                    public void onPositive(MaterialDialog dialog) {
-                        showToast("Positive!");
-                    }
-
+                .onAny(new MaterialDialog.SingleButtonCallback() {
                     @Override
-                    public void onNeutral(MaterialDialog dialog) {
-                        showToast("Neutral");
-                    }
-
-                    @Override
-                    public void onNegative(MaterialDialog dialog) {
-                        showToast("Negative…");
+                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
+                        showToast(which.name() + "!");
                     }
                 })
                 .show();
@@ -282,9 +266,9 @@ public class MainActivity extends AppCompatActivity implements
                         return true; // allow selection
                     }
                 })
-                .callback(new MaterialDialog.ButtonCallback() {
+                .onNeutral(new MaterialDialog.SingleButtonCallback() {
                     @Override
-                    public void onNeutral(MaterialDialog dialog) {
+                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                         dialog.clearSelectedIndices();
                     }
                 })
@@ -364,15 +348,11 @@ public class MainActivity extends AppCompatActivity implements
                 .customView(R.layout.dialog_customview, true)
                 .positiveText(R.string.connect)
                 .negativeText(android.R.string.cancel)
-                .callback(new MaterialDialog.ButtonCallback() {
+                .onPositive(new MaterialDialog.SingleButtonCallback() {
                     @Override
-                    public void onPositive(MaterialDialog dialog) {
+                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                         showToast("Password: " + passwordInput.getText().toString());
                     }
-
-                    @Override
-                    public void onNegative(MaterialDialog dialog) {
-                    }
                 }).build();
 
         positiveAction = dialog.getActionButton(DialogAction.POSITIVE);