Ensure item is updated when modified

This commit is contained in:
TheXamlGuy
2024-05-21 21:57:26 +01:00
parent 48e43f008a
commit 4ae33c2498
4 changed files with 44 additions and 28 deletions
+27 -16
View File
@@ -12,27 +12,38 @@ public class ConfirmItemHandler(IValueStore<Item> valueStore,
ItemHeaderConfiguration? configuration = await mediator.Handle<ConfirmEventArgs<Item>, ItemHeaderConfiguration? configuration = await mediator.Handle<ConfirmEventArgs<Item>,
ItemHeaderConfiguration>(args); ItemHeaderConfiguration>(args);
if (valueStore?.Value is Item item) if (configuration is not null)
{ {
(bool Success, int Id, string Name) = await mediator.Handle<EditEventArgs<(int, ItemConfiguration)>, bool success = false;
(bool, int, string)>(new EditEventArgs<(int, ItemConfiguration)>((item.Id, new ItemConfiguration { Name = configuration?.Name }))); if (valueStore?.Value is Item item)
if (Success)
{ {
Item newItem = new Item { Id = Id, Name = Name }; (bool Success, int Id, string Name) = await mediator.Handle<EditEventArgs<(int, ItemConfiguration)>,
publisher.Publish(Modified.As(item, newItem)); (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
else
{
(bool Success, int Id, string Name) = await mediator.Handle<CreateEventArgs<ItemConfiguration>,
(bool, int, string)>(new CreateEventArgs<ItemConfiguration>(new ItemConfiguration { Name = configuration?.Name }));
if (Success)
{ {
publisher.Publish(Created.As(new Item { Id = Id, Name = Name })); (bool Success, int Id, string Name) = await mediator.Handle<CreateEventArgs<ItemConfiguration>,
(bool, int, string)>(new CreateEventArgs<ItemConfiguration>(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));
} }
} }
} }
+1 -1
View File
@@ -2,5 +2,5 @@
public record ItemHeaderConfiguration public record ItemHeaderConfiguration
{ {
public string? Name { get; init; } public string Name { get; init; } = "";
} }
+10
View File
@@ -22,6 +22,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
INotificationHandler<UnarchiveEventArgs<Item>>, INotificationHandler<UnarchiveEventArgs<Item>>,
INotificationHandler<FavouriteEventArgs<Item>>, INotificationHandler<FavouriteEventArgs<Item>>,
INotificationHandler<UnfavouriteEventArgs<Item>>, INotificationHandler<UnfavouriteEventArgs<Item>>,
INotificationHandler<NotifyEventArgs<ItemHeaderConfiguration>>,
ISelectable, ISelectable,
IRemovable IRemovable
{ {
@@ -59,4 +60,13 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
public Task Handle(UnfavouriteEventArgs<Item> args) => public Task Handle(UnfavouriteEventArgs<Item> args) =>
Task.FromResult(Favourite = false); Task.FromResult(Favourite = false);
public Task Handle(NotifyEventArgs<ItemHeaderConfiguration> args)
{
if (args.Value is ItemHeaderConfiguration configuration)
{
Name = configuration.Name;
}
return Task.CompletedTask;
}
} }
+6 -11
View File
@@ -18,22 +18,17 @@ public class ModifiedItemHandler(IServiceProvider serviceProvider,
if (cachedItem is not null) if (cachedItem is not null)
{ {
IServiceScope serviceScope = serviceProvider.CreateScope(); IServiceScope serviceScope = serviceProvider.CreateScope();
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
IValueStore<Item> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item>>(); IValueStore<Item> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item>>();
if (serviceFactory.Create<ItemNavigationViewModel>(newItem.Id, newItem.Name, "Description", true) int oldIndex = cache.IndexOf(cachedItem);
is ItemNavigationViewModel viewModel) cache.Remove(cachedItem);
{
int oldIndex = cache.IndexOf(cachedItem);
cache.Remove(cachedItem);
cache.Add(newItem); cache.Add(newItem);
int newIndex = cache.IndexOf(newItem); int newIndex = cache.IndexOf(newItem);
valueStore.Set(newItem); valueStore.Set(newItem);
publisher.Publish(RemoveAndInsertAt.As(oldIndex, newIndex, viewModel), nameof(ContainerViewModel)); publisher.Publish(MoveTo.As<ItemNavigationViewModel>(oldIndex, newIndex), nameof(ContainerViewModel));
}
} }
} }