using Microsoft.Extensions.DependencyInjection; using Toolkit.Foundation; namespace Wallet; public class ItemCreatedHandler(IServiceProvider serviceProvider, ICache> cache, IPublisher publisher) : INotificationHandler>> { public Task Handle(CreatedEventArgs> args) { if (args.Sender is Item<(Guid, string)> item) { (Guid id, string name) = item.Value; IServiceScope serviceScope = serviceProvider.CreateScope(); IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService(); IDecoratorService> decoratorService = serviceScope.ServiceProvider.GetRequiredService>>(); if (serviceFactory.Create(args => args.Initialize(), id, name, "Description", true) is ItemNavigationViewModel viewModel) { cache.Add(item); int index = cache.IndexOf(item); decoratorService.Set(item); publisher.Publish(Insert.As(index, viewModel), nameof(ItemCollectionViewModel)); } } return Task.CompletedTask; } }