using Microsoft.Extensions.DependencyInjection; using Toolkit.Foundation; namespace Bitvault; public class ItemModifiedHandler(IServiceProvider serviceProvider, IPublisher publisher) : INotificationHandler>> { public Task Handle(ModifiedEventArgs> args) { Item<(Guid, string)> oldItem = args.OldView; Item<(Guid, string)> newItem = args.NewValue; ICache> cache = serviceProvider.GetRequiredService>>(); if (cache.TryGetValue(oldItem, out Item<(Guid, string)>? cachedItem)) { if (cachedItem is not null) { IServiceScope serviceScope = serviceProvider.CreateScope(); IDecoratorService> decoratorService = serviceScope.ServiceProvider.GetRequiredService>>(); int oldIndex = cache.IndexOf(cachedItem); cache.Remove(cachedItem); cache.Add(newItem); int newIndex = cache.IndexOf(newItem); decoratorService.Set(newItem); publisher.Publish(MoveTo.As(oldIndex, newIndex), nameof(ItemCollectionViewModel)); } } return Task.CompletedTask; } }