mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
WIP ViewModelLocator - Messenger and Recent issues
Refactoring Code cleanup
This commit is contained in:
@@ -5,6 +5,7 @@ using Windows.UI.Core;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Navigation;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
|
||||
namespace ModernKeePass.Common
|
||||
{
|
||||
@@ -77,8 +78,8 @@ namespace ModernKeePass.Common
|
||||
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
|
||||
#else
|
||||
// Keyboard and mouse navigation only apply when occupying the entire window
|
||||
if (this.Page.ActualHeight == Window.Current.Bounds.Height &&
|
||||
this.Page.ActualWidth == Window.Current.Bounds.Width)
|
||||
if (Page.ActualHeight == Window.Current.Bounds.Height &&
|
||||
Page.ActualWidth == Window.Current.Bounds.Width)
|
||||
{
|
||||
// Listen to the window directly so focus isn't required
|
||||
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated +=
|
||||
|
@@ -1,156 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Windows.Foundation.Collections;
|
||||
|
||||
namespace ModernKeePass.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of IObservableMap that supports reentrancy for use as a default view
|
||||
/// model.
|
||||
/// </summary>
|
||||
public class ObservableDictionary : IObservableMap<string, object>
|
||||
{
|
||||
private class ObservableDictionaryChangedEventArgs : IMapChangedEventArgs<string>
|
||||
{
|
||||
public ObservableDictionaryChangedEventArgs(CollectionChange change, string key)
|
||||
{
|
||||
CollectionChange = change;
|
||||
Key = key;
|
||||
}
|
||||
|
||||
public CollectionChange CollectionChange { get; private set; }
|
||||
public string Key { get; private set; }
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, object> _dictionary = new Dictionary<string, object>();
|
||||
public event MapChangedEventHandler<string, object> MapChanged;
|
||||
|
||||
private void InvokeMapChanged(CollectionChange change, string key)
|
||||
{
|
||||
var eventHandler = MapChanged;
|
||||
if (eventHandler != null)
|
||||
{
|
||||
eventHandler(this, new ObservableDictionaryChangedEventArgs(change, key));
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(string key, object value)
|
||||
{
|
||||
_dictionary.Add(key, value);
|
||||
InvokeMapChanged(CollectionChange.ItemInserted, key);
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<string, object> item)
|
||||
{
|
||||
Add(item.Key, item.Value);
|
||||
}
|
||||
|
||||
public void AddRange(IEnumerable<KeyValuePair<string, object>> values)
|
||||
{
|
||||
foreach (var value in values)
|
||||
{
|
||||
Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(string key)
|
||||
{
|
||||
if (_dictionary.Remove(key))
|
||||
{
|
||||
InvokeMapChanged(CollectionChange.ItemRemoved, key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<string, object> item)
|
||||
{
|
||||
object currentValue;
|
||||
if (_dictionary.TryGetValue(item.Key, out currentValue) &&
|
||||
Equals(item.Value, currentValue) && _dictionary.Remove(item.Key))
|
||||
{
|
||||
InvokeMapChanged(CollectionChange.ItemRemoved, item.Key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public object this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
return _dictionary[key];
|
||||
}
|
||||
set
|
||||
{
|
||||
_dictionary[key] = value;
|
||||
InvokeMapChanged(CollectionChange.ItemChanged, key);
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
var priorKeys = _dictionary.Keys.ToArray();
|
||||
_dictionary.Clear();
|
||||
foreach (var key in priorKeys)
|
||||
{
|
||||
InvokeMapChanged(CollectionChange.ItemRemoved, key);
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection<string> Keys
|
||||
{
|
||||
get { return _dictionary.Keys; }
|
||||
}
|
||||
|
||||
public bool ContainsKey(string key)
|
||||
{
|
||||
return _dictionary.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool TryGetValue(string key, out object value)
|
||||
{
|
||||
return _dictionary.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
public ICollection<object> Values
|
||||
{
|
||||
get { return _dictionary.Values; }
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<string, object> item)
|
||||
{
|
||||
return _dictionary.Contains(item);
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { return _dictionary.Count; }
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
|
||||
{
|
||||
return _dictionary.GetEnumerator();
|
||||
}
|
||||
|
||||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
||||
{
|
||||
return _dictionary.GetEnumerator();
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
|
||||
{
|
||||
int arraySize = array.Length;
|
||||
foreach (var pair in _dictionary)
|
||||
{
|
||||
if (arrayIndex >= arraySize) break;
|
||||
array[arrayIndex++] = pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,78 +0,0 @@
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace ModernKeePass.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// A command whose sole purpose is to relay its functionality
|
||||
/// to other objects by invoking delegates.
|
||||
/// The default return value for the CanExecute method is 'true'.
|
||||
/// <see cref="RaiseCanExecuteChanged"/> needs to be called whenever
|
||||
/// <see cref="CanExecute"/> is expected to return a different value.
|
||||
/// </summary>
|
||||
public class RelayCommand : ICommand
|
||||
{
|
||||
private readonly Action _execute;
|
||||
private readonly Func<bool> _canExecute;
|
||||
|
||||
/// <summary>
|
||||
/// Raised when RaiseCanExecuteChanged is called.
|
||||
/// </summary>
|
||||
public event EventHandler CanExecuteChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command that can always execute.
|
||||
/// </summary>
|
||||
/// <param name="execute">The execution logic.</param>
|
||||
public RelayCommand(Action execute)
|
||||
: this(execute, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command.
|
||||
/// </summary>
|
||||
/// <param name="execute">The execution logic.</param>
|
||||
/// <param name="canExecute">The execution status logic.</param>
|
||||
public RelayCommand(Action execute, Func<bool> canExecute)
|
||||
{
|
||||
if (execute == null)
|
||||
throw new ArgumentNullException(nameof(execute));
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
|
||||
/// </summary>
|
||||
/// <param name="parameter">
|
||||
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
|
||||
/// </param>
|
||||
/// <returns>true if this command can be executed; otherwise, false.</returns>
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return _canExecute?.Invoke() ?? true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the <see cref="RelayCommand"/> on the current command target.
|
||||
/// </summary>
|
||||
/// <param name="parameter">
|
||||
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
|
||||
/// </param>
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_execute();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method used to raise the <see cref="CanExecuteChanged"/> event
|
||||
/// to indicate that the return value of the <see cref="CanExecute"/>
|
||||
/// method has changed.
|
||||
/// </summary>
|
||||
public void RaiseCanExecuteChanged()
|
||||
{
|
||||
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace ModernKeePass.Events
|
||||
{
|
||||
public class PasswordEventArgs: EventArgs
|
||||
{
|
||||
public string RootGroupId { get; set; }
|
||||
|
||||
public PasswordEventArgs(string groupId)
|
||||
{
|
||||
RootGroupId = groupId;
|
||||
}
|
||||
}
|
||||
}
|
@@ -4,6 +4,6 @@ namespace Messages
|
||||
{
|
||||
public class DatabaseAlreadyOpenedMessage
|
||||
{
|
||||
public DatabaseVm OpenedDatabase { get; set; }
|
||||
public object Parameter { get; set; }
|
||||
}
|
||||
}
|
@@ -2,6 +2,6 @@
|
||||
{
|
||||
public class DatabaseClosedMessage
|
||||
{
|
||||
|
||||
public object Parameter { get; set; }
|
||||
}
|
||||
}
|
@@ -3,7 +3,6 @@ using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using GalaSoft.MvvmLight;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Application.Database.Models;
|
||||
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
||||
@@ -76,9 +75,7 @@ namespace ModernKeePass.ViewModels.ListItems
|
||||
if (!IsNewRecycleBin) _mediator.Send(new SetRecycleBinCommand { RecycleBinId = value.Id}).Wait();
|
||||
}
|
||||
}
|
||||
|
||||
public SettingsDatabaseVm() : this(App.Services.GetRequiredService<IMediator>()) { }
|
||||
|
||||
|
||||
public SettingsDatabaseVm(IMediator mediator)
|
||||
{
|
||||
_mediator = mediator;
|
||||
|
@@ -1,6 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Common;
|
||||
|
||||
@@ -9,10 +8,7 @@ namespace ModernKeePass.ViewModels.ListItems
|
||||
public class SettingsNewVm
|
||||
{
|
||||
private readonly ISettingsProxy _settings;
|
||||
|
||||
public SettingsNewVm() : this(App.Services.GetRequiredService<ISettingsProxy>())
|
||||
{ }
|
||||
|
||||
|
||||
public SettingsNewVm(ISettingsProxy settings)
|
||||
{
|
||||
_settings = settings;
|
||||
|
@@ -1,4 +1,3 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Common;
|
||||
|
||||
@@ -7,10 +6,7 @@ namespace ModernKeePass.ViewModels.ListItems
|
||||
public class SettingsSaveVm
|
||||
{
|
||||
private readonly ISettingsProxy _settings;
|
||||
|
||||
public SettingsSaveVm() : this(App.Services.GetRequiredService<ISettingsProxy>())
|
||||
{ }
|
||||
|
||||
|
||||
public SettingsSaveVm(ISettingsProxy settings)
|
||||
{
|
||||
_settings = settings;
|
||||
|
@@ -1,34 +1,26 @@
|
||||
using System.Threading.Tasks;
|
||||
using GalaSoft.MvvmLight.Messaging;
|
||||
using GalaSoft.MvvmLight.Views;
|
||||
using GalaSoft.MvvmLight;
|
||||
using MediatR;
|
||||
using Messages;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Application.Database.Commands.UpdateCredentials;
|
||||
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
||||
|
||||
namespace ModernKeePass.ViewModels.ListItems
|
||||
{
|
||||
public class SettingsSecurityVm
|
||||
public class SettingsSecurityVm: ViewModelBase
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly IResourceProxy _resource;
|
||||
private readonly INotificationService _notification;
|
||||
|
||||
public SettingsSecurityVm(): this(
|
||||
App.Services.GetRequiredService<IMediator>(),
|
||||
App.Services.GetRequiredService<IMessenger>(),
|
||||
App.Services.GetRequiredService<IResourceProxy>(),
|
||||
App.Services.GetRequiredService<INotificationService>()) { }
|
||||
|
||||
public SettingsSecurityVm(IMediator mediator, IMessenger messenger, IResourceProxy resource, INotificationService notification)
|
||||
|
||||
public SettingsSecurityVm(IMediator mediator, IResourceProxy resource, INotificationService notification)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_resource = resource;
|
||||
_notification = notification;
|
||||
|
||||
messenger.Register<CredentialsSetMessage>(this, async message => await UpdateDatabaseCredentials(message));
|
||||
MessengerInstance.Register<CredentialsSetMessage>(this, async message => await UpdateDatabaseCredentials(message));
|
||||
}
|
||||
|
||||
public async Task UpdateDatabaseCredentials(CredentialsSetMessage message)
|
||||
|
@@ -1,10 +1,8 @@
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage;
|
||||
using GalaSoft.MvvmLight.Messaging;
|
||||
using GalaSoft.MvvmLight.Views;
|
||||
using MediatR;
|
||||
using Messages;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Application.Database.Commands.CreateDatabase;
|
||||
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
||||
@@ -37,23 +35,28 @@ namespace ModernKeePass.ViewModels
|
||||
RaisePropertyChanged(nameof(ImportFormatHelp));
|
||||
}
|
||||
}
|
||||
|
||||
public NewVm(): this(
|
||||
App.Services.GetRequiredService<IMediator>(),
|
||||
App.Services.GetRequiredService<ISettingsProxy>(),
|
||||
App.Services.GetRequiredService<IMessenger>(),
|
||||
App.Services.GetRequiredService<INavigationService>()) { }
|
||||
|
||||
public NewVm(IMediator mediator, ISettingsProxy settings, IMessenger messenger, INavigationService navigation)
|
||||
|
||||
public NewVm(IMediator mediator, IRecentProxy recent, ISettingsProxy settings, INavigationService navigation) : base(recent)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_settings = settings;
|
||||
_navigation = navigation;
|
||||
|
||||
messenger.Register<CredentialsSetMessage>(this, async message => await CreateDatabase(message));
|
||||
MessengerInstance.Register<CredentialsSetMessage>(this, async m => await TryCreateDatabase(m));
|
||||
}
|
||||
|
||||
public async Task CreateDatabase(CredentialsSetMessage message)
|
||||
private async Task TryCreateDatabase(CredentialsSetMessage message)
|
||||
{
|
||||
var database = await _mediator.Send(new GetDatabaseQuery());
|
||||
if (database.IsDirty)
|
||||
{
|
||||
MessengerInstance.Register<DatabaseClosedMessage>(this, async m => await CreateDatabase(m.Parameter as CredentialsSetMessage));
|
||||
MessengerInstance.Send(new DatabaseAlreadyOpenedMessage { Parameter = message });
|
||||
}
|
||||
else await CreateDatabase(message);
|
||||
}
|
||||
|
||||
private async Task CreateDatabase(CredentialsSetMessage message)
|
||||
{
|
||||
await _mediator.Send(new CreateDatabaseCommand
|
||||
{
|
||||
|
@@ -1,12 +1,11 @@
|
||||
using System.Threading.Tasks;
|
||||
using GalaSoft.MvvmLight;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Domain.Dtos;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class OpenVm: ObservableObject
|
||||
public class OpenVm: ViewModelBase
|
||||
{
|
||||
private readonly IRecentProxy _recent;
|
||||
private string _name;
|
||||
@@ -31,9 +30,7 @@ namespace ModernKeePass.ViewModels
|
||||
get { return _path; }
|
||||
private set { Set(() => Path, ref _path, value); }
|
||||
}
|
||||
|
||||
public OpenVm(): this(App.Services.GetRequiredService<IRecentProxy>()) { }
|
||||
|
||||
|
||||
public OpenVm(IRecentProxy recent)
|
||||
{
|
||||
_recent = recent;
|
||||
|
@@ -1,16 +1,17 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Messages;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Domain.Interfaces;
|
||||
using ModernKeePass.ViewModels.ListItems;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class RecentVm : ObservableObject, IHasSelectableObject
|
||||
public class RecentVm : ViewModelBase, IHasSelectableObject
|
||||
{
|
||||
private readonly IRecentProxy _recent;
|
||||
private ISelectableModel _selectedItem;
|
||||
@@ -41,9 +42,7 @@ namespace ModernKeePass.ViewModels
|
||||
}
|
||||
|
||||
public ICommand ClearAllCommand { get; }
|
||||
|
||||
public RecentVm() : this (App.Services.GetRequiredService<IRecentProxy>()) { }
|
||||
|
||||
|
||||
public RecentVm(IRecentProxy recent)
|
||||
{
|
||||
_recent = recent;
|
||||
@@ -53,11 +52,13 @@ namespace ModernKeePass.ViewModels
|
||||
RecentItems = new ObservableCollection<RecentItemVm>(recentItems);
|
||||
if (RecentItems.Count > 0)
|
||||
SelectedItem = RecentItems[0];
|
||||
|
||||
MessengerInstance.Register<DatabaseOpeningMessage>(this, async action => await UpdateAccessTime(action.Token));
|
||||
}
|
||||
|
||||
public void UpdateAccessTime(string token)
|
||||
private async Task UpdateAccessTime(string token)
|
||||
{
|
||||
_recent.Get(token, true).Wait();
|
||||
await _recent.Get(token, true);
|
||||
}
|
||||
|
||||
private void ClearAll()
|
||||
|
@@ -3,7 +3,6 @@ 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;
|
||||
@@ -21,9 +20,7 @@ namespace ModernKeePass.ViewModels
|
||||
|
||||
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;
|
||||
@@ -41,7 +38,7 @@ namespace ModernKeePass.ViewModels
|
||||
|
||||
public async Task Save(StorageFile file)
|
||||
{
|
||||
var token = StorageApplicationPermissions.FutureAccessList.Add(file);
|
||||
var token = StorageApplicationPermissions.FutureAccessList.Add(file, file.Name);
|
||||
await _mediator.Send(new SaveDatabaseCommand { FilePath = token });
|
||||
_navigation.NavigateTo(Constants.Navigation.MainPage);
|
||||
}
|
||||
|
@@ -3,20 +3,15 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using GalaSoft.MvvmLight.Messaging;
|
||||
using GalaSoft.MvvmLight.Views;
|
||||
using MediatR;
|
||||
using Messages;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
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.Application.Database.Queries.OpenDatabase;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class OpenDatabaseControlVm : ObservableObject
|
||||
public class OpenDatabaseControlVm : ViewModelBase
|
||||
{
|
||||
public enum StatusTypes
|
||||
{
|
||||
@@ -100,9 +95,6 @@ namespace ModernKeePass.ViewModels
|
||||
|
||||
private readonly IMediator _mediator;
|
||||
private readonly IResourceProxy _resource;
|
||||
private readonly IMessenger _messenger;
|
||||
private readonly IDialogService _dialog;
|
||||
private readonly INotificationService _notification;
|
||||
private bool _hasPassword;
|
||||
private bool _hasKeyFile;
|
||||
private bool _isOpening;
|
||||
@@ -112,22 +104,11 @@ namespace ModernKeePass.ViewModels
|
||||
private string _keyFilePath;
|
||||
private string _keyFileText;
|
||||
private string _openButtonLabel;
|
||||
|
||||
public OpenDatabaseControlVm() : this(
|
||||
App.Services.GetRequiredService<IMediator>(),
|
||||
App.Services.GetRequiredService<IResourceProxy>(),
|
||||
App.Services.GetRequiredService<IMessenger>(),
|
||||
App.Services.GetRequiredService<IDialogService>(),
|
||||
App.Services.GetRequiredService<INotificationService>())
|
||||
{ }
|
||||
|
||||
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, IMessenger messenger, IDialogService dialog, INotificationService notification)
|
||||
|
||||
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_resource = resource;
|
||||
_messenger = messenger;
|
||||
_dialog = dialog;
|
||||
_notification = notification;
|
||||
OpenDatabaseCommand = new RelayCommand<string>(async databaseFilePath => await TryOpenDatabase(databaseFilePath), _ => IsValid);
|
||||
_keyFileText = _resource.GetResourceValue("CompositeKeyDefaultKeyFile");
|
||||
_openButtonLabel = _resource.GetResourceValue("CompositeKeyOpenButtonLabel");
|
||||
@@ -135,30 +116,13 @@ namespace ModernKeePass.ViewModels
|
||||
|
||||
public async Task TryOpenDatabase(string databaseFilePath)
|
||||
{
|
||||
_messenger.Send(new DatabaseOpeningMessage {Token = databaseFilePath});
|
||||
MessengerInstance.Send(new DatabaseOpeningMessage {Token = databaseFilePath});
|
||||
|
||||
var database = await _mediator.Send(new GetDatabaseQuery());
|
||||
if (database.IsOpen)
|
||||
if (database.IsDirty)
|
||||
{
|
||||
await _dialog.ShowMessage(_resource.GetResourceValue("MessageDialogDBOpenTitle"),
|
||||
string.Format(_resource.GetResourceValue("MessageDialogDBOpenDesc"), database.Name),
|
||||
_resource.GetResourceValue("MessageDialogDBOpenButtonSave"),
|
||||
_resource.GetResourceValue("MessageDialogDBOpenButtonDiscard"),
|
||||
async isOk =>
|
||||
{
|
||||
if (isOk)
|
||||
{
|
||||
await _mediator.Send(new SaveDatabaseCommand());
|
||||
_notification.Show(database.Name, _resource.GetResourceValue("ToastSavedMessage"));
|
||||
await _mediator.Send(new CloseDatabaseCommand());
|
||||
await OpenDatabase(databaseFilePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _mediator.Send(new CloseDatabaseCommand());
|
||||
await OpenDatabase(databaseFilePath);
|
||||
}
|
||||
});
|
||||
MessengerInstance.Register<DatabaseClosedMessage>(this, async message => await OpenDatabase(message.Parameter as string));
|
||||
MessengerInstance.Send(new DatabaseAlreadyOpenedMessage {Parameter = databaseFilePath});
|
||||
}
|
||||
else await OpenDatabase(databaseFilePath);
|
||||
}
|
||||
@@ -180,7 +144,7 @@ namespace ModernKeePass.ViewModels
|
||||
});
|
||||
var rootGroupId = (await _mediator.Send(new GetDatabaseQuery())).RootGroupId;
|
||||
|
||||
_messenger.Send(new DatabaseOpenedMessage { RootGroupId = rootGroupId });
|
||||
MessengerInstance.Send(new DatabaseOpenedMessage { RootGroupId = rootGroupId });
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
|
@@ -1,20 +1,17 @@
|
||||
using System.Threading.Tasks;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using GalaSoft.MvvmLight.Messaging;
|
||||
using MediatR;
|
||||
using Messages;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Application.Security.Commands.GenerateKeyFile;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class SetCredentialsVm : ObservableObject
|
||||
public class SetCredentialsVm : ViewModelBase
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ICredentialsProxy _credentials;
|
||||
private readonly IMessenger _messenger;
|
||||
|
||||
public bool HasPassword
|
||||
{
|
||||
@@ -103,18 +100,11 @@ namespace ModernKeePass.ViewModels
|
||||
private string _keyFilePath;
|
||||
private string _keyFileText;
|
||||
private string _openButtonLabel;
|
||||
|
||||
public SetCredentialsVm(): this(
|
||||
App.Services.GetRequiredService<IMediator>(),
|
||||
App.Services.GetRequiredService<ICredentialsProxy>(),
|
||||
App.Services.GetRequiredService<IMessenger>(),
|
||||
App.Services.GetRequiredService<IResourceProxy>()) { }
|
||||
|
||||
public SetCredentialsVm(IMediator mediator, ICredentialsProxy credentials, IMessenger messenger, IResourceProxy resource)
|
||||
|
||||
public SetCredentialsVm(IMediator mediator, ICredentialsProxy credentials, IResourceProxy resource)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_credentials = credentials;
|
||||
_messenger = messenger;
|
||||
GenerateCredentialsCommand = new RelayCommand(GenerateCredentials, () => IsValid);
|
||||
|
||||
_keyFileText = resource.GetResourceValue("CompositeKeyDefaultKeyFile");
|
||||
@@ -127,7 +117,7 @@ namespace ModernKeePass.ViewModels
|
||||
|
||||
private void GenerateCredentials()
|
||||
{
|
||||
_messenger.Send(new CredentialsSetMessage
|
||||
MessengerInstance.Send(new CredentialsSetMessage
|
||||
{
|
||||
Password = HasPassword ? Password : null,
|
||||
KeyFilePath = HasKeyFile ? KeyFilePath : null
|
||||
|
@@ -69,17 +69,17 @@ namespace ModernKeePass.ViewModels
|
||||
SimpleIoc.Default.Register<SaveVm>();
|
||||
}
|
||||
|
||||
public MainVm Main => ServiceLocator.Current.GetInstance<MainVm>();
|
||||
public SettingsVm Settings => ServiceLocator.Current.GetInstance<SettingsVm>();
|
||||
public SettingsDatabaseVm SettingsDatabase => ServiceLocator.Current.GetInstance<SettingsDatabaseVm>();
|
||||
public SettingsNewVm SettingsNew => ServiceLocator.Current.GetInstance<SettingsNewVm>();
|
||||
public SettingsSaveVm SettingsSave => ServiceLocator.Current.GetInstance<SettingsSaveVm>();
|
||||
public SettingsSecurityVm SettingsSecurity => ServiceLocator.Current.GetInstance<SettingsSecurityVm>();
|
||||
public MainVm Main => ServiceLocator.Current.GetInstance<MainVm>(Guid.NewGuid().ToString());
|
||||
public SettingsVm Settings => ServiceLocator.Current.GetInstance<SettingsVm>(Guid.NewGuid().ToString());
|
||||
public SettingsDatabaseVm SettingsDatabase => ServiceLocator.Current.GetInstance<SettingsDatabaseVm>(Guid.NewGuid().ToString());
|
||||
public SettingsNewVm SettingsNew => ServiceLocator.Current.GetInstance<SettingsNewVm>(Guid.NewGuid().ToString());
|
||||
public SettingsSaveVm SettingsSave => ServiceLocator.Current.GetInstance<SettingsSaveVm>(Guid.NewGuid().ToString());
|
||||
public SettingsSecurityVm SettingsSecurity => ServiceLocator.Current.GetInstance<SettingsSecurityVm>(Guid.NewGuid().ToString());
|
||||
public OpenDatabaseControlVm OpenDatabaseControl => ServiceLocator.Current.GetInstance<OpenDatabaseControlVm>(Guid.NewGuid().ToString());
|
||||
public SetCredentialsVm SetCredentials => ServiceLocator.Current.GetInstance<SetCredentialsVm>(Guid.NewGuid().ToString());
|
||||
public NewVm New => ServiceLocator.Current.GetInstance<NewVm>();
|
||||
public OpenVm Open => ServiceLocator.Current.GetInstance<OpenVm>();
|
||||
public RecentVm Recent => ServiceLocator.Current.GetInstance<RecentVm>();
|
||||
public RecentVm Recent => ServiceLocator.Current.GetInstance<RecentVm>(Guid.NewGuid().ToString());
|
||||
public SaveVm Save => ServiceLocator.Current.GetInstance<SaveVm>();
|
||||
|
||||
public static void Cleanup()
|
||||
|
@@ -15,8 +15,6 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Actions\ToastAction.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Common\Constants.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Common\NavigationHelper.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Common\ObservableDictionary.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Common\RelayCommand.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Common\SuspensionManager.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\ListViewWithDisable.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\TextBoxWithButton.cs" />
|
||||
@@ -31,7 +29,6 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Converters\PluralizationConverter.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Converters\ProgressBarLegalValuesConverter.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Converters\TextToWidthConverter.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Events\PasswordEventArgs.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ColorExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\DispatcherTaskExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Messages\CredentialsMessage.cs" />
|
||||
@@ -57,5 +54,6 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\SaveVm.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\UserControls\OpenDatabaseControlVm.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\UserControls\SetCredentialsVm.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\ViewModelLocator.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
Reference in New Issue
Block a user