Created a Settings Service

Created a Recent Service
Created a Resources Service
Code refactor
Updated tests
This commit is contained in:
BONNEVILLE Geoffroy
2017-12-01 17:59:38 +01:00
parent f172e31250
commit 744858df81
35 changed files with 552 additions and 141 deletions

View File

@@ -29,7 +29,8 @@ namespace ModernKeePass.ViewModels
private string _status;
private StatusTypes _statusType;
private StorageFile _keyFile;
private string _keyFileText = "Select key file from disk...";
private string _keyFileText;
private readonly IResource _resource;
public IDatabase Database { get; set; }
@@ -100,10 +101,12 @@ namespace ModernKeePass.ViewModels
public double PasswordComplexityIndicator => QualityEstimation.EstimatePasswordBits(Password?.ToCharArray());
public CompositeKeyVm() : this((Application.Current as App)?.Database) { }
public CompositeKeyVm() : this((Application.Current as App)?.Database, new ResourcesService()) { }
public CompositeKeyVm(IDatabase database)
public CompositeKeyVm(IDatabase database, IResource resource)
{
_resource = resource;
_keyFileText = _resource.GetResourceValue("CompositeKeyDefaultKeyFile");
Database = database;
}
@@ -116,18 +119,18 @@ namespace ModernKeePass.ViewModels
}
catch (Exception e)
{
error = $"Error: {e.Message}";
error = $"{_resource.GetResourceValue("CompositeKeyErrorOpen")}: {e.Message}";
}
switch ((DatabaseService.DatabaseStatus)Database.Status)
{
case DatabaseService.DatabaseStatus.Opened:
await Task.Run( () => RootGroup = Database.RootGroup);
await Task.Run(() => RootGroup = Database.RootGroup);
return true;
case DatabaseService.DatabaseStatus.CompositeKeyError:
var errorMessage = new StringBuilder("Error: wrong ");
if (HasPassword) errorMessage.Append("password");
if (HasPassword && HasKeyFile) errorMessage.Append(" or ");
if (HasKeyFile) errorMessage.Append("key file");
var errorMessage = new StringBuilder(_resource.GetResourceValue("CompositeKeyErrorUserStart"));
if (HasPassword) errorMessage.Append(_resource.GetResourceValue("CompositeKeyErrorUserPassword"));
if (HasPassword && HasKeyFile) errorMessage.Append(_resource.GetResourceValue("CompositeKeyErrorUserOr"));
if (HasKeyFile) errorMessage.Append(_resource.GetResourceValue("CompositeKeyErrorUserKeyFile"));
UpdateStatus(errorMessage.ToString(), StatusTypes.Error);
break;
case DatabaseService.DatabaseStatus.Error:
@@ -140,7 +143,7 @@ namespace ModernKeePass.ViewModels
public void UpdateKey()
{
Database.UpdateCompositeKey(CreateCompositeKey());
UpdateStatus("Database composite key updated.", StatusTypes.Success);
UpdateStatus(_resource.GetResourceValue("CompositeKeyUpdated"), StatusTypes.Success);
}
public void CreateKeyFile(StorageFile file)

View File

@@ -5,6 +5,7 @@ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using ModernKeePass.Interfaces;
using ModernKeePass.Mappings;
using ModernKeePass.Services;
using ModernKeePassLib;
using ModernKeePassLib.Cryptography.PasswordGenerator;
using ModernKeePassLib.Security;
@@ -48,8 +49,9 @@ namespace ModernKeePass.ViewModels
{
get
{
var title = GetEntryValue(PwDefs.TitleField);
return title == null ? "< New entry >" : title;
/*var title = GetEntryValue(PwDefs.TitleField);
return title == null ? _resource.GetResourceValue("EntryNew") : title;*/
return GetEntryValue(PwDefs.TitleField);
}
set { SetEntryValue(PwDefs.TitleField, value); }
}

View File

@@ -19,10 +19,10 @@ namespace ModernKeePass.ViewModels
public GroupVm ParentGroup { get; private set; }
public GroupVm PreviousGroup { get; private set; }
public ObservableCollection<EntryVm> Entries { get; set; } = new ObservableCollection<EntryVm>();
public ObservableCollection<GroupVm> Groups { get; set; } = new ObservableCollection<GroupVm>();
public int EntryCount => Entries.Count() - 1;
public int EntryCount => Entries.Count;
// TODO: put in a converter
public FontWeight FontWeight => _pwGroup == null ? FontWeights.Bold : FontWeights.Normal;
public int GroupCount => Groups.Count - 1;
public PwUuid IdUuid => _pwGroup?.Uuid;
@@ -50,7 +50,7 @@ namespace ModernKeePass.ViewModels
public string Name
{
get { return _pwGroup == null ? "< New group >" : _pwGroup.Name; }
get { return _pwGroup == null ? string.Empty : _pwGroup.Name; }
set { _pwGroup.Name = value; }
}
@@ -86,10 +86,9 @@ namespace ModernKeePass.ViewModels
private readonly IDatabase _database;
private bool _isEditMode;
private PwEntry _reorderedEntry;
//private int _reorderedEntryIndex;
public GroupVm() {}
internal GroupVm(PwGroup pwGroup, GroupVm parent, PwUuid recycleBinId = null) : this(pwGroup, parent,
(Application.Current as App)?.Database, recycleBinId)
{ }
@@ -104,7 +103,7 @@ namespace ModernKeePass.ViewModels
Entries = new ObservableCollection<EntryVm>(pwGroup.Entries.Select(e => new EntryVm(e, this)));
Entries.CollectionChanged += Entries_CollectionChanged;
Groups = new ObservableCollection<GroupVm>(pwGroup.Groups.Select(g => new GroupVm(g, this, recycleBinId)));
Groups.Insert(0, new GroupVm ());
Groups.Insert(0, new GroupVm());
}
private void Entries_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
@@ -148,7 +147,6 @@ namespace ModernKeePass.ViewModels
Move(_database.RecycleBinEnabled && !IsSelected ? _database.RecycleBin : null);
}
public void UndoDelete()
{
Move(PreviousGroup);
@@ -192,7 +190,7 @@ namespace ModernKeePass.ViewModels
}
catch (Exception e)
{
MessageDialogService.ShowErrorDialog(e);
MessageDialogHelper.ShowErrorDialog(e);
}
}
@@ -206,7 +204,7 @@ namespace ModernKeePass.ViewModels
}
catch (Exception e)
{
MessageDialogService.ShowErrorDialog(e);
MessageDialogHelper.ShowErrorDialog(e);
}
}

View File

@@ -1,24 +1,15 @@
using System;
using Windows.Storage;
using Windows.Storage;
using ModernKeePass.Common;
using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;
namespace ModernKeePass.ViewModels
{
public class RecentItemVm: NotifyPropertyChangedBase, ISelectableModel
public class RecentItemVm: NotifyPropertyChangedBase, ISelectableModel, IRecentItem
{
private bool _isSelected;
public RecentItemVm() {}
public RecentItemVm(AccessListEntry entry, StorageFile file)
{
Token = entry.Token;
Name = entry.Metadata;
DatabaseFile = file;
}
public StorageFile DatabaseFile { get; }
public string Token { get; }
public string Name { get; }
@@ -30,6 +21,14 @@ namespace ModernKeePass.ViewModels
set { SetProperty(ref _isSelected, value); }
}
public RecentItemVm() {}
public RecentItemVm(string token, string metadata, IStorageItem file)
{
Token = token;
Name = metadata;
DatabaseFile = file as StorageFile;
}
public void OpenDatabaseFile()
{
OpenDatabaseFile((Application.Current as App)?.Database);
@@ -40,10 +39,14 @@ namespace ModernKeePass.ViewModels
database.DatabaseFile = DatabaseFile;
}
public async void UpdateAccessTime()
public void UpdateAccessTime()
{
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
await mru.GetFileAsync(Token);
UpdateAccessTime(new RecentService());
}
public async void UpdateAccessTime(IRecent recent)
{
await recent.GetFileAsync(Token);
}
}
}

View File

@@ -11,6 +11,7 @@ using ModernKeePassLib.Cryptography.KeyDerivation;
namespace ModernKeePass.ViewModels
{
// TODO: implement Kdf settings
public class SettingsDatabaseVm: NotifyPropertyChangedBase, IHasSelectableObject
{
private readonly IDatabase _database;

View File

@@ -1,23 +1,33 @@
using System.Collections.Generic;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;
namespace ModernKeePass.ViewModels
{
public class SettingsNewVm
{
private ISettings _settings;
public SettingsNewVm() : this(new SettingsService())
{ }
public SettingsNewVm(ISettings settings)
{
_settings = settings;
}
public bool IsCreateSample
{
get { return SettingsService.GetSetting<bool>("Sample"); }
set { SettingsService.PutSetting("Sample", value); }
get { return _settings.GetSetting<bool>("Sample"); }
set { _settings.PutSetting("Sample", value); }
}
public IEnumerable<string> FileFormats => new []{"2", "4"};
public string FileFormatVersion
{
get { return SettingsService.GetSetting<string>("DefaultFileFormat"); }
set { SettingsService.PutSetting("DefaultFileFormat", value); }
get { return _settings.GetSetting<string>("DefaultFileFormat"); }
set { _settings.PutSetting("DefaultFileFormat", value); }
}
}
}

View File

@@ -1,7 +1,6 @@
using System.Collections.ObjectModel;
using System.Linq;
using Windows.ApplicationModel;
using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using ModernKeePass.Common;
@@ -46,40 +45,68 @@ namespace ModernKeePass.ViewModels
public MainVm() {}
internal MainVm(Frame referenceFrame, Frame destinationFrame) : this(referenceFrame, destinationFrame, (Application.Current as App)?.Database) { }
internal MainVm(Frame referenceFrame, Frame destinationFrame) : this(referenceFrame, destinationFrame,
(Application.Current as App)?.Database, new ResourcesService(), new RecentService())
{ }
public MainVm(Frame referenceFrame, Frame destinationFrame, IDatabase database)
public MainVm(Frame referenceFrame, Frame destinationFrame, IDatabase database, IResource resource, IRecent recent)
{
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
var isDatabaseOpen = database != null && database.Status == (int) DatabaseService.DatabaseStatus.Opened;
var mainMenuItems = new ObservableCollection<MainMenuItemVm>
{
new MainMenuItemVm
{
Title = "Open", PageType = typeof(OpenDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Page2,
Title = resource.GetResourceValue("MainMenuItemOpen"),
PageType = typeof(OpenDatabasePage),
Destination = destinationFrame,
Parameter = referenceFrame,
SymbolIcon = Symbol.Page2,
IsSelected = database != null && database.Status == (int) DatabaseService.DatabaseStatus.Opening
},
new MainMenuItemVm
{
Title = "New" , PageType = typeof(NewDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Add
Title = resource.GetResourceValue("MainMenuItemNew"),
PageType = typeof(NewDatabasePage),
Destination = destinationFrame,
Parameter = referenceFrame,
SymbolIcon = Symbol.Add
},
new MainMenuItemVm
{
Title = "Save" , PageType = typeof(SaveDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Save,
IsSelected = isDatabaseOpen, IsEnabled = isDatabaseOpen
},
new MainMenuItemVm {
Title = "Recent" , PageType = typeof(RecentDatabasesPage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Copy,
IsSelected = (database == null || database.Status == (int) DatabaseService.DatabaseStatus.Closed) && mru.Entries.Count > 0, IsEnabled = mru.Entries.Count > 0
Title = resource.GetResourceValue("MainMenuItemSave"),
PageType = typeof(SaveDatabasePage),
Destination = destinationFrame,
Parameter = referenceFrame,
SymbolIcon = Symbol.Save,
IsSelected = isDatabaseOpen,
IsEnabled = isDatabaseOpen
},
new MainMenuItemVm
{
Title = "Settings" , PageType = typeof(SettingsPage), Destination = referenceFrame, SymbolIcon = Symbol.Setting
Title = resource.GetResourceValue("MainMenuItemRecent"),
PageType = typeof(RecentDatabasesPage),
Destination = destinationFrame,
Parameter = referenceFrame,
SymbolIcon = Symbol.Copy,
IsSelected =
(database == null || database.Status == (int) DatabaseService.DatabaseStatus.Closed) &&
recent.EntryCount > 0,
IsEnabled = recent.EntryCount > 0
},
new MainMenuItemVm
{
Title = "About" , PageType = typeof(AboutPage), Destination = destinationFrame, SymbolIcon = Symbol.Help
Title = resource.GetResourceValue("MainMenuItemSettings"),
PageType = typeof(SettingsPage),
Destination = referenceFrame,
SymbolIcon = Symbol.Setting
},
new MainMenuItemVm
{
Title = resource.GetResourceValue("MainMenuItemAbout"),
PageType = typeof(AboutPage),
Destination = destinationFrame,
SymbolIcon = Symbol.Help
}
};
// Auto-select the Recent Items menu item if the conditions are met

View File

@@ -1,5 +1,4 @@
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using ModernKeePass.Common;
using ModernKeePass.Interfaces;
@@ -23,19 +22,23 @@ namespace ModernKeePass.ViewModels
if (database == null || database.Status != (int) DatabaseService.DatabaseStatus.Opening) return;
OpenFile(database.DatabaseFile);
}
public void OpenFile(StorageFile file)
{
OpenFile(file, new RecentService());
}
public void OpenFile(StorageFile file, IRecent recent)
{
_database.DatabaseFile = file;
OnPropertyChanged("Name");
OnPropertyChanged("ShowPasswordBox");
AddToRecentList(file);
AddToRecentList(file, recent);
}
private void AddToRecentList(StorageFile file)
private void AddToRecentList(StorageFile file, IRecent recent)
{
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
mru.Add(file, file.DisplayName);
recent.Add(file, file.DisplayName);
}
}
}

View File

@@ -1,17 +1,16 @@
using System;
using System.Collections.ObjectModel;
using Windows.Storage.AccessCache;
using System.Collections.ObjectModel;
using ModernKeePass.Common;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;
namespace ModernKeePass.ViewModels
{
public class RecentVm : NotifyPropertyChangedBase, IHasSelectableObject
{
private ISelectableModel _selectedItem;
private ObservableCollection<RecentItemVm> _recentItems = new ObservableCollection<RecentItemVm>();
private ObservableCollection<IRecentItem> _recentItems = new ObservableCollection<IRecentItem>();
public ObservableCollection<RecentItemVm> RecentItems
public ObservableCollection<IRecentItem> RecentItems
{
get { return _recentItems; }
set { SetProperty(ref _recentItems, value); }
@@ -35,23 +34,14 @@ namespace ModernKeePass.ViewModels
}
}
public RecentVm()
public RecentVm() : this (new RecentService())
{ }
public RecentVm(IRecent recent)
{
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
foreach (var entry in mru.Entries)
{
try
{
var file = mru.GetFileAsync(entry.Token, AccessCacheOptions.SuppressAccessTimeUpdate).GetAwaiter().GetResult();
RecentItems.Add(new RecentItemVm(entry, file));
}
catch (Exception)
{
mru.Remove(entry.Token);
}
}
RecentItems = recent.GetAllFiles();
if (RecentItems.Count > 0)
SelectedItem = RecentItems[0];
SelectedItem = RecentItems[0] as RecentItemVm;
}
}

View File

@@ -6,6 +6,7 @@ using ModernKeePass.Common;
using ModernKeePass.Interfaces;
using ModernKeePass.Pages;
using ModernKeePass.Pages.SettingsPageFrames;
using ModernKeePass.Services;
namespace ModernKeePass.ViewModels
{
@@ -41,32 +42,32 @@ namespace ModernKeePass.ViewModels
}
}
public SettingsVm() : this((Application.Current as App)?.Database) { }
public SettingsVm() : this((Application.Current as App)?.Database, new ResourcesService()) { }
public SettingsVm(IDatabase database)
public SettingsVm(IDatabase database, IResource resource)
{
var menuItems = new ObservableCollection<ListMenuItemVm>
{
new ListMenuItemVm
{
Title = "New",
Group = "Application",
Title = resource.GetResourceValue("SettingsMenuItemNew"),
Group = resource.GetResourceValue("SettingsMenuGroupApplication"),
SymbolIcon = Symbol.Add,
PageType = typeof(SettingsNewDatabasePage),
IsSelected = true
},
new ListMenuItemVm
{
Title = "General",
Group = "Database",
Title = resource.GetResourceValue("SettingsMenuItemGeneral"),
Group = resource.GetResourceValue("SettingsMenuGroupDatabase"),
SymbolIcon = Symbol.Setting,
PageType = typeof(SettingsDatabasePage),
IsEnabled = database?.Status == 2
},
new ListMenuItemVm
{
Title = "Security",
Group = "Database",
Title = resource.GetResourceValue("SettingsMenuItemSecurity"),
Group = resource.GetResourceValue("SettingsMenuGroupDatabase"),
SymbolIcon = Symbol.Permissions,
PageType = typeof(SettingsSecurityPage),
IsEnabled = database?.Status == 2