From d6f5a8bd91a85a536251bf3b444568468a43f4cc Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Tue, 10 Jan 2023 13:49:13 +0600 Subject: [PATCH] 4824-s-Add_confirmation_dialog_on_event_save issue: https://gitlab.e.foundation/e/backlog/-/issues/4824 Show discard alert dialog onBackPress on EditEventFragment, instead of saving automatically onPause. --- res/values/strings.xml | 3 ++ .../calendar/event/EditEventActivity.java | 12 +++++- .../calendar/event/EditEventFragment.java | 40 ++++++++++++++----- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index b2c2e583c..e6aabdc3e 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -783,4 +783,7 @@ Offline Calendar + Do you want to discard changes? + Discard + Cancel diff --git a/src/com/android/calendar/event/EditEventActivity.java b/src/com/android/calendar/event/EditEventActivity.java index ef3d1ee2b..077aa961f 100644 --- a/src/com/android/calendar/event/EditEventActivity.java +++ b/src/com/android/calendar/event/EditEventActivity.java @@ -170,9 +170,19 @@ public class EditEventActivity extends AbstractCalendarActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { - Utils.returnToCalendarHome(this); + onBackPressed(); return true; } return super.onOptionsItemSelected(item); } + + @Override + public void onBackPressed() { + if (mEditFragment != null) { + mEditFragment.onBackPressed(); + return; + } + + super.onBackPressed(); + } } diff --git a/src/com/android/calendar/event/EditEventFragment.java b/src/com/android/calendar/event/EditEventFragment.java index 291c77838..eb60c443f 100644 --- a/src/com/android/calendar/event/EditEventFragment.java +++ b/src/com/android/calendar/event/EditEventFragment.java @@ -554,19 +554,37 @@ public class EditEventFragment extends Fragment implements EventHandler, OnColor return mModel.isEmpty(); } - @Override - public void onPause() { + public void onBackPressed() { + if (canSave()) { + showDiscardConfirmAlert(); + return; + } + + Utils.returnToCalendarHome(getActivity()); + } + + private boolean canSave() { Activity act = getActivity(); - if (mSaveOnDetach && act != null && !mIsReadOnly && !act.isChangingConfigurations() - && mView.prepareForSave()) { - mOnDone.setDoneCode(Utils.DONE_SAVE); + return mSaveOnDetach && act != null && !mIsReadOnly && !act.isChangingConfigurations() + && mView.prepareForSave(); + } + + private void showDiscardConfirmAlert() { + new AlertDialog.Builder(getActivity()) + .setMessage(R.string.discard_event_changes) + .setCancelable(true) + .setPositiveButton(R.string.discard, ((dialog, which) -> { + revertEventChanges(); + Utils.returnToCalendarHome(getActivity()); + dialog.cancel(); + })) + .setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.cancel())) + .show(); + } + + private void revertEventChanges() { + mOnDone.setDoneCode(Utils.DONE_REVERT); mOnDone.run(); - } - if (act !=null && (Build.VERSION.SDK_INT < 23 || - ContextCompat.checkSelfPermission(EditEventFragment.this.getActivity(), - Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED)) - act.finish(); - super.onPause(); } @Override -- GitLab