Files
Hyperbar/Hyperbar.Widget.Primary.Windows/WidgetComponentViewModelEnumerator.cs
TheXamlGuy 565c6866d8 wip
2024-02-10 20:19:01 +00:00

38 lines
1.6 KiB
C#

namespace Hyperbar.Widget.Primary.Windows;
public class WidgetComponentViewModelEnumerator(PrimaryWidgetConfiguration configuration,
IPublisher publisher,
IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory,
ICache<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> cache) :
INotificationHandler<Enumerate<IWidgetComponentViewModel>>
{
public async Task Handle(Enumerate<IWidgetComponentViewModel> notification,
CancellationToken cancellationToken)
{
Stack<(Guid, List<PrimaryCommandConfiguration>)> stack = new();
stack.Push((Guid.Empty, configuration.Commands));
while (stack.Count > 0)
{
(Guid currentParentId, List<PrimaryCommandConfiguration> currentConfigurations) = stack.Pop();
foreach (PrimaryCommandConfiguration configuration in currentConfigurations)
{
cache.Add((currentParentId, configuration.Id), configuration);
if (configuration.Commands is not null && configuration.Commands.Count > 0)
{
stack.Push((configuration.Id, configuration.Commands));
}
}
}
foreach (PrimaryCommandConfiguration item in configuration.Commands.OrderBy(x => x.Order))
{
if (factory.Create(item) is IWidgetComponentViewModel viewModel)
{
await publisher.PublishAsync(new Create<IWidgetComponentViewModel>(viewModel), nameof(PrimaryWidgetViewModel),
cancellationToken);
}
}
}
}