|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 Àlex Magaz Graça <alexandre.magaz@gmail.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.gnucash.android.ui.settings.dialog; |
| 18 | + |
| 19 | +import android.content.DialogInterface; |
| 20 | +import android.support.annotation.NonNull; |
| 21 | +import android.support.v4.app.DialogFragment; |
| 22 | +import android.support.v7.app.AlertDialog; |
| 23 | +import android.widget.CheckBox; |
| 24 | +import android.widget.CompoundButton; |
| 25 | + |
| 26 | +import org.gnucash.android.R; |
| 27 | + |
| 28 | +/** |
| 29 | + * Confirmation dialog with additional checkbox to confirm the action. |
| 30 | + * |
| 31 | + * <p>It's meant to avoid the user confirming irreversible actions by |
| 32 | + * mistake. The positive button to confirm the action is only enabled |
| 33 | + * when the checkbox is checked.</p> |
| 34 | + * |
| 35 | + * <p>Extend this class and override onCreateDialog to finish setting |
| 36 | + * up the dialog. See getDialogBuilder().</p> |
| 37 | + * |
| 38 | + * @author Àlex Magaz <alexandre.magaz@gmail.com> |
| 39 | + */ |
| 40 | +public abstract class DoubleConfirmationDialog extends DialogFragment { |
| 41 | + /** |
| 42 | + * Returns the dialog builder with the defaults for a double confirmation |
| 43 | + * dialog already set up. |
| 44 | + * |
| 45 | + * <p>Call it from onCreateDialog to finish setting up the dialog. |
| 46 | + * At least the following should be set:</p> |
| 47 | + * |
| 48 | + * <ul> |
| 49 | + * <li>The title.</li> |
| 50 | + * <li>The positive button.</li> |
| 51 | + * </ul> |
| 52 | + * |
| 53 | + * @return AlertDialog.Builder with the defaults for a double confirmation |
| 54 | + * dialog already set up. |
| 55 | + */ |
| 56 | + @NonNull |
| 57 | + protected AlertDialog.Builder getDialogBuilder() { |
| 58 | + return new AlertDialog.Builder(getActivity()) |
| 59 | + .setView(R.layout.dialog_double_confirm) |
| 60 | + .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() { |
| 61 | + @Override |
| 62 | + public void onClick(DialogInterface dialog, int which) { |
| 63 | + onNegativeButton(); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onStart() { |
| 70 | + super.onStart(); |
| 71 | + if (getDialog() != null) { |
| 72 | + ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); |
| 73 | + setUpConfirmCheckBox(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @SuppressWarnings("ConstantConditions") |
| 78 | + private void setUpConfirmCheckBox() { |
| 79 | + final AlertDialog dialog = (AlertDialog) getDialog(); |
| 80 | + CheckBox confirmCheckBox = dialog.findViewById(R.id.checkbox_confirm); |
| 81 | + confirmCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { |
| 82 | + @Override |
| 83 | + public void onCheckedChanged(CompoundButton compoundButton, boolean b) { |
| 84 | + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(b); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Called when the negative button is pressed. |
| 91 | + * |
| 92 | + * <p>By default it just dismisses the dialog.</p> |
| 93 | + */ |
| 94 | + protected void onNegativeButton() { |
| 95 | + getDialog().dismiss(); |
| 96 | + } |
| 97 | +} |
0 commit comments