mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Create database works with new Vm
Refactoring
This commit is contained in:
55
WinAppCommon/ViewModels/SaveVm.cs
Normal file
55
WinAppCommon/ViewModels/SaveVm.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user