From 8fce558bea72d5c67ade2aa741f4f31b53c18848 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. --- .../calendar/event/EditEventActivity.java | 12 +++++- .../calendar/event/EditEventFragment.java | 40 ++++++++++++++----- app/src/main/res/values/strings.xml | 3 ++ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/android/calendar/event/EditEventActivity.java b/app/src/main/java/com/android/calendar/event/EditEventActivity.java index 7acd98a4e..9fbadc85b 100644 --- a/app/src/main/java/com/android/calendar/event/EditEventActivity.java +++ b/app/src/main/java/com/android/calendar/event/EditEventActivity.java @@ -171,9 +171,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/app/src/main/java/com/android/calendar/event/EditEventFragment.java b/app/src/main/java/com/android/calendar/event/EditEventFragment.java index 1b8847bd4..65634597b 100644 --- a/app/src/main/java/com/android/calendar/event/EditEventFragment.java +++ b/app/src/main/java/com/android/calendar/event/EditEventFragment.java @@ -555,19 +555,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 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a978701ba..96ee1a17c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -801,4 +801,7 @@ light + Do you want to discard changes? + Discard + Cancel -- GitLab