From 46c8429cdeecc7704b5778865ad8db63602bbb03 Mon Sep 17 00:00:00 2001 From: Geoffroy Bonneville Date: Wed, 25 Oct 2017 18:29:50 +0200 Subject: [PATCH] Lots of code refactoring to use XAML behaviors instead of code-behind New Save button in the AppBar EntryPage now uses the same AppBar as GroupPage (but not shared...) Some new Symbol mappings --- ModernKeePass/Actions/CloseFlyoutAction.cs | 17 ----- ModernKeePass/Actions/SetupFocusAction.cs | 10 ++- ModernKeePass/Common/DatabaseHelper.cs | 17 ++++- ModernKeePass/Common/MessageDialogHelper.cs | 35 ++++++++++ .../Common/ToastNotificationHelper.cs | 5 +- ModernKeePass/Interfaces/IPwEntity.cs | 2 + .../Mappings/PwIconToSegoeMapping.cs | 22 +++--- ModernKeePass/ModernKeePass.csproj | 6 +- ModernKeePass/Pages/AboutPage.xaml | 6 +- ModernKeePass/Pages/EntryDetailPage.xaml | 70 +++++++++++++------ ModernKeePass/Pages/EntryDetailPage.xaml.cs | 32 +-------- ModernKeePass/Pages/GroupDetailPage.xaml | 67 +++++++++++++----- ModernKeePass/Pages/GroupDetailPage.xaml.cs | 51 ++------------ ModernKeePass/{ => Pages}/MainPage.xaml | 2 +- ModernKeePass/{ => Pages}/MainPage.xaml.cs | 0 ModernKeePass/Pages/NewDatabasePage.xaml | 13 +++- ModernKeePass/Pages/NewDatabasePage.xaml.cs | 5 -- ModernKeePass/Pages/OpenDatabasePage.xaml | 9 ++- ModernKeePass/Pages/OpenDatabasePage.xaml.cs | 5 -- ModernKeePass/Pages/RecentDatabasesPage.xaml | 13 +++- .../Pages/RecentDatabasesPage.xaml.cs | 6 -- ModernKeePass/ViewModels/AboutVm.cs | 4 +- ModernKeePass/ViewModels/EntryVm.cs | 5 ++ ModernKeePass/ViewModels/GroupVm.cs | 14 ++-- ModernKeePass/ViewModels/MainVm.cs | 3 + ModernKeePass/ViewModels/RecentVm.cs | 1 + 26 files changed, 241 insertions(+), 179 deletions(-) delete mode 100644 ModernKeePass/Actions/CloseFlyoutAction.cs create mode 100644 ModernKeePass/Common/MessageDialogHelper.cs rename ModernKeePass/{ => Pages}/MainPage.xaml (96%) rename ModernKeePass/{ => Pages}/MainPage.xaml.cs (100%) diff --git a/ModernKeePass/Actions/CloseFlyoutAction.cs b/ModernKeePass/Actions/CloseFlyoutAction.cs deleted file mode 100644 index 66a2448..0000000 --- a/ModernKeePass/Actions/CloseFlyoutAction.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Microsoft.Xaml.Interactivity; - -namespace ModernKeePass.Actions -{ - public class CloseFlyoutAction : DependencyObject, IAction - { - public object Execute(object sender, object parameter) - { - var flyout = sender as Flyout; - flyout?.Hide(); - - return null; - } - } -} diff --git a/ModernKeePass/Actions/SetupFocusAction.cs b/ModernKeePass/Actions/SetupFocusAction.cs index 4dcf00f..8ef7c99 100644 --- a/ModernKeePass/Actions/SetupFocusAction.cs +++ b/ModernKeePass/Actions/SetupFocusAction.cs @@ -1,4 +1,6 @@ -using Windows.UI.Xaml; +using System.Threading.Tasks; +using Windows.UI.Core; +using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Microsoft.Xaml.Interactivity; @@ -13,11 +15,13 @@ namespace ModernKeePass.Actions } public static readonly DependencyProperty TargetObjectProperty = - DependencyProperty.Register("TargetObject", typeof(Control), typeof(SetupFocusAction), new PropertyMetadata(0)); + DependencyProperty.Register("TargetObject", typeof(Control), typeof(SetupFocusAction), new PropertyMetadata(null)); public object Execute(object sender, object parameter) { - return TargetObject?.Focus(FocusState.Programmatic); + return Task.Factory.StartNew( + () => Dispatcher.RunAsync(CoreDispatcherPriority.Low, + () => TargetObject?.Focus(FocusState.Programmatic))); } } } \ No newline at end of file diff --git a/ModernKeePass/Common/DatabaseHelper.cs b/ModernKeePass/Common/DatabaseHelper.cs index 020770c..7166d48 100644 --- a/ModernKeePass/Common/DatabaseHelper.cs +++ b/ModernKeePass/Common/DatabaseHelper.cs @@ -33,7 +33,12 @@ namespace ModernKeePass.Common } } - + /// + /// Open a KeePass database + /// + /// The database password + /// True to create a new database before opening it + /// An error message, if any public string Open(string password, bool createNew = false) { var key = new CompositeKey(); @@ -65,6 +70,10 @@ namespace ModernKeePass.Common return string.Empty; } + /// + /// Save the current database to another file and open it + /// + /// The new database file internal void Save(StorageFile file) { DatabaseFile = file; @@ -72,12 +81,18 @@ namespace ModernKeePass.Common Status = DatabaseStatus.Opened; } + /// + /// Commit the changes to the currently opened database to file + /// public void Save() { if (_pwDatabase != null && _pwDatabase.IsOpen) _pwDatabase.Save(new NullStatusLogger()); } + /// + /// Close the currently opened database + /// public void Close() { _pwDatabase?.Close(); diff --git a/ModernKeePass/Common/MessageDialogHelper.cs b/ModernKeePass/Common/MessageDialogHelper.cs new file mode 100644 index 0000000..1750314 --- /dev/null +++ b/ModernKeePass/Common/MessageDialogHelper.cs @@ -0,0 +1,35 @@ +using System; +using Windows.UI.Popups; +using Windows.UI.Xaml.Controls; +using ModernKeePass.Interfaces; +using ModernKeePass.ViewModels; + +namespace ModernKeePass.Common +{ + public static class MessageDialogHelper + { + public static async void ShowDeleteConfirmationDialog(string text, IPwEntity model, Frame backFrame) + { + // Create the message dialog and set its content + var messageDialog = new MessageDialog(text); + + // Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers + messageDialog.Commands.Add(new UICommand("Delete", delete => + { + ToastNotificationHelper.ShowUndoToast(model); + model.MarkForDelete(); + if (backFrame.CanGoBack) backFrame.GoBack(); + })); + messageDialog.Commands.Add(new UICommand("Cancel")); + + // Set the command that will be invoked by default + messageDialog.DefaultCommandIndex = 1; + + // Set the command to be invoked when escape is pressed + messageDialog.CancelCommandIndex = 1; + + // Show the message dialog + await messageDialog.ShowAsync(); + } + } +} diff --git a/ModernKeePass/Common/ToastNotificationHelper.cs b/ModernKeePass/Common/ToastNotificationHelper.cs index 35f66ae..a46d783 100644 --- a/ModernKeePass/Common/ToastNotificationHelper.cs +++ b/ModernKeePass/Common/ToastNotificationHelper.cs @@ -4,12 +4,13 @@ using Windows.Data.Xml.Dom; using Windows.UI.Notifications; using Windows.UI.Xaml; using ModernKeePass.Interfaces; +using ModernKeePass.ViewModels; namespace ModernKeePass.Common { public static class ToastNotificationHelper { - public static /*async*/ void ShowUndoToast(string entityType, IPwEntity entity) + public static /*async*/ void ShowUndoToast(IPwEntity entity) { // This is for Windows 10 // Construct the visuals of the toast @@ -64,6 +65,8 @@ namespace ModernKeePass.Common var toast = new ToastNotification(toastXml) {ExpirationTime = DateTime.Now.AddSeconds(5)}; toast.Dismissed += Toast_Dismissed; */ + + var entityType = entity is GroupVm ? "Group" : "Entry"; var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02); var toastElements = notificationXml.GetElementsByTagName("text"); toastElements[0].AppendChild(notificationXml.CreateTextNode($"{entityType} {entity.Name} deleted")); diff --git a/ModernKeePass/Interfaces/IPwEntity.cs b/ModernKeePass/Interfaces/IPwEntity.cs index 48c8730..3142f80 100644 --- a/ModernKeePass/Interfaces/IPwEntity.cs +++ b/ModernKeePass/Interfaces/IPwEntity.cs @@ -13,5 +13,7 @@ namespace ModernKeePass.Interfaces void CommitDelete(); void UndoDelete(); + void Save(); + void MarkForDelete(); } } \ No newline at end of file diff --git a/ModernKeePass/Mappings/PwIconToSegoeMapping.cs b/ModernKeePass/Mappings/PwIconToSegoeMapping.cs index 75dc1f4..f1285fc 100644 --- a/ModernKeePass/Mappings/PwIconToSegoeMapping.cs +++ b/ModernKeePass/Mappings/PwIconToSegoeMapping.cs @@ -10,18 +10,19 @@ namespace ModernKeePass.Mappings switch (icon) { case PwIcon.Key: return Symbol.Permissions; + case PwIcon.WorldSocket: case PwIcon.World: return Symbol.World; case PwIcon.Warning: return Symbol.Important; case PwIcon.WorldComputer: + case PwIcon.Drive: case PwIcon.DriveWindows: case PwIcon.NetworkServer: return Symbol.MapDrive; - //case PwIcon.MarkedDirectory: return Symbol.; + case PwIcon.MarkedDirectory: return Symbol.Map; case PwIcon.UserCommunication: return Symbol.ContactInfo; - //case PwIcon.Parts: return Symbol.; + case PwIcon.Parts: return Symbol.ViewAll; case PwIcon.Notepad: return Symbol.Document; - //case PwIcon.WorldScoket: return Symbol.; case PwIcon.Identity: return Symbol.Contact2; - //case PwIcon.PaperReady: return Symbol.; + case PwIcon.PaperReady: return Symbol.SyncFolder; case PwIcon.Digicam: return Symbol.Camera; case PwIcon.IRCommunication: return Symbol.View; case PwIcon.Energy: return Symbol.ZeroBars; @@ -36,17 +37,16 @@ namespace ModernKeePass.Mappings case PwIcon.Screen: return Symbol.GoToStart; case PwIcon.EnergyCareful: return Symbol.FourBars; case PwIcon.Disk: return Symbol.Save; - //case PwIcon.Drive: return Symbol.; //case PwIcon.PaperQ: return Symbol.; //case PwIcon.TerminalEncrypted: return Symbol.; - //case PwIcon.Console: return Symbol.; - //case PwIcon.Printer: return Symbol.; + case PwIcon.Console: return Symbol.SlideShow; + case PwIcon.Printer: return Symbol.Scan; case PwIcon.ProgramIcons: return Symbol.GoToStart; //case PwIcon.Run: return Symbol.; case PwIcon.Settings: case PwIcon.Tool: return Symbol.Repair; - //case PwIcon.Archive: return Symbol.; - case PwIcon.Count: return Symbol.MapDrive; + case PwIcon.Archive: return Symbol.Crop; + case PwIcon.Count: return Symbol.Calculator; case PwIcon.Clock: return Symbol.Clock; case PwIcon.EMailSearch: return Symbol.Find; case PwIcon.PaperFlag: return Symbol.Flag; @@ -72,10 +72,10 @@ namespace ModernKeePass.Mappings //case PwIcon.Feather: return Symbol.; //case PwIcon.Apple: return Symbol.; //case PwIcon.Wiki: return Symbol.; - //case PwIcon.Money: return Symbol.; + case PwIcon.Money: return Symbol.Shop; case PwIcon.Certificate: return Symbol.PreviewLink; case PwIcon.BlackBerry: return Symbol.CellPhone; - default: return Symbol.More; + default: return Symbol.Stop; } } } diff --git a/ModernKeePass/ModernKeePass.csproj b/ModernKeePass/ModernKeePass.csproj index 9f7f218..77c0789 100644 --- a/ModernKeePass/ModernKeePass.csproj +++ b/ModernKeePass/ModernKeePass.csproj @@ -107,12 +107,12 @@ true - App.xaml + @@ -136,7 +136,7 @@ - + MainPage.xaml @@ -198,7 +198,7 @@ Designer MSBuild:Compile - + MSBuild:Compile Designer diff --git a/ModernKeePass/Pages/AboutPage.xaml b/ModernKeePass/Pages/AboutPage.xaml index fff7697..0cbb0dc 100644 --- a/ModernKeePass/Pages/AboutPage.xaml +++ b/ModernKeePass/Pages/AboutPage.xaml @@ -15,7 +15,11 @@ - + + + + + diff --git a/ModernKeePass/Pages/EntryDetailPage.xaml b/ModernKeePass/Pages/EntryDetailPage.xaml index 21d2945..45f4fd2 100644 --- a/ModernKeePass/Pages/EntryDetailPage.xaml +++ b/ModernKeePass/Pages/EntryDetailPage.xaml @@ -6,12 +6,12 @@ xmlns:viewModels="using:ModernKeePass.ViewModels" xmlns:converters="using:ModernKeePass.Converters" xmlns:local="using:ModernKeePass.Controls" - xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" xmlns:Core="using:Microsoft.Xaml.Interactions.Core" - xmlns:Actions="using:ModernKeePass.Actions" + xmlns:interactivity="using:Microsoft.Xaml.Interactivity" + xmlns:core="using:Microsoft.Xaml.Interactions.Core" + xmlns:actions="using:ModernKeePass.Actions" x:Name="PageRoot" x:Class="ModernKeePass.Pages.EntryDetailPage" mc:Ignorable="d"> - @@ -327,15 +327,17 @@ FontSize="{TemplateBinding FontSize}" VerticalAlignment="Stretch" > - - - - - - + + + + + + + - + + @@ -346,9 +348,14 @@ - - @@ -358,11 +365,32 @@ - - + + + + + + + + + + + + + + + + + + + + + + + @@ -374,7 +402,7 @@ - +