allow new items to be added to sub menus at runtime, from a config file

This commit is contained in:
TheXamlGuy
2024-01-18 22:28:53 +00:00
parent 78cedcdeb8
commit 2fed876182
8 changed files with 110 additions and 47 deletions
@@ -1,62 +1,71 @@
namespace Hyperbar.Windows.Primary;
namespace Hyperbar.Windows.Primary;
public class PrimaryWidgetConfigurationHandler(IMediator mediator, public class PrimaryWidgetConfigurationHandler(IMediator mediator,
PrimaryWidgetConfiguration configuration, PrimaryWidgetConfiguration configuration,
IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory, IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory,
ICache<Guid, IWidgetComponentViewModel> cache) : IProvider<PrimaryCommandConfiguration, IWidgetComponentViewModel?> provider,
ICache<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> cache) :
INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>> INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>>
{ {
public async Task Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification, public async Task Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
List<(Guid ParentId, Guid Id, PrimaryCommandConfiguration Configuration)> items = []; List<KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration>> items = [];
void AddToItems(Guid parentId, Guid id, List<PrimaryCommandConfiguration> configurations) Stack<(Guid, List<PrimaryCommandConfiguration>)> stack = new();
stack.Push((Guid.Empty, configuration.Commands));
while (stack.Count > 0)
{ {
if (configurations is null) (Guid currentParentId, List<PrimaryCommandConfiguration> currentConfigurations) = stack.Pop();
foreach (PrimaryCommandConfiguration configuration in currentConfigurations)
{ {
return; items.Add(new KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration>((currentParentId, configuration.Id), configuration));
} if (configuration.Commands is not null && configuration.Commands.Count > 0)
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)); stack.Push((configuration.Id, configuration.Commands));
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 ParentId, Guid Id), PrimaryCommandConfiguration> added in
items.ExceptBy(cache.Select(x => x.Key.Id), x => x.Key.Id))
foreach (KeyValuePair<Guid, IWidgetComponentViewModel> item in cache
.Where(x => !items.Any(k => x.Key == k.Id)))
{ {
await mediator.PublishAsync(new Removed<IWidgetComponentViewModel>(item.Value), if (added.Value is PrimaryCommandConfiguration configuration)
nameof(PrimaryWidgetViewModel),
cancellationToken);
cache.Remove(item.Key);
}
foreach ((Guid ParentId, Guid Id, PrimaryCommandConfiguration Configuration) item in
items.Where(x => !cache.Any(k => x.Id == k.Key)))
{
if (factory.Create(item.Configuration) is IWidgetComponentViewModel viewModel)
{ {
await mediator.PublishAsync(new Inserted<IWidgetComponentViewModel>(item.Configuration.Order, viewModel), if (factory.Create(configuration) is IWidgetComponentViewModel viewModel)
nameof(PrimaryWidgetViewModel), {
cancellationToken); await mediator.PublishAsync(new Inserted<IWidgetComponentViewModel>(configuration.Order, viewModel),
added.Key.ParentId == Guid.Empty ? nameof(PrimaryWidgetViewModel) : added.Key.ParentId,
cancellationToken);
}
cache.Add(added.Key, added.Value);
} }
} }
foreach (KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> removed in
cache.ExceptBy(items.Select(x => x.Key.Id), x => x.Key.Id))
{
if (removed.Value is PrimaryCommandConfiguration configuration)
{
if (provider.Get(configuration) is IWidgetComponentViewModel viewModel)
{
await mediator.PublishAsync(new Removed<IWidgetComponentViewModel>(viewModel),
removed.Key.ParentId == Guid.Empty ? nameof(PrimaryWidgetViewModel) : removed.Key.ParentId,
cancellationToken);
cache.Remove(removed.Key);
}
}
}
//foreach (KeyValuePair<Guid, IWidgetComponentViewModel> item in cache
// .Where(x => !items.Any(k => x.Key == k.Id)))
//{
// await mediator.PublishAsync(new Removed<IWidgetComponentViewModel>(item.Value),
// nameof(PrimaryWidgetViewModel),
// cancellationToken);
//}
} }
} }
@@ -8,7 +8,9 @@ public class PrimaryWidgetProvider :
{ {
public void Create(HostBuilderContext comtext, IServiceCollection services) => public void Create(HostBuilderContext comtext, IServiceCollection services) =>
services.AddConfiguration<PrimaryWidgetConfiguration>() services.AddConfiguration<PrimaryWidgetConfiguration>()
.AddCache<(Guid ParentId, Guid Id), PrimaryCommandConfiguration>()
.AddCache<Guid, IWidgetComponentViewModel>() .AddCache<Guid, IWidgetComponentViewModel>()
.AddTransient<IProvider<PrimaryCommandConfiguration, IWidgetComponentViewModel?>, WidgetComponentViewModelProvider>()
.AddTransient<IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>, WidgetComponentViewModelFactory>() .AddTransient<IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>, WidgetComponentViewModelFactory>()
.AddTransient<IViewModelEnumerator<IWidgetComponentViewModel>, WidgetComponentViewModelEnumerator>() .AddTransient<IViewModelEnumerator<IWidgetComponentViewModel>, WidgetComponentViewModelEnumerator>()
.AddWidgetTemplate<PrimaryWidgetViewModel>() .AddWidgetTemplate<PrimaryWidgetViewModel>()
@@ -1,11 +1,28 @@
namespace Hyperbar.Windows.Primary; namespace Hyperbar.Windows.Primary;
public class WidgetComponentViewModelEnumerator(PrimaryWidgetConfiguration configuration, public class WidgetComponentViewModelEnumerator(PrimaryWidgetConfiguration configuration,
IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory) : IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory,
ICache<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> cache) :
IViewModelEnumerator<IWidgetComponentViewModel> IViewModelEnumerator<IWidgetComponentViewModel>
{ {
public IEnumerable<IWidgetComponentViewModel?> Next() public IEnumerable<IWidgetComponentViewModel?> Next()
{ {
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)) foreach (PrimaryCommandConfiguration item in configuration.Commands.OrderBy(x => x.Order))
{ {
yield return factory.Create(item); yield return factory.Create(item);
@@ -2,6 +2,20 @@
namespace Hyperbar.Windows.Primary; namespace Hyperbar.Windows.Primary;
public class WidgetComponentViewModelProvider(ICache<Guid, IWidgetComponentViewModel> cache) :
IProvider<PrimaryCommandConfiguration, IWidgetComponentViewModel?>
{
public IWidgetComponentViewModel? Get(PrimaryCommandConfiguration value)
{
if (cache.TryGetValue(value.Id, out IWidgetComponentViewModel? viewModel))
{
return viewModel;
}
return default;
}
}
public class WidgetComponentViewModelFactory(IServiceFactory service, public class WidgetComponentViewModelFactory(IServiceFactory service,
IMediator mediator, IMediator mediator,
ICache<Guid, IWidgetComponentViewModel> cache) : ICache<Guid, IWidgetComponentViewModel> cache) :
@@ -12,6 +12,8 @@ public static class IServiceCollectionExtensions
public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services) public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services)
where TKey : where TKey :
notnull notnull
where TValue :
notnull
{ {
services.AddScoped<ICache<TKey, TValue>, Cache<TKey, TValue>>(); services.AddScoped<ICache<TKey, TValue>, Cache<TKey, TValue>>();
services.AddTransient(provider => provider.GetService<ICache<TKey, TValue>>()!.Select(x => x.Value)); services.AddTransient(provider => provider.GetService<ICache<TKey, TValue>>()!.Select(x => x.Value));
+9 -3
View File
@@ -30,8 +30,10 @@ public class Cache<TValue>(IDisposer disposer) :
public class Cache<TKey, TValue>(IDisposer disposer) : public class Cache<TKey, TValue>(IDisposer disposer) :
ICache<TKey, TValue> ICache<TKey, TValue>
where TKey : notnull where TKey :
where TValue : notnull notnull
where TValue :
notnull
{ {
private readonly ConcurrentDictionary<TKey, TValue> cache = new(); private readonly ConcurrentDictionary<TKey, TValue> cache = new();
@@ -40,9 +42,13 @@ public class Cache<TKey, TValue>(IDisposer disposer) :
{ {
cache.TryAdd(key, value); cache.TryAdd(key, value);
disposer.Add(value, Disposable.Create(() =>
{
Remove(key);
}));
disposer.Add(key, Disposable.Create(() => disposer.Add(key, Disposable.Create(() =>
{ {
disposer.Dispose(value);
Remove(key); Remove(key);
})); }));
} }
+5 -4
View File
@@ -1,6 +1,4 @@
using Microsoft.Extensions.DependencyInjection; namespace Hyperbar;
namespace Hyperbar;
public interface ICache<TValue> : public interface ICache<TValue> :
IEnumerable<TValue> IEnumerable<TValue>
@@ -14,7 +12,10 @@ public interface ICache<TValue> :
public interface ICache<TKey, TValue> : public interface ICache<TKey, TValue> :
IEnumerable<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>
where TKey : notnull where TKey :
notnull
where TValue :
notnull
{ {
void Add(TKey key, void Add(TKey key,
TValue value); TValue value);
+12
View File
@@ -10,3 +10,15 @@ public interface IFactory<TService>
{ {
TService? Create(); TService? Create();
} }
public interface IProvider<TParameter, TService>
{
TService? Get(TParameter value);
}
public interface IProvider<TService>
{
TService? Get();
}