using Microsoft.Extensions.DependencyInjection; using Toolkit.Foundation; namespace Wallet; public class ItemNavigationCollectionViewModelActivatedHandler(IMediator mediator, IServiceProvider serviceProvider, ICache> cache, IPublisher publisher) : INotificationHandler> { public async Task Handle(ActivationEventArgs args) { if (args.Value is ItemNavigationCollectionConfiguration configuration) { cache.Clear(); bool selected = true; IReadOnlyCollection<(Guid Id, string Name, string Category, bool Favourite, bool Archived)>? results = await mediator.Handle>, IReadOnlyCollection<(Guid Id, string Name, string Category, bool Favourite, bool Archived)>>(Query.As(new Wallet<(string?, string?)>((configuration.Filter, configuration.Query)))); if (results is not null) { foreach ((Guid Id, string Name, string Category, bool Favourite, bool Archived) in results) { IServiceScope serviceScope = serviceProvider.CreateScope(); IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService(); IDecoratorService> decoratorService = serviceScope.ServiceProvider .GetRequiredService>>(); if (serviceFactory.Create(args => args.Initialize(), Id, Name, "Description", Category, selected, Favourite, Archived) is ItemNavigationViewModel viewModel) { Item<(Guid, string)> item = new((Id, Name)); decoratorService.Set(item); cache.Add(item); publisher.Publish(Create.As(viewModel), nameof(ItemNavigationCollectionViewModel)); } selected = false; } } } } }