mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Major OpenDatabaseUserControl refactor, now has proper ViewModel
Status text and password box border colors are updated according to database status Update composite key in Settings work Some code cleanup
This commit is contained in:
@@ -1,23 +1,19 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using Windows.Storage.Pickers;
|
||||
using Windows.System;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Input;
|
||||
using ModernKeePass.Common;
|
||||
using ModernKeePass.Events;
|
||||
using ModernKeePassLib.Cryptography;
|
||||
using ModernKeePass.ViewModels;
|
||||
|
||||
// Pour en savoir plus sur le modèle d'élément Contrôle utilisateur, consultez la page http://go.microsoft.com/fwlink/?LinkId=234236
|
||||
|
||||
namespace ModernKeePass.Controls
|
||||
{
|
||||
public sealed partial class OpenDatabaseUserControl: INotifyPropertyChanged
|
||||
public sealed partial class OpenDatabaseUserControl
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public double PasswordComplexityIndicator { get; private set; }
|
||||
public CompositeKeyVm Model => Grid.DataContext as CompositeKeyVm;
|
||||
|
||||
public bool CreateNew
|
||||
{
|
||||
@@ -31,22 +27,19 @@ namespace ModernKeePass.Controls
|
||||
typeof(OpenDatabaseUserControl),
|
||||
new PropertyMetadata(false, (o, args) => { }));
|
||||
|
||||
public string Password
|
||||
public bool UpdateKey
|
||||
{
|
||||
get { return (string) GetValue(PasswordProperty); }
|
||||
set { SetValue(PasswordProperty, value); }
|
||||
get { return (bool)GetValue(UpdateKeyProperty); }
|
||||
set { SetValue(UpdateKeyProperty, value); }
|
||||
}
|
||||
public static readonly DependencyProperty PasswordProperty =
|
||||
public static readonly DependencyProperty UpdateKeyProperty =
|
||||
DependencyProperty.Register(
|
||||
"Password",
|
||||
typeof(string),
|
||||
"UpdateKey",
|
||||
typeof(bool),
|
||||
typeof(OpenDatabaseUserControl),
|
||||
new PropertyMetadata(string.Empty, (o, args) => { }));
|
||||
new PropertyMetadata(false, (o, args) => { }));
|
||||
|
||||
private void NotifyPropertyChanged(string propertyName)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
public bool ShowComplexityIndicator => CreateNew || UpdateKey;
|
||||
|
||||
public OpenDatabaseUserControl()
|
||||
{
|
||||
@@ -61,26 +54,17 @@ namespace ModernKeePass.Controls
|
||||
private void OpenButton_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ValidationChecking?.Invoke(this, new EventArgs());
|
||||
var app = (App)Application.Current;
|
||||
StatusTextBlock.Text = app.Database.Open(PasswordCheckBox.IsChecked.HasValue && PasswordCheckBox.IsChecked.Value ? PasswordBox.Password : null, CreateNew);
|
||||
if (app.Database.Status == DatabaseHelper.DatabaseStatus.Opened)
|
||||
|
||||
if (UpdateKey) Model.UpdateKey();
|
||||
else if (Model.OpenDatabase(CreateNew) == DatabaseHelper.DatabaseStatus.Opened)
|
||||
{
|
||||
ValidationChecked?.Invoke(this, new PasswordEventArgs(app.Database.RootGroup));
|
||||
}
|
||||
else
|
||||
{
|
||||
VisualStateManager.GoToState(PasswordBox, "Error", true);
|
||||
ValidationChecked?.Invoke(this, new PasswordEventArgs(Model.RootGroup));
|
||||
}
|
||||
}
|
||||
|
||||
private void PasswordBox_KeyDown(object sender, KeyRoutedEventArgs e)
|
||||
{
|
||||
if (e.Key == VirtualKey.Enter) OpenButton_OnClick(null, null);
|
||||
else
|
||||
{
|
||||
VisualStateManager.GoToState(PasswordBox, "Normal", true);
|
||||
StatusTextBlock.Text = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private async void KeyFileButton_Click(object sender, RoutedEventArgs e)
|
||||
@@ -94,18 +78,7 @@ namespace ModernKeePass.Controls
|
||||
picker.FileTypeFilter.Add(".key");
|
||||
|
||||
// Application now has read/write access to the picked file
|
||||
var file = await picker.PickSingleFileAsync();
|
||||
if (file == null) return;
|
||||
var app = (App)Application.Current;
|
||||
app.Database.KeyFile = file;
|
||||
StatusTextBlock.Text = $"Key file: {file.Name}";
|
||||
}
|
||||
|
||||
private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var passwordBox = sender as PasswordBox;
|
||||
PasswordComplexityIndicator = QualityEstimation.EstimatePasswordBits(passwordBox?.Password.ToCharArray());
|
||||
NotifyPropertyChanged("PasswordComplexityIndicator");
|
||||
Model.KeyFile = await picker.PickSingleFileAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user