Build hierarchy instead of using Automapper

Add entities before removing them
This commit is contained in:
Geoffroy BONNEVILLE
2020-04-01 12:48:36 +02:00
parent 90c592d7ee
commit 57be6bb917
22 changed files with 84 additions and 77 deletions

View File

@@ -39,7 +39,7 @@ namespace ModernKeePass.Application.Database.Commands.CreateDatabase
KeyFilePath = message.KeyFilePath,
Password = message.Password
});
return _mapper.Map<GroupVm>(rootGroup);
return GroupVm.BuildHierarchy(null, rootGroup, _mapper);
}
}

View File

@@ -28,7 +28,7 @@ namespace ModernKeePass.Application.Database.Queries.GetDatabase
if (database.IsOpen)
{
database.Name = _databaseProxy.Name;
database.RootGroup = _mapper.Map<GroupVm>(_databaseProxy.RootGroup);
database.RootGroup = GroupVm.BuildHierarchy(null, _databaseProxy.RootGroup, _mapper);
database.IsRecycleBinEnabled = _databaseProxy.IsRecycleBinEnabled;
database.RecycleBin = _mapper.Map<GroupVm>(_databaseProxy.RecycleBin);
database.Compression = _databaseProxy.Compression;

View File

@@ -42,30 +42,9 @@ namespace ModernKeePass.Application.Database.Queries.OpenDatabase
KeyFilePath = request.KeyFilePath,
Password = request.Password
});
return BuildHierarchy(null, rootGroup);
}
private GroupVm BuildHierarchy(GroupVm parentGroup, GroupEntity groupEntity)
{
var groupVm = _mapper.Map<GroupVm>(groupEntity);
groupVm.ParentGroup = parentGroup;
if (parentGroup != null)
{
groupVm.Breadcrumb.AddRange(parentGroup.Breadcrumb);
groupVm.Breadcrumb.Add(parentGroup);
}
groupVm.Entries = groupEntity.Entries.Select(e =>
{
var entry = _mapper.Map<EntryVm>(e);
entry.ParentGroup = groupVm;
entry.Breadcrumb.AddRange(groupVm.Breadcrumb);
entry.Breadcrumb.Add(groupVm);
return entry;
}).OrderBy(e => e.Title).ToList();
groupVm.SubGroups = groupEntity.SubGroups.Select(g => BuildHierarchy(groupVm, g)).ToList();
return groupVm;
return GroupVm.BuildHierarchy(null, rootGroup, _mapper);
}
}
}
}

View File

@@ -25,7 +25,7 @@ namespace ModernKeePass.Application.Database.Queries.ReOpenDatabase
if (!_database.IsOpen) throw new DatabaseClosedException();
var rootGroup = await _database.ReOpen();
return _mapper.Map<GroupVm>(rootGroup);
return GroupVm.BuildHierarchy(null, rootGroup, _mapper);
}
}
}

View File

@@ -37,7 +37,7 @@ namespace ModernKeePass.Application.Entry.Models
public void Mapping(Profile profile)
{
profile.CreateMap<EntryEntity, EntryVm>()
//.ForMember(d => d.ParentGroup, opts => opts.MapFrom(s => s.Parent))
.ForMember(d => d.ParentGroup, opts => opts.Ignore())
.ForMember(d => d.Id, opts => opts.MapFrom(s => s.Id))
.ForMember(d => d.Title, opts => opts.MapFrom(s => s.Name))
.ForMember(d => d.Username, opts => opts.MapFrom(s => s.UserName))

View File

@@ -29,6 +29,7 @@ namespace ModernKeePass.Application.Group.Commands.CreateGroup
var group = _database.CreateGroup(message.ParentGroup.Id, message.Name, message.IsRecycleBin);
var groupVm = _mapper.Map<GroupVm>(group);
groupVm.ParentGroup = message.ParentGroup;
message.ParentGroup.SubGroups.Add(groupVm);
return groupVm;
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using ModernKeePass.Application.Common.Interfaces;
@@ -29,13 +30,34 @@ namespace ModernKeePass.Application.Group.Models
public void Mapping(Profile profile)
{
profile.CreateMap<GroupEntity, GroupVm>()
//.ForMember(d => d.ParentGroup, opts => opts.MapFrom(s => s.Parent))
.ForMember(d => d.ParentGroup, opts => opts.Ignore())
.ForMember(d => d.Id, opts => opts.MapFrom(s => s.Id))
.ForMember(d => d.Title, opts => opts.MapFrom(s => s.Name))
.ForMember(d => d.Icon, opts => opts.MapFrom(s => s.Icon));
//.ForMember(d => d.Entries, opts => opts.MapFrom(s => s.Entries.OrderBy(e => e.Name)))
//.ForMember(d => d.SubGroups, opts => opts.MapFrom(s => s.SubGroups));
.ForMember(d => d.Icon, opts => opts.MapFrom(s => s.Icon))
.ForMember(d => d.Entries, opts => opts.Ignore())
.ForMember(d => d.SubGroups, opts => opts.Ignore());
}
internal static GroupVm BuildHierarchy(GroupVm parentGroup, GroupEntity groupEntity, IMapper mapper)
{
var groupVm = mapper.Map<GroupVm>(groupEntity);
groupVm.ParentGroup = parentGroup;
if (parentGroup != null)
{
groupVm.Breadcrumb.AddRange(parentGroup.Breadcrumb);
groupVm.Breadcrumb.Add(parentGroup);
}
groupVm.Entries = groupEntity.Entries.Select(e =>
{
var entry = mapper.Map<EntryVm>(e);
entry.ParentGroup = groupVm;
entry.Breadcrumb.AddRange(groupVm.Breadcrumb);
entry.Breadcrumb.Add(groupVm);
return entry;
}).OrderBy(e => e.Title).ToList();
groupVm.SubGroups = groupEntity.SubGroups.Select(g => BuildHierarchy(groupVm, g, mapper)).ToList();
return groupVm;
}
}
}

View File

@@ -6,7 +6,7 @@ namespace ModernKeePass.Domain.Entities
{
public string Id { get; set; }
public string Name { get; set; }
public GroupEntity Parent { get; set; }
public GroupEntity ParentGroup { get; set; }
public string ParentId { get; set; }
public DateTimeOffset LastModificationDate { get; set; }
}

View File

@@ -19,6 +19,7 @@ namespace ModernKeePass.Infrastructure.KeePass
{
Uri url;
CreateMap<PwEntry, EntryEntity>()
.ForMember(dest => dest.ParentGroup, opt => opt.Ignore())
.ForMember(dest => dest.ParentId, opt => opt.MapFrom(src => src.ParentGroup.Uuid.ToHexString()))
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Uuid.ToHexString()))
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => GetEntryValue(src, PwDefs.TitleField)))

View File

@@ -15,13 +15,14 @@ namespace ModernKeePass.Infrastructure.KeePass
private void FromDtoToModel()
{
CreateMap<PwGroup, GroupEntity>()
.ForMember(dest => dest.ParentGroup, opt => opt.Ignore())
.ForMember(d => d.ParentId, opts => opts.MapFrom(s => s.ParentGroup.Uuid.ToHexString()))
.ForMember(d => d.Id, opts => opts.MapFrom(s => s.Uuid.ToHexString()))
.ForMember(d => d.Name, opts => opts.MapFrom(s => s.Name))
.ForMember(d => d.Icon, opts => opts.MapFrom(s => IconMapper.MapPwIconToIcon(s.IconId)))
.ForMember(d => d.LastModificationDate, opts => opts.MapFrom(s => s.LastModificationTime));
//.ForMember(d => d.Entries, opts => opts.MapFrom(s => s.Entries))
//.ForMember(d => d.SubGroups, opts => opts.MapFrom(s => s.Groups));
.ForMember(d => d.LastModificationDate, opts => opts.MapFrom(s => s.LastModificationTime))
.ForMember(d => d.Entries, opts => opts.Ignore())
.ForMember(d => d.SubGroups, opts => opts.Ignore());
}
private void FromModelToDto()

View File

@@ -340,11 +340,11 @@ namespace ModernKeePass.Infrastructure.KeePass
private GroupEntity BuildHierarchy(GroupEntity parentGroup, PwGroup pwGroup)
{
var group = _mapper.Map<GroupEntity>(pwGroup);
group.Parent = parentGroup;
group.ParentGroup = parentGroup;
group.Entries = pwGroup.Entries.Select(e =>
{
var entry = _mapper.Map<EntryEntity>(e);
entry.Parent = group;
entry.ParentGroup = group;
return entry;
}).ToList();
group.SubGroups = pwGroup.Groups.Select(g => BuildHierarchy(group, g)).ToList();

View File

@@ -44,7 +44,7 @@ namespace ModernKeePass.Actions
public object Execute(object sender, object parameter)
{
var resource = new ResourcesService();
var type = Entity is GroupVm ? "Group" : "Entry";
var type = Entity is GroupDetailVm ? "Group" : "Entry";
var isRecycleOnDelete = _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult().IsRecycleBinEnabled;
var message = isRecycleOnDelete

View File

@@ -22,7 +22,7 @@ using ModernKeePass.Interfaces;
namespace ModernKeePass.ViewModels
{
public class EntryVm : NotifyPropertyChangedBase, IVmEntity, ISelectableModel
public class EntryDetailVm : NotifyPropertyChangedBase, IVmEntity, ISelectableModel
{
public bool IsRevealPasswordEnabled => !string.IsNullOrEmpty(Password);
public bool HasExpired => HasExpirationDate && ExpiryDate < DateTime.Now;
@@ -68,11 +68,7 @@ namespace ModernKeePass.ViewModels
public string UserName
{
get { return _entry.Username; }
set
{
_mediator.Send(new SetFieldValueCommand { EntryId = Id, FieldName = nameof(UserName), FieldValue = value }).Wait();
_entry.Username = value;
}
set { _entry.Username = value; }
}
public string Password
@@ -225,11 +221,11 @@ namespace ModernKeePass.ViewModels
private double _passwordLength = 25;
private bool _isVisible = true;
public EntryVm() { }
public EntryDetailVm() { }
internal EntryVm(Application.Entry.Models.EntryVm entry, bool isNewEntry = false) : this(entry, App.Mediator, isNewEntry) { }
internal EntryDetailVm(Application.Entry.Models.EntryVm entry, bool isNewEntry = false) : this(entry, App.Mediator, isNewEntry) { }
public EntryVm(Application.Entry.Models.EntryVm entry, IMediator mediator, bool isNewEntry = false)
public EntryDetailVm(Application.Entry.Models.EntryVm entry, IMediator mediator, bool isNewEntry = false)
{
_entry = entry;
_mediator = mediator;
@@ -263,19 +259,18 @@ namespace ModernKeePass.ViewModels
public async Task MarkForDelete(string recycleBinTitle)
{
if (_database.IsRecycleBinEnabled && _database.RecycleBin == null)
await _mediator.Send(new CreateGroupCommand { ParentGroup = _database.RootGroup, IsRecycleBin = true, Name = recycleBinTitle});
await Move(_database.IsRecycleBinEnabled && _entry.ParentGroup == _database.RecycleBin ? _database.RecycleBin : null);
_database.RecycleBin = await _mediator.Send(new CreateGroupCommand { ParentGroup = _database.RootGroup, IsRecycleBin = true, Name = recycleBinTitle});
await Move(_database.IsRecycleBinEnabled && _entry.ParentGroup != _database.RecycleBin ? _database.RecycleBin : null);
}
public async Task Move(Application.Group.Models.GroupVm destination)
{
await _mediator.Send(new AddEntryCommand { ParentGroup = destination, Entry = _entry });
await _mediator.Send(new RemoveEntryCommand { ParentGroup = _entry.ParentGroup, Entry = _entry });
if (destination == null)
{
await _mediator.Send(new DeleteEntryCommand { Entry = _entry });
return;
}
await _mediator.Send(new AddEntryCommand { ParentGroup = destination, Entry = _entry });
}
public async Task CommitDelete()
@@ -287,5 +282,10 @@ namespace ModernKeePass.ViewModels
{
return _entry;
}
public async Task SetFieldValue(string fieldName, object value)
{
await _mediator.Send(new SetFieldValueCommand { EntryId = Id, FieldName = fieldName, FieldValue = value });
}
}
}

View File

@@ -26,7 +26,7 @@ using ModernKeePass.Interfaces;
namespace ModernKeePass.ViewModels
{
public class GroupVm : NotifyPropertyChangedBase, IVmEntity, ISelectableModel
public class GroupDetailVm : NotifyPropertyChangedBase, IVmEntity, ISelectableModel
{
public ObservableCollection<Application.Entry.Models.EntryVm> Entries => new ObservableCollection<Application.Entry.Models.EntryVm>(_group.Entries);
@@ -47,7 +47,6 @@ namespace ModernKeePass.ViewModels
}
}
public bool IsNotRoot => _database.RootGroup != _group;
public bool ShowRestore => IsNotRoot && _database.RecycleBin != _group;
@@ -117,12 +116,12 @@ namespace ModernKeePass.ViewModels
private Application.Entry.Models.EntryVm _reorderedEntry;
private bool _isMenuClosed = true;
public GroupVm() {}
public GroupDetailVm() {}
internal GroupVm(Application.Group.Models.GroupVm group) : this(group, App.Mediator)
internal GroupDetailVm(Application.Group.Models.GroupVm group) : this(group, App.Mediator)
{ }
public GroupVm(Application.Group.Models.GroupVm group, IMediator mediator, bool isEditMode = false)
public GroupDetailVm(Application.Group.Models.GroupVm group, IMediator mediator, bool isEditMode = false)
{
_group = group;
_mediator = mediator;
@@ -151,7 +150,7 @@ namespace ModernKeePass.ViewModels
case NotifyCollectionChangedAction.Add:
if (_reorderedEntry == null)
{
var entry = ((EntryVm) e.NewItems[0]).GetEntry();
var entry = ((EntryDetailVm) e.NewItems[0]).GetEntry();
await _mediator.Send(new AddEntryCommand {Entry = entry, ParentGroup = _group});
}
else
@@ -175,20 +174,19 @@ namespace ModernKeePass.ViewModels
public async Task MarkForDelete(string recycleBinTitle)
{
if (_database.IsRecycleBinEnabled && _database.RecycleBin == null)
await _mediator.Send(new CreateGroupCommand {ParentGroup = _database.RootGroup, IsRecycleBin = true, Name = recycleBinTitle});
_database.RecycleBin = 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 Move(Application.Group.Models.GroupVm destination)
{
await _mediator.Send(new AddGroupCommand {ParentGroup = destination, Group = _group});
await _mediator.Send(new RemoveGroupCommand {ParentGroup = _group.ParentGroup, Group = _group});
if (destination == null)
{
await _mediator.Send(new DeleteGroupCommand { Group = _group });
return;
}
await _mediator.Send(new AddGroupCommand {ParentGroup = destination, Group = _group});
}
public async Task CommitDelete()

View File

@@ -23,7 +23,7 @@ namespace ModernKeePass.ViewModels
{
private readonly IMediator _mediator;
private readonly DatabaseVm _database;
private GroupVm _selectedItem;
private GroupDetailVm _selectedItem;
public bool HasRecycleBin
{
@@ -104,7 +104,7 @@ namespace ModernKeePass.ViewModels
_selectedItem.IsSelected = false;
}
SetProperty(ref _selectedItem, (GroupVm)value);
SetProperty(ref _selectedItem, (GroupDetailVm)value);
if (_selectedItem != null)
{

View File

@@ -372,7 +372,7 @@
</Style>
</Page.Resources>
<Page.DataContext>
<viewModels:EntryVm />
<viewModels:EntryDetailVm />
</Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ChildrenTransitions>
@@ -410,7 +410,7 @@
</Style>
</StackPanel.Resources>
<TextBlock x:Uid="EntryLogin" />
<local:TextBoxWithButton x:Uid="LoginTextBox" Text="{Binding UserName, Mode=TwoWay}" Style="{StaticResource EntryTextBoxWithButtonStyle}" ButtonSymbol="&#xE16F;" IsEnabled="{Binding IsSelected}">
<local:TextBoxWithButton x:Uid="LoginTextBox" Text="{Binding UserName, Mode=TwoWay}" LostFocus="Username_OnLostFocus" Style="{StaticResource EntryTextBoxWithButtonStyle}" ButtonSymbol="&#xE16F;" IsEnabled="{Binding IsSelected}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="ButtonClick">
<actions:ClipboardAction Text="{Binding UserName}" />

View File

@@ -14,7 +14,7 @@ namespace ModernKeePass.Views
/// </summary>
public sealed partial class EntryDetailPage
{
public EntryVm Model => (EntryVm) DataContext;
public EntryDetailVm Model => (EntryDetailVm) DataContext;
/// <summary>
/// NavigationHelper est utilisé sur chaque page pour faciliter la navigation et
@@ -45,7 +45,7 @@ namespace ModernKeePass.Views
/*if (!(e.Parameter is EntryVm)) return;
DataContext = (EntryVm)e.Parameter;*/
var args = e.Parameter as Application.Entry.Models.EntryVm;
if (args != null) DataContext = new EntryVm(args);
if (args != null) DataContext = new EntryDetailVm(args);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
@@ -75,5 +75,10 @@ namespace ModernKeePass.Views
break;
}
}
private async void Username_OnLostFocus(object sender, RoutedEventArgs e)
{
await Model.SetFieldValue(nameof(Model.UserName), ((TextBox) sender).Text);
}
}
}

View File

@@ -19,7 +19,7 @@
<converters:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter"/>
</Page.Resources>
<Page.DataContext>
<viewModels:GroupVm />
<viewModels:GroupDetailVm />
</Page.DataContext>
<Grid>
<Grid.Resources>

View File

@@ -25,7 +25,7 @@ namespace ModernKeePass.Views
/// process lifetime management
/// </summary>
public NavigationHelper NavigationHelper { get; }
public GroupVm Model => (GroupVm)DataContext;
public GroupDetailVm Model => (GroupDetailVm)DataContext;
public GroupDetailPage()
{
@@ -50,12 +50,12 @@ namespace ModernKeePass.Views
var args = e.Parameter as PasswordEventArgs;
if (args != null)
DataContext = new GroupVm(args.RootGroup);
DataContext = new GroupDetailVm(args.RootGroup);
else
{
var vm = e.Parameter as Application.Group.Models.GroupVm;
if (vm != null)
DataContext = new GroupVm(vm);
DataContext = new GroupDetailVm(vm);
}
}

View File

@@ -224,8 +224,8 @@
<DependentUpon>SaveDatabasePage.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModels\EntryVm.cs" />
<Compile Include="ViewModels\GroupVm.cs" />
<Compile Include="ViewModels\EntryDetailVm.cs" />
<Compile Include="ViewModels\GroupDetailVm.cs" />
<Compile Include="ViewModels\Items\SettingsNewVm.cs" />
<Compile Include="ViewModels\SettingsVm.cs" />
<Compile Include="ViewModels\MainVm.cs" />

View File

@@ -26,11 +26,11 @@ namespace ModernKeePassApp.Test.Mock
public string Name => "MockDatabase";
public GroupVm RecycleBin { get; set; }
public GroupDetailVm RecycleBin { get; set; }
public bool RecycleBinEnabled { get; set; }
public GroupVm RootGroup { get; set; }
public GroupDetailVm RootGroup { get; set; }
public void AddDeletedItem(PwUuid id)
{

View File

@@ -116,7 +116,7 @@ namespace ModernKeePassApp.Test
public void TestEntryVm()
{
var database = new DatabaseServiceMock();
var entryVm = new EntryVm(new PwEntry(true, true), new GroupVm(), database, _resource)
var entryVm = new EntryDetailVm(new PwEntry(true, true), new GroupDetailVm(), database, _resource)
{
Title = "Test",
UserName = "login",
@@ -128,7 +128,7 @@ namespace ModernKeePassApp.Test
public void TestGroupVm()
{
var database = new DatabaseServiceMock();
var entryVm = new GroupVm(new PwGroup(true, true), new GroupVm(), database)
var entryVm = new GroupDetailVm(new PwGroup(true, true), new GroupDetailVm(), database)
{
Title = "Test"
};