2017-10-09 18:40:02 +02:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
2017-10-25 18:29:50 +02:00
|
|
|
|
using Windows.ApplicationModel;
|
2017-10-09 18:40:02 +02:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2020-04-22 16:21:47 +02:00
|
|
|
|
using GalaSoft.MvvmLight;
|
2020-03-24 19:14:34 +01:00
|
|
|
|
using MediatR;
|
2020-04-23 19:00:38 +02:00
|
|
|
|
using Messages;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
2020-03-24 19:14:34 +01:00
|
|
|
|
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
using ModernKeePass.Domain.Dtos;
|
2020-03-30 19:43:04 +02:00
|
|
|
|
using ModernKeePass.Domain.Interfaces;
|
2020-04-15 12:02:30 +02:00
|
|
|
|
using ModernKeePass.Models;
|
2020-04-20 20:02:43 +02:00
|
|
|
|
using ModernKeePass.ViewModels.ListItems;
|
2017-12-08 19:38:33 +01:00
|
|
|
|
using ModernKeePass.Views;
|
2020-04-23 19:00:38 +02:00
|
|
|
|
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;
|
2017-09-27 18:01:21 +02:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
public class MainVm : ViewModelBase, IHasSelectableObject
|
2017-09-27 18:01:21 +02:00
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
private readonly IMediator _mediator;
|
|
|
|
|
private readonly IRecentProxy _recent;
|
|
|
|
|
private readonly IResourceProxy _resource;
|
|
|
|
|
private readonly IDialogService _dialog;
|
|
|
|
|
private readonly INotificationService _notification;
|
|
|
|
|
private readonly INavigationService _navigation;
|
2017-11-27 15:26:36 +01:00
|
|
|
|
private IOrderedEnumerable<IGrouping<string, MainMenuItemVm>> _mainMenuItems;
|
2020-04-22 16:21:47 +02:00
|
|
|
|
private ISelectableModel _selectedItem;
|
2017-09-29 17:23:35 -04:00
|
|
|
|
|
2017-10-25 18:29:50 +02:00
|
|
|
|
public string Name { get; } = Package.Current.DisplayName;
|
|
|
|
|
|
2017-11-27 15:26:36 +01:00
|
|
|
|
public IOrderedEnumerable<IGrouping<string, MainMenuItemVm>> MainMenuItems
|
2017-09-29 17:23:35 -04:00
|
|
|
|
{
|
2017-10-03 16:06:49 +02:00
|
|
|
|
get { return _mainMenuItems; }
|
2020-04-22 16:21:47 +02:00
|
|
|
|
set { Set(() => MainMenuItems, ref _mainMenuItems, value); }
|
2017-09-29 17:23:35 -04:00
|
|
|
|
}
|
2017-10-09 18:40:02 +02:00
|
|
|
|
|
2017-10-26 18:57:39 +02:00
|
|
|
|
public ISelectableModel SelectedItem
|
2017-10-09 18:40:02 +02:00
|
|
|
|
{
|
|
|
|
|
get { return _selectedItem; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_selectedItem == value) return;
|
|
|
|
|
if (_selectedItem != null)
|
|
|
|
|
{
|
|
|
|
|
_selectedItem.IsSelected = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 16:21:47 +02:00
|
|
|
|
Set(() => SelectedItem, ref _selectedItem, value);
|
2017-10-09 18:40:02 +02:00
|
|
|
|
|
|
|
|
|
if (_selectedItem != null)
|
|
|
|
|
{
|
|
|
|
|
_selectedItem.IsSelected = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 19:00:38 +02:00
|
|
|
|
public MainVm(
|
|
|
|
|
IMediator mediator,
|
|
|
|
|
IRecentProxy recent,
|
|
|
|
|
IResourceProxy resource,
|
|
|
|
|
IDialogService dialog,
|
|
|
|
|
INotificationService notification,
|
2020-04-25 22:03:47 +02:00
|
|
|
|
INavigationService navigation)
|
2017-10-09 18:40:02 +02:00
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
_mediator = mediator;
|
|
|
|
|
_recent = recent;
|
|
|
|
|
_resource = resource;
|
|
|
|
|
_dialog = dialog;
|
|
|
|
|
_notification = notification;
|
|
|
|
|
_navigation = navigation;
|
2020-04-25 22:03:47 +02:00
|
|
|
|
|
2020-04-23 19:00:38 +02:00
|
|
|
|
MessengerInstance.Register<DatabaseOpenedMessage>(this, NavigateToPage);
|
|
|
|
|
MessengerInstance.Register<DatabaseAlreadyOpenedMessage>(this, async message => await DisplaySaveConfirmation(message));
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 22:03:47 +02:00
|
|
|
|
public async Task Initialize(Frame referenceFrame, Frame destinationFrame, FileInfo databaseFile = null)
|
2020-04-23 19:00:38 +02:00
|
|
|
|
{
|
2020-04-25 22:03:47 +02:00
|
|
|
|
var database = await _mediator.Send(new GetDatabaseQuery());
|
2017-10-09 18:40:02 +02:00
|
|
|
|
var mainMenuItems = new ObservableCollection<MainMenuItemVm>
|
|
|
|
|
{
|
2017-10-10 15:00:31 +02:00
|
|
|
|
new MainMenuItemVm
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
Title = _resource.GetResourceValue("MainMenuItemOpen"),
|
2017-12-01 17:59:38 +01:00
|
|
|
|
PageType = typeof(OpenDatabasePage),
|
|
|
|
|
Destination = destinationFrame,
|
2018-06-20 11:52:17 +02:00
|
|
|
|
Parameter = databaseFile,
|
|
|
|
|
SymbolIcon = Symbol.Page2,
|
2020-04-24 16:16:48 +02:00
|
|
|
|
IsSelected = databaseFile != null
|
2017-10-10 15:00:31 +02:00
|
|
|
|
},
|
2017-10-11 18:43:27 +02:00
|
|
|
|
new MainMenuItemVm
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
Title = _resource.GetResourceValue("MainMenuItemNew"),
|
2017-12-01 17:59:38 +01:00
|
|
|
|
PageType = typeof(NewDatabasePage),
|
|
|
|
|
Destination = destinationFrame,
|
|
|
|
|
SymbolIcon = Symbol.Add
|
2017-10-11 18:43:27 +02:00
|
|
|
|
},
|
2017-10-10 15:00:31 +02:00
|
|
|
|
new MainMenuItemVm
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
Title = _resource.GetResourceValue("MainMenuItemSave"),
|
2017-12-01 17:59:38 +01:00
|
|
|
|
PageType = typeof(SaveDatabasePage),
|
|
|
|
|
Destination = destinationFrame,
|
|
|
|
|
Parameter = referenceFrame,
|
|
|
|
|
SymbolIcon = Symbol.Save,
|
2020-03-24 19:14:34 +01:00
|
|
|
|
IsSelected = database.IsOpen,
|
|
|
|
|
IsEnabled = database.IsOpen
|
2017-10-10 15:00:31 +02:00
|
|
|
|
},
|
2017-12-01 17:59:38 +01:00
|
|
|
|
new MainMenuItemVm
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
Title = _resource.GetResourceValue("MainMenuItemRecent"),
|
2017-12-01 17:59:38 +01:00
|
|
|
|
PageType = typeof(RecentDatabasesPage),
|
|
|
|
|
Destination = destinationFrame,
|
|
|
|
|
Parameter = referenceFrame,
|
|
|
|
|
SymbolIcon = Symbol.Copy,
|
2020-04-23 19:00:38 +02:00
|
|
|
|
IsSelected = !database.IsOpen && _recent.EntryCount > 0,
|
|
|
|
|
IsEnabled = _recent.EntryCount > 0
|
2017-10-16 10:57:39 +02:00
|
|
|
|
},
|
2017-11-28 16:57:16 -05:00
|
|
|
|
new MainMenuItemVm
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
Title = _resource.GetResourceValue("MainMenuItemSettings"),
|
2017-12-01 17:59:38 +01:00
|
|
|
|
PageType = typeof(SettingsPage),
|
|
|
|
|
Destination = referenceFrame,
|
|
|
|
|
SymbolIcon = Symbol.Setting
|
2017-11-28 16:57:16 -05:00
|
|
|
|
},
|
2017-10-16 10:57:39 +02:00
|
|
|
|
new MainMenuItemVm
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
Title = _resource.GetResourceValue("MainMenuItemAbout"),
|
2017-12-01 17:59:38 +01:00
|
|
|
|
PageType = typeof(AboutPage),
|
|
|
|
|
Destination = destinationFrame,
|
|
|
|
|
SymbolIcon = Symbol.Help
|
2018-02-23 18:09:21 +01:00
|
|
|
|
},
|
2017-12-08 19:38:33 +01:00
|
|
|
|
new MainMenuItemVm
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
Title = _resource.GetResourceValue("MainMenuItemDonate"),
|
2017-12-08 19:38:33 +01:00
|
|
|
|
PageType = typeof(DonatePage),
|
|
|
|
|
Destination = destinationFrame,
|
|
|
|
|
SymbolIcon = Symbol.Shop
|
2018-02-23 18:09:21 +01:00
|
|
|
|
}
|
2017-10-09 18:40:02 +02:00
|
|
|
|
};
|
|
|
|
|
// Auto-select the Recent Items menu item if the conditions are met
|
|
|
|
|
SelectedItem = mainMenuItems.FirstOrDefault(m => m.IsSelected);
|
2017-12-04 10:46:01 +01:00
|
|
|
|
|
|
|
|
|
// Add currently opened database to the menu
|
2020-03-24 19:14:34 +01:00
|
|
|
|
if (database.IsOpen)
|
2017-10-09 18:40:02 +02:00
|
|
|
|
mainMenuItems.Add(new MainMenuItemVm
|
|
|
|
|
{
|
2017-11-23 15:26:57 +01:00
|
|
|
|
Title = database.Name,
|
2017-10-09 18:40:02 +02:00
|
|
|
|
PageType = typeof(GroupDetailPage),
|
|
|
|
|
Destination = referenceFrame,
|
2020-04-15 12:02:30 +02:00
|
|
|
|
Parameter = new NavigationItem { Id = database.RootGroupId },
|
2017-11-27 15:26:36 +01:00
|
|
|
|
Group = "Databases",
|
2017-10-09 18:40:02 +02:00
|
|
|
|
SymbolIcon = Symbol.ProtectedDocument
|
|
|
|
|
});
|
2020-04-23 19:00:38 +02:00
|
|
|
|
|
2017-10-09 18:40:02 +02:00
|
|
|
|
MainMenuItems = from item in mainMenuItems group item by item.Group into grp orderby grp.Key select grp;
|
|
|
|
|
}
|
2020-04-23 19:00:38 +02:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2020-05-04 12:48:27 +02:00
|
|
|
|
// TODO: Implement save as
|
2020-04-23 19:00:38 +02:00
|
|
|
|
_notification.Show(exception.Source, exception.Message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _mediator.Send(new CloseDatabaseCommand());
|
|
|
|
|
MessengerInstance.Send(new DatabaseClosedMessage { Parameter = message.Parameter });
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-09-27 18:01:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|