Fixed dyanmically adding/removing items without the need to reload the whole bar

This commit is contained in:
TheXamlGuy
2024-01-13 11:40:40 +00:00
parent ff1d400531
commit 70e0ae9492
5 changed files with 55 additions and 38 deletions
@@ -6,30 +6,30 @@ public class ConfigurationChangedHandler(IMediator mediator,
IViewModelCache<Guid, IWidgetComponentViewModel> cache) : IViewModelCache<Guid, IWidgetComponentViewModel> cache) :
INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>> INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>>
{ {
public async ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification, public async ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
foreach (KeyValuePair<Guid, IWidgetComponentViewModel> cached in cache) foreach (KeyValuePair<Guid, IWidgetComponentViewModel> 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<IWidgetComponentViewModel>(cached.Value), await mediator.PublishAsync(new Removed<IWidgetComponentViewModel>(item.Value),
cancellationToken); cancellationToken);
cache.Remove(cached.Key); cache.Remove(item.Key);
} }
} }
foreach (PrimaryCommandConfiguration item in configuration) foreach (PrimaryCommandConfiguration item in configuration)
{ {
if (!cache.ContainsKey(item.Id))
//if (!cache.ContainsKey(item.Id)) {
//{ if (factory.Create(item) is IWidgetComponentViewModel value)
// factory.CreateAsync(item); {
//} await mediator.PublishAsync(new Created<IWidgetComponentViewModel>(value),
//else cancellationToken);
//{ }
}
//} }
} } }
} }
+5 -5
View File
@@ -6,7 +6,7 @@ namespace Hyperbar;
public class Mediator(IServiceProvider provider) : public class Mediator(IServiceProvider provider) :
IMediator IMediator
{ {
private readonly ConditionalWeakTable<dynamic, Type> subjects = []; private readonly ConditionalWeakTable<Type, dynamic> subjects = [];
public async ValueTask PublishAsync<TNotification>(TNotification notification, public async ValueTask PublishAsync<TNotification>(TNotification notification,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
@@ -16,11 +16,11 @@ public class Mediator(IServiceProvider provider) :
List<INotificationHandler<TNotification>> handlers = List<INotificationHandler<TNotification>> handlers =
provider.GetServices<INotificationHandler<TNotification>>().ToList(); provider.GetServices<INotificationHandler<TNotification>>().ToList();
foreach (KeyValuePair<dynamic, Type> handler in subjects) foreach (KeyValuePair<Type, dynamic> 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) if (interfaceType.GetGenericArguments() is { Length: 1 } arguments)
{ {
Type notificationType = arguments[0]; Type notificationType = arguments[0];
subjects.Add(subject, notificationType); subjects.Add(notificationType, subject);
} }
} }
} }
-3
View File
@@ -1,3 +0,0 @@
namespace Hyperbar;
public record CollectionChanged<TValue> : INotification;
@@ -10,7 +10,9 @@ namespace Hyperbar;
public partial class ObservableCollectionViewModel<TItem> : public partial class ObservableCollectionViewModel<TItem> :
ObservableObject, ObservableObject,
IObservableCollectionViewModel<TItem> IObservableCollectionViewModel<TItem>,
INotificationHandler<Removed<TItem>>,
INotificationHandler<Created<TItem>>
{ {
public ObservableCollection<TItem> collection = []; public ObservableCollection<TItem> collection = [];
private readonly SynchronizationContext? context; private readonly SynchronizationContext? context;
@@ -214,6 +216,37 @@ public partial class ObservableCollectionViewModel<TItem> :
IEnumerator IEnumerable.GetEnumerator() => IEnumerator IEnumerable.GetEnumerator() =>
((IEnumerable)collection).GetEnumerator(); ((IEnumerable)collection).GetEnumerator();
public ValueTask Handle(Removed<TItem> 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<TItem> notification,
CancellationToken cancellationToken)
{
if (notification.Value is not null)
{
Add(notification.Value);
}
return ValueTask.CompletedTask;
}
public int IndexOf(TItem item) => collection.IndexOf(item); public int IndexOf(TItem item) => collection.IndexOf(item);
int IList.IndexOf(object? value) => int IList.IndexOf(object? value) =>
@@ -256,7 +289,6 @@ public partial class ObservableCollectionViewModel<TItem> :
return true; return true;
} }
void IList.Remove(object? value) void IList.Remove(object? value)
{ {
if (IsCompatibleObject(value)) if (IsCompatibleObject(value))
+1 -13
View File
@@ -5,8 +5,7 @@ namespace Hyperbar;
public partial class WidgetComponentViewModel : public partial class WidgetComponentViewModel :
ObservableViewModel, ObservableViewModel,
IWidgetComponentViewModel, IWidgetComponentViewModel,
ITemplatedViewModel, ITemplatedViewModel
INotificationHandler<Removed<IWidgetComponentViewModel>>
{ {
private readonly IMediator mediator; private readonly IMediator mediator;
private readonly IServiceFactory serviceFactory; private readonly IServiceFactory serviceFactory;
@@ -30,15 +29,4 @@ public partial class WidgetComponentViewModel :
} }
public ITemplateFactory TemplateFactory { get; private set; } public ITemplateFactory TemplateFactory { get; private set; }
public ValueTask Handle(Removed<IWidgetComponentViewModel> notification,
CancellationToken cancellationToken)
{
if (notification.Value.Equals(this))
{
Dispose();
}
return ValueTask.CompletedTask;
}
} }