mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-04 08:00:16 -04:00
WIP ViewModelLocator - Messenger and Recent issues
Refactoring Code cleanup
This commit is contained in:
@@ -4,6 +4,7 @@ using Windows.ApplicationModel;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using GalaSoft.MvvmLight;
|
||||
using MediatR;
|
||||
using Messages;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
||||
@@ -12,11 +13,23 @@ using ModernKeePass.Domain.Interfaces;
|
||||
using ModernKeePass.Models;
|
||||
using ModernKeePass.ViewModels.ListItems;
|
||||
using ModernKeePass.Views;
|
||||
using System.Threading.Tasks;
|
||||
using GalaSoft.MvvmLight.Views;
|
||||
using ModernKeePass.Application.Database.Commands.CloseDatabase;
|
||||
using ModernKeePass.Application.Database.Commands.SaveDatabase;
|
||||
using ModernKeePass.Common;
|
||||
using ModernKeePass.Domain.Exceptions;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class MainVm : ObservableObject, IHasSelectableObject
|
||||
public class MainVm : ViewModelBase, IHasSelectableObject
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly IRecentProxy _recent;
|
||||
private readonly IResourceProxy _resource;
|
||||
private readonly IDialogService _dialog;
|
||||
private readonly INotificationService _notification;
|
||||
private readonly INavigationService _navigation;
|
||||
private IOrderedEnumerable<IGrouping<string, MainMenuItemVm>> _mainMenuItems;
|
||||
private ISelectableModel _selectedItem;
|
||||
|
||||
@@ -55,19 +68,45 @@ namespace ModernKeePass.ViewModels
|
||||
destinationFrame,
|
||||
App.Services.GetRequiredService<IMediator>(),
|
||||
App.Services.GetRequiredService<IRecentProxy>(),
|
||||
App.Services.GetRequiredService<IResourceProxy>(),
|
||||
App.Services.GetRequiredService<IResourceProxy>(),
|
||||
App.Services.GetRequiredService<IDialogService>(),
|
||||
App.Services.GetRequiredService<INotificationService>(),
|
||||
App.Services.GetRequiredService<INavigationService>(),
|
||||
databaseFile)
|
||||
{ }
|
||||
|
||||
public MainVm(Frame referenceFrame, Frame destinationFrame, IMediator mediator, IRecentProxy recent, IResourceProxy resource, FileInfo databaseFile = null)
|
||||
public MainVm(
|
||||
Frame referenceFrame,
|
||||
Frame destinationFrame,
|
||||
IMediator mediator,
|
||||
IRecentProxy recent,
|
||||
IResourceProxy resource,
|
||||
IDialogService dialog,
|
||||
INotificationService notification,
|
||||
INavigationService navigation,
|
||||
FileInfo databaseFile = null)
|
||||
{
|
||||
var database = mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
|
||||
_mediator = mediator;
|
||||
_recent = recent;
|
||||
_resource = resource;
|
||||
_dialog = dialog;
|
||||
_notification = notification;
|
||||
_navigation = navigation;
|
||||
|
||||
BuildMainMenuItems(referenceFrame, destinationFrame, databaseFile);
|
||||
|
||||
MessengerInstance.Register<DatabaseOpenedMessage>(this, NavigateToPage);
|
||||
MessengerInstance.Register<DatabaseAlreadyOpenedMessage>(this, async message => await DisplaySaveConfirmation(message));
|
||||
}
|
||||
|
||||
private void BuildMainMenuItems(Frame referenceFrame, Frame destinationFrame, FileInfo databaseFile)
|
||||
{
|
||||
var database = _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
|
||||
var mainMenuItems = new ObservableCollection<MainMenuItemVm>
|
||||
{
|
||||
new MainMenuItemVm
|
||||
{
|
||||
Title = resource.GetResourceValue("MainMenuItemOpen"),
|
||||
Title = _resource.GetResourceValue("MainMenuItemOpen"),
|
||||
PageType = typeof(OpenDatabasePage),
|
||||
Destination = destinationFrame,
|
||||
Parameter = databaseFile,
|
||||
@@ -76,14 +115,14 @@ namespace ModernKeePass.ViewModels
|
||||
},
|
||||
new MainMenuItemVm
|
||||
{
|
||||
Title = resource.GetResourceValue("MainMenuItemNew"),
|
||||
Title = _resource.GetResourceValue("MainMenuItemNew"),
|
||||
PageType = typeof(NewDatabasePage),
|
||||
Destination = destinationFrame,
|
||||
SymbolIcon = Symbol.Add
|
||||
},
|
||||
new MainMenuItemVm
|
||||
{
|
||||
Title = resource.GetResourceValue("MainMenuItemSave"),
|
||||
Title = _resource.GetResourceValue("MainMenuItemSave"),
|
||||
PageType = typeof(SaveDatabasePage),
|
||||
Destination = destinationFrame,
|
||||
Parameter = referenceFrame,
|
||||
@@ -93,31 +132,31 @@ namespace ModernKeePass.ViewModels
|
||||
},
|
||||
new MainMenuItemVm
|
||||
{
|
||||
Title = resource.GetResourceValue("MainMenuItemRecent"),
|
||||
Title = _resource.GetResourceValue("MainMenuItemRecent"),
|
||||
PageType = typeof(RecentDatabasesPage),
|
||||
Destination = destinationFrame,
|
||||
Parameter = referenceFrame,
|
||||
SymbolIcon = Symbol.Copy,
|
||||
IsSelected = !database.IsOpen && recent.EntryCount > 0,
|
||||
IsEnabled = recent.EntryCount > 0
|
||||
IsSelected = !database.IsOpen && _recent.EntryCount > 0,
|
||||
IsEnabled = _recent.EntryCount > 0
|
||||
},
|
||||
new MainMenuItemVm
|
||||
{
|
||||
Title = resource.GetResourceValue("MainMenuItemSettings"),
|
||||
Title = _resource.GetResourceValue("MainMenuItemSettings"),
|
||||
PageType = typeof(SettingsPage),
|
||||
Destination = referenceFrame,
|
||||
SymbolIcon = Symbol.Setting
|
||||
},
|
||||
new MainMenuItemVm
|
||||
{
|
||||
Title = resource.GetResourceValue("MainMenuItemAbout"),
|
||||
Title = _resource.GetResourceValue("MainMenuItemAbout"),
|
||||
PageType = typeof(AboutPage),
|
||||
Destination = destinationFrame,
|
||||
SymbolIcon = Symbol.Help
|
||||
},
|
||||
new MainMenuItemVm
|
||||
{
|
||||
Title = resource.GetResourceValue("MainMenuItemDonate"),
|
||||
Title = _resource.GetResourceValue("MainMenuItemDonate"),
|
||||
PageType = typeof(DonatePage),
|
||||
Destination = destinationFrame,
|
||||
SymbolIcon = Symbol.Shop
|
||||
@@ -137,8 +176,41 @@ namespace ModernKeePass.ViewModels
|
||||
Group = "Databases",
|
||||
SymbolIcon = Symbol.ProtectedDocument
|
||||
});
|
||||
|
||||
|
||||
MainMenuItems = from item in mainMenuItems group item by item.Group into grp orderby grp.Key select grp;
|
||||
}
|
||||
|
||||
private void NavigateToPage(DatabaseOpenedMessage message)
|
||||
{
|
||||
_navigation.NavigateTo(Constants.Navigation.GroupPage, new NavigationItem { Id = message.RootGroupId });
|
||||
}
|
||||
|
||||
private async Task DisplaySaveConfirmation(DatabaseAlreadyOpenedMessage message)
|
||||
{
|
||||
var database = await _mediator.Send(new GetDatabaseQuery());
|
||||
await _dialog.ShowMessage(string.Format(_resource.GetResourceValue("MessageDialogDBOpenDesc"), database.Name),
|
||||
_resource.GetResourceValue("MessageDialogDBOpenTitle"),
|
||||
_resource.GetResourceValue("MessageDialogDBOpenButtonSave"),
|
||||
_resource.GetResourceValue("MessageDialogDBOpenButtonDiscard"),
|
||||
async isOk =>
|
||||
{
|
||||
if (isOk)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _mediator.Send(new SaveDatabaseCommand());
|
||||
_notification.Show(database.Name, _resource.GetResourceValue("ToastSavedMessage"));
|
||||
}
|
||||
catch (SaveException exception)
|
||||
{
|
||||
_notification.Show(exception.Source, exception.Message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await _mediator.Send(new CloseDatabaseCommand());
|
||||
MessengerInstance.Send(new DatabaseClosedMessage { Parameter = message.Parameter });
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user