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
+14 -3
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 (configuration is not null)
{
bool success = false;
if (valueStore?.Value is Item item) if (valueStore?.Value is Item item)
{ {
(bool Success, int Id, string Name) = await mediator.Handle<EditEventArgs<(int, ItemConfiguration)>, (bool Success, int Id, string Name) = await mediator.Handle<EditEventArgs<(int, ItemConfiguration)>,
(bool, int, string)>(new EditEventArgs<(int, ItemConfiguration)>((item.Id, new ItemConfiguration { Name = configuration?.Name }))); (bool, int, string)>(new EditEventArgs<(int, ItemConfiguration)>((item.Id, new ItemConfiguration { Name = configuration.Name })));
if (Success) if (Success)
{ {
Item newItem = new Item { Id = Id, Name = Name }; Item newItem = new() { Id = Id, Name = Name };
publisher.Publish(Modified.As(item, newItem)); publisher.Publish(Modified.As(item, newItem));
valueStore.Set(newItem); valueStore.Set(newItem);
success = true;
} }
} }
else else
{ {
(bool Success, int Id, string Name) = await mediator.Handle<CreateEventArgs<ItemConfiguration>, (bool Success, int Id, string Name) = await mediator.Handle<CreateEventArgs<ItemConfiguration>,
(bool, int, string)>(new CreateEventArgs<ItemConfiguration>(new ItemConfiguration { Name = configuration?.Name })); (bool, int, string)>(new CreateEventArgs<ItemConfiguration>(new ItemConfiguration { Name = configuration.Name }));
if (Success) if (Success)
{ {
publisher.Publish(Created.As(new Item { Id = Id, Name = Name })); 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;
}
} }
+1 -6
View File
@@ -18,12 +18,8 @@ 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)
is ItemNavigationViewModel viewModel)
{
int oldIndex = cache.IndexOf(cachedItem); int oldIndex = cache.IndexOf(cachedItem);
cache.Remove(cachedItem); cache.Remove(cachedItem);
@@ -32,8 +28,7 @@ public class ModifiedItemHandler(IServiceProvider serviceProvider,
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));
}
} }
} }