Skip to content

Commit c6518cb

Browse files
committed
Move shareable code from DeleteBookConfirmationDialog to a base class
We'll need other dialogs to have double confirmation.
1 parent 6c705d7 commit c6518cb

2 files changed

Lines changed: 112 additions & 43 deletions

File tree

app/src/main/java/org/gnucash/android/ui/settings/dialog/DeleteBookConfirmationDialog.java

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
import android.content.DialogInterface;
2121
import android.os.Bundle;
2222
import android.support.annotation.NonNull;
23-
import android.support.v4.app.DialogFragment;
24-
import android.support.v7.app.AlertDialog;
25-
import android.widget.CheckBox;
26-
import android.widget.CompoundButton;
2723

2824
import org.gnucash.android.R;
2925
import org.gnucash.android.db.adapter.BooksDbAdapter;
@@ -34,8 +30,8 @@
3430
*
3531
* @author Àlex Magaz <alexandre.magaz@gmail.com>
3632
*/
37-
public class DeleteBookConfirmationDialog extends DialogFragment {
38-
33+
public class DeleteBookConfirmationDialog extends DoubleConfirmationDialog {
34+
@NonNull
3935
public static DeleteBookConfirmationDialog newInstance(String bookUID) {
4036
DeleteBookConfirmationDialog frag = new DeleteBookConfirmationDialog();
4137
Bundle args = new Bundle();
@@ -47,43 +43,19 @@ public static DeleteBookConfirmationDialog newInstance(String bookUID) {
4743
@NonNull
4844
@Override
4945
public Dialog onCreateDialog(Bundle savedInstanceState) {
50-
final String bookUID = getArguments().getString("bookUID");
51-
52-
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
53-
dialogBuilder.setTitle(getString(R.string.title_confirm_delete_book))
46+
return getDialogBuilder()
47+
.setTitle(R.string.title_confirm_delete_book)
5448
.setIcon(R.drawable.ic_close_black_24dp)
55-
.setView(R.layout.dialog_double_confirm)
56-
.setMessage(getString(R.string.msg_all_book_data_will_be_deleted));
57-
dialogBuilder.setPositiveButton(getString(R.string.btn_delete_book), new DialogInterface.OnClickListener() {
58-
@Override
59-
public void onClick(DialogInterface dialog, int which) {
60-
BooksDbAdapter.getInstance().deleteBook(bookUID);
61-
((Refreshable) getTargetFragment()).refresh();
62-
}
63-
});
64-
dialogBuilder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
65-
@Override
66-
public void onClick(DialogInterface dialog, int which) {
67-
dialog.dismiss();
68-
}
69-
});
70-
71-
return dialogBuilder.create();
72-
}
73-
74-
@Override
75-
public void onStart() {
76-
super.onStart();
77-
final AlertDialog dialog = (AlertDialog) getDialog();
78-
if (dialog != null) {
79-
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
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-
}
49+
.setMessage(R.string.msg_all_book_data_will_be_deleted)
50+
.setPositiveButton(R.string.btn_delete_book, new DialogInterface.OnClickListener() {
51+
@SuppressWarnings("ConstantConditions")
52+
@Override
53+
public void onClick(DialogInterface dialogInterface, int which) {
54+
final String bookUID = getArguments().getString("bookUID");
55+
BooksDbAdapter.getInstance().deleteBook(bookUID);
56+
((Refreshable) getTargetFragment()).refresh();
57+
}
58+
})
59+
.create();
8860
}
8961
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)