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:
2017-11-07 18:45:35 +01:00
committed by BONNEVILLE Geoffroy
parent c3b8c97eea
commit 10a4941b26
24 changed files with 261 additions and 106 deletions

View File

@@ -0,0 +1,113 @@
using Windows.Storage;
using Windows.UI.Xaml;
using ModernKeePass.Common;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Serialization;
namespace ModernKeePass.ViewModels
{
public class CompositeKeyVm: NotifyPropertyChangedBase
{
public enum StatusTypes
{
Normal = 0,
Error = 1,
Warning = 3,
Success = 5
}
private readonly App _app = Application.Current as App;
private bool _hasPassword;
private bool _hasKeyFile;
private string _password;
private string _status;
private StatusTypes _statusType;
private StorageFile _keyFile;
public bool HasPassword
{
get { return _hasPassword; }
set
{
SetProperty(ref _hasPassword, value);
OnPropertyChanged("IsValid");
}
}
public bool HasKeyFile
{
get { return _hasKeyFile; }
set
{
SetProperty(ref _hasKeyFile, value);
OnPropertyChanged("IsValid");
}
}
public bool IsValid => HasPassword || HasKeyFile;
public string Status
{
get { return _status; }
set { SetProperty(ref _status, value); }
}
public int StatusType
{
get { return (int)_statusType; }
set { SetProperty(ref _statusType, (StatusTypes)value); }
}
public string Password
{
get { return _password; }
set
{
_password = value;
OnPropertyChanged("PasswordComplexityIndicator");
StatusType = (int)StatusTypes.Normal;
}
}
public StorageFile KeyFile
{
get { return _keyFile; }
set
{
_keyFile = value;
UpdateStatus($"Key file: {value.Name}", StatusTypes.Normal);
}
}
public GroupVm RootGroup { get; set; }
public double PasswordComplexityIndicator => QualityEstimation.EstimatePasswordBits(Password?.ToCharArray());
public DatabaseHelper.DatabaseStatus OpenDatabase(bool createNew)
{
UpdateStatus(_app.Database.Open(CreateCompositeKey(), createNew), StatusTypes.Error);
RootGroup = _app.Database.RootGroup;
return _app.Database.Status;
}
public void UpdateKey()
{
_app.Database.UpdateCompositeKey(CreateCompositeKey());
UpdateStatus("Database composite key updated.", StatusTypes.Success);
}
private void UpdateStatus(string text, StatusTypes type)
{
Status = text;
StatusType = (int)type;
}
private CompositeKey CreateCompositeKey()
{
var compositeKey = new CompositeKey();
if (HasPassword) compositeKey.AddUserKey(new KcpPassword(Password));
if (HasKeyFile) compositeKey.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromFile(KeyFile)));
return compositeKey;
}
}
}

View File

@@ -150,7 +150,7 @@ namespace ModernKeePass.ViewModels
public event PropertyChangedEventHandler PropertyChanged;
private readonly PwEntry _pwEntry;
private readonly App _app = (App)Application.Current;
private readonly App _app = Application.Current as App;
private bool _isEditMode;
private bool _isRevealPassword;
private double _passwordLength = 25;

View File

@@ -86,7 +86,7 @@ namespace ModernKeePass.ViewModels
}
private readonly PwGroup _pwGroup;
private readonly App _app = (App)Application.Current;
private readonly App _app = Application.Current as App;
private bool _isEditMode;
public GroupVm() {}

View File

@@ -1,13 +1,15 @@
using Windows.Storage;
using ModernKeePass.Common;
using Windows.Storage.AccessCache;
using ModernKeePass.Interfaces;
namespace ModernKeePass.ViewModels
{
public class RecentItemVm: NotifyPropertyChangedBase
public class RecentItemVm: NotifyPropertyChangedBase, ISelectableModel
{
private bool _isSelected;
public RecentItemVm() {}
public RecentItemVm(AccessListEntry entry, StorageFile file)
{
Token = entry.Token;
@@ -15,15 +17,15 @@ namespace ModernKeePass.ViewModels
DatabaseFile = file;
}
public StorageFile DatabaseFile { get; private set; }
public string Token { get; private set; }
public string Name { get; private set; } = "Recent file";
public string Path => DatabaseFile.Path;
public StorageFile DatabaseFile { get; }
public string Token { get; }
public string Name { get; }
public string Path => DatabaseFile?.Path;
public bool IsSelected
{
get { return _isSelected; }
internal set { SetProperty(ref _isSelected, value); }
set { SetProperty(ref _isSelected, value); }
}
}
}

View File

@@ -14,7 +14,7 @@ namespace ModernKeePass.ViewModels
{
public class SettingsDatabaseVm: NotifyPropertyChangedBase, IHasSelectableObject
{
private readonly App _app = (App)Application.Current;
private readonly App _app = Application.Current as App;
private readonly ApplicationDataContainer _localSettings = ApplicationData.Current.LocalSettings;
private GroupVm _selectedItem;

View File

@@ -20,9 +20,9 @@ namespace ModernKeePass.ViewModels
public OpenVm()
{
var database = ((App)Application.Current).Database;
if (database == null || database.Status != DatabaseHelper.DatabaseStatus.Opening) return;
OpenFile(database.DatabaseFile);
var app = Application.Current as App;
if (app?.Database == null || app.Database.Status != DatabaseHelper.DatabaseStatus.Opening) return;
OpenFile(app.Database.DatabaseFile);
}
public event PropertyChangedEventHandler PropertyChanged;

View File

@@ -2,12 +2,13 @@
using System.Collections.ObjectModel;
using Windows.Storage.AccessCache;
using ModernKeePass.Common;
using ModernKeePass.Interfaces;
namespace ModernKeePass.ViewModels
{
public class RecentVm : NotifyPropertyChangedBase
public class RecentVm : NotifyPropertyChangedBase, IHasSelectableObject
{
private RecentItemVm _selectedItem;
private ISelectableModel _selectedItem;
private ObservableCollection<RecentItemVm> _recentItems = new ObservableCollection<RecentItemVm>();
public ObservableCollection<RecentItemVm> RecentItems
@@ -16,7 +17,7 @@ namespace ModernKeePass.ViewModels
set { SetProperty(ref _recentItems, value); }
}
public RecentItemVm SelectedItem
public ISelectableModel SelectedItem
{
get { return _selectedItem; }
set

View File

@@ -37,6 +37,7 @@ namespace ModernKeePass.ViewModels
MenuItems = new ObservableCollection<ListMenuItemVm>
{
new ListMenuItemVm { Title = "Database", SymbolIcon = Symbol.Setting, PageType = typeof(SettingsDatabasePage), IsSelected = true },
new ListMenuItemVm { Title = "Security", SymbolIcon = Symbol.Permissions, PageType = typeof(SettingsSecurityPage) },
//new ListMenuItemVm { Title = "General", SymbolIcon = Symbol.Edit, PageType = typeof(SettingsGeneralPage) }
};
SelectedItem = MenuItems.FirstOrDefault(m => m.IsSelected);