diff --git a/Bitvault/ConfirmItemHandler.cs b/Bitvault/ConfirmItemHandler.cs index eb68e8d..77403de 100644 --- a/Bitvault/ConfirmItemHandler.cs +++ b/Bitvault/ConfirmItemHandler.cs @@ -12,27 +12,38 @@ public class ConfirmItemHandler(IValueStore valueStore, ItemHeaderConfiguration? configuration = await mediator.Handle, ItemHeaderConfiguration>(args); - if (valueStore?.Value is Item item) + if (configuration is not null) { - (bool Success, int Id, string Name) = await mediator.Handle, - (bool, int, string)>(new EditEventArgs<(int, ItemConfiguration)>((item.Id, new ItemConfiguration { Name = configuration?.Name }))); - - if (Success) + bool success = false; + if (valueStore?.Value is Item item) { - Item newItem = new Item { Id = Id, Name = Name }; - publisher.Publish(Modified.As(item, newItem)); + (bool Success, int Id, string Name) = await mediator.Handle, + (bool, int, string)>(new EditEventArgs<(int, ItemConfiguration)>((item.Id, new ItemConfiguration { Name = configuration.Name }))); - valueStore.Set(newItem); + if (Success) + { + Item newItem = new() { Id = Id, Name = Name }; + publisher.Publish(Modified.As(item, newItem)); + + valueStore.Set(newItem); + success = true; + } } - } - else - { - (bool Success, int Id, string Name) = await mediator.Handle, - (bool, int, string)>(new CreateEventArgs(new ItemConfiguration { Name = configuration?.Name })); - - if (Success) + else { - publisher.Publish(Created.As(new Item { Id = Id, Name = Name })); + (bool Success, int Id, string Name) = await mediator.Handle, + (bool, int, string)>(new CreateEventArgs(new ItemConfiguration { Name = configuration.Name })); + + if (Success) + { + publisher.Publish(Created.As(new Item { Id = Id, Name = Name })); + success = true; + } + } + + if (success) + { + publisher.Publish(Notify.As(configuration)); } } } diff --git a/Bitvault/ItemHeaderConfiguration.cs b/Bitvault/ItemHeaderConfiguration.cs index 95ab444..20592b9 100644 --- a/Bitvault/ItemHeaderConfiguration.cs +++ b/Bitvault/ItemHeaderConfiguration.cs @@ -2,5 +2,5 @@ public record ItemHeaderConfiguration { - public string? Name { get; init; } + public string Name { get; init; } = ""; } diff --git a/Bitvault/ItemNavigationViewModel.cs b/Bitvault/ItemNavigationViewModel.cs index bc33d40..03f42e7 100644 --- a/Bitvault/ItemNavigationViewModel.cs +++ b/Bitvault/ItemNavigationViewModel.cs @@ -22,6 +22,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider, INotificationHandler>, INotificationHandler>, INotificationHandler>, + INotificationHandler>, ISelectable, IRemovable { @@ -59,4 +60,13 @@ public partial class ItemNavigationViewModel(IServiceProvider provider, public Task Handle(UnfavouriteEventArgs args) => Task.FromResult(Favourite = false); + public Task Handle(NotifyEventArgs args) + { + if (args.Value is ItemHeaderConfiguration configuration) + { + Name = configuration.Name; + } + + return Task.CompletedTask; + } } \ No newline at end of file diff --git a/Bitvault/ModifiedItemHandler.cs b/Bitvault/ModifiedItemHandler.cs index 6f8e86c..39bc09a 100644 --- a/Bitvault/ModifiedItemHandler.cs +++ b/Bitvault/ModifiedItemHandler.cs @@ -18,22 +18,17 @@ public class ModifiedItemHandler(IServiceProvider serviceProvider, if (cachedItem is not null) { IServiceScope serviceScope = serviceProvider.CreateScope(); - IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService(); IValueStore valueStore = serviceScope.ServiceProvider.GetRequiredService>(); - if (serviceFactory.Create(newItem.Id, newItem.Name, "Description", true) - is ItemNavigationViewModel viewModel) - { - int oldIndex = cache.IndexOf(cachedItem); - cache.Remove(cachedItem); + int oldIndex = cache.IndexOf(cachedItem); + cache.Remove(cachedItem); - cache.Add(newItem); + cache.Add(newItem); - int newIndex = cache.IndexOf(newItem); - valueStore.Set(newItem); + int newIndex = cache.IndexOf(newItem); + valueStore.Set(newItem); - publisher.Publish(RemoveAndInsertAt.As(oldIndex, newIndex, viewModel), nameof(ContainerViewModel)); - } + publisher.Publish(MoveTo.As(oldIndex, newIndex), nameof(ContainerViewModel)); } }