reduce mediator duffs

This commit is contained in:
TheXamlGuy
2024-01-07 18:02:04 +00:00
parent 05150c2c03
commit 2e8af23784
24 changed files with 77 additions and 255 deletions
@@ -1,4 +1,3 @@
using Hyperbar.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -10,7 +9,6 @@ public class PrimaryWidgetProvider :
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
services.AddConfiguration(comtext.Configuration.GetSection(nameof(PrimaryWidgetConfiguration)),
PrimaryWidgetConfiguration.Defaults)
.AddTransient<WidgetComponentMappingFactory>()
.AddTransient(provider => provider.GetRequiredService<WidgetComponentMappingFactory>().Create())
.AddHandler<WidgetComponentMappingHandler>()
.AddWidgetTemplate<PrimaryWidgetViewModel>();
}
@@ -1,4 +1,4 @@
namespace Hyperbar.Windows.Primary;
namespace Hyperbar.Windows.Primary;
public class PrimaryWidgetViewModel :
ObservableCollectionViewModel<IWidgetComponentViewModel>,
@@ -0,0 +1,20 @@
namespace Hyperbar.Windows.Primary;
public class WidgetComponentMappingHandler(PrimaryWidgetConfiguration configuration,
IServiceFactory service,
IMediator mediator) :
IMappingHandler<PrimaryWidgetConfiguration, IEnumerable<IWidgetComponentViewModel>>
{
public IEnumerable<IWidgetComponentViewModel> Handle()
{
foreach (IPrimaryCommandConfiguration item in configuration)
{
if (item is KeyAcceleratorCommandConfiguration keyAcceleratorCommand)
{
yield return service.Create<WidgetButtonViewModel>(keyAcceleratorCommand.Icon, new Action(async () =>
await mediator.SendAsync(new KeyAcceleratorCommand(VirtualKey.LeftWindows))));
}
}
}
}
@@ -1,30 +0,0 @@
namespace Hyperbar.Windows.Primary;
public class WidgetComponentMappingFactory :
IMappingFactory<PrimaryWidgetConfiguration, IEnumerable<IWidgetComponentViewModel>>
{
private readonly PrimaryWidgetConfiguration configuration;
private readonly IServiceFactory service;
private readonly IMediator mediator;
public WidgetComponentMappingFactory(PrimaryWidgetConfiguration configuration,
IServiceFactory service,
IMediator mediator)
{
this.configuration = configuration;
this.service = service;
this.mediator = mediator;
}
public IEnumerable<IWidgetComponentViewModel> Create()
{
foreach (IPrimaryCommandConfiguration item in configuration)
{
if (item is KeyAcceleratorCommandConfiguration keyAcceleratorCommand)
{
yield return service.Create<WidgetButtonViewModel>(keyAcceleratorCommand.Icon, new Action(() =>
mediator.Send(new KeyAcceleratorCommand(VirtualKey.LeftWindows))));
}
}
}
}