using Microsoft.Extensions.DependencyInjection; using Toolkit.Foundation; namespace Bitvault; public class AggerateContainerViewModelHandler(IMediator mediator, IServiceProvider serviceProvider, ICache cache, IPublisher publisher) : INotificationHandler> { public async Task Handle(AggerateEventArgs args) { if (args.Options is ContainerViewModelConfiguration configuration) { cache.Clear(); bool selected = true; if (await mediator.Handle, IReadOnlyCollection<(int Id, string Name, bool Favourite, bool Archived)>>(Request.As(new QueryContainerConfiguration { Filter = configuration.Filter, Query = configuration.Query })) is IReadOnlyCollection<(int Id, string Name, bool Favourite, bool Archived)> results) { foreach ((int Id, string Name, bool Favourite, bool Archived) in results) { IServiceScope serviceScope = serviceProvider.CreateScope(); IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService(); IValueStore valueStore = serviceScope.ServiceProvider.GetRequiredService>(); if (serviceFactory.Create(Id, Name, "Description", selected, Favourite, Archived) is ItemNavigationViewModel viewModel) { Item item = new() { Id = Id, Name = Name }; valueStore.Set(item); cache.Add(item); publisher.Publish(Create.As(viewModel), nameof(ContainerViewModel)); } selected = false; } } } } }