Skip to content

Commit

Permalink
Require double confirmation for deleting a book
Browse files Browse the repository at this point in the history
The user must check a checkbox to enable the delete button. This tries
to avoid confirming irreversible actions by mistake.

Fixes #544
  • Loading branch information
rivaldi8 committed Sep 13, 2017
1 parent c161118 commit 410cc28
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
Expand Down Expand Up @@ -199,30 +201,8 @@ public boolean onMenuItemClick(MenuItem item) {
case R.id.ctx_menu_sync_book:
//TODO implement sync
return false;
case R.id.ctx_menu_delete_book: {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
dialogBuilder.setTitle(getString(R.string.title_confirm_delete_book))
.setIcon(R.drawable.ic_close_black_24dp)
.setMessage(getString(R.string.msg_all_book_data_will_be_deleted));
dialogBuilder.setPositiveButton(getString(R.string.btn_delete_book), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
BooksDbAdapter.getInstance().deleteBook(bookUID);
refresh();
}
});
dialogBuilder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = dialogBuilder.create();
dialog.show(); //must be called before you can access buttons
dialog.getButton(AlertDialog.BUTTON_POSITIVE)
.setTextColor(ContextCompat.getColor(context, R.color.account_red));
}
return true;
case R.id.ctx_menu_delete_book:
return handleMenuDeleteBook(bookUID);
default:
return true;
}
Expand All @@ -238,6 +218,38 @@ public void onClick(DialogInterface dialog, int which) {
});
}

private boolean handleMenuDeleteBook(final String bookUID) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
dialogBuilder.setTitle(getString(R.string.title_confirm_delete_book))
.setIcon(R.drawable.ic_close_black_24dp)
.setView(R.layout.dialog_double_confirm)
.setMessage(getString(R.string.msg_all_book_data_will_be_deleted));
dialogBuilder.setPositiveButton(getString(R.string.btn_delete_book), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
BooksDbAdapter.getInstance().deleteBook(bookUID);
refresh();
}
});
dialogBuilder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog dialog = dialogBuilder.create();
dialog.show(); //must be called before you can access buttons
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
CheckBox confirmCheckBox = dialog.findViewById(R.id.checkbox_confirm);
confirmCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(b);
}
});
return true;
}

/**
* Opens a dialog for renaming a book
* @param bookName Current name of the book
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/res/layout/dialog_double_confirm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2017 Ngewi Fet <ngewif@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/dialog_padding" >

<CheckBox
android:id="@+id/checkbox_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yes_sure"/>
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,5 @@
<string name="label_select_destination_after_export">Select the destination after export is complete</string>
<string name="label_dropbox_export_destination">Export to \'/Apps/GnuCash Android/\' folder on Dropbox</string>
<string name="title_section_preferences">Preferences</string>
<string name="yes_sure">Yes, I\'m sure</string>
</resources>

0 comments on commit 410cc28

Please sign in to comment.