Skip to content

Commit 410cc28

Browse files
committed
Require double confirmation for deleting a book
The user must check a checkbox to enable the delete button. This tries to avoid confirming irreversible actions by mistake. Fixes #544
1 parent c161118 commit 410cc28

3 files changed

Lines changed: 66 additions & 24 deletions

File tree

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

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
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;
4143
import android.widget.EditText;
4244
import android.widget.ImageView;
4345
import android.widget.ListView;
@@ -199,30 +201,8 @@ public boolean onMenuItemClick(MenuItem item) {
199201
case R.id.ctx_menu_sync_book:
200202
//TODO implement sync
201203
return false;
202-
case R.id.ctx_menu_delete_book: {
203-
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
204-
dialogBuilder.setTitle(getString(R.string.title_confirm_delete_book))
205-
.setIcon(R.drawable.ic_close_black_24dp)
206-
.setMessage(getString(R.string.msg_all_book_data_will_be_deleted));
207-
dialogBuilder.setPositiveButton(getString(R.string.btn_delete_book), new DialogInterface.OnClickListener() {
208-
@Override
209-
public void onClick(DialogInterface dialog, int which) {
210-
BooksDbAdapter.getInstance().deleteBook(bookUID);
211-
refresh();
212-
}
213-
});
214-
dialogBuilder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
215-
@Override
216-
public void onClick(DialogInterface dialog, int which) {
217-
dialog.dismiss();
218-
}
219-
});
220-
AlertDialog dialog = dialogBuilder.create();
221-
dialog.show(); //must be called before you can access buttons
222-
dialog.getButton(AlertDialog.BUTTON_POSITIVE)
223-
.setTextColor(ContextCompat.getColor(context, R.color.account_red));
224-
}
225-
return true;
204+
case R.id.ctx_menu_delete_book:
205+
return handleMenuDeleteBook(bookUID);
226206
default:
227207
return true;
228208
}
@@ -238,6 +218,38 @@ public void onClick(DialogInterface dialog, int which) {
238218
});
239219
}
240220

221+
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+
});
250+
return true;
251+
}
252+
241253
/**
242254
* Opens a dialog for renaming a book
243255
* @param bookName Current name of the book
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (c) 2017 Ngewi Fet <ngewif@gmail.com>
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:tools="http://schemas.android.com/tools"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:orientation="vertical"
22+
android:padding="@dimen/dialog_padding" >
23+
24+
<CheckBox
25+
android:id="@+id/checkbox_confirm"
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:text="@string/yes_sure"/>
29+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,5 @@
462462
<string name="label_select_destination_after_export">Select the destination after export is complete</string>
463463
<string name="label_dropbox_export_destination">Export to \'/Apps/GnuCash Android/\' folder on Dropbox</string>
464464
<string name="title_section_preferences">Preferences</string>
465+
<string name="yes_sure">Yes, I\'m sure</string>
465466
</resources>

0 commit comments

Comments
 (0)