From 70e0ae94929e204b9f2408d021819a4db436fb52 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sat, 13 Jan 2024 11:40:40 +0000 Subject: [PATCH] Fixed dyanmically adding/removing items without the need to reload the whole bar --- .../ConfigurationChangedHandler.cs | 30 ++++++++-------- Hyperbar/Mediators/Mediator.cs | 10 +++--- Hyperbar/Views/CollectionChanged.cs | 3 -- .../Views/ObservableCollectionViewModel.cs | 36 +++++++++++++++++-- Hyperbar/Views/WidgetComponentViewModel.cs | 14 +------- 5 files changed, 55 insertions(+), 38 deletions(-) delete mode 100644 Hyperbar/Views/CollectionChanged.cs diff --git a/Hyperbar.Windows.Primary/ConfigurationChangedHandler.cs b/Hyperbar.Windows.Primary/ConfigurationChangedHandler.cs index 08fbc18..55a562c 100644 --- a/Hyperbar.Windows.Primary/ConfigurationChangedHandler.cs +++ b/Hyperbar.Windows.Primary/ConfigurationChangedHandler.cs @@ -6,30 +6,30 @@ public class ConfigurationChangedHandler(IMediator mediator, IViewModelCache cache) : INotificationHandler> { - public async ValueTask Handle(ConfigurationChanged notification, + public async ValueTask Handle(ConfigurationChanged notification, CancellationToken cancellationToken) { - foreach (KeyValuePair cached in cache) + foreach (KeyValuePair item in cache) { - if (configuration.FirstOrDefault(x => x.Id == cached.Key) == null) + if (configuration.FirstOrDefault(x => x.Id == item.Key) == null) { - await mediator.PublishAsync(new Removed(cached.Value), + await mediator.PublishAsync(new Removed(item.Value), cancellationToken); - cache.Remove(cached.Key); + cache.Remove(item.Key); } } foreach (PrimaryCommandConfiguration item in configuration) { - - //if (!cache.ContainsKey(item.Id)) - //{ - // factory.CreateAsync(item); - //} - //else - //{ - - //} - } } + if (!cache.ContainsKey(item.Id)) + { + if (factory.Create(item) is IWidgetComponentViewModel value) + { + await mediator.PublishAsync(new Created(value), + cancellationToken); + } + } + } + } } diff --git a/Hyperbar/Mediators/Mediator.cs b/Hyperbar/Mediators/Mediator.cs index f0c7eeb..cf1e045 100644 --- a/Hyperbar/Mediators/Mediator.cs +++ b/Hyperbar/Mediators/Mediator.cs @@ -6,7 +6,7 @@ namespace Hyperbar; public class Mediator(IServiceProvider provider) : IMediator { - private readonly ConditionalWeakTable subjects = []; + private readonly ConditionalWeakTable subjects = []; public async ValueTask PublishAsync(TNotification notification, CancellationToken cancellationToken = default) @@ -16,11 +16,11 @@ public class Mediator(IServiceProvider provider) : List> handlers = provider.GetServices>().ToList(); - foreach (KeyValuePair handler in subjects) + foreach (KeyValuePair handler in subjects) { - if (handler.Value == typeof(TNotification)) + if (handler.Key == typeof(TNotification)) { - handlers.Add(handler.Key); + handlers.Add(handler.Value); } } @@ -76,7 +76,7 @@ public class Mediator(IServiceProvider provider) : if (interfaceType.GetGenericArguments() is { Length: 1 } arguments) { Type notificationType = arguments[0]; - subjects.Add(subject, notificationType); + subjects.Add(notificationType, subject); } } } diff --git a/Hyperbar/Views/CollectionChanged.cs b/Hyperbar/Views/CollectionChanged.cs deleted file mode 100644 index 5040e6b..0000000 --- a/Hyperbar/Views/CollectionChanged.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Hyperbar; - -public record CollectionChanged : INotification; \ No newline at end of file diff --git a/Hyperbar/Views/ObservableCollectionViewModel.cs b/Hyperbar/Views/ObservableCollectionViewModel.cs index 4b0e10f..6bd8df9 100644 --- a/Hyperbar/Views/ObservableCollectionViewModel.cs +++ b/Hyperbar/Views/ObservableCollectionViewModel.cs @@ -10,7 +10,9 @@ namespace Hyperbar; public partial class ObservableCollectionViewModel : ObservableObject, - IObservableCollectionViewModel + IObservableCollectionViewModel, + INotificationHandler>, + INotificationHandler> { public ObservableCollection collection = []; private readonly SynchronizationContext? context; @@ -214,6 +216,37 @@ public partial class ObservableCollectionViewModel : IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)collection).GetEnumerator(); + public ValueTask Handle(Removed notification, + CancellationToken cancellationToken) + { + foreach (TItem item in this) + { + if (notification.Value is not null) + { + if (notification.Value.Equals(item)) + { + if (item is IDisposable disposable) + { + disposable.Dispose(); + } + } + } + } + + return ValueTask.CompletedTask; + } + + public ValueTask Handle(Created notification, + CancellationToken cancellationToken) + { + if (notification.Value is not null) + { + Add(notification.Value); + } + + return ValueTask.CompletedTask; + } + public int IndexOf(TItem item) => collection.IndexOf(item); int IList.IndexOf(object? value) => @@ -256,7 +289,6 @@ public partial class ObservableCollectionViewModel : return true; } - void IList.Remove(object? value) { if (IsCompatibleObject(value)) diff --git a/Hyperbar/Views/WidgetComponentViewModel.cs b/Hyperbar/Views/WidgetComponentViewModel.cs index 72a9e58..8c0481d 100644 --- a/Hyperbar/Views/WidgetComponentViewModel.cs +++ b/Hyperbar/Views/WidgetComponentViewModel.cs @@ -5,8 +5,7 @@ namespace Hyperbar; public partial class WidgetComponentViewModel : ObservableViewModel, IWidgetComponentViewModel, - ITemplatedViewModel, - INotificationHandler> + ITemplatedViewModel { private readonly IMediator mediator; private readonly IServiceFactory serviceFactory; @@ -30,15 +29,4 @@ public partial class WidgetComponentViewModel : } public ITemplateFactory TemplateFactory { get; private set; } - - public ValueTask Handle(Removed notification, - CancellationToken cancellationToken) - { - if (notification.Value.Equals(this)) - { - Dispose(); - } - - return ValueTask.CompletedTask; - } } \ No newline at end of file