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

View File

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

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Windows.Storage;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
@@ -13,6 +14,18 @@ namespace ModernKeePass.Controls
{
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()
{
InitializeComponent();
@@ -24,7 +37,7 @@ namespace ModernKeePass.Controls
private void OpenButton_OnClick(object sender, RoutedEventArgs e)
{
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)
ValidationChecked?.Invoke(this, new PasswordEventArgs(app.Database.RootGroup));
}

View File

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

View File

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

View File

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

View File

@@ -19,6 +19,6 @@
<HyperlinkButton Content="Browse files..." Click="ButtonBase_OnClick" />
<HyperlinkButton Content="From Url..." IsEnabled="False" />
<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>
</Page>

View File

@@ -23,7 +23,7 @@
<DataTemplate>
<StackPanel Margin="10,0,10,0">
<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>
</DataTemplate>
</ListView.ItemTemplate>

View File

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

View File

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

View File

@@ -2,7 +2,6 @@
using System.Collections.ObjectModel;
using System.Linq;
using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using ModernKeePass.Common;
namespace ModernKeePass.ViewModels
@@ -12,16 +11,6 @@ namespace ModernKeePass.ViewModels
private RecentItemVm _selectedItem;
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
{
get { return _recentItems; }
@@ -47,10 +36,19 @@ namespace ModernKeePass.ViewModels
}
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
var file = mru.GetFileAsync(SelectedItem.Token).GetAwaiter().GetResult();
var app = (App)Application.Current;
app.Database.DatabaseFile = file;
_selectedItem.File = mru.GetFileAsync(SelectedItem.Token).GetAwaiter().GetResult();
}
}
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];
}
}
}