1st working version in clean arch

WIP Parent group mapping issues
This commit is contained in:
Geoffroy BONNEVILLE
2020-03-30 19:43:04 +02:00
parent d1ba73ee9d
commit e4bd788ed3
54 changed files with 319 additions and 283 deletions

View File

@@ -3,13 +3,16 @@ using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.UI.Xaml.Controls;
using MediatR;
using ModernKeePass.Application.Database.Commands.SaveDatabase;
using ModernKeePass.Application.Database.Models;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Application.Entry.Commands.SetFieldValue;
using ModernKeePass.Application.Group.Commands.AddEntry;
using ModernKeePass.Application.Group.Commands.CreateGroup;
using ModernKeePass.Application.Group.Commands.DeleteEntry;
using ModernKeePass.Application.Group.Commands.RemoveEntry;
using ModernKeePass.Application.Security.Commands.GeneratePassword;
using ModernKeePass.Application.Security.Queries.EstimatePasswordComplexity;
using ModernKeePass.Common;
@@ -34,8 +37,23 @@ namespace ModernKeePass.ViewModels
public bool BracketsPatternSelected { get; set; }
public string CustomChars { get; set; } = string.Empty;
public string Id => _entry.Id;
public bool IsRecycleOnDelete => _database.IsRecycleBinEnabled && !ParentGroup.IsSelected;
public IEnumerable<IVmEntity> BreadCrumb => new List<IVmEntity>(ParentGroup.BreadCrumb) {ParentGroup};
public IEnumerable<Application.Group.Models.GroupVm> BreadCrumb
{
get
{
var groups = new Stack<Application.Group.Models.GroupVm>();
var group = _entry.ParentGroup;
while (group.ParentGroup != null)
{
group = group.ParentGroup;
groups.Push(group);
}
return groups;
}
}
/// <summary>
/// Determines if the Entry is current or from history
/// </summary>
@@ -87,12 +105,12 @@ namespace ModernKeePass.ViewModels
set { _mediator.Send(new SetFieldValueCommand { EntryId = Id, FieldName = nameof(Notes), FieldValue = value }); }
}
public int Icon
public Symbol Icon
{
get
{
if (HasExpired) return (int)Domain.Enums.Icon.ReportHacked;
return (int) _entry.Icon;
if (HasExpired) return Symbol.ReportHacked;
return (Symbol) _entry.Icon;
}
set { _mediator.Send(new SetFieldValueCommand { EntryId = Id, FieldName = nameof(Icon), FieldValue = value }); }
}
@@ -156,20 +174,7 @@ namespace ModernKeePass.ViewModels
}
}
public IEnumerable<IVmEntity> History
{
get
{
var history = new Stack<EntryVm>();
foreach (var historyEntry in _entry.History)
{
history.Push(new EntryVm(historyEntry, ParentGroup) {IsSelected = false});
}
history.Push(this);
return history;
}
}
public IEnumerable<Application.Entry.Models.EntryVm> History => _entry.History;
public Color? BackgroundColor
@@ -190,6 +195,8 @@ namespace ModernKeePass.ViewModels
}
}
public bool CanRestore => _entry.ParentGroup == _database.RecycleBin;
public ICommand SaveCommand { get; }
public ICommand GeneratePasswordCommand { get; }
public ICommand UndoDeleteCommand { get; }
@@ -197,7 +204,7 @@ namespace ModernKeePass.ViewModels
private readonly Application.Entry.Models.EntryVm _entry;
private readonly IMediator _mediator;
private readonly IResourceService _resource;
private DatabaseVm _database;
private readonly DatabaseVm _database;
private bool _isEditMode;
private bool _isRevealPassword;
private double _passwordLength = 25;
@@ -205,18 +212,20 @@ namespace ModernKeePass.ViewModels
public EntryVm() { }
internal EntryVm(Application.Entry.Models.EntryVm entry, Application.Group.Models.GroupVm parent) : this(entry, parent, App.Mediator, new ResourcesService()) { }
internal EntryVm(Application.Entry.Models.EntryVm entry, bool isNewEntry = false) : this(entry, App.Mediator, new ResourcesService(), isNewEntry) { }
public EntryVm(Application.Entry.Models.EntryVm entry, Application.Group.Models.GroupVm parent, IMediator mediator, IResourceService resource)
public EntryVm(Application.Entry.Models.EntryVm entry, IMediator mediator, IResourceService resource, bool isNewEntry = false)
{
_entry = entry;
_mediator = mediator;
_resource = resource;
_database = _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
_isEditMode = isNewEntry;
if (isNewEntry) GeneratePassword().GetAwaiter().GetResult();
SaveCommand = new RelayCommand(() => _mediator.Send(new SaveDatabaseCommand()));
GeneratePasswordCommand = new RelayCommand(async () => await GeneratePassword());
UndoDeleteCommand = new RelayCommand(async () => await Move(PreviousGroup), () => PreviousGroup != null);
UndoDeleteCommand = new RelayCommand(async () => await Move(entry.ParentGroup), () => _entry.ParentGroup != null);
}
public async Task GeneratePassword()
@@ -242,20 +251,18 @@ namespace ModernKeePass.ViewModels
{
if (_database.IsRecycleBinEnabled && _database.RecycleBin == null)
await _mediator.Send(new CreateGroupCommand { ParentGroup = _database.RootGroup, IsRecycleBin = true, Name = recycleBinTitle});
await Move(_database.IsRecycleBinEnabled && !ParentGroup.IsSelected ? _database.RecycleBin : null);
await Move(_database.IsRecycleBinEnabled && _entry.ParentGroup == _database.RecycleBin ? _database.RecycleBin : null);
}
public async Task Move(Application.Group.Models.GroupVm destination)
{
PreviousGroup = ParentGroup;
PreviousGroup.Entries.Remove(this);
await _mediator.Send(new RemoveEntryCommand { ParentGroup = _entry.ParentGroup, Entry = _entry });
if (destination == null)
{
await _mediator.Send(new DeleteEntryCommand { Entry = _entry });
return;
}
ParentGroup = destination;
ParentGroup.Entries.Add(this);
await _mediator.Send(new AddEntryCommand { ParentGroup = destination, Entry = _entry });
}
public async Task CommitDelete()

View File

@@ -4,6 +4,7 @@ using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.UI.Xaml.Controls;
using MediatR;
using ModernKeePass.Application.Database.Commands.SaveDatabase;
using ModernKeePass.Application.Database.Models;
@@ -27,38 +28,29 @@ namespace ModernKeePass.ViewModels
{
public class GroupVm : NotifyPropertyChangedBase, IVmEntity, ISelectableModel
{
public GroupVm ParentGroup { get; private set; }
public GroupVm PreviousGroup { get; private set; }
public ObservableCollection<Application.Entry.Models.EntryVm> Entries => new ObservableCollection<Application.Entry.Models.EntryVm>(_group.Entries);
public ObservableCollection<EntryVm> Entries
{
get { return _entries; }
private set { SetProperty(ref _entries, value); }
}
public ObservableCollection<Application.Group.Models.GroupVm> Groups => new ObservableCollection<Application.Group.Models.GroupVm>(_group.SubGroups);
public IEnumerable<EntryVm> SubEntries
public IEnumerable<Application.Entry.Models.EntryVm> SubEntries
{
get
{
var subEntries = new List<EntryVm>();
var subEntries = new List<Application.Entry.Models.EntryVm>();
subEntries.AddRange(Entries);
foreach (var group in Groups)
{
subEntries.AddRange(group.SubEntries);
subEntries.AddRange(group.Entries);
}
return subEntries;
}
}
public ObservableCollection<GroupVm> Groups { get; set; } = new ObservableCollection<GroupVm>();
public string Id => _group.Id;
public bool IsNotRoot => ParentGroup != null;
public bool IsNotRoot => _database.RootGroup != _group;
public bool ShowRestore => IsNotRoot && ParentGroup.IsSelected;
public bool IsRecycleOnDelete => GetDatabase().IsRecycleBinEnabled && !IsSelected && !ParentGroup.IsSelected;
public bool ShowRestore => IsNotRoot && _database.RecycleBin != _group;
/// <summary>
/// Is the Group the database Recycle Bin?
@@ -67,33 +59,30 @@ namespace ModernKeePass.ViewModels
{
get
{
var database = GetDatabase();
return database.IsRecycleBinEnabled && database.RecycleBinId == Id;
return _database.IsRecycleBinEnabled && _database.RecycleBin == _group;
}
set
{
if (value && _group != null) _database.RecycleBin = this;
if (value && _group != null) _database.RecycleBin = _group;
}
}
public IOrderedEnumerable<IGrouping<char, EntryVm>> EntriesZoomedOut => from e in Entries
public IOrderedEnumerable<IGrouping<char, Application.Entry.Models.EntryVm>> EntriesZoomedOut => from e in Entries
group e by e.Title.ToUpper().FirstOrDefault() into grp
orderby grp.Key
select grp;
public string Id => _group.Id;
public string Title
{
get { return _group == null ? string.Empty : _group.Title; }
get { return _group.Title; }
set { _group.Title = value; }
}
public int Icon
public Symbol Icon
{
get
{
if (_group?.Icon != null) return (int) _group?.Icon;
return -1;
}
get { return (Symbol) _group.Icon; }
set { _group.Icon = (Icon)value; }
}
@@ -114,12 +103,12 @@ namespace ModernKeePass.ViewModels
set { SetProperty(ref _isMenuClosed, value); }
}
public IEnumerable<IVmEntity> BreadCrumb
public IEnumerable<Application.Group.Models.GroupVm> BreadCrumb
{
get
{
var groups = new Stack<GroupVm>();
var group = this;
var groups = new Stack<Application.Group.Models.GroupVm>();
var group = _group;
while (group.ParentGroup != null)
{
group = group.ParentGroup;
@@ -137,33 +126,31 @@ namespace ModernKeePass.ViewModels
private readonly Application.Group.Models.GroupVm _group;
private readonly IMediator _mediator;
private readonly DatabaseVm _database;
private bool _isEditMode;
private Application.Entry.Models.EntryVm _reorderedEntry;
private ObservableCollection<EntryVm> _entries = new ObservableCollection<EntryVm>();
private bool _isMenuClosed = true;
public GroupVm() {}
internal GroupVm(Application.Group.Models.GroupVm group, GroupVm parent, string recycleBinId = null) : this(group, parent, App.Mediator, recycleBinId)
internal GroupVm(Application.Group.Models.GroupVm group) : this(group, App.Mediator)
{ }
public GroupVm(Application.Group.Models.GroupVm group, GroupVm parent, IMediator mediator, string recycleBinId = null)
public GroupVm(Application.Group.Models.GroupVm group, IMediator mediator, bool isEditMode = false)
{
_group = group;
_mediator = mediator;
ParentGroup = parent;
_database = _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
_isEditMode = isEditMode;
SaveCommand = new RelayCommand(async () => await _mediator.Send(new SaveDatabaseCommand()));
SortEntriesCommand = new RelayCommand(async () =>
await SortEntriesAsync().ConfigureAwait(false), () => IsEditMode);
SortGroupsCommand = new RelayCommand(async () =>
await SortGroupsAsync().ConfigureAwait(false), () => IsEditMode);
UndoDeleteCommand = new RelayCommand(async () => await Move(PreviousGroup), () => PreviousGroup != null);
if (recycleBinId != null && _group.Id.Equals(recycleBinId)) _database.RecycleBin = this;
Entries = new ObservableCollection<EntryVm>(group.Entries.Select(e => new EntryVm(e, this)));
UndoDeleteCommand = new RelayCommand(async () => await Move(group.ParentGroup), () => _group.ParentGroup != null);
Entries.CollectionChanged += Entries_CollectionChanged;
Groups = new ObservableCollection<GroupVm>(group.SubGroups.Select(g => new GroupVm(g, this, recycleBinId)));
}
private async void Entries_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
@@ -189,50 +176,33 @@ namespace ModernKeePass.ViewModels
}
}
public async Task<GroupVm> AddNewGroup(string name = "")
public async Task<Application.Group.Models.GroupVm> AddNewGroup(string name = "")
{
var newGroup = await _mediator.Send(new CreateGroupCommand {Name = name, ParentGroup = _group});
var newGroupVm = new GroupVm(newGroup, this) {Title = name, IsEditMode = string.IsNullOrEmpty(name)};
Groups.Add(newGroupVm);
return newGroupVm;
return await _mediator.Send(new CreateGroupCommand {Name = name, ParentGroup = _group});
}
public async Task<EntryVm> AddNewEntry()
public async Task<Application.Entry.Models.EntryVm> AddNewEntry()
{
var newEntry = await _mediator.Send(new CreateEntryCommand { ParentGroup = _group });
var newEntryVm = new EntryVm(newEntry, this) {IsEditMode = true};
await newEntryVm.GeneratePassword();
Entries.Add(newEntryVm);
return newEntryVm;
return await _mediator.Send(new CreateEntryCommand { ParentGroup = _group });
}
public async Task MarkForDelete(string recycleBinTitle)
{
var database = GetDatabase();
if (database.IsRecycleBinEnabled && database.RecycleBinId == null)
await _mediator.Send(new CreateGroupCommand {ParentGroup = database.RootGroup, IsRecycleBin = true, Name = recycleBinTitle});
await Move(database.IsRecycleBinEnabled && !IsSelected ? _database.RecycleBin : null);
if (_database.IsRecycleBinEnabled && _database.RecycleBin == null)
await _mediator.Send(new CreateGroupCommand {ParentGroup = _database.RootGroup, IsRecycleBin = true, Name = recycleBinTitle});
await Move(_database.IsRecycleBinEnabled && !IsSelected ? _database.RecycleBin : null);
((RelayCommand)UndoDeleteCommand).RaiseCanExecuteChanged();
}
public async Task UndoDelete()
{
await Move(PreviousGroup);
}
public async Task Move(GroupVm destination)
public async Task Move(Application.Group.Models.GroupVm destination)
{
PreviousGroup = ParentGroup;
PreviousGroup.Groups.Remove(this);
await _mediator.Send(new RemoveGroupCommand {ParentGroup = PreviousGroup._group, Group = _group});
await _mediator.Send(new RemoveGroupCommand {ParentGroup = _group.ParentGroup, Group = _group});
if (destination == null)
{
await _mediator.Send(new DeleteGroupCommand { Group = _group });
return;
}
ParentGroup = destination;
ParentGroup.Groups.Add(this);
await _mediator.Send(new AddGroupCommand {ParentGroup = ParentGroup._group, Group = _group});
await _mediator.Send(new AddGroupCommand {ParentGroup = destination, Group = _group});
}
public async Task CommitDelete()
@@ -248,20 +218,13 @@ namespace ModernKeePass.ViewModels
private async Task SortEntriesAsync()
{
await _mediator.Send(new SortEntriesCommand {Group = _group});
Entries = new ObservableCollection<EntryVm>(Entries.OrderBy(e => e.Title));
OnPropertyChanged(nameof(Entries));
}
private async Task SortGroupsAsync()
{
await _mediator.Send(new SortGroupsCommand {Group = _group});
Groups = new ObservableCollection<GroupVm>(Groups.OrderBy(g => g.Title).ThenBy(g => g._group == null));
// TODO: should not be needed
OnPropertyChanged(nameof(Groups));
}
private DatabaseVm GetDatabase()
{
return _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using Windows.UI.Xaml.Controls;
using ModernKeePass.Common;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.Interfaces;
namespace ModernKeePass.ViewModels

View File

@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Windows.Storage;
using ModernKeePass.Common;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;

View File

@@ -2,10 +2,6 @@
using System.Collections.ObjectModel;
using System.Linq;
using MediatR;
using ModernKeePass.Application.Cryptography.Models;
using ModernKeePass.Application.Cryptography.Queries.GetCiphers;
using ModernKeePass.Application.Cryptography.Queries.GetCompressions;
using ModernKeePass.Application.Cryptography.Queries.GetKeyDerivations;
using ModernKeePass.Application.Database.Models;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Application.Parameters.Commands.SetCipher;
@@ -13,8 +9,12 @@ using ModernKeePass.Application.Parameters.Commands.SetCompression;
using ModernKeePass.Application.Parameters.Commands.SetHasRecycleBin;
using ModernKeePass.Application.Parameters.Commands.SetKeyDerivation;
using ModernKeePass.Application.Parameters.Commands.SetRecycleBin;
using ModernKeePass.Application.Parameters.Models;
using ModernKeePass.Application.Parameters.Queries.GetCiphers;
using ModernKeePass.Application.Parameters.Queries.GetCompressions;
using ModernKeePass.Application.Parameters.Queries.GetKeyDerivations;
using ModernKeePass.Common;
using ModernKeePass.Interfaces;
using ModernKeePass.Domain.Interfaces;
namespace ModernKeePass.ViewModels
{
@@ -37,14 +37,14 @@ namespace ModernKeePass.ViewModels
public bool IsNewRecycleBin
{
get { return _database.RecycleBinId == null; }
get { return _database.RecycleBin == null; }
set
{
if (value) _mediator.Send(new SetRecycleBinCommand() { RecycleBinId = null }).GetAwaiter().GetResult();
if (value) _mediator.Send(new SetRecycleBinCommand { RecycleBin = null }).GetAwaiter().GetResult();
}
}
public ObservableCollection<GroupVm> Groups { get; set; }
public ObservableCollection<Application.Group.Models.GroupVm> Groups { get; set; }
public IEnumerable<CipherVm> Ciphers => _mediator.Send(new GetCiphersQuery()).GetAwaiter().GetResult();
public IEnumerable<string> Compressions => _mediator.Send(new GetCompressionsQuery()).GetAwaiter().GetResult();
@@ -119,7 +119,7 @@ namespace ModernKeePass.ViewModels
{
_mediator = mediator;
_database = _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
//Groups = _database.RootGroup.SubGroups;
Groups = new ObservableCollection<Application.Group.Models.GroupVm>(_database.RootGroup.SubGroups);
}
}
}

View File

@@ -6,6 +6,7 @@ using Windows.UI.Xaml.Controls;
using MediatR;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Common;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;
using ModernKeePass.Views;

View File

@@ -1,21 +1,20 @@
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
using MediatR;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Application.Entry.Commands.SetFieldValue;
using ModernKeePass.Application.Group.Commands.CreateEntry;
using ModernKeePass.Application.Group.Commands.CreateGroup;
using ModernKeePass.Converters;
using ModernKeePass.Domain.Enums;
using ModernKeePass.ImportFormats;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;
namespace ModernKeePass.ViewModels
{
public class NewVm : OpenVm
{
private readonly IMediator _mediator;
private readonly ISettingsService _settings;
private string _importFormatHelp;
public string Password { get; set; }
@@ -37,18 +36,19 @@ namespace ModernKeePass.ViewModels
}
}
public NewVm(): this(App.Mediator) { }
public NewVm(): this(App.Mediator, new SettingsService()) { }
public NewVm(IMediator mediator)
public NewVm(IMediator mediator, ISettingsService settings)
{
_mediator = mediator;
_settings = settings;
}
public async Task PopulateInitialData(ISettingsService settings, IImportService<IFormat> importService)
public async Task<Application.Group.Models.GroupVm> PopulateInitialData()
{
var database = await _mediator.Send(new GetDatabaseQuery());
if (settings.GetSetting<bool>("Sample") && !IsImportChecked) await CreateSampleData(database.RootGroup);
else if (IsImportChecked && ImportFile != null && ! (ImportFormat is NullImportFormat)) importService.Import(ImportFormat, ImportFile, database.RootGroup);
if (_settings.GetSetting<bool>("Sample") && !IsImportChecked) await CreateSampleData(database.RootGroup);
return database.RootGroup;
}
private async Task CreateSampleData(Application.Group.Models.GroupVm group)

View File

@@ -7,29 +7,32 @@ namespace ModernKeePass.ViewModels
{
public class OpenVm: NotifyPropertyChangedBase
{
private readonly IRecentService _recent;
public bool IsFileSelected => DatabaseFile != null;
public string Name => DatabaseFile?.DisplayName;
public StorageFile DatabaseFile { get; private set; }
internal void OpenFile(StorageFile file)
{
OpenFile(file, RecentService.Instance);
}
public void OpenFile(StorageFile file, IRecentService recent)
public OpenVm(): this(new RecentService()) { }
public OpenVm(IRecentService recent)
{
_recent = recent;
}
public void OpenFile(StorageFile file)
{
DatabaseFile = file;
OnPropertyChanged("Name");
OnPropertyChanged("IsFileSelected");
OnPropertyChanged("DatabaseFile");
AddToRecentList(file, recent);
AddToRecentList(file);
}
private void AddToRecentList(StorageFile file, IRecentService recent)
private void AddToRecentList(StorageFile file)
{
recent.Add(file, file.DisplayName);
_recent.Add(file, file.DisplayName);
}
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections.ObjectModel;
using System.Windows.Input;
using ModernKeePass.Common;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;

View File

@@ -4,6 +4,7 @@ using Windows.UI.Xaml.Controls;
using MediatR;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Common;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.Interfaces;
using ModernKeePass.Views;
using ModernKeePass.Services;