|
| 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