Skip to content

Commit 6c705d7

Browse files
committed
Use a DialogFragment to confirm the deletion of a book
Showing an AlertDialog directly from a fragment is discouraged. It doesn't behave properly, for example, when rotating the screen. See https://developer.android.com/guide/topics/ui/dialogs.html
1 parent 410cc28 commit 6c705d7

2 files changed

Lines changed: 93 additions & 30 deletions

File tree

app/src/main/java/org/gnucash/android/ui/settings/BookManagerFragment.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
import android.view.MenuItem;
3939
import android.view.View;
4040
import android.view.ViewGroup;
41-
import android.widget.CheckBox;
42-
import android.widget.CompoundButton;
4341
import android.widget.EditText;
4442
import android.widget.ImageView;
4543
import android.widget.ListView;
@@ -56,6 +54,7 @@
5654
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
5755
import org.gnucash.android.ui.account.AccountsActivity;
5856
import org.gnucash.android.ui.common.Refreshable;
57+
import org.gnucash.android.ui.settings.dialog.DeleteBookConfirmationDialog;
5958
import org.gnucash.android.util.BookUtils;
6059
import org.gnucash.android.util.PreferencesHelper;
6160

@@ -219,34 +218,9 @@ public boolean onMenuItemClick(MenuItem item) {
219218
}
220219

221220
private boolean handleMenuDeleteBook(final String bookUID) {
222-
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
223-
dialogBuilder.setTitle(getString(R.string.title_confirm_delete_book))
224-
.setIcon(R.drawable.ic_close_black_24dp)
225-
.setView(R.layout.dialog_double_confirm)
226-
.setMessage(getString(R.string.msg_all_book_data_will_be_deleted));
227-
dialogBuilder.setPositiveButton(getString(R.string.btn_delete_book), new DialogInterface.OnClickListener() {
228-
@Override
229-
public void onClick(DialogInterface dialog, int which) {
230-
BooksDbAdapter.getInstance().deleteBook(bookUID);
231-
refresh();
232-
}
233-
});
234-
dialogBuilder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
235-
@Override
236-
public void onClick(DialogInterface dialog, int which) {
237-
dialog.dismiss();
238-
}
239-
});
240-
final AlertDialog dialog = dialogBuilder.create();
241-
dialog.show(); //must be called before you can access buttons
242-
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
243-
CheckBox confirmCheckBox = dialog.findViewById(R.id.checkbox_confirm);
244-
confirmCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
245-
@Override
246-
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
247-
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(b);
248-
}
249-
});
221+
DeleteBookConfirmationDialog dialog = DeleteBookConfirmationDialog.newInstance(bookUID);
222+
dialog.show(getFragmentManager(), "delete_book");
223+
dialog.setTargetFragment(BookManagerFragment.this, 0);
250224
return true;
251225
}
252226

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.app.Dialog;
20+
import android.content.DialogInterface;
21+
import android.os.Bundle;
22+
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;
27+
28+
import org.gnucash.android.R;
29+
import org.gnucash.android.db.adapter.BooksDbAdapter;
30+
import org.gnucash.android.ui.common.Refreshable;
31+
32+
/**
33+
* Confirmation dialog for deleting a book.
34+
*
35+
* @author Àlex Magaz <alexandre.magaz@gmail.com>
36+
*/
37+
public class DeleteBookConfirmationDialog extends DialogFragment {
38+
39+
public static DeleteBookConfirmationDialog newInstance(String bookUID) {
40+
DeleteBookConfirmationDialog frag = new DeleteBookConfirmationDialog();
41+
Bundle args = new Bundle();
42+
args.putString("bookUID", bookUID);
43+
frag.setArguments(args);
44+
return frag;
45+
}
46+
47+
@NonNull
48+
@Override
49+
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))
54+
.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+
}
88+
}
89+
}

0 commit comments

Comments
 (0)