Add suport for adding new items which are sorted without reloading the entrie list

This commit is contained in:
TheXamlGuy
2024-05-12 22:02:51 +01:00
parent 55d3971bfc
commit 3136a6458c
6 changed files with 37 additions and 18 deletions
+6 -1
View File
@@ -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<IComparer<Item>>(provider => Comparer<Item>.Create((x, z) => x.Name!.CompareTo(z.Name)));
services.AddCache<Item>();
services.AddTransient<IKeyGenerator, KeyGenerator>();
services.AddTransient<IEncryptor, AesEncryptor>();
services.AddTransient<IDecryptor, AesDecryptor>();
@@ -89,7 +94,7 @@ public partial class App : Application
services.AddTemplate<ItemHeaderViewModel, ItemHeaderView>();
services.AddHandler<CreateItemHandler>();
services.AddHandler<CreateItemHandler>(ServiceLifetime.Singleton);
services.AddHandler<ItemActivatedHandler>();
});
})!);
+1 -3
View File
@@ -3,6 +3,4 @@
namespace Bitvault;
public class ContainerComponent(IComponentBuilder builder) : Component(builder),
IContainerComponent
{
}
IContainerComponent;
+6 -3
View File
@@ -8,6 +8,7 @@ namespace Bitvault;
public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbContextFactory,
IServiceProvider serviceProvider,
ICache<Item> cache,
IPublisher publisher) :
INotificationHandler<Enumerate<ItemNavigationViewModel, ContainerViewModelConfiguration>>
{
@@ -16,6 +17,8 @@ public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbC
{
if (args.Options is ContainerViewModelConfiguration configuration)
{
cache.Clear();
ExpressionStarter<ItemEntry> predicate = PredicateBuilder.New<ItemEntry>(true);
if (configuration.Filter == "All")
@@ -40,7 +43,7 @@ public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbC
{
x.Id,
x.Name
}).ToListAsync();
}).OrderBy(x => x.Name).ToListAsync();
}, cancellationToken);
@@ -51,8 +54,8 @@ public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbC
if (serviceFactory.Create<ItemNavigationViewModel>(item.Id, item.Name, "Description " + 1) is ItemNavigationViewModel viewModel)
{
await publisher.Publish(new CreateEventArgs<ItemNavigationViewModel>(viewModel),
nameof(ContainerViewModel), cancellationToken);
cache.Add(new Item { Id = item.Id, Name = item.Name });
await publisher.Publish(Create.As(viewModel), nameof(ContainerViewModel), cancellationToken);
}
}
}
+6 -2
View File
@@ -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<ContainerDbContext> dbContextFactory, IPublisher publisher) :
public class CreateItemHandler(IDbContextFactory<ContainerDbContext> dbContextFactory,
IPublisher publisher) :
IHandler<CreateEventArgs<ItemConfiguration>, bool>
{
public async Task<bool> Handle(CreateEventArgs<ItemConfiguration> args,
@@ -27,7 +29,9 @@ public class CreateItemHandler(IDbContextFactory<ContainerDbContext> 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;
}
}
+3
View File
@@ -3,5 +3,8 @@
public record Item
{
public int Id { get; init; }
public string? Name { get; init; }
}
+15 -9
View File
@@ -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<IPublisher> proxyPublisher) :
ICache<Item> cache,
IPublisher publisher) :
INotificationHandler<ActivatedEventArgs<Item>>
{
public async Task Handle(ActivatedEventArgs<Item> args,
CancellationToken cancellationToken = default)
{
IServiceScope serviceScope = serviceProvider.CreateScope();
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
if (serviceFactory.Create<ItemNavigationViewModel>(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<ItemNavigationViewModel>(viewModel),
nameof(ContainerViewModel), cancellationToken);
IServiceScope serviceScope = serviceProvider.CreateScope();
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
cache.Add(item);
int index = cache.IndexOf(item);
if (serviceFactory.Create<ItemNavigationViewModel>(item.Id, item.Name, "Description " + 1) is ItemNavigationViewModel viewModel)
{
await publisher.Publish(Insert.As(index, viewModel), nameof(ContainerViewModel), cancellationToken);
}
}
}
}