2020-03-27 18:45:13 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
using GalaSoft.MvvmLight;
|
|
|
|
|
using GalaSoft.MvvmLight.Command;
|
2020-04-21 13:14:56 +02:00
|
|
|
|
using GalaSoft.MvvmLight.Views;
|
2020-03-27 18:45:13 +01:00
|
|
|
|
using MediatR;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
using Messages;
|
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
2020-03-27 18:45:13 +01:00
|
|
|
|
using ModernKeePass.Application.Database.Commands.CloseDatabase;
|
|
|
|
|
using ModernKeePass.Application.Database.Commands.SaveDatabase;
|
2020-04-16 19:43:17 +02:00
|
|
|
|
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
2020-04-21 13:14:56 +02:00
|
|
|
|
using ModernKeePass.Common;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
using ModernKeePass.Domain.Exceptions;
|
2017-10-10 15:00:31 +02:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
2020-05-04 12:48:27 +02:00
|
|
|
|
public class SaveVm: ViewModelBase
|
2017-10-10 15:00:31 +02:00
|
|
|
|
{
|
2020-04-16 19:43:17 +02:00
|
|
|
|
public bool IsSaveEnabled => _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult().IsDirty;
|
|
|
|
|
|
2020-05-04 12:48:27 +02:00
|
|
|
|
public RelayCommand SaveAsCommand { get; }
|
2020-04-16 19:43:17 +02:00
|
|
|
|
public RelayCommand SaveCommand { get; }
|
|
|
|
|
public RelayCommand CloseCommand { get; }
|
|
|
|
|
|
2020-03-27 18:45:13 +01:00
|
|
|
|
private readonly IMediator _mediator;
|
2020-04-21 13:14:56 +02:00
|
|
|
|
private readonly INavigationService _navigation;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
private readonly IFileProxy _file;
|
2020-05-05 17:32:07 +02:00
|
|
|
|
private readonly IResourceProxy _resource;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
|
2020-05-05 17:32:07 +02:00
|
|
|
|
public SaveVm(IMediator mediator, INavigationService navigation, IFileProxy file, IResourceProxy resource)
|
2017-11-23 15:26:57 +01:00
|
|
|
|
{
|
2020-03-27 18:45:13 +01:00
|
|
|
|
_mediator = mediator;
|
2020-04-21 13:14:56 +02:00
|
|
|
|
_navigation = navigation;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
_file = file;
|
2020-05-05 17:32:07 +02:00
|
|
|
|
_resource = resource;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
|
|
|
|
|
SaveAsCommand = new RelayCommand(async () => await SaveAs());
|
2020-04-16 19:43:17 +02:00
|
|
|
|
SaveCommand = new RelayCommand(async () => await Save(), () => IsSaveEnabled);
|
|
|
|
|
CloseCommand = new RelayCommand(async () => await Close());
|
2020-05-04 14:29:52 +02:00
|
|
|
|
|
|
|
|
|
MessengerInstance.Register<DatabaseSavedMessage>(this, _ => SaveCommand.RaiseCanExecuteChanged());
|
2017-11-23 15:26:57 +01:00
|
|
|
|
}
|
2020-05-04 12:48:27 +02:00
|
|
|
|
|
|
|
|
|
private async Task SaveAs()
|
2017-10-10 15:00:31 +02:00
|
|
|
|
{
|
2020-05-05 17:32:07 +02:00
|
|
|
|
var file = await _file.CreateFile(_resource.GetResourceValue("MessageDialogSaveNameSuggestion"),
|
|
|
|
|
Domain.Common.Constants.Extensions.Kdbx,
|
|
|
|
|
_resource.GetResourceValue("MessageDialogSaveErrorFileTypeDesc"),
|
|
|
|
|
true);
|
|
|
|
|
if (file == null) return;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
await _mediator.Send(new SaveDatabaseCommand { FilePath = file.Id });
|
2020-04-21 13:14:56 +02:00
|
|
|
|
_navigation.NavigateTo(Constants.Navigation.MainPage);
|
2017-10-10 15:00:31 +02:00
|
|
|
|
}
|
2017-10-12 11:45:00 +02:00
|
|
|
|
|
2020-05-04 12:48:27 +02:00
|
|
|
|
public async Task Save()
|
2017-10-12 11:45:00 +02:00
|
|
|
|
{
|
2020-05-04 12:48:27 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _mediator.Send(new SaveDatabaseCommand());
|
|
|
|
|
await Close();
|
|
|
|
|
}
|
|
|
|
|
catch (SaveException e)
|
|
|
|
|
{
|
|
|
|
|
MessengerInstance.Send(new SaveErrorMessage { Message = e.Message });
|
|
|
|
|
}
|
2017-10-12 11:45:00 +02:00
|
|
|
|
}
|
2020-05-04 12:48:27 +02:00
|
|
|
|
|
2020-04-09 19:43:06 +02:00
|
|
|
|
public async Task Close()
|
|
|
|
|
{
|
|
|
|
|
await _mediator.Send(new CloseDatabaseCommand());
|
2020-04-21 13:14:56 +02:00
|
|
|
|
_navigation.NavigateTo(Constants.Navigation.MainPage);
|
2020-04-09 19:43:06 +02:00
|
|
|
|
}
|
2021-05-10 20:28:13 +02:00
|
|
|
|
|
|
|
|
|
public override void Cleanup()
|
|
|
|
|
{
|
|
|
|
|
MessengerInstance.Unregister(this);
|
|
|
|
|
base.Cleanup();
|
|
|
|
|
}
|
2017-10-10 15:00:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|