diff --git a/Hyperbar.Windows.Primary/ConfigurationChangedHandler.cs b/Hyperbar.Windows.Primary/ConfigurationChangedHandler.cs new file mode 100644 index 0000000..fa1fc89 --- /dev/null +++ b/Hyperbar.Windows.Primary/ConfigurationChangedHandler.cs @@ -0,0 +1,11 @@ +namespace Hyperbar.Windows.Primary; + +public class ConfigurationChangedHandler : + INotificationHandler> +{ + public ValueTask Handle(ConfigurationChanged notification, + CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } +} diff --git a/Hyperbar.Windows.Primary/PrimaryWidgetProvider.cs b/Hyperbar.Windows.Primary/PrimaryWidgetProvider.cs index 1c4855a..845b13d 100644 --- a/Hyperbar.Windows.Primary/PrimaryWidgetProvider.cs +++ b/Hyperbar.Windows.Primary/PrimaryWidgetProvider.cs @@ -8,10 +8,10 @@ public class PrimaryWidgetProvider : { public void Create(HostBuilderContext comtext, IServiceCollection services) => services.AddConfiguration() - .AddTransient, WidgetComponentViewModelEnumerator>() + .AddTransient, ViewModelCache>() .AddTransient, WidgetComponentViewModelFactory>() + .AddTransient, WidgetComponentViewModelEnumerator>() .AddWidgetTemplate() - .AddNotificationPipeline, - CollectionChanged>>(); + .AddHandler(); } \ No newline at end of file diff --git a/Hyperbar.Windows.Primary/PrimaryWidgetViewModel.cs b/Hyperbar.Windows.Primary/PrimaryWidgetViewModel.cs index 81ca47c..7fb0be6 100644 --- a/Hyperbar.Windows.Primary/PrimaryWidgetViewModel.cs +++ b/Hyperbar.Windows.Primary/PrimaryWidgetViewModel.cs @@ -1,4 +1,5 @@  + namespace Hyperbar.Windows.Primary; public class PrimaryWidgetViewModel(ITemplateFactory templateFactory, diff --git a/Hyperbar.Windows.Primary/WidgetComponentViewModelCollectionFactory.cs b/Hyperbar.Windows.Primary/WidgetComponentViewModelFactory.cs similarity index 63% rename from Hyperbar.Windows.Primary/WidgetComponentViewModelCollectionFactory.cs rename to Hyperbar.Windows.Primary/WidgetComponentViewModelFactory.cs index ef0e3e7..d24ccf9 100644 --- a/Hyperbar.Windows.Primary/WidgetComponentViewModelCollectionFactory.cs +++ b/Hyperbar.Windows.Primary/WidgetComponentViewModelFactory.cs @@ -3,26 +3,34 @@ namespace Hyperbar.Windows.Primary; public class WidgetComponentViewModelFactory(IServiceFactory service, - IMediator mediator) : + IMediator mediator, + IViewModelCache cache) : IViewModelFactory { public async ValueTask CreateAsync(PrimaryCommandConfiguration value) { + IWidgetComponentViewModel? viewModel = null; + if (value is KeyAcceleratorCommandConfiguration keyAcceleratorCommand) { - return await ValueTask.FromResult(service.Create(keyAcceleratorCommand.Id, keyAcceleratorCommand.Icon, + viewModel = service.Create(keyAcceleratorCommand.Id, keyAcceleratorCommand.Icon, new RelayCommand(async () => await mediator.SendAsync(new KeyAcceleratorRequest((VirtualKey) keyAcceleratorCommand.Key, keyAcceleratorCommand.Modifiers? - .Select(modifier => (VirtualKey)modifier).ToArray()))))); + .Select(modifier => (VirtualKey)modifier).ToArray())))); } if (value is ProcessCommandConfiguration commandConfiguration) { - return await ValueTask.FromResult(service.Create(commandConfiguration.Id, + viewModel = service.Create(commandConfiguration.Id, commandConfiguration.Icon, new RelayCommand(async () => - await mediator.SendAsync(new ProcessRequest(commandConfiguration.Path))))); + await mediator.SendAsync(new ProcessRequest(commandConfiguration.Path)))); } - return default; + if (viewModel is not null) + { + cache.Add(value.Id, viewModel); + } + + return viewModel ?? default; } } \ No newline at end of file diff --git a/Hyperbar/Factories/IViewModelFactory.cs b/Hyperbar/Factories/IViewModelFactory.cs deleted file mode 100644 index fe380a6..0000000 --- a/Hyperbar/Factories/IViewModelFactory.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Hyperbar; - -public interface IViewModelFactory -{ - ValueTask CreateAsync(TIn value); -} \ No newline at end of file diff --git a/Hyperbar/Factories/IServiceFactory.cs b/Hyperbar/Lifecycles/IServiceFactory.cs similarity index 100% rename from Hyperbar/Factories/IServiceFactory.cs rename to Hyperbar/Lifecycles/IServiceFactory.cs diff --git a/Hyperbar/Templates/ITemplateFactory.cs b/Hyperbar/Lifecycles/ITemplateFactory.cs similarity index 100% rename from Hyperbar/Templates/ITemplateFactory.cs rename to Hyperbar/Lifecycles/ITemplateFactory.cs diff --git a/Hyperbar/Lifecycles/IViewModelFactory.cs b/Hyperbar/Lifecycles/IViewModelFactory.cs new file mode 100644 index 0000000..5fd2a78 --- /dev/null +++ b/Hyperbar/Lifecycles/IViewModelFactory.cs @@ -0,0 +1,30 @@ +namespace Hyperbar; + +public interface IViewModelFactory +{ + ValueTask CreateAsync(TParameter value); +} + +public interface IViewModelCache +{ + void Add(TKey key, TViewModel value); + + void Remove(TKey key); +} + +public class ViewModelCache : + IViewModelCache +{ + private readonly Dictionary cache = []; + + public void Add(TKey key, + TViewModel value) + { + cache.TryAdd(key, value); + } + + public void Remove(TKey key) + { + cache.Remove(key); + } +} \ No newline at end of file diff --git a/Hyperbar/Factories/ServiceFactory.cs b/Hyperbar/Lifecycles/ServiceFactory.cs similarity index 100% rename from Hyperbar/Factories/ServiceFactory.cs rename to Hyperbar/Lifecycles/ServiceFactory.cs