From a32289316664b3207ae90a8daad0907aae5fe437 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Thu, 25 Jan 2024 20:33:55 +0000 Subject: [PATCH] lots of tidying up --- .../IServiceCollectionExtensions.cs | 4 +- Hyperbar.Widget/WidgetBuilder.cs | 5 +- Hyperbar.Widget/WidgetContainerFactory.cs | 11 +--- Hyperbar.Widget/WidgetContainerViewModel.cs | 1 + ...umeratorHandler.cs => WidgetEnumerator.cs} | 2 +- Hyperbar.Widget/WidgetHost.cs | 11 ++-- Hyperbar.Widget/WidgetHostHandler.cs | 16 ++++-- Hyperbar.Widget/WidgetViewModelEnumerator.cs | 20 +++++-- .../Views/MediaInformationViewModel.cs | 7 +-- Hyperbar.Windows.Primary/PrimaryWidget.cs | 2 +- .../PrimaryWidgetViewModel.cs | 5 +- ... => WidgetComponentViewModelEnumerator.cs} | 13 +++-- Hyperbar.Windows/App.xaml.cs | 20 ++++--- Hyperbar.Windows/Lifecycles/Dispatcher.cs | 9 +-- .../Views/WidgetContainerView.xaml | 9 ++- Hyperbar.Windows/Views/WidgetView.xaml | 33 +++++++---- .../Configuration/ConfigurationInitializer.cs | 8 --- .../ObservableCollectionViewModel.cs | 56 ++++++------------- Hyperbar/Lifecycles/ObservableViewModel.cs | 4 +- Hyperbar/Mediators/Mediator.cs | 2 +- 20 files changed, 115 insertions(+), 123 deletions(-) rename Hyperbar.Widget/{WidgetEnumeratorHandler.cs => WidgetEnumerator.cs} (93%) rename Hyperbar.Windows.Primary/{WidgetComponentEnumerationHandler.cs => WidgetComponentViewModelEnumerator.cs} (64%) diff --git a/Hyperbar.Widget/IServiceCollectionExtensions.cs b/Hyperbar.Widget/IServiceCollectionExtensions.cs index 5470f3b..573b119 100644 --- a/Hyperbar.Widget/IServiceCollectionExtensions.cs +++ b/Hyperbar.Widget/IServiceCollectionExtensions.cs @@ -10,13 +10,11 @@ public static class IServiceCollectionExtensions services.AddTransient(); services.AddTransient, WidgetFactory>(); - services.AddHandler(); + services.AddHandler(); services.AddHandler(); services.AddHandler(); services.AddHandler(); - services.AddTransient, WidgetContainerFactory>(); - return services; } diff --git a/Hyperbar.Widget/WidgetBuilder.cs b/Hyperbar.Widget/WidgetBuilder.cs index 7ba795e..c1004e2 100644 --- a/Hyperbar.Widget/WidgetBuilder.cs +++ b/Hyperbar.Widget/WidgetBuilder.cs @@ -20,6 +20,8 @@ public class WidgetBuilder(TConfiguration configuration) : }) .ConfigureServices((context, services) => { + services.AddSingleton(); + services.AddScoped(provider => new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!))); @@ -50,8 +52,7 @@ public class WidgetBuilder(TConfiguration configuration) : public IWidgetHost Build() { IHost host = hostBuilder.Build(); - return (IWidgetHost)ActivatorUtilities.CreateInstance(host.Services, - typeof(WidgetHost), host); + return host.Services.GetRequiredService(); } public IWidgetBuilder ConfigureServices(Action configureDelegate) diff --git a/Hyperbar.Widget/WidgetContainerFactory.cs b/Hyperbar.Widget/WidgetContainerFactory.cs index 437ad96..2bfb525 100644 --- a/Hyperbar.Widget/WidgetContainerFactory.cs +++ b/Hyperbar.Widget/WidgetContainerFactory.cs @@ -1,17 +1,10 @@ -using Microsoft.Extensions.DependencyInjection; -namespace Hyperbar.Widget; +namespace Hyperbar.Widget; 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; + return factory.Create(value.Configuration.Id); } } diff --git a/Hyperbar.Widget/WidgetContainerViewModel.cs b/Hyperbar.Widget/WidgetContainerViewModel.cs index 83ea805..5f198ce 100644 --- a/Hyperbar.Widget/WidgetContainerViewModel.cs +++ b/Hyperbar.Widget/WidgetContainerViewModel.cs @@ -3,6 +3,7 @@ using CommunityToolkit.Mvvm.ComponentModel; namespace Hyperbar.Widget; +[NotificationHandler(nameof(WidgetContainerViewModel))] public partial class WidgetContainerViewModel(ITemplateFactory templateFactory, IServiceFactory serviceFactory, IMediator mediator, diff --git a/Hyperbar.Widget/WidgetEnumeratorHandler.cs b/Hyperbar.Widget/WidgetEnumerator.cs similarity index 93% rename from Hyperbar.Widget/WidgetEnumeratorHandler.cs rename to Hyperbar.Widget/WidgetEnumerator.cs index 20d36db..842ec3f 100644 --- a/Hyperbar.Widget/WidgetEnumeratorHandler.cs +++ b/Hyperbar.Widget/WidgetEnumerator.cs @@ -4,7 +4,7 @@ using System.Runtime.Loader; namespace Hyperbar.Widget; -public class WidgetEnumeratorHandler(IHostEnvironment hostEnvironment, +public class WidgetEnumerator(IHostEnvironment hostEnvironment, IMediator mediator) : INotificationHandler> { diff --git a/Hyperbar.Widget/WidgetHost.cs b/Hyperbar.Widget/WidgetHost.cs index e3c9b8f..f5b30ec 100644 --- a/Hyperbar.Widget/WidgetHost.cs +++ b/Hyperbar.Widget/WidgetHost.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; namespace Hyperbar.Widget; @@ -8,18 +7,18 @@ public class WidgetHost : IWidgetHost { private readonly IConfigurationInitializer configurationInitializer; - private readonly IHost host; + private readonly IServiceProvider services; private readonly IEnumerable initializers; private readonly IMediator mediator; private readonly IProxyService proxyMediator; - public WidgetHost(IHost host, + public WidgetHost(IServiceProvider services, IMediator mediator, IEnumerable initializers, IProxyService proxyMediator, IConfigurationInitializer configurationInitializer) { - this.host = host; + this.services = services; this.mediator = mediator; this.initializers = initializers; this.proxyMediator = proxyMediator; @@ -28,9 +27,9 @@ public class WidgetHost : mediator.Subscribe(this); } - public WidgetConfiguration Configuration => host.Services.GetRequiredService(); + public WidgetConfiguration Configuration => services.GetRequiredService(); - public IServiceProvider Services => host.Services; + public IServiceProvider Services => services; public async Task Handle(Changed notification, CancellationToken cancellationToken) diff --git a/Hyperbar.Widget/WidgetHostHandler.cs b/Hyperbar.Widget/WidgetHostHandler.cs index 0c5d59a..bcd112e 100644 --- a/Hyperbar.Widget/WidgetHostHandler.cs +++ b/Hyperbar.Widget/WidgetHostHandler.cs @@ -1,7 +1,8 @@ -namespace Hyperbar.Widget; +using Microsoft.Extensions.DependencyInjection; -public class WidgetHostHandler(IMediator mediator, - IFactory factory) : +namespace Hyperbar.Widget; + +public class WidgetHostHandler(IMediator mediator) : INotificationHandler>, INotificationHandler> { @@ -10,10 +11,13 @@ public class WidgetHostHandler(IMediator mediator, { if (notification.Value is IWidgetHost host) { - if (factory.Create(host) is WidgetContainerViewModel containerViewModel) + if (host.Services.GetRequiredService>() is { } factory) { - await mediator.PublishAsync(new Created(containerViewModel), - nameof(WidgetBarViewModel), cancellationToken); + if (factory.Create(host) is WidgetContainerViewModel containerViewModel) + { + await mediator.PublishAsync(new Created(containerViewModel), + nameof(WidgetBarViewModel), cancellationToken); + } } } } diff --git a/Hyperbar.Widget/WidgetViewModelEnumerator.cs b/Hyperbar.Widget/WidgetViewModelEnumerator.cs index e7d243a..d3ac785 100644 --- a/Hyperbar.Widget/WidgetViewModelEnumerator.cs +++ b/Hyperbar.Widget/WidgetViewModelEnumerator.cs @@ -1,11 +1,21 @@ -namespace Hyperbar.Widget; +using Microsoft.Extensions.DependencyInjection; -public class WidgetViewModelEnumerator : +namespace Hyperbar.Widget; + +public class WidgetViewModelEnumerator(IWidgetHost host, + IMediator mediator) : INotificationHandler> { - public Task Handle(Enumerate notification, + public async Task Handle(Enumerate notification, CancellationToken cancellationToken) { - throw new NotImplementedException(); - } + if (host.Services.GetServices() is IEnumerable viewModels) + { + foreach (IWidgetViewModel viewModel in viewModels) + { + await mediator.PublishAsync(new Created(viewModel), + nameof(WidgetContainerViewModel), cancellationToken); + } + } + } } \ No newline at end of file diff --git a/Hyperbar.Windows.MediaController/Views/MediaInformationViewModel.cs b/Hyperbar.Windows.MediaController/Views/MediaInformationViewModel.cs index 7782320..801bee0 100644 --- a/Hyperbar.Windows.MediaController/Views/MediaInformationViewModel.cs +++ b/Hyperbar.Windows.MediaController/Views/MediaInformationViewModel.cs @@ -1,7 +1,5 @@ using CommunityToolkit.Mvvm.ComponentModel; -using CommunityToolkit.Mvvm.Input; using Hyperbar.Widget; -using System.Windows.Input; namespace Hyperbar.Windows.MediaController; @@ -19,9 +17,6 @@ public partial class MediaInformationViewModel(IServiceFactory serviceFactory, [ObservableProperty] private string? title; - public ICommand Initialize => - new AsyncRelayCommand(InitializeAsync); - public Task Handle(Changed notification, CancellationToken cancellationToken) { @@ -34,6 +29,6 @@ public partial class MediaInformationViewModel(IServiceFactory serviceFactory, return Task.CompletedTask; } - public async Task InitializeAsync() => + public override async Task InitializeAsync() => await Mediator.PublishAsync>(); } diff --git a/Hyperbar.Windows.Primary/PrimaryWidget.cs b/Hyperbar.Windows.Primary/PrimaryWidget.cs index 8fd4f5b..a47c21f 100644 --- a/Hyperbar.Windows.Primary/PrimaryWidget.cs +++ b/Hyperbar.Windows.Primary/PrimaryWidget.cs @@ -27,8 +27,8 @@ public class PrimaryWidget : .AddCache() .AddTransient, WidgetComponentProvider>() .AddTransient, WidgetComponentFactory>() - .AddTransient, WidgetComponentEnumerationHandler>() .AddWidgetTemplate() + .AddHandler() .AddHandler(); }); } \ No newline at end of file diff --git a/Hyperbar.Windows.Primary/PrimaryWidgetViewModel.cs b/Hyperbar.Windows.Primary/PrimaryWidgetViewModel.cs index d6c507e..5f161d4 100644 --- a/Hyperbar.Windows.Primary/PrimaryWidgetViewModel.cs +++ b/Hyperbar.Windows.Primary/PrimaryWidgetViewModel.cs @@ -6,9 +6,8 @@ namespace Hyperbar.Windows.Primary; public class PrimaryWidgetViewModel(ITemplateFactory templateFactory, IServiceFactory serviceFactory, IMediator mediator, - IDisposer disposer, - IEnumerator enumerator) : - ObservableCollectionViewModel(serviceFactory, mediator, disposer, enumerator), + IDisposer disposer) : + ObservableCollectionViewModel(serviceFactory, mediator, disposer), IWidgetViewModel, ITemplatedViewModel { diff --git a/Hyperbar.Windows.Primary/WidgetComponentEnumerationHandler.cs b/Hyperbar.Windows.Primary/WidgetComponentViewModelEnumerator.cs similarity index 64% rename from Hyperbar.Windows.Primary/WidgetComponentEnumerationHandler.cs rename to Hyperbar.Windows.Primary/WidgetComponentViewModelEnumerator.cs index e73375a..5f75bfb 100644 --- a/Hyperbar.Windows.Primary/WidgetComponentEnumerationHandler.cs +++ b/Hyperbar.Windows.Primary/WidgetComponentViewModelEnumerator.cs @@ -2,12 +2,13 @@ namespace Hyperbar.Windows.Primary; -public class WidgetComponentEnumerationHandler(PrimaryWidgetConfiguration configuration, +public class WidgetComponentViewModelEnumerator(PrimaryWidgetConfiguration configuration, + IMediator mediator, IFactory factory, ICache<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> cache) : - IEnumerator + INotificationHandler> { - public IEnumerable Get() + public async Task Handle(Enumerate notification, CancellationToken cancellationToken) { Stack<(Guid, List)> stack = new(); stack.Push((Guid.Empty, configuration.Commands)); @@ -27,7 +28,11 @@ public class WidgetComponentEnumerationHandler(PrimaryWidgetConfiguration config foreach (PrimaryCommandConfiguration item in configuration.Commands.OrderBy(x => x.Order)) { - yield return factory.Create(item); + if (factory.Create(item) is IWidgetComponentViewModel viewModel) + { + await mediator.PublishAsync(new Created(viewModel), nameof(PrimaryWidgetViewModel), + cancellationToken); + } } } } diff --git a/Hyperbar.Windows/App.xaml.cs b/Hyperbar.Windows/App.xaml.cs index 046c19b..c36760e 100644 --- a/Hyperbar.Windows/App.xaml.cs +++ b/Hyperbar.Windows/App.xaml.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.UI.Dispatching; using Microsoft.UI.Xaml; using System; using System.Reflection; @@ -21,7 +22,6 @@ public partial class App : protected override async void OnLaunched(LaunchActivatedEventArgs args) { base.OnLaunched(args); - IHost? host = new HostBuilder() .UseContentRoot(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Assembly.GetEntryAssembly()?.GetName().Name!), true) @@ -32,10 +32,10 @@ public partial class App : .ConfigureServices((context, services) => { services.AddDefault(); - + services.AddWidget(); services.AddHostedService(); - services.AddSingleton(); + services.AddSingleton(new Dispatcher(DispatcherQueue.GetForCurrentThread())); services.AddTransient(); services.AddHandler(); @@ -48,20 +48,26 @@ public partial class App : services.AddSingleton(); services.AddContentTemplate(); - services.AddContentTemplate(); services.AddTransient>(provider => new ProxyServiceCollection(services => { - services.AddSingleton(); - services.AddTransient(); - services.AddScoped(); + services.AddSingleton(new Dispatcher(DispatcherQueue.GetForCurrentThread())); + services.AddTransient, + WidgetContainerFactory>(); + + services.AddTransient(); + + services.AddScoped(); services.AddHandler(); services.AddHandler(); + services.AddHandler(); + services.AddTransient(); + services.AddContentTemplate(); services.AddContentTemplate(); services.AddContentTemplate(); })); diff --git a/Hyperbar.Windows/Lifecycles/Dispatcher.cs b/Hyperbar.Windows/Lifecycles/Dispatcher.cs index e772e7d..d76e1ac 100644 --- a/Hyperbar.Windows/Lifecycles/Dispatcher.cs +++ b/Hyperbar.Windows/Lifecycles/Dispatcher.cs @@ -3,12 +3,9 @@ using Microsoft.UI.Dispatching; namespace Hyperbar.Windows; -public class Dispatcher : +public class Dispatcher(DispatcherQueue dispatcherQueue) : IDispatcher { - private DispatcherQueue dispatcherQueue; - - public Dispatcher() => dispatcherQueue = DispatcherQueue.GetForCurrentThread(); - - public async Task InvokeAsync(Action action) => await dispatcherQueue.EnqueueAsync(action.Invoke); + public async Task InvokeAsync(Action action) => + await dispatcherQueue.EnqueueAsync(action.Invoke); } diff --git a/Hyperbar.Windows/Views/WidgetContainerView.xaml b/Hyperbar.Windows/Views/WidgetContainerView.xaml index fbdf920..75561cb 100644 --- a/Hyperbar.Windows/Views/WidgetContainerView.xaml +++ b/Hyperbar.Windows/Views/WidgetContainerView.xaml @@ -3,6 +3,8 @@ x:Class="Hyperbar.Windows.WidgetContainerView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:interactions="using:Microsoft.Xaml.Interactions.Core" + xmlns:interactivity="using:Microsoft.Xaml.Interactivity" xmlns:ui="using:Hyperbar.Windows.UI"> @@ -10,7 +12,7 @@ @@ -23,6 +25,11 @@ + + + + + diff --git a/Hyperbar.Windows/Views/WidgetView.xaml b/Hyperbar.Windows/Views/WidgetView.xaml index 3ef7310..7cdf03c 100644 --- a/Hyperbar.Windows/Views/WidgetView.xaml +++ b/Hyperbar.Windows/Views/WidgetView.xaml @@ -3,17 +3,26 @@ x:Class="Hyperbar.Windows.WidgetView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:interactions="using:Microsoft.Xaml.Interactions.Core" + xmlns:interactivity="using:Microsoft.Xaml.Interactivity" xmlns:ui="using:Hyperbar.Windows.UI"> - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Hyperbar/Configuration/ConfigurationInitializer.cs b/Hyperbar/Configuration/ConfigurationInitializer.cs index e7dd978..69b224f 100644 --- a/Hyperbar/Configuration/ConfigurationInitializer.cs +++ b/Hyperbar/Configuration/ConfigurationInitializer.cs @@ -9,16 +9,8 @@ public class ConfigurationInitializer(IConfigurationMonitor : where TItem : IDisposable { - public ObservableCollection collection = []; - private readonly IEnumerator? enumerator; + private readonly ObservableCollection collection = new(); public ObservableCollectionViewModel(IServiceFactory serviceFactory, IMediator mediator, @@ -33,33 +34,6 @@ public partial class ObservableCollectionViewModel : collection.CollectionChanged += OnCollectionChanged; } - public ObservableCollectionViewModel(IServiceFactory serviceFactory, - IMediator mediator, - IDisposer disposer, - IEnumerator enumerator) - { - ServiceFactory = serviceFactory; - Mediator = mediator; - Disposer = disposer; - - this.enumerator = enumerator; - - mediator.Subscribe(this); - - collection.CollectionChanged += OnCollectionChanged; - - if (enumerator is not null) - { - foreach (TItem? item in enumerator.Get()) - { - if (item is not null) - { - Add(item); - } - } - } - } - public ObservableCollectionViewModel(IServiceFactory serviceFactory, IMediator mediator, IDisposer disposer, @@ -80,6 +54,9 @@ public partial class ObservableCollectionViewModel : public int Count => collection.Count; + public ICommand Initialize => + new AsyncRelayCommand(InitializeAsync); + bool IList.IsFixedSize => false; bool ICollection.IsReadOnly => false; @@ -101,10 +78,7 @@ public partial class ObservableCollectionViewModel : public TItem this[int index] { get => collection[index]; - set - { - SetItem(index, value); - } + set => SetItem(index, value); } object? IList.this[int index] @@ -187,24 +161,25 @@ public partial class ObservableCollectionViewModel : public void Clear() => ClearItems(); - public bool Contains(TItem item) => + public bool Contains(TItem item) => collection.Contains(item); bool IList.Contains(object? value) => IsCompatibleObject(value) && Contains((TItem)value!); - public void CopyTo(TItem[] array, int index) => + public void CopyTo(TItem[] array, int index) => collection.CopyTo(array, index); void ICollection.CopyTo(Array array, int index) => collection.CopyTo((TItem[])array, index); - public void Dispose() => Disposer.Dispose(this); + public void Dispose() => + Disposer.Dispose(this); public System.Collections.Generic.IEnumerator GetEnumerator() => collection.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => + IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)collection).GetEnumerator(); public Task Handle(Removed notification, @@ -263,10 +238,13 @@ public partial class ObservableCollectionViewModel : public int IndexOf(TItem item) => collection.IndexOf(item); - int IList.IndexOf(object? value) => - IsCompatibleObject(value) ? + int IList.IndexOf(object? value) => + IsCompatibleObject(value) ? IndexOf((TItem)value!) : -1; + public virtual async Task InitializeAsync() => + await Mediator.PublishAsync>(); + public void Insert(int index, TItem item) => InsertItem(index, item); diff --git a/Hyperbar/Lifecycles/ObservableViewModel.cs b/Hyperbar/Lifecycles/ObservableViewModel.cs index 56a747a..ed78950 100644 --- a/Hyperbar/Lifecycles/ObservableViewModel.cs +++ b/Hyperbar/Lifecycles/ObservableViewModel.cs @@ -2,9 +2,7 @@ namespace Hyperbar; -public class ObservableViewModel(IServiceFactory serviceFactory, - IMediator mediator, - IDisposer disposer) : +public class ObservableViewModel(IDisposer disposer) : ObservableObject, IDisposable { diff --git a/Hyperbar/Mediators/Mediator.cs b/Hyperbar/Mediators/Mediator.cs index 505ef5f..7e63619 100644 --- a/Hyperbar/Mediators/Mediator.cs +++ b/Hyperbar/Mediators/Mediator.cs @@ -36,7 +36,7 @@ public class Mediator(IServiceProvider provider, key, cancellationToken); } - public Task PublishAsync(TNotification notification, + public Task PublishAsync(TNotification notification, Func, Task> marshal, object? key = null, CancellationToken cancellationToken = default)