Error messages are now caught at the app level (see if it's a good solution in the long term)

Redesign of the create key file button
This commit is contained in:
BONNEVILLE Geoffroy
2017-11-23 19:02:49 +01:00
parent 675a718107
commit 1b2d25e171
11 changed files with 54 additions and 47 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
using Windows.ApplicationModel; using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.Activation;
using Windows.Data.Json; using Windows.Data.Json;
@@ -29,9 +30,23 @@ namespace ModernKeePass
{ {
InitializeComponent(); InitializeComponent();
Suspending += OnSuspending; Suspending += OnSuspending;
UnhandledException += OnUnhandledException;
} }
#region Event Handlers #region Event Handlers
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
// TODO: catch only save errors for now, rethrow (do not handle) otherwise
var exception = unhandledExceptionEventArgs.Exception;
MessageDialogHelper.ShowErrorDialog(
exception is TargetInvocationException &&
exception.InnerException != null
? exception.InnerException
: exception);
unhandledExceptionEventArgs.Handled = true;
}
/// <summary> /// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points /// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file. /// will be used such as when the application is launched to open a specific file.
@@ -126,6 +141,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();
UnhandledException -= OnUnhandledException;
Database.Save(); Database.Save();
deferral.Complete(); deferral.Complete();
} }
@@ -143,6 +159,7 @@ namespace ModernKeePass
Window.Current.Content = rootFrame; Window.Current.Content = rootFrame;
Window.Current.Activate(); Window.Current.Activate();
} }
#endregion #endregion
} }
} }

View File

@@ -105,6 +105,7 @@ namespace ModernKeePass.Common
catch (Exception ex) catch (Exception ex)
{ {
Status = (int)DatabaseStatus.Error; Status = (int)DatabaseStatus.Error;
throw;
} }
} }
@@ -112,38 +113,20 @@ namespace ModernKeePass.Common
/// Save the current database to another file and open it /// Save the current database to another file and open it
/// </summary> /// </summary>
/// <param name="file">The new database file</param> /// <param name="file">The new database file</param>
public bool Save(StorageFile file) public void Save(StorageFile file)
{ {
DatabaseFile = file; DatabaseFile = file;
try
{
_pwDatabase.SaveAs(IOConnectionInfo.FromFile(DatabaseFile), true, new NullStatusLogger()); _pwDatabase.SaveAs(IOConnectionInfo.FromFile(DatabaseFile), true, new NullStatusLogger());
Status = (int)DatabaseStatus.Opened; Status = (int)DatabaseStatus.Opened;
return true;
}
catch (Exception ex)
{
return false;
}
} }
/// <summary> /// <summary>
/// Commit the changes to the currently opened database to file /// Commit the changes to the currently opened database to file
/// </summary> /// </summary>
public bool Save() public void Save()
{
if (_pwDatabase == null || !_pwDatabase.IsOpen) return false;
try
{ {
if (_pwDatabase == null || !_pwDatabase.IsOpen) return;
_pwDatabase.Save(new NullStatusLogger()); _pwDatabase.Save(new NullStatusLogger());
return true;
}
catch (Exception ex)
{
return false;
// TODO: put this at a better place (e.g. in views)
MessageDialogHelper.ShowErrorDialog(ex);
}
} }
/// <summary> /// <summary>

View File

@@ -26,6 +26,7 @@ namespace ModernKeePass.Common
public static async void ShowErrorDialog(Exception exception) public static async void ShowErrorDialog(Exception exception)
{ {
if (exception == null) return;
// Create the message dialog and set its content // Create the message dialog and set its content
var messageDialog = new MessageDialog(exception.Message, "Error occured"); var messageDialog = new MessageDialog(exception.Message, "Error occured");

View File

@@ -48,7 +48,9 @@
Visibility="{Binding ShowComplexityIndicator, ElementName=UserControl, Converter={StaticResource BooleanToVisibilityConverter}}" /> Visibility="{Binding ShowComplexityIndicator, ElementName=UserControl, Converter={StaticResource BooleanToVisibilityConverter}}" />
<CheckBox Grid.Row="1" Grid.Column="0" IsChecked="{Binding HasKeyFile, Mode=TwoWay}" /> <CheckBox Grid.Row="1" Grid.Column="0" IsChecked="{Binding HasKeyFile, Mode=TwoWay}" />
<HyperlinkButton Grid.Row="1" Grid.Column="1" Margin="-15,0,0,0" Content="{Binding KeyFileText}" IsEnabled="{Binding HasKeyFile}" Click="KeyFileButton_Click" /> <HyperlinkButton Grid.Row="1" Grid.Column="1" Margin="-15,0,0,0" Content="{Binding KeyFileText}" IsEnabled="{Binding HasKeyFile}" Click="KeyFileButton_Click" />
<Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" Content="Create new key" Visibility="{Binding ShowComplexityIndicator, ElementName=UserControl, Converter={StaticResource BooleanToVisibilityConverter}}" IsEnabled="{Binding HasKeyFile}" Click="CreateKeyFileButton_Click" /> <HyperlinkButton Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" Visibility="{Binding ShowComplexityIndicator, ElementName=UserControl, Converter={StaticResource BooleanToVisibilityConverter}}" IsEnabled="{Binding HasKeyFile}" Click="CreateKeyFileButton_Click">
<SymbolIcon Symbol="Add" />
</HyperlinkButton>
<Button Grid.Column="0" Grid.Row="2" Content="OK" Click="OpenButton_OnClick" Background="{ThemeResource ListViewItemSelectedPointerOverBorderThemeBrush}" Foreground="{ThemeResource TextBoxBackgroundThemeBrush}" IsEnabled="{Binding IsValid}" /> <Button Grid.Column="0" Grid.Row="2" Content="OK" Click="OpenButton_OnClick" Background="{ThemeResource ListViewItemSelectedPointerOverBorderThemeBrush}" Foreground="{ThemeResource TextBoxBackgroundThemeBrush}" IsEnabled="{Binding IsValid}" />
<TextBlock Grid.Column="1" Grid.Row="2" Height="28" FontSize="14" FontWeight="Light" HorizontalAlignment="Right" Text="{Binding Status}" Foreground="{Binding StatusType, Converter={StaticResource DiscreteIntToSolidColorBrushConverter}}" /> <TextBlock Grid.Column="1" Grid.Row="2" Height="28" FontSize="14" FontWeight="Light" HorizontalAlignment="Right" Text="{Binding Status}" Foreground="{Binding StatusType, Converter={StaticResource DiscreteIntToSolidColorBrushConverter}}" />
</Grid> </Grid>

View File

@@ -20,8 +20,8 @@ namespace ModernKeePass.Interfaces
void Open(CompositeKey key, bool createNew); void Open(CompositeKey key, bool createNew);
void UpdateCompositeKey(CompositeKey key); void UpdateCompositeKey(CompositeKey key);
bool Save(); void Save();
bool Save(StorageFile file); void Save(StorageFile file);
void CreateRecycleBin(); void CreateRecycleBin();
void AddDeletedItem(PwUuid id); void AddDeletedItem(PwUuid id);
void Close(); void Close();

View File

@@ -24,14 +24,15 @@ namespace ModernKeePass.Pages
{ {
ListView_SelectionChanged(sender, e); ListView_SelectionChanged(sender, e);
var selectedItem = Model.SelectedItem as MainMenuItemVm; var selectedItem = Model.SelectedItem as MainMenuItemVm;
selectedItem?.Destination.Navigate(selectedItem.PageType, selectedItem.Parameter); if (selectedItem == null) MenuFrame.Navigate(typeof(WelcomePage));
else selectedItem.Destination.Navigate(selectedItem.PageType, selectedItem.Parameter);
} }
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
base.OnNavigatedTo(e); base.OnNavigatedTo(e);
DataContext = new MainVm(Frame, MenuFrame); DataContext = new MainVm(Frame, MenuFrame);
if (Model.SelectedItem == null) MenuFrame.Navigate(typeof(WelcomePage)); //if (Model.SelectedItem == null) MenuFrame.Navigate(typeof(WelcomePage));
} }
} }
} }

View File

@@ -2,9 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using Windows.Storage.Pickers; using Windows.Storage.Pickers;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using ModernKeePass.Events;
using ModernKeePass.ViewModels; using ModernKeePass.ViewModels;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
@@ -16,8 +13,6 @@ namespace ModernKeePass.Pages
/// </summary> /// </summary>
public sealed partial class NewDatabasePage public sealed partial class NewDatabasePage
{ {
private Frame _mainFrame;
public NewVm Model => (NewVm)DataContext; public NewVm Model => (NewVm)DataContext;
public NewDatabasePage() public NewDatabasePage()
@@ -25,12 +20,6 @@ namespace ModernKeePass.Pages
InitializeComponent(); InitializeComponent();
} }
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_mainFrame = e.Parameter as Frame;
}
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e) private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{ {
var savePicker = new FileSavePicker var savePicker = new FileSavePicker

View File

@@ -1,4 +1,5 @@
using System.Text; using System;
using System.Text;
using Windows.Storage; using Windows.Storage;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using ModernKeePass.Common; using ModernKeePass.Common;
@@ -104,8 +105,16 @@ namespace ModernKeePass.ViewModels
} }
public bool OpenDatabase(bool createNew) public bool OpenDatabase(bool createNew)
{
var error = string.Empty;
try
{ {
Database.Open(CreateCompositeKey(), createNew); Database.Open(CreateCompositeKey(), createNew);
}
catch (Exception e)
{
error = $"Error: {e.Message}";
}
switch ((DatabaseHelper.DatabaseStatus)Database.Status) switch ((DatabaseHelper.DatabaseStatus)Database.Status)
{ {
case DatabaseHelper.DatabaseStatus.Opened: case DatabaseHelper.DatabaseStatus.Opened:
@@ -118,6 +127,9 @@ namespace ModernKeePass.ViewModels
if (HasKeyFile) errorMessage.Append("key file"); if (HasKeyFile) errorMessage.Append("key file");
UpdateStatus(errorMessage.ToString(), StatusTypes.Error); UpdateStatus(errorMessage.ToString(), StatusTypes.Error);
break; break;
case DatabaseHelper.DatabaseStatus.Error:
UpdateStatus(error, StatusTypes.Error);
break;
} }
return false; return false;
} }

View File

@@ -16,10 +16,11 @@ namespace ModernKeePass.ViewModels
public void Save(bool close = true) public void Save(bool close = true)
{ {
if (close && _database.Save()) _database.Close(); _database.Save();
if (close) _database.Close();
} }
internal void Save(StorageFile file) public void Save(StorageFile file)
{ {
_database.Save(file); _database.Save(file);
} }

View File

@@ -36,7 +36,8 @@ namespace ModernKeePassApp.Test
public void TestSave() public void TestSave()
{ {
TestOpen(); TestOpen();
Assert.IsTrue(_database.Save(ApplicationData.Current.TemporaryFolder.CreateFileAsync("SaveDatabase.kdbx").GetAwaiter().GetResult())); _database.Save(ApplicationData.Current.TemporaryFolder.CreateFileAsync("SaveDatabase.kdbx").GetAwaiter().GetResult());
Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.Opened, _database.Status);
_database.Close(); _database.Close();
Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.Closed, _database.Status); Assert.AreEqual((int)DatabaseHelper.DatabaseStatus.Closed, _database.Status);
TestOpen(); TestOpen();

View File

@@ -48,12 +48,12 @@ namespace ModernKeePassApp.Test.Mock
throw new NotImplementedException(); throw new NotImplementedException();
} }
public bool Save() public void Save()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public bool Save(StorageFile file) public void Save(StorageFile file)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }