mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Save error is now handled via Messenger instead of unhandled exception handler (which didn't work)
Save as actually works now WIP Styles Code cleanup
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Threading.Tasks;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using GalaSoft.MvvmLight.Views;
|
||||
using MediatR;
|
||||
using Messages;
|
||||
@@ -15,16 +16,31 @@ namespace ModernKeePass.ViewModels
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ISettingsProxy _settings;
|
||||
private readonly INavigationService _navigation;
|
||||
private readonly IFileProxy _file;
|
||||
|
||||
public RelayCommand CreateDatabaseFileCommand { get; }
|
||||
|
||||
public NewVm(IMediator mediator, IRecentProxy recent, ISettingsProxy settings, INavigationService navigation)
|
||||
public NewVm(IMediator mediator, ISettingsProxy settings, INavigationService navigation, IFileProxy file): base(file)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_settings = settings;
|
||||
_navigation = navigation;
|
||||
_file = file;
|
||||
|
||||
CreateDatabaseFileCommand = new RelayCommand(async () => await CreateDatabaseFile());
|
||||
|
||||
MessengerInstance.Register<CredentialsSetMessage>(this, async m => await TryCreateDatabase(m));
|
||||
}
|
||||
|
||||
private async Task CreateDatabaseFile()
|
||||
{
|
||||
// TODO: get these from resource
|
||||
var file = await _file.CreateFile("New Database", Domain.Common.Constants.Extensions.Kdbx, "KeePass 2.x database", true);
|
||||
Token = file.Id;
|
||||
Path = file.Path;
|
||||
Name = file.Name;
|
||||
}
|
||||
|
||||
private async Task TryCreateDatabase(CredentialsSetMessage message)
|
||||
{
|
||||
var database = await _mediator.Send(new GetDatabaseQuery());
|
||||
@@ -45,7 +61,7 @@ namespace ModernKeePass.ViewModels
|
||||
Password = message.Password,
|
||||
Name = "ModernKeePass",
|
||||
Version = _settings.GetSetting(Constants.Settings.DefaultFileFormat, "4"),
|
||||
CreateSampleData = _settings.GetSetting<bool>(Constants.Settings.Sample, true)
|
||||
CreateSampleData = _settings.GetSetting(Constants.Settings.Sample, true)
|
||||
});
|
||||
|
||||
var database = await _mediator.Send(new GetDatabaseQuery());
|
||||
|
@@ -1,9 +1,15 @@
|
||||
using GalaSoft.MvvmLight;
|
||||
using System.Threading.Tasks;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Domain.Common;
|
||||
using ModernKeePass.Domain.Dtos;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class OpenVm: ViewModelBase
|
||||
{
|
||||
private readonly IFileProxy _file;
|
||||
private string _name;
|
||||
private string _path;
|
||||
private string _token;
|
||||
@@ -30,5 +36,27 @@ namespace ModernKeePass.ViewModels
|
||||
get { return _path; }
|
||||
set { Set(() => Path, ref _path, value); }
|
||||
}
|
||||
|
||||
public RelayCommand OpenDatabaseFileCommand { get; }
|
||||
|
||||
public OpenVm(IFileProxy file)
|
||||
{
|
||||
_file = file;
|
||||
OpenDatabaseFileCommand = new RelayCommand(async () => await OpenDatabaseFile());
|
||||
}
|
||||
|
||||
public void SetFileInformation(FileInfo file)
|
||||
{
|
||||
if (file == null) return;
|
||||
Token = file.Id;
|
||||
Path = file.Path;
|
||||
Name = file.Name;
|
||||
}
|
||||
|
||||
private async Task OpenDatabaseFile()
|
||||
{
|
||||
var file = await _file.OpenFile(string.Empty, Constants.Extensions.Kdbx, true);
|
||||
SetFileInformation(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,48 +1,63 @@
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.AccessCache;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using GalaSoft.MvvmLight.Views;
|
||||
using MediatR;
|
||||
using Messages;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
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;
|
||||
using ModernKeePass.Domain.Exceptions;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class SaveVm
|
||||
public class SaveVm: ViewModelBase
|
||||
{
|
||||
public bool IsSaveEnabled => _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult().IsDirty;
|
||||
|
||||
public RelayCommand SaveAsCommand { get; }
|
||||
public RelayCommand SaveCommand { get; }
|
||||
public RelayCommand CloseCommand { get; }
|
||||
|
||||
private readonly IMediator _mediator;
|
||||
private readonly INavigationService _navigation;
|
||||
|
||||
public SaveVm(IMediator mediator, INavigationService navigation)
|
||||
private readonly IFileProxy _file;
|
||||
|
||||
public SaveVm(IMediator mediator, INavigationService navigation, IFileProxy file)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_navigation = navigation;
|
||||
_file = file;
|
||||
|
||||
SaveAsCommand = new RelayCommand(async () => await SaveAs());
|
||||
SaveCommand = new RelayCommand(async () => await Save(), () => IsSaveEnabled);
|
||||
CloseCommand = new RelayCommand(async () => await Close());
|
||||
}
|
||||
|
||||
public async Task Save(bool close = true)
|
||||
|
||||
private async Task SaveAs()
|
||||
{
|
||||
await _mediator.Send(new SaveDatabaseCommand());
|
||||
if (close) await _mediator.Send(new CloseDatabaseCommand());
|
||||
// TODO: get these from resource
|
||||
var file = await _file.CreateFile("New Database", Domain.Common.Constants.Extensions.Kdbx, "KeePass 2.x database", true);
|
||||
|
||||
await _mediator.Send(new SaveDatabaseCommand { FilePath = file.Id });
|
||||
_navigation.NavigateTo(Constants.Navigation.MainPage);
|
||||
}
|
||||
|
||||
public async Task Save(StorageFile file)
|
||||
public async Task Save()
|
||||
{
|
||||
var token = StorageApplicationPermissions.FutureAccessList.Add(file, file.Name);
|
||||
await _mediator.Send(new SaveDatabaseCommand { FilePath = token });
|
||||
_navigation.NavigateTo(Constants.Navigation.MainPage);
|
||||
try
|
||||
{
|
||||
await _mediator.Send(new SaveDatabaseCommand());
|
||||
await Close();
|
||||
}
|
||||
catch (SaveException e)
|
||||
{
|
||||
MessengerInstance.Send(new SaveErrorMessage { Message = e.Message });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task Close()
|
||||
{
|
||||
await _mediator.Send(new CloseDatabaseCommand());
|
||||
|
@@ -9,6 +9,7 @@ using Messages;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
||||
using ModernKeePass.Application.Database.Queries.OpenDatabase;
|
||||
using ModernKeePass.Domain.Common;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
@@ -85,11 +86,13 @@ namespace ModernKeePass.ViewModels
|
||||
|
||||
public bool IsValid => !IsOpening && (HasPassword || HasKeyFile && !string.IsNullOrEmpty(KeyFilePath));
|
||||
|
||||
public RelayCommand OpenKeyFileCommand { get; }
|
||||
public RelayCommand<string> OpenDatabaseCommand { get; }
|
||||
|
||||
private readonly IMediator _mediator;
|
||||
private readonly IResourceProxy _resource;
|
||||
private readonly INotificationService _notification;
|
||||
private readonly IFileProxy _file;
|
||||
private bool _hasPassword;
|
||||
private bool _hasKeyFile;
|
||||
private bool _isOpening;
|
||||
@@ -99,15 +102,26 @@ namespace ModernKeePass.ViewModels
|
||||
private string _keyFileText;
|
||||
private bool _isError;
|
||||
|
||||
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, INotificationService notification)
|
||||
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, INotificationService notification, IFileProxy file)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_resource = resource;
|
||||
_notification = notification;
|
||||
_file = file;
|
||||
OpenKeyFileCommand = new RelayCommand(async () => await OpenKeyFile());
|
||||
OpenDatabaseCommand = new RelayCommand<string>(async databaseFilePath => await TryOpenDatabase(databaseFilePath), _ => IsValid);
|
||||
_keyFileText = _resource.GetResourceValue("CompositeKeyDefaultKeyFile");
|
||||
}
|
||||
|
||||
private async Task OpenKeyFile()
|
||||
{
|
||||
var file = await _file.OpenFile(string.Empty, Constants.Extensions.Any, false);
|
||||
if (file == null) return;
|
||||
KeyFilePath = file.Id;
|
||||
KeyFileText = file.Name;
|
||||
HasKeyFile = true;
|
||||
}
|
||||
|
||||
public async Task TryOpenDatabase(string databaseFilePath)
|
||||
{
|
||||
MessengerInstance.Send(new DatabaseOpeningMessage {Token = databaseFilePath});
|
||||
|
@@ -5,6 +5,8 @@ using MediatR;
|
||||
using Messages;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Application.Security.Commands.GenerateKeyFile;
|
||||
using ModernKeePass.Domain.Common;
|
||||
using ModernKeePass.Domain.Dtos;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
@@ -12,7 +14,8 @@ namespace ModernKeePass.ViewModels
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ICredentialsProxy _credentials;
|
||||
|
||||
private readonly IFileProxy _file;
|
||||
|
||||
public bool HasPassword
|
||||
{
|
||||
get { return _hasPassword; }
|
||||
@@ -33,6 +36,8 @@ namespace ModernKeePass.ViewModels
|
||||
Set(() => HasKeyFile, ref _hasKeyFile, value);
|
||||
RaisePropertyChanged(nameof(IsKeyFileValid));
|
||||
RaisePropertyChanged(nameof(IsValid));
|
||||
OpenKeyFileCommand.RaiseCanExecuteChanged();
|
||||
CreateKeyFileCommand.RaiseCanExecuteChanged();
|
||||
GenerateCredentialsCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
@@ -85,6 +90,8 @@ namespace ModernKeePass.ViewModels
|
||||
public bool IsKeyFileValid => HasKeyFile && !string.IsNullOrEmpty(KeyFilePath) || !HasKeyFile;
|
||||
public bool IsValid => HasPassword && Password == ConfirmPassword || HasKeyFile && !string.IsNullOrEmpty(KeyFilePath) && (HasPassword || HasKeyFile);
|
||||
|
||||
public RelayCommand OpenKeyFileCommand { get; }
|
||||
public RelayCommand CreateKeyFileCommand { get; }
|
||||
public RelayCommand GenerateCredentialsCommand{ get; }
|
||||
|
||||
private bool _hasPassword;
|
||||
@@ -94,20 +101,41 @@ namespace ModernKeePass.ViewModels
|
||||
private string _keyFilePath;
|
||||
private string _keyFileText;
|
||||
|
||||
public SetCredentialsVm(IMediator mediator, ICredentialsProxy credentials, IResourceProxy resource)
|
||||
public SetCredentialsVm(IMediator mediator, ICredentialsProxy credentials, IResourceProxy resource, IFileProxy file)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_credentials = credentials;
|
||||
_file = file;
|
||||
|
||||
OpenKeyFileCommand = new RelayCommand(async () => await OpenKeyFile(), () => HasKeyFile);
|
||||
CreateKeyFileCommand = new RelayCommand(async () => await CreateKeyFile(), () => HasKeyFile);
|
||||
GenerateCredentialsCommand = new RelayCommand(GenerateCredentials, () => IsValid);
|
||||
|
||||
_keyFileText = resource.GetResourceValue("CompositeKeyDefaultKeyFile");
|
||||
}
|
||||
|
||||
public async Task GenerateKeyFile()
|
||||
|
||||
private async Task OpenKeyFile()
|
||||
{
|
||||
await _mediator.Send(new GenerateKeyFileCommand {KeyFilePath = KeyFilePath});
|
||||
var file = await _file.OpenFile(string.Empty, Constants.Extensions.Any, false);
|
||||
SetKeyFileInfo(file);
|
||||
}
|
||||
|
||||
private async Task CreateKeyFile()
|
||||
{
|
||||
var file = await _file.CreateFile("Key", Constants.Extensions.Any, "Key file", false);
|
||||
SetKeyFileInfo(file);
|
||||
|
||||
await _mediator.Send(new GenerateKeyFileCommand { KeyFilePath = KeyFilePath });
|
||||
}
|
||||
|
||||
private void SetKeyFileInfo(FileInfo file)
|
||||
{
|
||||
if (file == null) return;
|
||||
KeyFilePath = file.Id;
|
||||
KeyFileText = file.Name;
|
||||
HasKeyFile = true;
|
||||
}
|
||||
|
||||
private void GenerateCredentials()
|
||||
{
|
||||
MessengerInstance.Send(new CredentialsSetMessage
|
||||
|
@@ -50,6 +50,7 @@ namespace ModernKeePass.ViewModels
|
||||
SimpleIoc.Default.Register(() => App.Services.GetRequiredService<IRecentProxy>());
|
||||
SimpleIoc.Default.Register(() => App.Services.GetRequiredService<IResourceProxy>());
|
||||
SimpleIoc.Default.Register(() => App.Services.GetRequiredService<ISettingsProxy>());
|
||||
SimpleIoc.Default.Register(() => App.Services.GetRequiredService<IFileProxy>());
|
||||
SimpleIoc.Default.Register(() => App.Services.GetRequiredService<ICredentialsProxy>());
|
||||
SimpleIoc.Default.Register(() => App.Services.GetRequiredService<IDialogService>());
|
||||
SimpleIoc.Default.Register(() => App.Services.GetRequiredService<INavigationService>());
|
||||
|
Reference in New Issue
Block a user