From 6ba4068a28860111f4ca7be886128744ada8dd32 Mon Sep 17 00:00:00 2001 From: PhilippC Date: Sun, 17 Mar 2013 06:25:53 +0100 Subject: [PATCH] ask for comfirmation when leaving edit entry with back buttom after making changes --- src/keepass2android/EntryEditActivity.cs | 48 +++++++++++++++++-- .../Resources/Resource.designer.cs | 6 +++ .../Resources/values/strings.xml | 2 + src/keepass2android/views/EntryEditSection.cs | 13 ++++- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/keepass2android/EntryEditActivity.cs b/src/keepass2android/EntryEditActivity.cs index 4a738cf3..8e498596 100644 --- a/src/keepass2android/EntryEditActivity.cs +++ b/src/keepass2android/EntryEditActivity.cs @@ -52,6 +52,8 @@ namespace keepass2android private PwIcon mSelectedIconID; private PwUuid mSelectedCustomIconID = PwUuid.Zero; private bool mSelectedIcon = false; + + bool mEntryModified; public static void Launch(Activity act, PwEntry pw) { Intent i = new Intent(act, typeof(EntryEditActivity)); @@ -145,6 +147,7 @@ namespace keepass2android } }*/ mIsNew = true; + mEntryModified = true; } else { @@ -356,7 +359,9 @@ namespace keepass2android ees.setData("", new ProtectedString(false, "")); ees.getDeleteButton().Click += (senderEes, eEes) => deleteAdvancedString((View)senderEes); container.AddView(ees); - + + mEntryModified = true; + // Scroll bottom scroll.Post(() => { scroll.FullScroll(FocusSearchDirection.Down); @@ -374,7 +379,7 @@ namespace keepass2android mEntry.ExpiryTime = DateTime.Now; } updateExpires(); - + mEntryModified = true; }; } @@ -446,8 +451,39 @@ namespace keepass2android { Toast.MakeText(this, GetString(Resource.String.AttachFailed)+" "+exAttach.Message, ToastLength.Long).Show(); } + mEntryModified = true; populateBinaries(); } + + public override void OnBackPressed() + { + if (mEntryModified == false) + { + base.OnBackPressed(); + } else + { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.SetTitle(GetString(Resource.String.AskDiscardChanges_title)); + + builder.SetMessage(GetString(Resource.String.AskDiscardChanges)); + + builder.SetPositiveButton(GetString(Android.Resource.String.Yes), new EventHandler((dlgSender, dlgEvt) => + { + Finish(); + + })); + + builder.SetNegativeButton(GetString(Android.Resource.String.No), new EventHandler((dlgSender, dlgEvt) => + { + + })); + + + Dialog dialog = builder.Create(); + dialog.Show(); + } + + } protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { @@ -463,6 +499,7 @@ namespace keepass2android ImageButton currIconButton = (ImageButton) FindViewById(Resource.Id.icon_button); //TODO: custom image currIconButton.SetImageResource(Icons.iconToResId(mSelectedIconID)); + mEntryModified = true; break; case RESULT_OK_PASSWORD_GENERATOR: @@ -472,7 +509,7 @@ namespace keepass2android password.Text = generatedPassword; confPassword.Text = generatedPassword; - + mEntryModified = true; break; case (int)Result.Ok: if (requestCode == Intents.REQUEST_CODE_FILE_BROWSE) @@ -511,6 +548,7 @@ namespace keepass2android binaryButton.SetCompoundDrawablesWithIntrinsicBounds( Resources.GetDrawable(Android.Resource.Drawable.IcMenuDelete),null, null, null); binaryButton.Click += (object sender, EventArgs e) => { + mEntryModified = true; Button btnSender = (Button)(sender); mEntry.Binaries.Remove(key); populateBinaries(); @@ -632,6 +670,7 @@ namespace keepass2android if (!PwDefs.IsStandardField(key)) { EntryEditSection ees = (EntryEditSection) LayoutInflater.Inflate(Resource.Layout.entry_edit_section, null); ees.setData(key, pair.Value); + ees.ContentChanged += (sender, e) => {mEntryModified=true;}; ees.getDeleteButton().Click += (sender, e) => deleteAdvancedString((View)sender); container.AddView(ees); } @@ -652,7 +691,7 @@ namespace keepass2android public void deleteAdvancedString(View view) { EntryEditSection section = (EntryEditSection) view.Parent; LinearLayout container = (LinearLayout) FindViewById(Resource.Id.advanced_container); - + mEntryModified = true; for (int i = 0; i < container.ChildCount; i++) { EntryEditSection ees = (EntryEditSection) container.GetChildAt(i); if (ees == section) { @@ -714,6 +753,7 @@ namespace keepass2android private void populateText(int viewId, String text) { TextView tv = (TextView) FindViewById(viewId); tv.Text = text; + tv.TextChanged += (sender, e) => {mEntryModified = true;}; } private class AfterSave : OnFinish { diff --git a/src/keepass2android/Resources/Resource.designer.cs b/src/keepass2android/Resources/Resource.designer.cs index ab2b5e52..7db0b33c 100644 --- a/src/keepass2android/Resources/Resource.designer.cs +++ b/src/keepass2android/Resources/Resource.designer.cs @@ -1009,6 +1009,12 @@ namespace keepass2android // aapt resource value: 0x7f0500f1 public const int AskDeletePermanently_title = 2131034353; + // aapt resource value: 0x7f0500f4 + public const int AskDiscardChanges = 2131034356; + + // aapt resource value: 0x7f0500f5 + public const int AskDiscardChanges_title = 2131034357; + // aapt resource value: 0x7f0500e9 public const int AskOverwriteBinary = 2131034345; diff --git a/src/keepass2android/Resources/values/strings.xml b/src/keepass2android/Resources/values/strings.xml index 1be88d0d..56934176 100644 --- a/src/keepass2android/Resources/values/strings.xml +++ b/src/keepass2android/Resources/values/strings.xml @@ -214,6 +214,8 @@ Delete permanently? Reload file? The file which is currently open was changed by another program. Do you want to reload it? + Do you really want to discard the changes made? (The Save button is at the top of the form.) + Discard changes? 30 seconds 1 minute diff --git a/src/keepass2android/views/EntryEditSection.cs b/src/keepass2android/views/EntryEditSection.cs index e7112ab8..64e60886 100644 --- a/src/keepass2android/views/EntryEditSection.cs +++ b/src/keepass2android/views/EntryEditSection.cs @@ -33,6 +33,8 @@ namespace keepass2android.view { public class EntryEditSection : RelativeLayout { + public event EventHandler ContentChanged; + public EntryEditSection (IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { @@ -62,6 +64,9 @@ namespace keepass2android.view CheckBox cb = (CheckBox) FindViewById(Resource.Id.protection); cb.Checked = value.IsProtected; + cb.CheckedChange += (sender, e) => {if (ContentChanged != null) + ContentChanged(this, new EventArgs()); + }; } public ImageButton getDeleteButton() @@ -73,8 +78,12 @@ namespace keepass2android.view { if (str != null) { - TextView tvTitle = (TextView)FindViewById(resId); - tvTitle.Text = str; + TextView tv = (TextView)FindViewById(resId); + tv.Text = str; + tv.TextChanged += (sender, e) => { + if (ContentChanged != null) + ContentChanged(this, new EventArgs()); + }; } }