Create database works with new Vm

Refactoring
This commit is contained in:
Geoffroy BONNEVILLE
2020-04-22 16:21:47 +02:00
parent a88051bc0c
commit a7da427ded
36 changed files with 371 additions and 274 deletions

View File

@@ -0,0 +1,27 @@
using Windows.ApplicationModel;
namespace ModernKeePass.ViewModels
{
public class AboutVm
{
private readonly Package _package;
public string Name => _package.DisplayName;
public string Version
{
get
{
var version = _package.Id.Version;
return $"{version.Major}.{version.Minor}";
}
}
public AboutVm() : this(Package.Current) { }
public AboutVm(Package package)
{
_package = package;
}
}
}

View File

@@ -1,11 +1,11 @@
using System;
using Windows.UI.Xaml.Controls;
using ModernKeePass.Domain.AOP;
using GalaSoft.MvvmLight;
using ModernKeePass.Domain.Interfaces;
namespace ModernKeePass.ViewModels.ListItems
{
public class ListMenuItemVm : NotifyPropertyChangedBase, IIsEnabled, ISelectableModel
public class ListMenuItemVm : ObservableObject, IIsEnabled, ISelectableModel
{
private bool _isSelected;
@@ -19,7 +19,7 @@ namespace ModernKeePass.ViewModels.ListItems
public bool IsSelected
{
get { return _isSelected; }
set { SetProperty(ref _isSelected, value); }
set { Set(() => IsSelected, ref _isSelected, value); }
}
public override string ToString()

View File

@@ -1,10 +1,10 @@
using ModernKeePass.Domain.AOP;
using GalaSoft.MvvmLight;
using ModernKeePass.Domain.Dtos;
using ModernKeePass.Domain.Interfaces;
namespace ModernKeePass.ViewModels.ListItems
{
public class RecentItemVm: NotifyPropertyChangedBase, ISelectableModel
public class RecentItemVm: ObservableObject, ISelectableModel
{
private bool _isSelected;
private string _name;
@@ -14,25 +14,25 @@ namespace ModernKeePass.ViewModels.ListItems
public string Token
{
get { return _token; }
set { SetProperty(ref _token, value); }
set { Set(() => Token, ref _token, value); }
}
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value); }
set { Set(() => Name, ref _name, value); }
}
public string Path
{
get { return _path; }
set { SetProperty(ref _path, value); }
set { Set(() => Path, ref _path, value); }
}
public bool IsSelected
{
get { return _isSelected; }
set { SetProperty(ref _isSelected, value); }
set { Set(() => IsSelected, ref _isSelected, value); }
}
public RecentItemVm(FileInfo file)

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
@@ -16,12 +17,11 @@ using ModernKeePass.Application.Parameters.Models;
using ModernKeePass.Application.Parameters.Queries.GetCiphers;
using ModernKeePass.Application.Parameters.Queries.GetCompressions;
using ModernKeePass.Application.Parameters.Queries.GetKeyDerivations;
using ModernKeePass.Domain.AOP;
namespace ModernKeePass.ViewModels.ListItems
{
// TODO: implement Kdf settings
public class SettingsDatabaseVm: NotifyPropertyChangedBase
public class SettingsDatabaseVm: ObservableObject
{
private readonly IMediator _mediator;
private readonly DatabaseVm _database;
@@ -32,7 +32,7 @@ namespace ModernKeePass.ViewModels.ListItems
set
{
_mediator.Send(new SetHasRecycleBinCommand {HasRecycleBin = value}).Wait();
OnPropertyChanged(nameof(HasRecycleBin));
RaisePropertyChanged(nameof(HasRecycleBin));
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Common;
@@ -23,12 +24,34 @@ namespace ModernKeePass.ViewModels.ListItems
set { _settings.PutSetting(Constants.Settings.Sample, value); }
}
public IEnumerable<string> FileFormats => new []{"2", "4"};
public string FileFormatVersion
public IEnumerable<DatabaseFormat> FileFormats => new []
{
get { return _settings.GetSetting<string>(Constants.Settings.DefaultFileFormat); }
set { _settings.PutSetting(Constants.Settings.DefaultFileFormat, value); }
new DatabaseFormat
{
Version = "4",
DisplayText = "4 (Argon2, ChaCha20)"
},
new DatabaseFormat
{
Version = "3",
DisplayText = "3 (AES-KDF, AES/Rijndael)"
}
};
public DatabaseFormat DatabaseFormatVersion
{
get
{
var version = _settings.GetSetting<string>(Constants.Settings.DefaultFileFormat);
return FileFormats.FirstOrDefault(f => f.Version == version);
}
set { _settings.PutSetting(Constants.Settings.DefaultFileFormat, value.Version); }
}
}
public class DatabaseFormat
{
public string Version { get; set; }
public string DisplayText { get; set; }
}
}

View File

@@ -0,0 +1,71 @@
using System.Threading.Tasks;
using Windows.Storage;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
using MediatR;
using Messages;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Database.Commands.CreateDatabase;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Common;
using ModernKeePass.Models;
namespace ModernKeePass.ViewModels
{
public class NewVm : OpenVm
{
private readonly IMediator _mediator;
private readonly ISettingsProxy _settings;
private readonly INavigationService _navigation;
private string _importFormatHelp;
public bool IsImportChecked { get; set; }
public IStorageFile ImportFile { get; set; }
public string ImportFileExtensionFilter { get; set; } = "*";
public IImportFormat ImportFormat { get; set; }
public string ImportFormatHelp
{
get { return _importFormatHelp; }
set
{
_importFormatHelp = value;
RaisePropertyChanged(nameof(ImportFormatHelp));
}
}
public NewVm(): this(
App.Services.GetRequiredService<IMediator>(),
App.Services.GetRequiredService<ISettingsProxy>(),
App.Services.GetRequiredService<IMessenger>(),
App.Services.GetRequiredService<INavigationService>()) { }
public NewVm(IMediator mediator, ISettingsProxy settings, IMessenger messenger, INavigationService navigation)
{
_mediator = mediator;
_settings = settings;
_navigation = navigation;
messenger.Register<CredentialsSetMessage>(this, async message => await CreateDatabase(message));
}
public async Task CreateDatabase(CredentialsSetMessage message)
{
await _mediator.Send(new CreateDatabaseCommand
{
FilePath = Token,
KeyFilePath = message.KeyFilePath,
Password = message.Password,
Name = "ModernKeePass",
Version = _settings.GetSetting(Constants.Settings.DefaultFileFormat, "4"),
CreateSampleData = _settings.GetSetting<bool>(Constants.Settings.Sample)
});
var database = await _mediator.Send(new GetDatabaseQuery());
_navigation.NavigateTo(Constants.Navigation.GroupPage, new NavigationItem { Id = database.RootGroupId });
}
}
}

View File

@@ -0,0 +1,56 @@
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.Dtos;
namespace ModernKeePass.ViewModels
{
public class OpenVm: ObservableObject
{
private readonly IRecentProxy _recent;
private string _name;
private string _path;
private string _token;
public bool IsFileSelected => !string.IsNullOrEmpty(Path);
public string Token
{
get { return _token; }
set { Set(() => Token, ref _token, value); }
}
public string Name
{
get { return _name; }
private set { Set(() => Name, ref _name, value); }
}
public string Path
{
get { return _path; }
private set { Set(() => Path, ref _path, value); }
}
public OpenVm(): this(App.Services.GetRequiredService<IRecentProxy>()) { }
public OpenVm(IRecentProxy recent)
{
_recent = recent;
}
public async Task OpenFile(FileInfo file)
{
Token = file.Id;
Name = file.Name;
Path = file.Path;
RaisePropertyChanged(nameof(IsFileSelected));
await AddToRecentList(file);
}
private async Task AddToRecentList(FileInfo file)
{
await _recent.Add(file);
}
}
}

View File

@@ -0,0 +1,69 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.ViewModels.ListItems;
namespace ModernKeePass.ViewModels
{
public class RecentVm : ObservableObject, IHasSelectableObject
{
private readonly IRecentProxy _recent;
private ISelectableModel _selectedItem;
private ObservableCollection<RecentItemVm> _recentItems;
public ObservableCollection<RecentItemVm> RecentItems
{
get { return _recentItems; }
set { Set(() => RecentItems, ref _recentItems, value); }
}
public ISelectableModel SelectedItem
{
get { return _selectedItem; }
set
{
if (_selectedItem == value) return;
if (_selectedItem != null)
{
_selectedItem.IsSelected = false;
}
Set(() => SelectedItem, ref _selectedItem, value);
if (_selectedItem == null) return;
_selectedItem.IsSelected = true;
}
}
public ICommand ClearAllCommand { get; }
public RecentVm() : this (App.Services.GetRequiredService<IRecentProxy>()) { }
public RecentVm(IRecentProxy recent)
{
_recent = recent;
ClearAllCommand = new RelayCommand(ClearAll);
var recentItems = _recent.GetAll().Select(r => new RecentItemVm(r));
RecentItems = new ObservableCollection<RecentItemVm>(recentItems);
if (RecentItems.Count > 0)
SelectedItem = RecentItems[0];
}
public void UpdateAccessTime(string token)
{
_recent.Get(token, true).Wait();
}
private void ClearAll()
{
_recent.ClearAll();
RecentItems.Clear();
}
}
}

View File

@@ -0,0 +1,55 @@
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.AccessCache;
using GalaSoft.MvvmLight.Views;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Database.Commands.CloseDatabase;
using ModernKeePass.Application.Database.Commands.SaveDatabase;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Common;
using RelayCommand = GalaSoft.MvvmLight.Command.RelayCommand;
namespace ModernKeePass.ViewModels
{
public class SaveVm
{
public bool IsSaveEnabled => _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult().IsDirty;
public RelayCommand SaveCommand { get; }
public RelayCommand CloseCommand { get; }
private readonly IMediator _mediator;
private readonly INavigationService _navigation;
public SaveVm() : this(App.Services.GetRequiredService<IMediator>(), App.Services.GetRequiredService<INavigationService>()) { }
public SaveVm(IMediator mediator, INavigationService navigation)
{
_mediator = mediator;
_navigation = navigation;
SaveCommand = new RelayCommand(async () => await Save(), () => IsSaveEnabled);
CloseCommand = new RelayCommand(async () => await Close());
}
public async Task Save(bool close = true)
{
await _mediator.Send(new SaveDatabaseCommand());
if (close) await _mediator.Send(new CloseDatabaseCommand());
_navigation.NavigateTo(Constants.Navigation.MainPage);
}
public async Task Save(StorageFile file)
{
var token = StorageApplicationPermissions.FutureAccessList.Add(file);
await _mediator.Send(new SaveDatabaseCommand { FilePath = token });
_navigation.NavigateTo(Constants.Navigation.MainPage);
}
public async Task Close()
{
await _mediator.Send(new CloseDatabaseCommand());
_navigation.NavigateTo(Constants.Navigation.MainPage);
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
@@ -13,11 +14,10 @@ using ModernKeePass.Application.Database.Commands.SaveDatabase;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Application.Database.Queries.OpenDatabase;
using ModernKeePass.Common;
using ModernKeePass.Domain.AOP;
namespace ModernKeePass.ViewModels
{
public class OpenDatabaseControlVm : NotifyPropertyChangedBase
public class OpenDatabaseControlVm : ObservableObject
{
public enum StatusTypes
{
@@ -32,8 +32,8 @@ namespace ModernKeePass.ViewModels
get { return _hasPassword; }
set
{
SetProperty(ref _hasPassword, value);
OnPropertyChanged(nameof(IsValid));
Set(() => HasPassword, ref _hasPassword, value);
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
}
}
@@ -43,8 +43,8 @@ namespace ModernKeePass.ViewModels
get { return _hasKeyFile; }
set
{
SetProperty(ref _hasKeyFile, value);
OnPropertyChanged(nameof(IsValid));
Set(() => HasKeyFile, ref _hasKeyFile, value);
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
}
}
@@ -54,13 +54,13 @@ namespace ModernKeePass.ViewModels
public string Status
{
get { return _status; }
set { SetProperty(ref _status, value); }
set { Set(() => Status, ref _status, value); }
}
public int StatusType
{
get { return (int)_statusType; }
set { SetProperty(ref _statusType, (StatusTypes)value); }
get { return _statusType; }
set { Set(() => StatusType, ref _statusType, value); }
}
public string Password
@@ -80,7 +80,7 @@ namespace ModernKeePass.ViewModels
set
{
_keyFilePath = value;
OnPropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
}
}
@@ -88,18 +88,18 @@ namespace ModernKeePass.ViewModels
public string KeyFileText
{
get { return _keyFileText; }
set { SetProperty(ref _keyFileText, value); }
set { Set(() => KeyFileText, ref _keyFileText, value); }
}
public string OpenButtonLabel
{
get { return _openButtonLabel; }
set { SetProperty(ref _openButtonLabel, value); }
set { Set(() => OpenButtonLabel, ref _openButtonLabel, value); }
}
public RelayCommand<string> OpenDatabaseCommand { get; }
protected readonly IMediator Mediator;
private readonly IMediator _mediator;
private readonly IResourceProxy _resource;
private readonly IMessenger _messenger;
private readonly IDialogService _dialog;
@@ -108,7 +108,7 @@ namespace ModernKeePass.ViewModels
private bool _isOpening;
private string _password = string.Empty;
private string _status;
private StatusTypes _statusType;
private int _statusType;
private string _keyFilePath;
private string _keyFileText;
private string _openButtonLabel;
@@ -123,7 +123,7 @@ namespace ModernKeePass.ViewModels
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, IMessenger messenger, IDialogService dialog)
{
Mediator = mediator;
_mediator = mediator;
_resource = resource;
_messenger = messenger;
_dialog = dialog;
@@ -136,7 +136,7 @@ namespace ModernKeePass.ViewModels
{
_messenger.Send(new DatabaseOpeningMessage {Token = databaseFilePath});
var database = await Mediator.Send(new GetDatabaseQuery());
var database = await _mediator.Send(new GetDatabaseQuery());
if (database.IsOpen)
{
await _dialog.ShowMessage(_resource.GetResourceValue("MessageDialogDBOpenTitle"),
@@ -147,16 +147,16 @@ namespace ModernKeePass.ViewModels
{
if (isOk)
{
await Mediator.Send(new SaveDatabaseCommand());
await _mediator.Send(new SaveDatabaseCommand());
ToastNotificationHelper.ShowGenericToast(
database.Name,
_resource.GetResourceValue("ToastSavedMessage"));
await Mediator.Send(new CloseDatabaseCommand());
await _mediator.Send(new CloseDatabaseCommand());
await OpenDatabase(databaseFilePath);
}
else
{
await Mediator.Send(new CloseDatabaseCommand());
await _mediator.Send(new CloseDatabaseCommand());
await OpenDatabase(databaseFilePath);
}
});
@@ -171,15 +171,15 @@ namespace ModernKeePass.ViewModels
_isOpening = true;
try
{
OnPropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
await Mediator.Send(new OpenDatabaseQuery
await _mediator.Send(new OpenDatabaseQuery
{
FilePath = databaseFilePath,
KeyFilePath = HasKeyFile ? KeyFilePath : null,
Password = HasPassword ? Password : null,
});
var rootGroupId = (await Mediator.Send(new GetDatabaseQuery())).RootGroupId;
var rootGroupId = (await _mediator.Send(new GetDatabaseQuery())).RootGroupId;
_messenger.Send(new DatabaseOpenedMessage { RootGroupId = rootGroupId });
}
@@ -198,7 +198,7 @@ namespace ModernKeePass.ViewModels
finally
{
_isOpening = false;
OnPropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
OpenButtonLabel = oldLabel;
}

View File

@@ -1,46 +1,120 @@
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Messages;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Database.Commands.CreateDatabase;
using ModernKeePass.Common;
using ModernKeePass.Domain.Dtos;
namespace ModernKeePass.ViewModels
{
public class SetCredentialsViewModel : OpenDatabaseControlVm
public class SetCredentialsViewModel : ObservableObject
{
private readonly ICredentialsProxy _credentials;
private readonly ISettingsProxy _settings;
private string _confirmPassword;
private readonly IMessenger _messenger;
public bool HasPassword
{
get { return _hasPassword; }
set
{
Set(() => HasPassword, ref _hasPassword, value);
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public bool HasKeyFile
{
get { return _hasKeyFile; }
set
{
Set(() => HasKeyFile, ref _hasKeyFile, value);
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string Status
{
get { return _status; }
set { Set(() => Status, ref _status, value); }
}
public string Password
{
get { return _password; }
set
{
_password = value;
RaisePropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(PasswordComplexityIndicator));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string ConfirmPassword
{
get { return _confirmPassword; }
set { SetProperty(ref _confirmPassword, value); }
set
{
_confirmPassword = value;
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string KeyFilePath
{
get { return _keyFilePath; }
set
{
_keyFilePath = value;
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string KeyFileText
{
get { return _keyFileText; }
set { Set(() => KeyFileText, ref _keyFileText, value); }
}
public string OpenButtonLabel
{
get { return _openButtonLabel; }
set { Set(() => OpenButtonLabel, ref _openButtonLabel, value); }
}
public double PasswordComplexityIndicator => _credentials.EstimatePasswordComplexity(Password);
public new bool IsValid => HasPassword && Password == ConfirmPassword || HasKeyFile && KeyFilePath != string.Empty;
public bool IsValid => HasPassword && Password == ConfirmPassword || HasKeyFile && KeyFilePath != string.Empty;
public SetCredentialsViewModel(): this(App.Services.GetRequiredService<ICredentialsProxy>(), App.Services.GetRequiredService<ISettingsProxy>()) { }
public RelayCommand GenerateCredentialsCommand{ get; }
public SetCredentialsViewModel(ICredentialsProxy credentials, ISettingsProxy settings)
private bool _hasPassword;
private bool _hasKeyFile;
private string _password = string.Empty;
private string _confirmPassword;
private string _status;
private string _keyFilePath;
private string _keyFileText;
private string _openButtonLabel;
public SetCredentialsViewModel(): this(App.Services.GetRequiredService<ICredentialsProxy>(), App.Services.GetRequiredService<IMessenger>()) { }
public SetCredentialsViewModel(ICredentialsProxy credentials, IMessenger messenger)
{
_credentials = credentials;
_settings = settings;
_messenger = messenger;
GenerateCredentialsCommand = new RelayCommand(GenerateCredentials, () => IsValid);
}
public async Task CreateDatabase(FileInfo fileInfo)
private void GenerateCredentials()
{
await Mediator.Send(new CreateDatabaseCommand
_messenger.Send(new CredentialsSetMessage
{
FilePath = fileInfo.Path,
KeyFilePath = HasKeyFile ? KeyFilePath : null,
Password = HasPassword ? Password : null,
Name = "New Database",
CreateSampleData = _settings.GetSetting<bool>(Constants.Settings.Sample)
KeyFilePath = HasKeyFile ? KeyFilePath : null
});
}
}