add keyed based publication to mediator
This commit is contained in:
@@ -7,6 +7,7 @@ namespace Hyperbar.Windows.Primary;
|
||||
public class PrimaryCommandConfiguration
|
||||
{
|
||||
public List<PrimaryCommandConfiguration> Commands { get; set; } = [];
|
||||
|
||||
public required string Icon { get; set; }
|
||||
|
||||
public required Guid Id { get; set; }
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class PrimaryWidgetConfiguration :
|
||||
List<PrimaryCommandConfiguration>
|
||||
public class PrimaryWidgetConfiguration
|
||||
{
|
||||
public static PrimaryWidgetConfiguration Defaults => new()
|
||||
{
|
||||
new KeyAcceleratorCommandConfiguration { Id = Guid.NewGuid(), Order = 1, Icon = "\uE720", Text = "Test", Key = 91, Modifiers = [] }
|
||||
};
|
||||
public List<PrimaryCommandConfiguration> Commands { get; set; } = [];
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class PrimaryWidgetConfigurationHandler(IMediator mediator,
|
||||
PrimaryWidgetConfiguration configuration,
|
||||
@@ -9,26 +10,52 @@ public class PrimaryWidgetConfigurationHandler(IMediator mediator,
|
||||
public async Task Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
HashSet<Guid> configurationIds = new(configuration.SelectMany(item => new[] { item }
|
||||
.Concat(item.Commands).Select(x => x.Id)));
|
||||
List<(Guid ParentId, Guid Id, PrimaryCommandConfiguration Configuration)> items = [];
|
||||
|
||||
foreach (KeyValuePair<Guid, IWidgetComponentViewModel> item in cache.Where(x => !configurationIds.Contains(x.Key)))
|
||||
void AddToItems(Guid parentId, Guid id, List<PrimaryCommandConfiguration> configurations)
|
||||
{
|
||||
if (configurations is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Stack<(Guid, List<PrimaryCommandConfiguration>)> stack = new();
|
||||
stack.Push((parentId, configurations));
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
(Guid currentParentId, List<PrimaryCommandConfiguration> currentConfigurations) = stack.Pop();
|
||||
foreach (PrimaryCommandConfiguration configuration in currentConfigurations)
|
||||
{
|
||||
items.Add((currentParentId, configuration.Id, configuration));
|
||||
if (configuration.Commands is not null && configuration.Commands.Count > 0)
|
||||
{
|
||||
stack.Push((configuration.Id, configuration.Commands));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AddToItems(Guid.Empty, Guid.Empty, configuration.Commands);
|
||||
|
||||
foreach (KeyValuePair<Guid, IWidgetComponentViewModel> item in cache
|
||||
.Where(x => !items.Any(k => x.Key == k.Id)))
|
||||
{
|
||||
await mediator.PublishAsync(new Removed<IWidgetComponentViewModel>(item.Value),
|
||||
cancellationToken);
|
||||
nameof(PrimaryWidgetViewModel),
|
||||
cancellationToken);
|
||||
|
||||
cache.Remove(item.Key);
|
||||
}
|
||||
|
||||
foreach (PrimaryCommandConfiguration item in configuration)
|
||||
foreach ((Guid ParentId, Guid Id, PrimaryCommandConfiguration Configuration) item in
|
||||
items.Where(x => !cache.Any(k => x.Id == k.Key)))
|
||||
{
|
||||
if (!cache.ContainsKey(item.Id))
|
||||
if (factory.Create(item.Configuration) is IWidgetComponentViewModel viewModel)
|
||||
{
|
||||
if (factory.Create(item) is IWidgetComponentViewModel viewModel)
|
||||
{
|
||||
await mediator.PublishAsync(Inserted<IWidgetComponentViewModel>
|
||||
.For<PrimaryWidgetViewModel>(item.Order, viewModel),
|
||||
cancellationToken);
|
||||
}
|
||||
await mediator.PublishAsync(new Inserted<IWidgetComponentViewModel>(item.Configuration.Order, viewModel),
|
||||
nameof(PrimaryWidgetViewModel),
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
[NotificationHandler(nameof(PrimaryWidgetViewModel))]
|
||||
public class PrimaryWidgetViewModel(ITemplateFactory templateFactory,
|
||||
IServiceFactory serviceFactory,
|
||||
IMediator mediator,
|
||||
|
||||
@@ -6,7 +6,7 @@ public class WidgetComponentViewModelEnumerator(PrimaryWidgetConfiguration confi
|
||||
{
|
||||
public IEnumerable<IWidgetComponentViewModel?> Next()
|
||||
{
|
||||
foreach (PrimaryCommandConfiguration item in configuration.OrderBy(x => x.Order))
|
||||
foreach (PrimaryCommandConfiguration item in configuration.Commands.OrderBy(x => x.Order))
|
||||
{
|
||||
yield return factory.Create(item);
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class WidgetComponentViewModelFactory(IServiceFactory service,
|
||||
if (childViewModel is not null)
|
||||
{
|
||||
childViewModels.Add(childViewModel);
|
||||
cache.Add(childViewModel.Id, childViewModel);
|
||||
cache.Add(childCommandConfiguration.Id, childViewModel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ public class WidgetComponentViewModelFactory(IServiceFactory service,
|
||||
|
||||
if (viewModel is not null)
|
||||
{
|
||||
cache.Add(viewModel.Id, viewModel);
|
||||
cache.Add(configuration.Id, viewModel);
|
||||
}
|
||||
|
||||
return viewModel;
|
||||
|
||||
Reference in New Issue
Block a user