diff --git a/Bitvault.Avalonia/App.axaml.cs b/Bitvault.Avalonia/App.axaml.cs index 33af2a9..f68a264 100644 --- a/Bitvault.Avalonia/App.axaml.cs +++ b/Bitvault.Avalonia/App.axaml.cs @@ -9,6 +9,8 @@ using Toolkit.Foundation; using Microsoft.Extensions.DependencyInjection.Extensions; using HotAvalonia; using Bitvault.Data; +using System.Collections; +using System.Collections.Generic; namespace Bitvault.Avalonia; @@ -41,6 +43,9 @@ public partial class App : Application { args.AddServices(services => { + services.AddTransient>(provider => Comparer.Create((x, z) => x.Name!.CompareTo(z.Name))); + services.AddCache(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -89,7 +94,7 @@ public partial class App : Application services.AddTemplate(); - services.AddHandler(); + services.AddHandler(ServiceLifetime.Singleton); services.AddHandler(); }); })!); diff --git a/Bitvault/ContainerComponent.cs b/Bitvault/ContainerComponent.cs index 3893f3a..b987556 100644 --- a/Bitvault/ContainerComponent.cs +++ b/Bitvault/ContainerComponent.cs @@ -3,6 +3,4 @@ namespace Bitvault; public class ContainerComponent(IComponentBuilder builder) : Component(builder), - IContainerComponent -{ -} \ No newline at end of file + IContainerComponent; \ No newline at end of file diff --git a/Bitvault/ContainerViewModelHandler.cs b/Bitvault/ContainerViewModelHandler.cs index 2ac3249..4d8f2ac 100644 --- a/Bitvault/ContainerViewModelHandler.cs +++ b/Bitvault/ContainerViewModelHandler.cs @@ -8,6 +8,7 @@ namespace Bitvault; public class ContainerViewModelHandler(IDbContextFactory dbContextFactory, IServiceProvider serviceProvider, + ICache cache, IPublisher publisher) : INotificationHandler> { @@ -16,6 +17,8 @@ public class ContainerViewModelHandler(IDbContextFactory dbC { if (args.Options is ContainerViewModelConfiguration configuration) { + cache.Clear(); + ExpressionStarter predicate = PredicateBuilder.New(true); if (configuration.Filter == "All") @@ -40,7 +43,7 @@ public class ContainerViewModelHandler(IDbContextFactory dbC { x.Id, x.Name - }).ToListAsync(); + }).OrderBy(x => x.Name).ToListAsync(); }, cancellationToken); @@ -51,8 +54,8 @@ public class ContainerViewModelHandler(IDbContextFactory dbC if (serviceFactory.Create(item.Id, item.Name, "Description " + 1) is ItemNavigationViewModel viewModel) { - await publisher.Publish(new CreateEventArgs(viewModel), - nameof(ContainerViewModel), cancellationToken); + cache.Add(new Item { Id = item.Id, Name = item.Name }); + await publisher.Publish(Create.As(viewModel), nameof(ContainerViewModel), cancellationToken); } } } diff --git a/Bitvault/CreateItemHandler.cs b/Bitvault/CreateItemHandler.cs index 630e110..08b164b 100644 --- a/Bitvault/CreateItemHandler.cs +++ b/Bitvault/CreateItemHandler.cs @@ -1,11 +1,13 @@ using Bitvault.Data; +using HarfBuzzSharp; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Toolkit.Foundation; namespace Bitvault; -public class CreateItemHandler(IDbContextFactory dbContextFactory, IPublisher publisher) : +public class CreateItemHandler(IDbContextFactory dbContextFactory, + IPublisher publisher) : IHandler, bool> { public async Task Handle(CreateEventArgs args, @@ -27,7 +29,9 @@ public class CreateItemHandler(IDbContextFactory dbContextFa if (result is not null) { - await publisher.Publish(Activated.As(new Item { Id = result.Entity.Id }), cancellationToken); + Item item = new() { Id = result.Entity.Id, Name = configuration.Name }; + await publisher.Publish(Activated.As(item), cancellationToken); + return true; } } diff --git a/Bitvault/Item.cs b/Bitvault/Item.cs index bf4c395..3f54422 100644 --- a/Bitvault/Item.cs +++ b/Bitvault/Item.cs @@ -3,5 +3,8 @@ public record Item { public int Id { get; init; } + + public string? Name { get; init; } + } diff --git a/Bitvault/ItemActivatedHandler.cs b/Bitvault/ItemActivatedHandler.cs index edbbcf7..ad9d040 100644 --- a/Bitvault/ItemActivatedHandler.cs +++ b/Bitvault/ItemActivatedHandler.cs @@ -1,23 +1,29 @@ -using Microsoft.Extensions.DependencyInjection; +using FluentAvalonia.Core; +using Microsoft.Extensions.DependencyInjection; using Toolkit.Foundation; namespace Bitvault; public class ItemActivatedHandler(IServiceProvider serviceProvider, - IProxyService proxyPublisher) : + ICache cache, + IPublisher publisher) : INotificationHandler> { public async Task Handle(ActivatedEventArgs args, CancellationToken cancellationToken = default) { - IServiceScope serviceScope = serviceProvider.CreateScope(); - IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService(); - - if (serviceFactory.Create(2, "efesf", "Description " + 1) is ItemNavigationViewModel viewModel) + if (args.Value is Item item) { - // somehow, we need to get back out of the scope back to the compoment level, this currently doesnt work, and we need a better and cleaner way - await proxyPublisher.Proxy.Publish(new CreateEventArgs(viewModel), - nameof(ContainerViewModel), cancellationToken); + IServiceScope serviceScope = serviceProvider.CreateScope(); + IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService(); + + cache.Add(item); + int index = cache.IndexOf(item); + + if (serviceFactory.Create(item.Id, item.Name, "Description " + 1) is ItemNavigationViewModel viewModel) + { + await publisher.Publish(Insert.As(index, viewModel), nameof(ContainerViewModel), cancellationToken); + } } } } \ No newline at end of file