From 77e79a603e16194a8617c10aee06cf53091ef776 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sun, 12 May 2024 18:28:13 +0100 Subject: [PATCH] Enabled ability to order containers and the ability to insert new containers to their correct order --- Bitvault.Avalonia/App.axaml.cs | 4 +-- Bitvault/ContainerActivatedHandler.cs | 13 ++++++-- Bitvault/ContainerComponentFactory.cs | 18 ----------- Bitvault/ContainerFactory.cs | 37 +++++------------------ Bitvault/ContainerStorageFactory.cs | 41 ++++++++++++++++++++++++++ Bitvault/CreateContainerHandler.cs | 4 +-- Bitvault/IContainerComponentFactory.cs | 9 ------ Bitvault/IContainerFactory.cs | 9 ++++-- Bitvault/IContainerStorageFactory.cs | 6 ++++ Bitvault/MainViewModelHandler.cs | 3 +- Bitvault/OpenContainerHandler.cs | 2 +- 11 files changed, 77 insertions(+), 69 deletions(-) delete mode 100644 Bitvault/ContainerComponentFactory.cs create mode 100644 Bitvault/ContainerStorageFactory.cs delete mode 100644 Bitvault/IContainerComponentFactory.cs create mode 100644 Bitvault/IContainerStorageFactory.cs diff --git a/Bitvault.Avalonia/App.axaml.cs b/Bitvault.Avalonia/App.axaml.cs index 7937b05..31ef6cd 100644 --- a/Bitvault.Avalonia/App.axaml.cs +++ b/Bitvault.Avalonia/App.axaml.cs @@ -49,7 +49,7 @@ public partial class App : Application services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.TryAddSingleton, ValueStore>(); services.TryAddSingleton, ValueStore>(); @@ -93,7 +93,7 @@ public partial class App : Application }); })!); - services.AddTransient(); + services.AddTransient(); services.AddHandler(); services.AddSingleton(); diff --git a/Bitvault/ContainerActivatedHandler.cs b/Bitvault/ContainerActivatedHandler.cs index aafb093..f1082f6 100644 --- a/Bitvault/ContainerActivatedHandler.cs +++ b/Bitvault/ContainerActivatedHandler.cs @@ -1,22 +1,29 @@ -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Toolkit.Foundation; namespace Bitvault; -public record ContainerActivatedHandler(IPublisher publisher) : +public class ContainerActivatedHandler(IContainerHostCollection containers, + IPublisher publisher) : INotificationHandler> { public async Task Handle(Activated args, CancellationToken cancellationToken = default) { if (args.Value is IComponentHost container) { + List sortedContainers = [.. containers, container]; + sortedContainers = [.. sortedContainers.OrderBy(x => x.GetConfiguration< ContainerConfiguration>() is ContainerConfiguration configuration ? configuration.Name : null)]; + + int index = sortedContainers.IndexOf(container); + if (container.Services.GetRequiredService() is ContainerConfiguration configuration) { if (container.Services.GetRequiredService() is IServiceFactory factory) { if (factory.Create(configuration.Name) is ContainerNavigationViewModel viewModel) { - await publisher.Publish(new Create(viewModel), + await publisher.Publish(new Insert(index, viewModel), nameof(MainViewModel), cancellationToken); } } diff --git a/Bitvault/ContainerComponentFactory.cs b/Bitvault/ContainerComponentFactory.cs deleted file mode 100644 index 5df67cd..0000000 --- a/Bitvault/ContainerComponentFactory.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Toolkit.Foundation; - -namespace Bitvault; - -public class ContainerComponentFactory(IComponentFactory componentFactory) : - IContainerComponentFactory -{ - public IComponentHost? Create(string name) - { - if (componentFactory.Create($"Vault:{name}", - new ContainerConfiguration { Name = name }) is IComponentHost host) - { - return host; - } - - return default; - } -} diff --git a/Bitvault/ContainerFactory.cs b/Bitvault/ContainerFactory.cs index fbac9bb..05e1df1 100644 --- a/Bitvault/ContainerFactory.cs +++ b/Bitvault/ContainerFactory.cs @@ -1,41 +1,18 @@ -using Bitvault.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Toolkit.Foundation; +using Toolkit.Foundation; namespace Bitvault; -public class ContainerFactory(IValueStore connection, - IHostEnvironment environment, - IServiceProvider provider) : +public class ContainerFactory(IComponentFactory componentFactory) : IContainerFactory { - public async Task Create(string name, - SecurityKey key) + public IComponentHost? Create(string name) { - connection.Set(new ContainerConnection($"Data Source={Path.Combine(environment.ContentRootPath, name)}" + - $".vault;Mode=ReadWriteCreate;Pooling=false;Password={Convert.ToBase64String(key.DecryptedKey)}")); - - IDbContextFactory dbContextFactory = provider.GetRequiredService>(); - using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync(); - - try + if (componentFactory.Create($"Vault:{name}", + new ContainerConfiguration { Name = name }) is IComponentHost host) { - await Task.Run(async () => - { - await context.Database.EnsureCreatedAsync().ConfigureAwait(false); - await context.Database.CloseConnectionAsync().ConfigureAwait(false); - - context.Database.SetConnectionString(null); - - }).ConfigureAwait(false); - } - catch - { - return false; + return host; } - return true; + return default; } } diff --git a/Bitvault/ContainerStorageFactory.cs b/Bitvault/ContainerStorageFactory.cs new file mode 100644 index 0000000..c797b2d --- /dev/null +++ b/Bitvault/ContainerStorageFactory.cs @@ -0,0 +1,41 @@ +using Bitvault.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Toolkit.Foundation; + +namespace Bitvault; + +public class ContainerStorageFactory(IValueStore connection, + IHostEnvironment environment, + IServiceProvider provider) : + IContainerStorageFactory +{ + public async Task Create(string name, + SecurityKey key) + { + connection.Set(new ContainerConnection($"Data Source={Path.Combine(environment.ContentRootPath, name)}" + + $".vault;Mode=ReadWriteCreate;Pooling=false;Password={Convert.ToBase64String(key.DecryptedKey)}")); + + IDbContextFactory dbContextFactory = provider.GetRequiredService>(); + using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync(); + + try + { + await Task.Run(async () => + { + await context.Database.EnsureCreatedAsync().ConfigureAwait(false); + await context.Database.CloseConnectionAsync().ConfigureAwait(false); + + context.Database.SetConnectionString(null); + + }).ConfigureAwait(false); + } + catch + { + return false; + } + + return true; + } +} diff --git a/Bitvault/CreateContainerHandler.cs b/Bitvault/CreateContainerHandler.cs index fb4298b..f451ab8 100644 --- a/Bitvault/CreateContainerHandler.cs +++ b/Bitvault/CreateContainerHandler.cs @@ -5,7 +5,7 @@ using Toolkit.Foundation; namespace Bitvault; -public class CreateContainerHandler(IContainerComponentFactory componentFactory, +public class CreateContainerHandler(IContainerFactory componentFactory, IPublisher publisher) : IHandler, bool> { @@ -19,7 +19,7 @@ public class CreateContainerHandler(IContainerComponentFactory componentFactory, { ISecurityKeyFactory keyVaultFactory = host.Services.GetRequiredService(); IValueStore secureKeyStore = host.Services.GetRequiredService>(); - IContainerFactory containerFactory = host.Services.GetRequiredService(); + IContainerStorageFactory containerFactory = host.Services.GetRequiredService(); if (keyVaultFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key) { diff --git a/Bitvault/IContainerComponentFactory.cs b/Bitvault/IContainerComponentFactory.cs deleted file mode 100644 index 53a7cc2..0000000 --- a/Bitvault/IContainerComponentFactory.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Toolkit.Foundation; - -namespace Bitvault -{ - public interface IContainerComponentFactory - { - IComponentHost? Create(string name); - } -} \ No newline at end of file diff --git a/Bitvault/IContainerFactory.cs b/Bitvault/IContainerFactory.cs index 0c3bd3e..01b3697 100644 --- a/Bitvault/IContainerFactory.cs +++ b/Bitvault/IContainerFactory.cs @@ -1,6 +1,9 @@ -namespace Bitvault; +using Toolkit.Foundation; -public interface IContainerFactory +namespace Bitvault { - Task Create(string name, SecurityKey key); + public interface IContainerFactory + { + IComponentHost? Create(string name); + } } \ No newline at end of file diff --git a/Bitvault/IContainerStorageFactory.cs b/Bitvault/IContainerStorageFactory.cs new file mode 100644 index 0000000..0977662 --- /dev/null +++ b/Bitvault/IContainerStorageFactory.cs @@ -0,0 +1,6 @@ +namespace Bitvault; + +public interface IContainerStorageFactory +{ + Task Create(string name, SecurityKey key); +} \ No newline at end of file diff --git a/Bitvault/MainViewModelHandler.cs b/Bitvault/MainViewModelHandler.cs index e3d871e..96bcac1 100644 --- a/Bitvault/MainViewModelHandler.cs +++ b/Bitvault/MainViewModelHandler.cs @@ -10,7 +10,8 @@ public class MainViewModelHandler(IPublisher publisher, public async Task Handle(Enumerate args, CancellationToken cancellationToken = default) { - foreach (IComponentHost container in containers) + foreach (IComponentHost container in containers.OrderBy(x => x.GetConfiguration() + is ContainerConfiguration configuration ? configuration.Name : null)) { if (container.Services.GetRequiredService() is ContainerConfiguration configuration) { diff --git a/Bitvault/OpenContainerHandler.cs b/Bitvault/OpenContainerHandler.cs index 6af761e..941ec10 100644 --- a/Bitvault/OpenContainerHandler.cs +++ b/Bitvault/OpenContainerHandler.cs @@ -5,7 +5,7 @@ namespace Bitvault; public class OpenContainerHandler(ContainerConfiguration configuration, ISecurityKeyFactory keyVaultFactory, - IContainerFactory vaultStorage) : + IContainerStorageFactory vaultStorage) : IHandler, bool> { public async Task Handle(Open args,