Creation of a notification service

This commit is contained in:
Geoffroy BONNEVILLE
2020-04-22 18:12:28 +02:00
parent 61f5e9df0b
commit a9ed588c9a
18 changed files with 201 additions and 194 deletions

View File

@@ -6,26 +6,27 @@ using Messages;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Database.Commands.UpdateCredentials;
using ModernKeePass.Application.Database.Queries.GetDatabase;
namespace ModernKeePass.ViewModels.ListItems
{
public class SettingsSecurityVm
{
private readonly IMediator _mediator;
private readonly ISettingsProxy _settings;
private readonly INavigationService _navigation;
private readonly IResourceProxy _resource;
private readonly INotificationService _notification;
public SettingsSecurityVm(): this(
App.Services.GetRequiredService<IMediator>(),
App.Services.GetRequiredService<ISettingsProxy>(),
App.Services.GetRequiredService<IMessenger>(),
App.Services.GetRequiredService<INavigationService>()) { }
App.Services.GetRequiredService<IMessenger>(),
App.Services.GetRequiredService<IResourceProxy>(),
App.Services.GetRequiredService<INotificationService>()) { }
public SettingsSecurityVm(IMediator mediator, ISettingsProxy settings, IMessenger messenger, INavigationService navigation)
public SettingsSecurityVm(IMediator mediator, IMessenger messenger, IResourceProxy resource, INotificationService notification)
{
_mediator = mediator;
_settings = settings;
_navigation = navigation;
_resource = resource;
_notification = notification;
messenger.Register<CredentialsSetMessage>(this, async message => await UpdateDatabaseCredentials(message));
}
@@ -37,7 +38,8 @@ namespace ModernKeePass.ViewModels.ListItems
KeyFilePath = message.KeyFilePath,
Password = message.Password
});
//TODO: Add Toast
var database = await _mediator.Send(new GetDatabaseQuery());
_notification.Show(database.Name, _resource.GetResourceValue("CompositeKeyUpdated"));
}
}
}

View File

@@ -13,7 +13,6 @@ using ModernKeePass.Application.Database.Commands.CloseDatabase;
using ModernKeePass.Application.Database.Commands.SaveDatabase;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Application.Database.Queries.OpenDatabase;
using ModernKeePass.Common;
namespace ModernKeePass.ViewModels
{
@@ -103,6 +102,7 @@ namespace ModernKeePass.ViewModels
private readonly IResourceProxy _resource;
private readonly IMessenger _messenger;
private readonly IDialogService _dialog;
private readonly INotificationService _notification;
private bool _hasPassword;
private bool _hasKeyFile;
private bool _isOpening;
@@ -117,15 +117,17 @@ namespace ModernKeePass.ViewModels
App.Services.GetRequiredService<IMediator>(),
App.Services.GetRequiredService<IResourceProxy>(),
App.Services.GetRequiredService<IMessenger>(),
App.Services.GetRequiredService<IDialogService>())
App.Services.GetRequiredService<IDialogService>(),
App.Services.GetRequiredService<INotificationService>())
{ }
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, IMessenger messenger, IDialogService dialog)
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, IMessenger messenger, IDialogService dialog, INotificationService notification)
{
_mediator = mediator;
_resource = resource;
_messenger = messenger;
_dialog = dialog;
_notification = notification;
OpenDatabaseCommand = new RelayCommand<string>(async databaseFilePath => await TryOpenDatabase(databaseFilePath), _ => IsValid);
_keyFileText = _resource.GetResourceValue("CompositeKeyDefaultKeyFile");
_openButtonLabel = _resource.GetResourceValue("CompositeKeyOpenButtonLabel");
@@ -147,9 +149,7 @@ namespace ModernKeePass.ViewModels
if (isOk)
{
await _mediator.Send(new SaveDatabaseCommand());
ToastNotificationHelper.ShowGenericToast(
database.Name,
_resource.GetResourceValue("ToastSavedMessage"));
_notification.Show(database.Name, _resource.GetResourceValue("ToastSavedMessage"));
await _mediator.Send(new CloseDatabaseCommand());
await OpenDatabase(databaseFilePath);
}

View File

@@ -0,0 +1,137 @@
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MediatR;
using Messages;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Security.Commands.GenerateKeyFile;
namespace ModernKeePass.ViewModels
{
public class SetCredentialsVm : ObservableObject
{
private readonly IMediator _mediator;
private readonly ICredentialsProxy _credentials;
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
{
_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 bool IsValid => HasPassword && Password == ConfirmPassword || HasKeyFile && !string.IsNullOrEmpty(KeyFilePath);
public RelayCommand GenerateCredentialsCommand{ get; }
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 SetCredentialsVm(): this(
App.Services.GetRequiredService<IMediator>(),
App.Services.GetRequiredService<ICredentialsProxy>(),
App.Services.GetRequiredService<IMessenger>(),
App.Services.GetRequiredService<IResourceProxy>()) { }
public SetCredentialsVm(IMediator mediator, ICredentialsProxy credentials, IMessenger messenger, IResourceProxy resource)
{
_mediator = mediator;
_credentials = credentials;
_messenger = messenger;
GenerateCredentialsCommand = new RelayCommand(GenerateCredentials, () => IsValid);
_keyFileText = resource.GetResourceValue("CompositeKeyDefaultKeyFile");
}
public async Task GenerateKeyFile()
{
await _mediator.Send(new GenerateKeyFileCommand {KeyFilePath = KeyFilePath});
}
private void GenerateCredentials()
{
_messenger.Send(new CredentialsSetMessage
{
Password = HasPassword ? Password : null,
KeyFilePath = HasKeyFile ? KeyFilePath : null
});
}
}
}