From e4bd788ed36bf15bf0d1aaefdd51725913d95406 Mon Sep 17 00:00:00 2001 From: Geoffroy BONNEVILLE Date: Mon, 30 Mar 2020 19:43:04 +0200 Subject: [PATCH] 1st working version in clean arch WIP Parent group mapping issues --- ModernKeePass.Application/Application.csproj | 1 + .../Common/Interfaces/IDatabaseProxy.cs | 3 +- .../Common/Interfaces/IEntityVm.cs | 11 ++ .../CloseDatabase/CloseDatabaseCommand.cs | 5 +- .../Queries/GetDatabase/GetDatabaseQuery.cs | 19 +-- .../DependencyInjection.cs | 2 - .../Entry/Models/EntryVm.cs | 6 +- .../Group/Models/GroupVm.cs | 19 ++- .../Parameters/Models/CipherVm.cs | 14 +-- .../Parameters/Models/KeyDerivationVm.cs | 14 +-- .../Queries/GetCiphers/GetCiphersQuery.cs | 16 +-- .../GetCompressions/GetCompressionsQuery.cs | 2 +- .../GetKeyDerivationsQuery.cs | 16 +-- ModernKeePass.Application/project.json | 2 +- ModernKeePass.Domain/Domain.csproj | 1 + ModernKeePass.Domain/Enums/DatabaseVersion.cs | 8 ++ .../DependencyInjection.cs | 7 +- .../KeePass/EntryMappingProfile.cs | 2 + .../KeePass/GroupMappingProfile.cs | 3 +- .../KeePass/KeePassDatabaseClient.cs | 11 +- ModernKeePass.Infrastructure/project.json | 4 +- .../ModernKeePass.KeePassDatabaseTests.csproj | 4 +- ModernKeePass/Actions/DeleteEntityAction.cs | 16 ++- ModernKeePass/App.xaml.cs | 9 +- ModernKeePass/Controls/ListViewWithDisable.cs | 2 +- ...lConverter.cs => IconToSymbolConverter.cs} | 6 +- ModernKeePass/DependencyInjection.cs | 18 +++ ModernKeePass/Events/PasswordEventArgs.cs | 2 +- ModernKeePass/Interfaces/IVmEntity.cs | 10 +- ModernKeePass/Package.appxmanifest | 2 +- .../SelectableDataTemplateSelector.cs | 2 +- ModernKeePass/ViewModels/EntryVm.cs | 63 +++++----- ModernKeePass/ViewModels/GroupVm.cs | 111 ++++++------------ .../ViewModels/Items/ListMenuItemVm.cs | 1 + .../ViewModels/Items/RecentItemVm.cs | 1 + .../ViewModels/Items/SettingsDatabaseVm.cs | 18 +-- ModernKeePass/ViewModels/MainVm.cs | 1 + ModernKeePass/ViewModels/NewVm.cs | 16 +-- ModernKeePass/ViewModels/OpenVm.cs | 21 ++-- ModernKeePass/ViewModels/RecentVm.cs | 1 + ModernKeePass/ViewModels/SettingsVm.cs | 1 + .../Views/BasePages/LayoutAwarePageBase.cs | 1 + ModernKeePass/Views/EntryDetailPage.xaml | 11 +- ModernKeePass/Views/EntryDetailPage.xaml.cs | 8 +- ModernKeePass/Views/GroupDetailPage.xaml | 12 +- ModernKeePass/Views/GroupDetailPage.xaml.cs | 9 +- .../MainPageFrames/NewDatabasePage.xaml.cs | 17 ++- .../MainPageFrames/OpenDatabasePage.xaml.cs | 11 +- .../CompositeKeyUserControl.xaml.cs | 4 +- .../HamburgerMenuUserControl.xaml | 6 +- .../HamburgerMenuUserControl.xaml.cs | 10 +- .../SymbolPickerUserControl.xaml.cs | 4 +- ModernKeePass/Win81App.csproj | 31 ++++- ModernKeePass/packages.config | 7 +- 54 files changed, 319 insertions(+), 283 deletions(-) create mode 100644 ModernKeePass.Application/Common/Interfaces/IEntityVm.cs create mode 100644 ModernKeePass.Domain/Enums/DatabaseVersion.cs rename ModernKeePass/Converters/{IntToSymbolConverter.cs => IconToSymbolConverter.cs} (97%) create mode 100644 ModernKeePass/DependencyInjection.cs diff --git a/ModernKeePass.Application/Application.csproj b/ModernKeePass.Application/Application.csproj index 32e72df..e32898d 100644 --- a/ModernKeePass.Application/Application.csproj +++ b/ModernKeePass.Application/Application.csproj @@ -42,6 +42,7 @@ + diff --git a/ModernKeePass.Application/Common/Interfaces/IDatabaseProxy.cs b/ModernKeePass.Application/Common/Interfaces/IDatabaseProxy.cs index 4beb4a7..99e85e7 100644 --- a/ModernKeePass.Application/Common/Interfaces/IDatabaseProxy.cs +++ b/ModernKeePass.Application/Common/Interfaces/IDatabaseProxy.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using ModernKeePass.Domain.Dtos; using ModernKeePass.Domain.Entities; +using ModernKeePass.Domain.Enums; namespace ModernKeePass.Application.Common.Interfaces { @@ -18,7 +19,7 @@ namespace ModernKeePass.Application.Common.Interfaces Task Open(FileInfo fileInfo, Credentials credentials); Task ReOpen(); - Task Create(FileInfo fileInfo, Credentials credentials); + Task Create(FileInfo fileInfo, Credentials credentials, DatabaseVersion version = DatabaseVersion.V2); Task SaveDatabase(); Task SaveDatabase(string filePath); void SetRecycleBin(string id); diff --git a/ModernKeePass.Application/Common/Interfaces/IEntityVm.cs b/ModernKeePass.Application/Common/Interfaces/IEntityVm.cs new file mode 100644 index 0000000..f221062 --- /dev/null +++ b/ModernKeePass.Application/Common/Interfaces/IEntityVm.cs @@ -0,0 +1,11 @@ +using ModernKeePass.Domain.Enums; + +namespace ModernKeePass.Application.Common.Interfaces +{ + public interface IEntityVm + { + string Id { get; set; } + string Title { get; set; } + Icon Icon { get; set; } + } +} \ No newline at end of file diff --git a/ModernKeePass.Application/Database/Commands/CloseDatabase/CloseDatabaseCommand.cs b/ModernKeePass.Application/Database/Commands/CloseDatabase/CloseDatabaseCommand.cs index 4c7a053..55db080 100644 --- a/ModernKeePass.Application/Database/Commands/CloseDatabase/CloseDatabaseCommand.cs +++ b/ModernKeePass.Application/Database/Commands/CloseDatabase/CloseDatabaseCommand.cs @@ -1,5 +1,4 @@ using MediatR; -using System.Threading.Tasks; using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Domain.Exceptions; @@ -7,7 +6,7 @@ namespace ModernKeePass.Application.Database.Commands.CloseDatabase { public class CloseDatabaseCommand: IRequest { - public class CloseDatabaseCommandHandler : IAsyncRequestHandler + public class CloseDatabaseCommandHandler : IRequestHandler { private readonly IDatabaseProxy _database; @@ -15,7 +14,7 @@ namespace ModernKeePass.Application.Database.Commands.CloseDatabase { _database = database; } - public async Task Handle(CloseDatabaseCommand message) + public void Handle(CloseDatabaseCommand message) { if (_database.IsOpen) _database.CloseDatabase(); else throw new DatabaseClosedException(); diff --git a/ModernKeePass.Application/Database/Queries/GetDatabase/GetDatabaseQuery.cs b/ModernKeePass.Application/Database/Queries/GetDatabase/GetDatabaseQuery.cs index 51d0beb..5a1cefb 100644 --- a/ModernKeePass.Application/Database/Queries/GetDatabase/GetDatabaseQuery.cs +++ b/ModernKeePass.Application/Database/Queries/GetDatabase/GetDatabaseQuery.cs @@ -23,15 +23,18 @@ namespace ModernKeePass.Application.Database.Queries.GetDatabase { var database = new DatabaseVm { - IsOpen = _databaseProxy.IsOpen, - Name = _databaseProxy.Name, - RootGroup = _mapper.Map(_databaseProxy.RootGroup), - IsRecycleBinEnabled = _databaseProxy.IsRecycleBinEnabled, - RecycleBin = _mapper.Map(_databaseProxy.RecycleBin), - Compression = _databaseProxy.Compression, - CipherId = _databaseProxy.CipherId, - KeyDerivationId = _databaseProxy.CipherId + IsOpen = _databaseProxy.IsOpen }; + if (database.IsOpen) + { + database.Name = _databaseProxy.Name; + database.RootGroup = _mapper.Map(_databaseProxy.RootGroup); + database.IsRecycleBinEnabled = _databaseProxy.IsRecycleBinEnabled; + database.RecycleBin = _mapper.Map(_databaseProxy.RecycleBin); + database.Compression = _databaseProxy.Compression; + database.CipherId = _databaseProxy.CipherId; + database.KeyDerivationId = _databaseProxy.CipherId; + } return database; } } diff --git a/ModernKeePass.Application/DependencyInjection.cs b/ModernKeePass.Application/DependencyInjection.cs index 4e0ebd1..c29f8ed 100644 --- a/ModernKeePass.Application/DependencyInjection.cs +++ b/ModernKeePass.Application/DependencyInjection.cs @@ -1,5 +1,4 @@ using System.Reflection; -using AutoMapper; using FluentValidation; using MediatR; using Microsoft.Extensions.DependencyInjection; @@ -11,7 +10,6 @@ namespace ModernKeePass.Application public static IServiceCollection AddApplication(this IServiceCollection services) { var assembly = typeof(DependencyInjection).GetTypeInfo().Assembly; - services.AddAutoMapper(assembly); services.AddMediatR(assembly); //services.AddValidatorsFromAssembly(assembly); diff --git a/ModernKeePass.Application/Entry/Models/EntryVm.cs b/ModernKeePass.Application/Entry/Models/EntryVm.cs index f218bd0..e07deb5 100644 --- a/ModernKeePass.Application/Entry/Models/EntryVm.cs +++ b/ModernKeePass.Application/Entry/Models/EntryVm.cs @@ -2,14 +2,17 @@ using System.Collections.Generic; using System.Drawing; using AutoMapper; +using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Application.Common.Mappings; +using ModernKeePass.Application.Group.Models; using ModernKeePass.Domain.Entities; using ModernKeePass.Domain.Enums; namespace ModernKeePass.Application.Entry.Models { - public class EntryVm: IMapFrom + public class EntryVm: IEntityVm, IMapFrom { + public GroupVm ParentGroup { get; set; } public string Id { get; set; } public string Title { get; set; } public string Username { get; set; } @@ -28,6 +31,7 @@ namespace ModernKeePass.Application.Entry.Models public void Mapping(Profile profile) { profile.CreateMap() + .ForMember(d => d.ParentGroup, opts => opts.MapFrom(s => s.Parent)) .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)) diff --git a/ModernKeePass.Application/Group/Models/GroupVm.cs b/ModernKeePass.Application/Group/Models/GroupVm.cs index 3dac9b2..e99ff1f 100644 --- a/ModernKeePass.Application/Group/Models/GroupVm.cs +++ b/ModernKeePass.Application/Group/Models/GroupVm.cs @@ -1,31 +1,40 @@ using System.Collections.Generic; using System.Linq; using AutoMapper; +using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Application.Common.Mappings; using ModernKeePass.Application.Entry.Models; using ModernKeePass.Domain.Entities; using ModernKeePass.Domain.Enums; +using ModernKeePass.Domain.Interfaces; namespace ModernKeePass.Application.Group.Models { - public class GroupVm: IMapFrom + public class GroupVm: IEntityVm, ISelectableModel, IMapFrom { + public GroupVm ParentGroup { get; set; } public string Id { get; set; } public string Title { get; set; } public Icon Icon { get; set; } - public GroupVm ParentGroup { get; set; } - public List SubGroups { get; set; } = new List(); - public List Entries { get; set; } = new List(); + public List SubGroups { get; set; } + public List Entries { get; set; } + public bool IsSelected { get; set; } + + public override string ToString() + { + return Title; + } public void Mapping(Profile profile) { profile.CreateMap() + .ForMember(d => d.ParentGroup, opts => opts.MapFrom(s => s.Parent)) .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.ParentGroup, opts => opts.MapFrom(s => s.Parent)) .ForMember(d => d.Entries, opts => opts.MapFrom(s => s.Entries.OrderBy(e => e.Name))) .ForMember(d => d.SubGroups, opts => opts.MapFrom(s => s.SubGroups)); } + } } \ No newline at end of file diff --git a/ModernKeePass.Application/Parameters/Models/CipherVm.cs b/ModernKeePass.Application/Parameters/Models/CipherVm.cs index 2a58e62..4a6076a 100644 --- a/ModernKeePass.Application/Parameters/Models/CipherVm.cs +++ b/ModernKeePass.Application/Parameters/Models/CipherVm.cs @@ -1,18 +1,8 @@ -using AutoMapper; -using ModernKeePass.Application.Common.Mappings; -using ModernKeePass.Domain.Entities; - -namespace ModernKeePass.Application.Cryptography.Models +namespace ModernKeePass.Application.Parameters.Models { - public class CipherVm: IMapFrom + public class CipherVm { public string Id { get; set; } public string Name { get; set; } - public void Mapping(Profile profile) - { - profile.CreateMap() - .ForMember(d => d.Id, opts => opts.MapFrom(s => s.Id)) - .ForMember(d => d.Name, opts => opts.MapFrom(s => s.Name)); - } } } \ No newline at end of file diff --git a/ModernKeePass.Application/Parameters/Models/KeyDerivationVm.cs b/ModernKeePass.Application/Parameters/Models/KeyDerivationVm.cs index e1662f6..7bd275a 100644 --- a/ModernKeePass.Application/Parameters/Models/KeyDerivationVm.cs +++ b/ModernKeePass.Application/Parameters/Models/KeyDerivationVm.cs @@ -1,19 +1,9 @@ -using AutoMapper; -using ModernKeePass.Application.Common.Mappings; -using ModernKeePass.Domain.Entities; - -namespace ModernKeePass.Application.Cryptography.Models +namespace ModernKeePass.Application.Parameters.Models { - public class KeyDerivationVm : IMapFrom + public class KeyDerivationVm { public string Id { get; set; } public string Name { get; set; } - public void Mapping(Profile profile) - { - profile.CreateMap() - .ForMember(d => d.Id, opts => opts.MapFrom(s => s.Id)) - .ForMember(d => d.Name, opts => opts.MapFrom(s => s.Name)); - } } } \ No newline at end of file diff --git a/ModernKeePass.Application/Parameters/Queries/GetCiphers/GetCiphersQuery.cs b/ModernKeePass.Application/Parameters/Queries/GetCiphers/GetCiphersQuery.cs index 2f9866b..e75ba33 100644 --- a/ModernKeePass.Application/Parameters/Queries/GetCiphers/GetCiphersQuery.cs +++ b/ModernKeePass.Application/Parameters/Queries/GetCiphers/GetCiphersQuery.cs @@ -1,27 +1,29 @@ using System.Collections.Generic; -using AutoMapper; +using System.Linq; using MediatR; using ModernKeePass.Application.Common.Interfaces; -using ModernKeePass.Application.Cryptography.Models; +using ModernKeePass.Application.Parameters.Models; -namespace ModernKeePass.Application.Cryptography.Queries.GetCiphers +namespace ModernKeePass.Application.Parameters.Queries.GetCiphers { public class GetCiphersQuery: IRequest> { public class GetCiphersQueryHandler: IRequestHandler> { private readonly ICryptographyClient _cryptography; - private readonly IMapper _mapper; - public GetCiphersQueryHandler(ICryptographyClient cryptography, IMapper mapper) + public GetCiphersQueryHandler(ICryptographyClient cryptography) { _cryptography = cryptography; - _mapper = mapper; } public IEnumerable Handle(GetCiphersQuery message) { - yield return _mapper.Map(_cryptography.Ciphers); + return _cryptography.Ciphers.Select(c => new CipherVm + { + Id = c.Id, + Name = c.Name + }); } } } diff --git a/ModernKeePass.Application/Parameters/Queries/GetCompressions/GetCompressionsQuery.cs b/ModernKeePass.Application/Parameters/Queries/GetCompressions/GetCompressionsQuery.cs index 14621ef..66c4271 100644 --- a/ModernKeePass.Application/Parameters/Queries/GetCompressions/GetCompressionsQuery.cs +++ b/ModernKeePass.Application/Parameters/Queries/GetCompressions/GetCompressionsQuery.cs @@ -3,7 +3,7 @@ using System.Linq; using MediatR; using ModernKeePass.Application.Common.Interfaces; -namespace ModernKeePass.Application.Cryptography.Queries.GetCompressions +namespace ModernKeePass.Application.Parameters.Queries.GetCompressions { public class GetCompressionsQuery : IRequest> { diff --git a/ModernKeePass.Application/Parameters/Queries/GetKeyDerivations/GetKeyDerivationsQuery.cs b/ModernKeePass.Application/Parameters/Queries/GetKeyDerivations/GetKeyDerivationsQuery.cs index 9c72210..9cb07b0 100644 --- a/ModernKeePass.Application/Parameters/Queries/GetKeyDerivations/GetKeyDerivationsQuery.cs +++ b/ModernKeePass.Application/Parameters/Queries/GetKeyDerivations/GetKeyDerivationsQuery.cs @@ -1,27 +1,29 @@ using System.Collections.Generic; -using AutoMapper; +using System.Linq; using MediatR; using ModernKeePass.Application.Common.Interfaces; -using ModernKeePass.Application.Cryptography.Models; +using ModernKeePass.Application.Parameters.Models; -namespace ModernKeePass.Application.Cryptography.Queries.GetKeyDerivations +namespace ModernKeePass.Application.Parameters.Queries.GetKeyDerivations { public class GetKeyDerivationsQuery : IRequest> { public class GetKeyDerivationsQueryHandler : IRequestHandler> { private readonly ICryptographyClient _cryptography; - private readonly IMapper _mapper; - public GetKeyDerivationsQueryHandler(ICryptographyClient cryptography, IMapper mapper) + public GetKeyDerivationsQueryHandler(ICryptographyClient cryptography) { _cryptography = cryptography; - _mapper = mapper; } public IEnumerable Handle(GetKeyDerivationsQuery message) { - yield return _mapper.Map(_cryptography.KeyDerivations); + return _cryptography.KeyDerivations.Select(c => new KeyDerivationVm + { + Id = c.Id, + Name = c.Name + }); } } } diff --git a/ModernKeePass.Application/project.json b/ModernKeePass.Application/project.json index 3af741f..9661f58 100644 --- a/ModernKeePass.Application/project.json +++ b/ModernKeePass.Application/project.json @@ -3,7 +3,7 @@ "dependencies": { "Autofac": "4.9.4", "Autofac.Extensions.DependencyInjection": "4.4.0", - "AutoMapper": "6.1.1", + "AutoMapper": "6.0.2", "AutoMapper.Extensions.Microsoft.DependencyInjection": "2.0.1", "FluentValidation": "8.6.2", "MediatR": "3.0.1", diff --git a/ModernKeePass.Domain/Domain.csproj b/ModernKeePass.Domain/Domain.csproj index 45dc551..5c2c667 100644 --- a/ModernKeePass.Domain/Domain.csproj +++ b/ModernKeePass.Domain/Domain.csproj @@ -47,6 +47,7 @@ + diff --git a/ModernKeePass.Domain/Enums/DatabaseVersion.cs b/ModernKeePass.Domain/Enums/DatabaseVersion.cs new file mode 100644 index 0000000..591d3f3 --- /dev/null +++ b/ModernKeePass.Domain/Enums/DatabaseVersion.cs @@ -0,0 +1,8 @@ +namespace ModernKeePass.Domain.Enums +{ + public enum DatabaseVersion + { + V2, + V4 + } +} \ No newline at end of file diff --git a/ModernKeePass.Infrastructure/DependencyInjection.cs b/ModernKeePass.Infrastructure/DependencyInjection.cs index 50d63a7..c1b50ce 100644 --- a/ModernKeePass.Infrastructure/DependencyInjection.cs +++ b/ModernKeePass.Infrastructure/DependencyInjection.cs @@ -1,6 +1,4 @@ -using System.Reflection; -using AutoMapper; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Domain.Interfaces; using ModernKeePass.Infrastructure.Common; @@ -13,9 +11,6 @@ namespace ModernKeePass.Infrastructure { public static IServiceCollection AddInfrastructure(this IServiceCollection services) { - var assembly = typeof(DependencyInjection).GetTypeInfo().Assembly; - services.AddAutoMapper(assembly); - services.AddSingleton(typeof(IDatabaseProxy), typeof(KeePassDatabaseClient)); services.AddTransient(typeof(ICryptographyClient), typeof(KeePassCryptographyClient)); services.AddTransient(typeof(IPasswordProxy), typeof(KeePassPasswordClient)); diff --git a/ModernKeePass.Infrastructure/KeePass/EntryMappingProfile.cs b/ModernKeePass.Infrastructure/KeePass/EntryMappingProfile.cs index 916b250..123e2bc 100644 --- a/ModernKeePass.Infrastructure/KeePass/EntryMappingProfile.cs +++ b/ModernKeePass.Infrastructure/KeePass/EntryMappingProfile.cs @@ -19,6 +19,7 @@ namespace ModernKeePass.Infrastructure.KeePass { Uri url; CreateMap() + //.ForMember(dest => dest.Parent, opt => opt.MapFrom(src => src.ParentGroup)) .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Uuid.ToHexString())) .ForMember(dest => dest.Name, opt => opt.MapFrom(src => GetEntryValue(src, PwDefs.TitleField))) .ForMember(dest => dest.UserName, opt => opt.MapFrom(src => GetEntryValue(src, PwDefs.UserNameField))) @@ -37,6 +38,7 @@ namespace ModernKeePass.Infrastructure.KeePass .ForMember(dest => dest.AdditionalFields, opt => opt.MapFrom(src => src.Strings.Where(s => !PwDefs.GetStandardFields().Contains(s.Key)).ToDictionary(s => s.Key, s => GetEntryValue(src, s.Key)))) .ForMember(dest => dest.LastModificationDate, opt => opt.MapFrom(src => new DateTimeOffset(src.LastModificationTime))); + //.MaxDepth(1); } private void FromModelToDto() diff --git a/ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs b/ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs index afeeb27..04d6efa 100644 --- a/ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs +++ b/ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs @@ -15,17 +15,18 @@ namespace ModernKeePass.Infrastructure.KeePass private void FromDtoToModel() { CreateMap() + //.ForMember(d => d.Parent, opts => opts.MapFrom(s => s.ParentGroup)) .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)); + //.MaxDepth(1); } private void FromModelToDto() { - throw new System.NotImplementedException(); } } } \ No newline at end of file diff --git a/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs b/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs index 6f73ada..1ad1935 100644 --- a/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs +++ b/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs @@ -20,7 +20,6 @@ namespace ModernKeePass.Infrastructure.KeePass { public class KeePassDatabaseClient: IDatabaseProxy { - private readonly ISettingsProxy _settings; private readonly IFileProxy _fileService; private readonly IMapper _mapper; private readonly IDateTime _dateTime; @@ -77,9 +76,8 @@ namespace ModernKeePass.Infrastructure.KeePass set { _pwDatabase.Compression = (PwCompressionAlgorithm) Enum.Parse(typeof(PwCompressionAlgorithm), value); } } - public KeePassDatabaseClient(ISettingsProxy settings, IFileProxy fileService, IMapper mapper, IDateTime dateTime) + public KeePassDatabaseClient(IFileProxy fileService, IMapper mapper, IDateTime dateTime) { - _settings = settings; _fileService = fileService; _mapper = mapper; _dateTime = dateTime; @@ -111,17 +109,16 @@ namespace ModernKeePass.Infrastructure.KeePass return await Open(new FileInfo {Path = _fileAccessToken}, _credentials); } - public async Task Create(FileInfo fileInfo, Credentials credentials) + public async Task Create(FileInfo fileInfo, Credentials credentials, DatabaseVersion version = DatabaseVersion.V2) { var compositeKey = await CreateCompositeKey(credentials); var ioConnection = await BuildConnectionInfo(fileInfo); _pwDatabase.New(ioConnection, compositeKey); - var fileFormat = _settings.GetSetting("DefaultFileFormat"); - switch (fileFormat) + switch (version) { - case "4": + case DatabaseVersion.V4: _pwDatabase.KdfParameters = KdfPool.Get("Argon2").GetDefaultParameters(); break; } diff --git a/ModernKeePass.Infrastructure/project.json b/ModernKeePass.Infrastructure/project.json index 5b920fc..28259f4 100644 --- a/ModernKeePass.Infrastructure/project.json +++ b/ModernKeePass.Infrastructure/project.json @@ -1,10 +1,10 @@ { "supports": {}, "dependencies": { - "AutoMapper": "6.1.1", + "AutoMapper": "6.0.2", "AutoMapper.Extensions.Microsoft.DependencyInjection": "2.0.1", "Microsoft.NETCore.Portable.Compatibility": "1.0.1", - "ModernKeePassLib": "2.44.1", + "ModernKeePassLib": "2.44.2", "NETStandard.Library": "2.0.3" }, "frameworks": { diff --git a/ModernKeePass.KeePassDatabaseTests/ModernKeePass.KeePassDatabaseTests.csproj b/ModernKeePass.KeePassDatabaseTests/ModernKeePass.KeePassDatabaseTests.csproj index ef43adc..4657383 100644 --- a/ModernKeePass.KeePassDatabaseTests/ModernKeePass.KeePassDatabaseTests.csproj +++ b/ModernKeePass.KeePassDatabaseTests/ModernKeePass.KeePassDatabaseTests.csproj @@ -17,8 +17,8 @@ - - + + diff --git a/ModernKeePass/Actions/DeleteEntityAction.cs b/ModernKeePass/Actions/DeleteEntityAction.cs index 27432fc..1a1bafb 100644 --- a/ModernKeePass/Actions/DeleteEntityAction.cs +++ b/ModernKeePass/Actions/DeleteEntityAction.cs @@ -1,6 +1,8 @@ using System.Windows.Input; using Windows.UI.Xaml; +using MediatR; using Microsoft.Xaml.Interactivity; +using ModernKeePass.Application.Database.Queries.GetDatabase; using ModernKeePass.Common; using ModernKeePass.Interfaces; using ModernKeePass.Services; @@ -10,6 +12,8 @@ namespace ModernKeePass.Actions { public class DeleteEntityAction : DependencyObject, IAction { + private readonly IMediator _mediator; + public IVmEntity Entity { get { return (IVmEntity)GetValue(EntityProperty); } @@ -30,15 +34,23 @@ namespace ModernKeePass.Actions DependencyProperty.Register("Command", typeof(ICommand), typeof(DeleteEntityAction), new PropertyMetadata(null)); + public DeleteEntityAction() : this(App.Mediator) { } + + public DeleteEntityAction(IMediator mediator) + { + _mediator = mediator; + } + public object Execute(object sender, object parameter) { var resource = new ResourcesService(); var type = Entity is GroupVm ? "Group" : "Entry"; + var isRecycleOnDelete = _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult().IsRecycleBinEnabled; - var message = Entity.IsRecycleOnDelete + var message = isRecycleOnDelete ? resource.GetResourceValue($"{type}RecyclingConfirmation") : resource.GetResourceValue($"{type}DeletingConfirmation"); - var text = Entity.IsRecycleOnDelete ? resource.GetResourceValue($"{type}Recycled") : resource.GetResourceValue($"{type}Deleted"); + var text = isRecycleOnDelete ? resource.GetResourceValue($"{type}Recycled") : resource.GetResourceValue($"{type}Deleted"); MessageDialogHelper.ShowActionDialog(resource.GetResourceValue("EntityDeleteTitle"), message, resource.GetResourceValue("EntityDeleteActionButton"), resource.GetResourceValue("EntityDeleteCancelButton"), a => diff --git a/ModernKeePass/App.xaml.cs b/ModernKeePass/App.xaml.cs index b3940bb..e3b95bb 100644 --- a/ModernKeePass/App.xaml.cs +++ b/ModernKeePass/App.xaml.cs @@ -19,7 +19,6 @@ using ModernKeePass.Application.Database.Commands.SaveDatabase; using ModernKeePass.Application.Database.Queries.GetDatabase; using ModernKeePass.Application.Database.Queries.ReOpenDatabase; using ModernKeePass.Common; -using ModernKeePass.Domain.Dtos; using ModernKeePass.Domain.Exceptions; using ModernKeePass.Infrastructure; using ModernKeePass.Services; @@ -58,6 +57,7 @@ namespace ModernKeePass IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddApplication(); serviceCollection.AddInfrastructure(); + serviceCollection.AddAppAutomapper(); Services = serviceCollection.BuildServiceProvider(); Mediator = Services.GetService(); } @@ -97,12 +97,7 @@ namespace ModernKeePass if (file != null) { var token = StorageApplicationPermissions.FutureAccessList.Add(file); - var fileInfo = new FileInfo - { - Name = file.DisplayName, - Path = token - }; - await Mediator.Send(new SaveDatabaseCommand { FileInfo = fileInfo }); + await Mediator.Send(new SaveDatabaseCommand { FilePath = token }); } }, null); } diff --git a/ModernKeePass/Controls/ListViewWithDisable.cs b/ModernKeePass/Controls/ListViewWithDisable.cs index 74b2a66..ff6c284 100644 --- a/ModernKeePass/Controls/ListViewWithDisable.cs +++ b/ModernKeePass/Controls/ListViewWithDisable.cs @@ -1,6 +1,6 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using ModernKeePass.Interfaces; +using ModernKeePass.Domain.Interfaces; namespace ModernKeePass.Controls { diff --git a/ModernKeePass/Converters/IntToSymbolConverter.cs b/ModernKeePass/Converters/IconToSymbolConverter.cs similarity index 97% rename from ModernKeePass/Converters/IntToSymbolConverter.cs rename to ModernKeePass/Converters/IconToSymbolConverter.cs index 8736e66..d2b3885 100644 --- a/ModernKeePass/Converters/IntToSymbolConverter.cs +++ b/ModernKeePass/Converters/IconToSymbolConverter.cs @@ -5,11 +5,11 @@ using ModernKeePass.Domain.Enums; namespace ModernKeePass.Converters { - public class IntToSymbolConverter : IValueConverter + public class IconToSymbolConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { - var icon = (Icon) value; + var icon = (Icon)value; switch (icon) { case Icon.Delete: return Symbol.Delete; @@ -67,7 +67,7 @@ namespace ModernKeePass.Converters public object ConvertBack(object value, Type targetType, object parameter, string language) { - var symbol = (Symbol) value; + var symbol = (Symbol)value; var defaultIcon = parameter != null ? int.Parse(parameter as string) : -1; switch (symbol) { diff --git a/ModernKeePass/DependencyInjection.cs b/ModernKeePass/DependencyInjection.cs new file mode 100644 index 0000000..9c18c87 --- /dev/null +++ b/ModernKeePass/DependencyInjection.cs @@ -0,0 +1,18 @@ +using System.Reflection; +using AutoMapper; +using Microsoft.Extensions.DependencyInjection; + +namespace ModernKeePass +{ + public static class DependencyInjection + { + public static IServiceCollection AddAppAutomapper(this IServiceCollection services) + { + var applicationAssembly = typeof(Application.DependencyInjection).GetTypeInfo().Assembly; + var infrastructureAssembly = typeof(Infrastructure.DependencyInjection).GetTypeInfo().Assembly; + services.AddAutoMapper(applicationAssembly, infrastructureAssembly); + + return services; + } + } +} \ No newline at end of file diff --git a/ModernKeePass/Events/PasswordEventArgs.cs b/ModernKeePass/Events/PasswordEventArgs.cs index 099a5f9..4311503 100644 --- a/ModernKeePass/Events/PasswordEventArgs.cs +++ b/ModernKeePass/Events/PasswordEventArgs.cs @@ -1,5 +1,5 @@ using System; -using ModernKeePass.ViewModels; +using ModernKeePass.Application.Group.Models; namespace ModernKeePass.Events { diff --git a/ModernKeePass/Interfaces/IVmEntity.cs b/ModernKeePass/Interfaces/IVmEntity.cs index 2354ca0..9860636 100644 --- a/ModernKeePass/Interfaces/IVmEntity.cs +++ b/ModernKeePass/Interfaces/IVmEntity.cs @@ -1,20 +1,18 @@ using System.Collections.Generic; using System.Threading.Tasks; using System.Windows.Input; -using ModernKeePass.ViewModels; +using Windows.UI.Xaml.Controls; +using ModernKeePass.Application.Group.Models; namespace ModernKeePass.Interfaces { public interface IVmEntity { - GroupVm ParentGroup { get; } - GroupVm PreviousGroup { get; } - int Icon { get; } + Symbol Icon { get; } string Id { get; } string Title { get; set; } - IEnumerable BreadCrumb { get; } + IEnumerable BreadCrumb { get; } bool IsEditMode { get; } - bool IsRecycleOnDelete { get; } /// /// Save changes to Model diff --git a/ModernKeePass/Package.appxmanifest b/ModernKeePass/Package.appxmanifest index 9866b70..9841e13 100644 --- a/ModernKeePass/Package.appxmanifest +++ b/ModernKeePass/Package.appxmanifest @@ -1,6 +1,6 @@  - + ModernKeePass wismna diff --git a/ModernKeePass/TemplateSelectors/SelectableDataTemplateSelector.cs b/ModernKeePass/TemplateSelectors/SelectableDataTemplateSelector.cs index 60c90ff..cd3bd8b 100644 --- a/ModernKeePass/TemplateSelectors/SelectableDataTemplateSelector.cs +++ b/ModernKeePass/TemplateSelectors/SelectableDataTemplateSelector.cs @@ -1,6 +1,6 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using ModernKeePass.Interfaces; +using ModernKeePass.Domain.Interfaces; namespace ModernKeePass.TemplateSelectors { diff --git a/ModernKeePass/ViewModels/EntryVm.cs b/ModernKeePass/ViewModels/EntryVm.cs index 90cfc12..cd0f944 100644 --- a/ModernKeePass/ViewModels/EntryVm.cs +++ b/ModernKeePass/ViewModels/EntryVm.cs @@ -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 BreadCrumb => new List(ParentGroup.BreadCrumb) {ParentGroup}; + + public IEnumerable BreadCrumb + { + get + { + var groups = new Stack(); + var group = _entry.ParentGroup; + while (group.ParentGroup != null) + { + group = group.ParentGroup; + groups.Push(group); + } + + return groups; + } + } + /// /// Determines if the Entry is current or from history /// @@ -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 History - { - get - { - var history = new Stack(); - foreach (var historyEntry in _entry.History) - { - history.Push(new EntryVm(historyEntry, ParentGroup) {IsSelected = false}); - } - history.Push(this); - - return history; - } - } + public IEnumerable 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() diff --git a/ModernKeePass/ViewModels/GroupVm.cs b/ModernKeePass/ViewModels/GroupVm.cs index d594a6f..78cb14a 100644 --- a/ModernKeePass/ViewModels/GroupVm.cs +++ b/ModernKeePass/ViewModels/GroupVm.cs @@ -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 Entries => new ObservableCollection(_group.Entries); - public ObservableCollection Entries - { - get { return _entries; } - private set { SetProperty(ref _entries, value); } - } + public ObservableCollection Groups => new ObservableCollection(_group.SubGroups); - public IEnumerable SubEntries + public IEnumerable SubEntries { get { - var subEntries = new List(); + var subEntries = new List(); subEntries.AddRange(Entries); foreach (var group in Groups) { - subEntries.AddRange(group.SubEntries); + subEntries.AddRange(group.Entries); } return subEntries; } } - public ObservableCollection Groups { get; set; } = new ObservableCollection(); - 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; /// /// 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> EntriesZoomedOut => from e in Entries + public IOrderedEnumerable> 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 BreadCrumb + public IEnumerable BreadCrumb { get { - var groups = new Stack(); - var group = this; + var groups = new Stack(); + 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 _entries = new ObservableCollection(); 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(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(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 AddNewGroup(string name = "") + public async Task 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 AddNewEntry() + public async Task 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(Entries.OrderBy(e => e.Title)); + OnPropertyChanged(nameof(Entries)); } private async Task SortGroupsAsync() { await _mediator.Send(new SortGroupsCommand {Group = _group}); - Groups = new ObservableCollection(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(); - } } } diff --git a/ModernKeePass/ViewModels/Items/ListMenuItemVm.cs b/ModernKeePass/ViewModels/Items/ListMenuItemVm.cs index b707ce1..4385de4 100644 --- a/ModernKeePass/ViewModels/Items/ListMenuItemVm.cs +++ b/ModernKeePass/ViewModels/Items/ListMenuItemVm.cs @@ -1,6 +1,7 @@ using System; using Windows.UI.Xaml.Controls; using ModernKeePass.Common; +using ModernKeePass.Domain.Interfaces; using ModernKeePass.Interfaces; namespace ModernKeePass.ViewModels diff --git a/ModernKeePass/ViewModels/Items/RecentItemVm.cs b/ModernKeePass/ViewModels/Items/RecentItemVm.cs index 5d978c4..c301537 100644 --- a/ModernKeePass/ViewModels/Items/RecentItemVm.cs +++ b/ModernKeePass/ViewModels/Items/RecentItemVm.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using Windows.Storage; using ModernKeePass.Common; +using ModernKeePass.Domain.Interfaces; using ModernKeePass.Interfaces; using ModernKeePass.Services; diff --git a/ModernKeePass/ViewModels/Items/SettingsDatabaseVm.cs b/ModernKeePass/ViewModels/Items/SettingsDatabaseVm.cs index 0db9fef..ba50b05 100644 --- a/ModernKeePass/ViewModels/Items/SettingsDatabaseVm.cs +++ b/ModernKeePass/ViewModels/Items/SettingsDatabaseVm.cs @@ -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 Groups { get; set; } + public ObservableCollection Groups { get; set; } public IEnumerable Ciphers => _mediator.Send(new GetCiphersQuery()).GetAwaiter().GetResult(); public IEnumerable 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(_database.RootGroup.SubGroups); } } } diff --git a/ModernKeePass/ViewModels/MainVm.cs b/ModernKeePass/ViewModels/MainVm.cs index 8389e97..959d6e5 100644 --- a/ModernKeePass/ViewModels/MainVm.cs +++ b/ModernKeePass/ViewModels/MainVm.cs @@ -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; diff --git a/ModernKeePass/ViewModels/NewVm.cs b/ModernKeePass/ViewModels/NewVm.cs index e347dd2..1543bfa 100644 --- a/ModernKeePass/ViewModels/NewVm.cs +++ b/ModernKeePass/ViewModels/NewVm.cs @@ -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 importService) + public async Task PopulateInitialData() { var database = await _mediator.Send(new GetDatabaseQuery()); - if (settings.GetSetting("Sample") && !IsImportChecked) await CreateSampleData(database.RootGroup); - else if (IsImportChecked && ImportFile != null && ! (ImportFormat is NullImportFormat)) importService.Import(ImportFormat, ImportFile, database.RootGroup); + if (_settings.GetSetting("Sample") && !IsImportChecked) await CreateSampleData(database.RootGroup); + return database.RootGroup; } private async Task CreateSampleData(Application.Group.Models.GroupVm group) diff --git a/ModernKeePass/ViewModels/OpenVm.cs b/ModernKeePass/ViewModels/OpenVm.cs index f6a91d4..b8161c4 100644 --- a/ModernKeePass/ViewModels/OpenVm.cs +++ b/ModernKeePass/ViewModels/OpenVm.cs @@ -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); } } } diff --git a/ModernKeePass/ViewModels/RecentVm.cs b/ModernKeePass/ViewModels/RecentVm.cs index 5dd3bf1..179c2a7 100644 --- a/ModernKeePass/ViewModels/RecentVm.cs +++ b/ModernKeePass/ViewModels/RecentVm.cs @@ -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; diff --git a/ModernKeePass/ViewModels/SettingsVm.cs b/ModernKeePass/ViewModels/SettingsVm.cs index bc0f99c..a62f87c 100644 --- a/ModernKeePass/ViewModels/SettingsVm.cs +++ b/ModernKeePass/ViewModels/SettingsVm.cs @@ -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; diff --git a/ModernKeePass/Views/BasePages/LayoutAwarePageBase.cs b/ModernKeePass/Views/BasePages/LayoutAwarePageBase.cs index a225a6c..5c10ba3 100644 --- a/ModernKeePass/Views/BasePages/LayoutAwarePageBase.cs +++ b/ModernKeePass/Views/BasePages/LayoutAwarePageBase.cs @@ -4,6 +4,7 @@ using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Navigation; using ModernKeePass.Common; +using ModernKeePass.Domain.Interfaces; using ModernKeePass.Interfaces; namespace ModernKeePass.Views.BasePages diff --git a/ModernKeePass/Views/EntryDetailPage.xaml b/ModernKeePass/Views/EntryDetailPage.xaml index cbbed2a..531b456 100644 --- a/ModernKeePass/Views/EntryDetailPage.xaml +++ b/ModernKeePass/Views/EntryDetailPage.xaml @@ -18,9 +18,8 @@ - + -