Refactor in file open mechanisms: file is passed to user control

Confirmation dialogs on group and entry delete
This commit is contained in:
2017-10-11 14:30:07 +02:00
committed by BONNEVILLE Geoffroy
parent 454e074c44
commit 97b1475100
12 changed files with 124 additions and 62 deletions

View File

@@ -0,0 +1,23 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Xaml.Interactivity;
namespace ModernKeePass.Actions
{
public class SetupFocusAction : DependencyObject, IAction
{
public Control TargetObject
{
get { return (Control)GetValue(TargetObjectProperty); }
set { SetValue(TargetObjectProperty, value); }
}
public static readonly DependencyProperty TargetObjectProperty =
DependencyProperty.Register("TargetObject", typeof(Control), typeof(SetupFocusAction), new PropertyMetadata(0));
public object Execute(object sender, object parameter)
{
return TargetObject?.Focus(FocusState.Programmatic);
}
}
}

View File

@@ -98,7 +98,7 @@ namespace ModernKeePass
private void OnSuspending(object sender, SuspendingEventArgs e) private void OnSuspending(object sender, SuspendingEventArgs e)
{ {
var deferral = e.SuspendingOperation.GetDeferral(); var deferral = e.SuspendingOperation.GetDeferral();
// TODO: save state? //TODO: Save application state and stop any background activity
//Database.Save(); //Database.Save();
deferral.Complete(); deferral.Complete();
} }
@@ -106,11 +106,11 @@ namespace ModernKeePass
protected override void OnFileActivated(FileActivatedEventArgs args) protected override void OnFileActivated(FileActivatedEventArgs args)
{ {
base.OnFileActivated(args); base.OnFileActivated(args);
Database.DatabaseFile = args.Files[0] as StorageFile; /*Database.DatabaseFile = args.Files[0] as StorageFile;
var rootFrame = new Frame(); var rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), args); rootFrame.Navigate(typeof(MainPage), args);
Window.Current.Content = rootFrame; Window.Current.Content = rootFrame;
Window.Current.Activate(); Window.Current.Activate();*/
} }
} }
} }

View File

@@ -17,31 +17,22 @@ namespace ModernKeePass.Common
Opened = 2 Opened = 2
} }
private readonly PwDatabase _pwDatabase = new PwDatabase(); private readonly PwDatabase _pwDatabase = new PwDatabase();
private StorageFile _databaseFile;
public GroupVm RootGroup { get; set; } public GroupVm RootGroup { get; set; }
public DatabaseStatus Status { get; private set; } = DatabaseStatus.Closed; public DatabaseStatus Status { get; private set; } = DatabaseStatus.Closed;
public string Name => DatabaseFile.Name;
public string Name { get; private set; }
public StorageFile DatabaseFile public string Open(StorageFile databaseFile, string password)
{
get { return _databaseFile; }
set
{
_databaseFile = value;
Status = DatabaseStatus.Opening;
}
}
public string Open(string password)
{ {
var key = new CompositeKey(); var key = new CompositeKey();
try try
{ {
key.AddUserKey(new KcpPassword(password)); key.AddUserKey(new KcpPassword(password));
_pwDatabase.Open(IOConnectionInfo.FromFile(DatabaseFile), key, new NullStatusLogger()); _pwDatabase.Open(IOConnectionInfo.FromFile(databaseFile), key, new NullStatusLogger());
if (_pwDatabase.IsOpen) if (_pwDatabase.IsOpen)
{ {
Name = databaseFile.Name;
Status = DatabaseStatus.Opened; Status = DatabaseStatus.Opened;
RootGroup = new GroupVm(_pwDatabase.RootGroup, null); RootGroup = new GroupVm(_pwDatabase.RootGroup, null);
} }

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.Storage;
using Windows.System; using Windows.System;
using Windows.UI.Core; using Windows.UI.Core;
using Windows.UI.Xaml; using Windows.UI.Xaml;
@@ -13,6 +14,18 @@ namespace ModernKeePass.Controls
{ {
public sealed partial class OpenDatabaseUserControl : UserControl public sealed partial class OpenDatabaseUserControl : UserControl
{ {
public StorageFile DatabaseFile
{
get { return (StorageFile)GetValue(DatabaseFileProperty); }
set { SetValue(DatabaseFileProperty, value); }
}
public static readonly DependencyProperty DatabaseFileProperty =
DependencyProperty.Register(
"DatabaseFile",
typeof(StorageFile),
typeof(OpenDatabaseUserControl),
new PropertyMetadata(null, (o, args) => { }));
public OpenDatabaseUserControl() public OpenDatabaseUserControl()
{ {
InitializeComponent(); InitializeComponent();
@@ -24,7 +37,7 @@ namespace ModernKeePass.Controls
private void OpenButton_OnClick(object sender, RoutedEventArgs e) private void OpenButton_OnClick(object sender, RoutedEventArgs e)
{ {
var app = (App)Application.Current; var app = (App)Application.Current;
StatusTextBlock.Text = app.Database.Open(PasswordBox.Password); StatusTextBlock.Text = app.Database.Open(DatabaseFile, PasswordBox.Password);
if (app.Database.Status == DatabaseHelper.DatabaseStatus.Opened) if (app.Database.Status == DatabaseHelper.DatabaseStatus.Opened)
ValidationChecked?.Invoke(this, new PasswordEventArgs(app.Database.RootGroup)); ValidationChecked?.Invoke(this, new PasswordEventArgs(app.Database.RootGroup));
} }

View File

@@ -107,6 +107,7 @@
<Prefer32Bit>true</Prefer32Bit> <Prefer32Bit>true</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Actions\SetupFocusAction.cs" />
<Compile Include="App.xaml.cs"> <Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon> <DependentUpon>App.xaml</DependentUpon>
</Compile> </Compile>

View File

@@ -1,4 +1,5 @@
using System; using System;
using Windows.UI.Popups;
using ModernKeePass.Common; using ModernKeePass.Common;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
@@ -25,9 +26,9 @@ namespace ModernKeePass.Pages
public EntryDetailPage() public EntryDetailPage()
{ {
this.InitializeComponent(); InitializeComponent();
this.navigationHelper = new NavigationHelper(this); navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += navigationHelper_LoadState; navigationHelper.LoadState += navigationHelper_LoadState;
} }
/// <summary> /// <summary>
@@ -70,11 +71,28 @@ namespace ModernKeePass.Pages
#endregion #endregion
private void AppBarButton_Click(object sender, RoutedEventArgs e) private async void AppBarButton_Click(object sender, RoutedEventArgs e)
{ {
var entry = DataContext as EntryVm; // Create the message dialog and set its content
entry?.RemoveEntry(); var messageDialog = new MessageDialog("Are you sure you want to delete this entry?");
if (Frame.CanGoBack) Frame.GoBack();
// 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 =>
{
var entry = DataContext as EntryVm;
entry?.RemoveEntry();
if (Frame.CanGoBack) Frame.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();
} }
private async void UrlButton_Click(object sender, RoutedEventArgs e) private async void UrlButton_Click(object sender, RoutedEventArgs e)

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using Windows.Storage.Streams; using Windows.Storage.Streams;
using Windows.UI.Popups;
using ModernKeePass.Common; using ModernKeePass.Common;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; using Windows.UI.Xaml.Navigation;
@@ -68,6 +69,9 @@ namespace ModernKeePass.Pages
#endregion #endregion
#region Event Handlers
private void groups_SelectionChanged(object sender, SelectionChangedEventArgs e) private void groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
if (LeftListView.SelectedIndex == 0) if (LeftListView.SelectedIndex == 0)
@@ -99,13 +103,30 @@ namespace ModernKeePass.Pages
Frame.Navigate(typeof(EntryDetailPage), GridView.SelectedItem as EntryVm); Frame.Navigate(typeof(EntryDetailPage), GridView.SelectedItem as EntryVm);
} }
private void DeleteButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) private async void DeleteButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{ {
var group = DataContext as GroupVm; // Create the message dialog and set its content
group?.RemoveGroup(); var messageDialog = new MessageDialog("Are you sure you want to delete the whole group and all its entries?");
if (Frame.CanGoBack) Frame.GoBack();
}
// 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 =>
{
var group = DataContext as GroupVm;
group?.RemoveGroup();
if (Frame.CanGoBack) Frame.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();
}
private void SemanticZoom_ViewChangeStarted(object sender, SemanticZoomViewChangedEventArgs e) private void SemanticZoom_ViewChangeStarted(object sender, SemanticZoomViewChangedEventArgs e)
{ {
if (e.IsSourceZoomedInView == false) if (e.IsSourceZoomedInView == false)
@@ -131,5 +152,8 @@ namespace ModernKeePass.Pages
var entry = viewModel.Entries.Skip(1).FirstOrDefault(e => e.Id == args.Tag); var entry = viewModel.Entries.Skip(1).FirstOrDefault(e => e.Id == args.Tag);
Frame.Navigate(typeof(EntryDetailPage), entry); Frame.Navigate(typeof(EntryDetailPage), entry);
} }
#endregion
} }
} }

View File

@@ -19,6 +19,6 @@
<HyperlinkButton Content="Browse files..." Click="ButtonBase_OnClick" /> <HyperlinkButton Content="Browse files..." Click="ButtonBase_OnClick" />
<HyperlinkButton Content="From Url..." IsEnabled="False" /> <HyperlinkButton Content="From Url..." IsEnabled="False" />
<TextBlock TextWrapping="Wrap" Text="{Binding Name}" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" /> <TextBlock TextWrapping="Wrap" Text="{Binding Name}" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" />
<local:OpenDatabaseUserControl Visibility="{Binding ShowPasswordBox, Converter={StaticResource BooleanToVisibilityConverter}}" ValidationChecked="PasswordUserControl_PasswordChecked" /> <local:OpenDatabaseUserControl DatabaseFile="{Binding File}" Visibility="{Binding ShowPasswordBox, Converter={StaticResource BooleanToVisibilityConverter}}" ValidationChecked="PasswordUserControl_PasswordChecked" />
</StackPanel> </StackPanel>
</Page> </Page>

View File

@@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="using:ModernKeePass.ViewModels" xmlns:viewModels="using:ModernKeePass.ViewModels"
xmlns:local="using:ModernKeePass.Controls" xmlns:local="using:ModernKeePass.Controls"
xmlns:converters="using:ModernKeePass.Converters" xmlns:converters="using:ModernKeePass.Converters"
x:Class="ModernKeePass.Pages.RecentDatabasesPage" x:Class="ModernKeePass.Pages.RecentDatabasesPage"
mc:Ignorable="d"> mc:Ignorable="d">
<Page.Resources> <Page.Resources>
@@ -23,7 +23,7 @@
<DataTemplate> <DataTemplate>
<StackPanel Margin="10,0,10,0"> <StackPanel Margin="10,0,10,0">
<TextBlock Text="{Binding Name}" Width="350" Padding="5" /> <TextBlock Text="{Binding Name}" Width="350" Padding="5" />
<local:OpenDatabaseUserControl Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" ValidationChecked="PasswordUserControl_PasswordChecked" /> <local:OpenDatabaseUserControl DatabaseFile="{Binding File}" Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" ValidationChecked="PasswordUserControl_PasswordChecked" />
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>
</ListView.ItemTemplate> </ListView.ItemTemplate>

View File

@@ -1,4 +1,5 @@
using ModernKeePass.Common; using Windows.Storage;
using ModernKeePass.Common;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
{ {
@@ -7,6 +8,7 @@ namespace ModernKeePass.ViewModels
private bool _isSelected; private bool _isSelected;
public string Token { get; set; } public string Token { get; set; }
public string Name { get; set; } public string Name { get; set; }
public StorageFile File { get; set; }
public bool IsSelected public bool IsSelected
{ {

View File

@@ -1,31 +1,24 @@
using System.ComponentModel; using System.ComponentModel;
using Windows.Storage; using Windows.Storage;
using Windows.Storage.AccessCache; using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using ModernKeePass.Common;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
{ {
public class OpenVm: INotifyPropertyChanged public class OpenVm: INotifyPropertyChanged
{ {
public StorageFile File { get; set; }
public bool ShowPasswordBox public bool ShowPasswordBox
{ {
get { return ((App) Application.Current).Database.Status == DatabaseHelper.DatabaseStatus.Opening; } get { return File != null; }
} }
public string Name public string Name
{ {
get { return ((App) Application.Current).Database.Name; } get { return File?.Name; }
} }
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
public OpenVm()
{
var database = ((App) Application.Current).Database;
if (database == null || database.Status != DatabaseHelper.DatabaseStatus.Opening) return;
OpenFile(database.DatabaseFile);
}
private void NotifyPropertyChanged(string propertyName) private void NotifyPropertyChanged(string propertyName)
{ {
@@ -34,8 +27,7 @@ namespace ModernKeePass.ViewModels
public void OpenFile(StorageFile file) public void OpenFile(StorageFile file)
{ {
var database = ((App)Application.Current).Database; File = file;
database.DatabaseFile = file;
NotifyPropertyChanged("Name"); NotifyPropertyChanged("Name");
NotifyPropertyChanged("ShowPasswordBox"); NotifyPropertyChanged("ShowPasswordBox");
AddToRecentList(file); AddToRecentList(file);

View File

@@ -2,7 +2,6 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using Windows.Storage.AccessCache; using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using ModernKeePass.Common; using ModernKeePass.Common;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
@@ -11,17 +10,7 @@ namespace ModernKeePass.ViewModels
{ {
private RecentItemVm _selectedItem; private RecentItemVm _selectedItem;
private ObservableCollection<RecentItemVm> _recentItems; private ObservableCollection<RecentItemVm> _recentItems;
public RecentVm()
{
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
RecentItems = new ObservableCollection<RecentItemVm>(
from entry in mru.Entries
select new RecentItemVm { Name = entry.Metadata, Token = entry.Token });
if (RecentItems.Count > 0)
SelectedItem = RecentItems[0];
}
public ObservableCollection<RecentItemVm> RecentItems public ObservableCollection<RecentItemVm> RecentItems
{ {
get { return _recentItems; } get { return _recentItems; }
@@ -47,10 +36,19 @@ namespace ModernKeePass.ViewModels
} }
var mru = StorageApplicationPermissions.MostRecentlyUsedList; var mru = StorageApplicationPermissions.MostRecentlyUsedList;
var file = mru.GetFileAsync(SelectedItem.Token).GetAwaiter().GetResult(); _selectedItem.File = mru.GetFileAsync(SelectedItem.Token).GetAwaiter().GetResult();
var app = (App)Application.Current;
app.Database.DatabaseFile = file;
} }
} }
public RecentVm()
{
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
RecentItems = new ObservableCollection<RecentItemVm>(
from entry in mru.Entries
select new RecentItemVm { Name = entry.Metadata, Token = entry.Token });
if (RecentItems.Count > 0)
SelectedItem = RecentItems[0];
}
} }
} }