Fixed dyanmically adding/removing items without the need to reload the whole bar
This commit is contained in:
@@ -6,30 +6,30 @@ public class ConfigurationChangedHandler(IMediator mediator,
|
||||
IViewModelCache<Guid, IWidgetComponentViewModel> cache) :
|
||||
INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>>
|
||||
{
|
||||
public async ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
|
||||
public async ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
|
||||
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);
|
||||
|
||||
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<IWidgetComponentViewModel>(value),
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Hyperbar;
|
||||
public class Mediator(IServiceProvider provider) :
|
||||
IMediator
|
||||
{
|
||||
private readonly ConditionalWeakTable<dynamic, Type> subjects = [];
|
||||
private readonly ConditionalWeakTable<Type, dynamic> subjects = [];
|
||||
|
||||
public async ValueTask PublishAsync<TNotification>(TNotification notification,
|
||||
CancellationToken cancellationToken = default)
|
||||
@@ -16,11 +16,11 @@ public class Mediator(IServiceProvider provider) :
|
||||
List<INotificationHandler<TNotification>> handlers =
|
||||
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)
|
||||
{
|
||||
Type notificationType = arguments[0];
|
||||
subjects.Add(subject, notificationType);
|
||||
subjects.Add(notificationType, subject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public record CollectionChanged<TValue> : INotification;
|
||||
@@ -10,7 +10,9 @@ namespace Hyperbar;
|
||||
|
||||
public partial class ObservableCollectionViewModel<TItem> :
|
||||
ObservableObject,
|
||||
IObservableCollectionViewModel<TItem>
|
||||
IObservableCollectionViewModel<TItem>,
|
||||
INotificationHandler<Removed<TItem>>,
|
||||
INotificationHandler<Created<TItem>>
|
||||
{
|
||||
public ObservableCollection<TItem> collection = [];
|
||||
private readonly SynchronizationContext? context;
|
||||
@@ -214,6 +216,37 @@ public partial class ObservableCollectionViewModel<TItem> :
|
||||
IEnumerator IEnumerable.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);
|
||||
|
||||
int IList.IndexOf(object? value) =>
|
||||
@@ -256,7 +289,6 @@ public partial class ObservableCollectionViewModel<TItem> :
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void IList.Remove(object? value)
|
||||
{
|
||||
if (IsCompatibleObject(value))
|
||||
|
||||
@@ -5,8 +5,7 @@ namespace Hyperbar;
|
||||
public partial class WidgetComponentViewModel :
|
||||
ObservableViewModel,
|
||||
IWidgetComponentViewModel,
|
||||
ITemplatedViewModel,
|
||||
INotificationHandler<Removed<IWidgetComponentViewModel>>
|
||||
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<IWidgetComponentViewModel> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (notification.Value.Equals(this))
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user