From d1b57d5d161f1c3d8ecbff1aeb8aa98fdc63c095 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Tue, 23 Jan 2024 23:14:51 +0000 Subject: [PATCH] get HB widgets showing up again --- .../WidgetComponentFactory.cs | 12 ++++----- Hyperbar.Windows/App.xaml.cs | 3 ++- .../Views/WidgetContainerView.xaml | 6 ++--- .../IServiceCollectionExtensions.cs | 4 ++- Hyperbar/Views/WidgetBarViewModel.cs | 6 ++--- Hyperbar/Views/WidgetContainerViewModel.cs | 9 ++----- Hyperbar/Widgets/IWidgetHost.cs | 2 ++ Hyperbar/Widgets/WidgetBuilder.cs | 9 ------- Hyperbar/Widgets/WidgetContainerFactory.cs | 17 ++++++++++++ Hyperbar/Widgets/WidgetHost.cs | 16 +++++++----- Hyperbar/Widgets/WidgetHostHander.cs | 20 -------------- Hyperbar/Widgets/WidgetHostHandler.cs | 26 +++++++++++++++++++ 12 files changed, 73 insertions(+), 57 deletions(-) create mode 100644 Hyperbar/Widgets/WidgetContainerFactory.cs delete mode 100644 Hyperbar/Widgets/WidgetHostHander.cs create mode 100644 Hyperbar/Widgets/WidgetHostHandler.cs diff --git a/Hyperbar.Windows.Primary/WidgetComponentFactory.cs b/Hyperbar.Windows.Primary/WidgetComponentFactory.cs index 4ad2509..574112f 100644 --- a/Hyperbar.Windows.Primary/WidgetComponentFactory.cs +++ b/Hyperbar.Windows.Primary/WidgetComponentFactory.cs @@ -2,7 +2,7 @@ namespace Hyperbar.Windows.Primary; -public class WidgetComponentFactory(IServiceFactory service, +public class WidgetComponentFactory(IServiceFactory factory, IMediator mediator, ICache cache) : IFactory @@ -13,7 +13,7 @@ public class WidgetComponentFactory(IServiceFactory service, if (configuration is KeyAcceleratorCommandConfiguration keyAcceleratorCommandConfiguration) { - viewModel = service.Create(keyAcceleratorCommandConfiguration.Id, + viewModel = factory.Create(keyAcceleratorCommandConfiguration.Id, keyAcceleratorCommandConfiguration.Text, keyAcceleratorCommandConfiguration.Icon, new RelayCommand(async () => await mediator.SendAsync(new KeyAccelerator((VirtualKey) keyAcceleratorCommandConfiguration.Key, keyAcceleratorCommandConfiguration.Modifiers? @@ -32,14 +32,14 @@ public class WidgetComponentFactory(IServiceFactory service, if (childCommandConfiguration is ProcessCommandConfiguration childProcessCommandConfiguration) { - childViewModel = service.Create(childProcessCommandConfiguration.Id, + childViewModel = factory.Create(childProcessCommandConfiguration.Id, childProcessCommandConfiguration.Icon, childProcessCommandConfiguration.Text, new RelayCommand(async () => await mediator.SendAsync(new StartProcess(childProcessCommandConfiguration.Path)))); } if (childCommandConfiguration is KeyAcceleratorCommandConfiguration childKeyAcceleratorCommandConfiguration) { - childViewModel = service.Create(childKeyAcceleratorCommandConfiguration.Id, + childViewModel = factory.Create(childKeyAcceleratorCommandConfiguration.Id, childKeyAcceleratorCommandConfiguration.Text, childKeyAcceleratorCommandConfiguration.Icon, new RelayCommand(async () => await mediator.SendAsync(new KeyAccelerator((VirtualKey)childKeyAcceleratorCommandConfiguration.Key, @@ -53,14 +53,14 @@ public class WidgetComponentFactory(IServiceFactory service, } } - viewModel = service.Create(childViewModels, + viewModel = factory.Create(childViewModels, processCommandConfiguration.Id, processCommandConfiguration.Text, processCommandConfiguration.Icon, new RelayCommand(async () => await mediator.SendAsync(new StartProcess(processCommandConfiguration.Path)))); } else { - viewModel = service.Create(processCommandConfiguration.Id, + viewModel = factory.Create(processCommandConfiguration.Id, processCommandConfiguration.Text, processCommandConfiguration.Icon, new RelayCommand(async () => await mediator.SendAsync(new StartProcess(processCommandConfiguration.Path)))); } diff --git a/Hyperbar.Windows/App.xaml.cs b/Hyperbar.Windows/App.xaml.cs index c7ad4b5..4059fa5 100644 --- a/Hyperbar.Windows/App.xaml.cs +++ b/Hyperbar.Windows/App.xaml.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.UI.Xaml; using System; using System.Reflection; +using Windows.Media.Control; namespace Hyperbar.Windows; @@ -46,6 +47,7 @@ public partial class App : services.AddSingleton(); services.AddContentTemplate(); + services.AddContentTemplate(); services.AddTransient>(provider => new ProxyServiceCollection(services => @@ -59,7 +61,6 @@ public partial class App : services.AddTransient(); - services.AddContentTemplate(); services.AddContentTemplate(); services.AddContentTemplate(); })); diff --git a/Hyperbar.Windows/Views/WidgetContainerView.xaml b/Hyperbar.Windows/Views/WidgetContainerView.xaml index b05e007..fbdf920 100644 --- a/Hyperbar.Windows/Views/WidgetContainerView.xaml +++ b/Hyperbar.Windows/Views/WidgetContainerView.xaml @@ -10,10 +10,10 @@ + Fill="Red" /> (); services.AddHandler(); services.AddHandler(); - services.AddHandler(); + services.AddHandler(); + + services.AddTransient, WidgetContainerFactory>(); return services; } diff --git a/Hyperbar/Views/WidgetBarViewModel.cs b/Hyperbar/Views/WidgetBarViewModel.cs index 2cf9398..1594e9a 100644 --- a/Hyperbar/Views/WidgetBarViewModel.cs +++ b/Hyperbar/Views/WidgetBarViewModel.cs @@ -1,11 +1,11 @@ namespace Hyperbar; +[NotificationHandler(nameof(WidgetBarViewModel))] public partial class WidgetBarViewModel(ITemplateFactory templateFactory, IServiceFactory serviceFactory, IMediator mediator, - IDisposer disposer, - IEnumerable items) : - ObservableCollectionViewModel(serviceFactory, mediator, disposer, items), + IDisposer disposer) : + ObservableCollectionViewModel(serviceFactory, mediator, disposer), ITemplatedViewModel { public ITemplateFactory TemplateFactory => templateFactory; diff --git a/Hyperbar/Views/WidgetContainerViewModel.cs b/Hyperbar/Views/WidgetContainerViewModel.cs index d5b9250..d1a5432 100644 --- a/Hyperbar/Views/WidgetContainerViewModel.cs +++ b/Hyperbar/Views/WidgetContainerViewModel.cs @@ -7,15 +7,10 @@ public partial class WidgetContainerViewModel(ITemplateFactory templateFactory, IServiceFactory serviceFactory, IMediator mediator, IDisposer disposer, - IEnumerable items, - Guid id, - bool alternate) : - ObservableCollectionViewModel(serviceFactory, mediator, disposer, items), + Guid id) : + ObservableCollectionViewModel(serviceFactory, mediator, disposer), ITemplatedViewModel { - [ObservableProperty] - private bool alternate = alternate; - [ObservableProperty] private Guid id = id; diff --git a/Hyperbar/Widgets/IWidgetHost.cs b/Hyperbar/Widgets/IWidgetHost.cs index 7e2c03d..0d01f41 100644 --- a/Hyperbar/Widgets/IWidgetHost.cs +++ b/Hyperbar/Widgets/IWidgetHost.cs @@ -3,5 +3,7 @@ public interface IWidgetHost : IInitializer { + WidgetConfiguration Configuration { get; } + IServiceProvider Services { get; } } diff --git a/Hyperbar/Widgets/WidgetBuilder.cs b/Hyperbar/Widgets/WidgetBuilder.cs index 4b81259..91f897a 100644 --- a/Hyperbar/Widgets/WidgetBuilder.cs +++ b/Hyperbar/Widgets/WidgetBuilder.cs @@ -50,15 +50,6 @@ public class WidgetBuilder(TConfiguration configuration) : public IWidgetHost Build() { IHost host = hostBuilder.Build(); - - //if (host.Services.GetRequiredService>() - // is IConfigurationInitializer configurationInitializer) - //{ - // configurationInitializer.InitializeAsync() - // .GetAwaiter() - // .GetResult(); - //} - return (IWidgetHost)ActivatorUtilities.CreateInstance(host.Services, typeof(WidgetHost), host); } diff --git a/Hyperbar/Widgets/WidgetContainerFactory.cs b/Hyperbar/Widgets/WidgetContainerFactory.cs new file mode 100644 index 0000000..2ea9953 --- /dev/null +++ b/Hyperbar/Widgets/WidgetContainerFactory.cs @@ -0,0 +1,17 @@ +using Microsoft.Extensions.DependencyInjection; +namespace Hyperbar; + +public class WidgetContainerFactory(IServiceFactory factory) : + IFactory +{ + public WidgetContainerViewModel? Create(IWidgetHost value) + { + if (value.Services.GetServices() is + IEnumerable viewModels) + { + return factory.Create(value.Configuration.Id); + } + + return default; + } +} diff --git a/Hyperbar/Widgets/WidgetHost.cs b/Hyperbar/Widgets/WidgetHost.cs index 9e50e49..e75f7d5 100644 --- a/Hyperbar/Widgets/WidgetHost.cs +++ b/Hyperbar/Widgets/WidgetHost.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace Hyperbar; @@ -27,6 +28,8 @@ public class WidgetHost : mediator.Subscribe(this); } + public WidgetConfiguration Configuration => host.Services.GetRequiredService(); + public IServiceProvider Services => host.Services; public async Task Handle(Changed notification, @@ -45,15 +48,14 @@ public class WidgetHost : private async Task StartAsync() { + if (proxyMediator.Proxy is IMediator mediator) + { + await mediator.PublishAsync(new Started(this)); + } + foreach (IInitializer initializer in initializers) { await initializer.InitializeAsync(); } - - - //if (proxyMediator.Proxy is IMediator mediator) - //{ - // await mediator.PublishAsync(new Started(this)); - //} } } \ No newline at end of file diff --git a/Hyperbar/Widgets/WidgetHostHander.cs b/Hyperbar/Widgets/WidgetHostHander.cs deleted file mode 100644 index 4ff8702..0000000 --- a/Hyperbar/Widgets/WidgetHostHander.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Hyperbar; - -public class WidgetHostHander : - INotificationHandler>, - INotificationHandler> -{ - public Task Handle(Started notification, - CancellationToken cancellationToken) - { - // throw new NotImplementedException(); - - return Task.CompletedTask; - } - - public Task Handle(Stopped notification, - CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } -} \ No newline at end of file diff --git a/Hyperbar/Widgets/WidgetHostHandler.cs b/Hyperbar/Widgets/WidgetHostHandler.cs new file mode 100644 index 0000000..7503e4c --- /dev/null +++ b/Hyperbar/Widgets/WidgetHostHandler.cs @@ -0,0 +1,26 @@ +namespace Hyperbar; + +public class WidgetHostHandler(IMediator mediator, + IFactory factory) : + INotificationHandler>, + INotificationHandler> +{ + public async Task Handle(Started notification, + CancellationToken cancellationToken) + { + if (notification.Value is IWidgetHost host) + { + if (factory.Create(host) is WidgetContainerViewModel containerViewModel) + { + await mediator.PublishAsync(new Created(containerViewModel), nameof(WidgetBarViewModel), + cancellationToken); + } + } + } + + public Task Handle(Stopped notification, + CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } +} \ No newline at end of file