From 221c46218f647be76cbcf79b1846c97258c38d21 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Fri, 2 Feb 2024 21:11:13 +0000 Subject: [PATCH] moerfixes --- .../MediaButtonViewModel.cs | 7 +- .../MediaController.cs | 10 ++- .../MediaControllerService.cs | 2 +- .../MediaControllerWidgetView.xaml | 5 +- .../MediaInformation.cs | 2 +- .../MediaInformationViewModel.cs | 4 +- .../IServiceCollectionExtensions.cs | 4 +- .../WidgetResourceInitializer.cs | 2 +- .../WidgetXamlMetadataInitializer.cs | 2 +- .../IServiceCollectionExtensions.cs | 2 +- Hyperbar.Widget/WidgetExtensionInitializer.cs | 2 +- Hyperbar.Widget/WidgetHost.cs | 19 +++-- Hyperbar.Widget/WidgetHostHandler.cs | 6 +- Hyperbar.Widget/WidgetMonitor.cs | 2 +- Hyperbar.Widget/WidgetService.cs | 6 +- Hyperbar.Windows/App.xaml.cs | 3 +- Hyperbar.Windows/Lifecycles/AppInitializer.cs | 2 +- .../Configuration/ConfigurationInitializer.cs | 2 +- .../Configuration/IConfigurationMonitor.cs | 2 +- .../IServiceCollectionExtensions.cs | 2 +- Hyperbar/Lifecycles/AppService.cs | 4 +- Hyperbar/Lifecycles/IInitialization.cs | 2 +- Hyperbar/Lifecycles/IInitializer.cs | 2 +- .../ObservableCollectionViewModel.cs | 72 +++++++++++-------- 24 files changed, 97 insertions(+), 69 deletions(-) diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs index a5e7a18..51d5316 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs @@ -13,8 +13,7 @@ public partial class MediaButtonViewModel(IServiceFactory serviceFactory, string? text = null, string? icon = null, RelayCommand? command = null) : - WidgetButtonViewModel(serviceFactory, mediator, disposer, templateFactory, guid, text, icon, command), - IInitialization + WidgetButtonViewModel(serviceFactory, mediator, disposer, templateFactory, guid, text, icon, command) { [ObservableProperty] private PlaybackButtonType playbackButtonType = playbackButtonType; @@ -38,6 +37,8 @@ public partial class MediaButtonViewModel(IServiceFactory serviceFactory, return Task.CompletedTask; } - //public override async Task InitializeAsync() => + //public override Task OnInitializeAsync() + //{ // await Mediator.PublishAsync>(); + //} } diff --git a/Hyperbar.Widget.MediaController.Windows/MediaController.cs b/Hyperbar.Widget.MediaController.Windows/MediaController.cs index 773d25a..82411ce 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaController.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaController.cs @@ -97,9 +97,15 @@ public class MediaController : GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties = await session.TryGetMediaPropertiesAsync(); - IRandomAccessStreamWithContentType randomAccessStream = await mediaProperties.Thumbnail.OpenReadAsync(); + IRandomAccessStreamWithContentType? randomAccessStream = null; + if (mediaProperties.Thumbnail is not null) + { + randomAccessStream = + await mediaProperties.Thumbnail.OpenReadAsync(); + } + await mediator.PublishAsync(new Changed(new MediaInformation(mediaProperties.Title, - mediaProperties.Artist, randomAccessStream.AsStream()))); + mediaProperties.Artist, randomAccessStream is not null ? randomAccessStream.AsStream() : default))); } catch { diff --git a/Hyperbar.Widget.MediaController.Windows/MediaControllerService.cs b/Hyperbar.Widget.MediaController.Windows/MediaControllerService.cs index 7e1dc70..86a06a6 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaControllerService.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaControllerService.cs @@ -27,7 +27,7 @@ public class MediaControllerService(IMediator mediator, public Task StopAsync(CancellationToken cancellationToken) { - throw new NotImplementedException(); + return Task.CompletedTask; } private async Task InitializeSessionAsync(GlobalSystemMediaTransportControlsSession session) diff --git a/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml b/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml index ed250e0..fe77d80 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml +++ b/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml @@ -4,9 +4,8 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:windows="using:Hyperbar.UI.Windows"> - - + ItemsSource="{Binding}" /> diff --git a/Hyperbar.Widget.MediaController.Windows/MediaInformation.cs b/Hyperbar.Widget.MediaController.Windows/MediaInformation.cs index 580500e..e518dde 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaInformation.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaInformation.cs @@ -2,6 +2,6 @@ public record MediaInformation(string Title, string Description, - Stream ThumbnailSource); + Stream? ThumbnailSource); diff --git a/Hyperbar.Widget.MediaController.Windows/MediaInformationViewModel.cs b/Hyperbar.Widget.MediaController.Windows/MediaInformationViewModel.cs index 1234951..cbb9d3a 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaInformationViewModel.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaInformationViewModel.cs @@ -1,5 +1,4 @@ using CommunityToolkit.Mvvm.ComponentModel; -using Microsoft.UI.Xaml.Media; namespace Hyperbar.Widget.MediaController.Windows; @@ -8,7 +7,6 @@ public partial class MediaInformationViewModel(IServiceFactory serviceFactory, IDisposer disposer, ITemplateFactory templateFactory) : WidgetComponentViewModel(serviceFactory, mediator, disposer, templateFactory), - IInitialization, INotificationHandler> { [ObservableProperty] @@ -33,6 +31,6 @@ public partial class MediaInformationViewModel(IServiceFactory serviceFactory, return Task.CompletedTask; } - public override async Task InitializeAsync() => + public override async Task OnInitializeAsync() => await Mediator.PublishAsync>(); } diff --git a/Hyperbar.Widget.Windows/IServiceCollectionExtensions.cs b/Hyperbar.Widget.Windows/IServiceCollectionExtensions.cs index 828f3e1..f072acc 100644 --- a/Hyperbar.Widget.Windows/IServiceCollectionExtensions.cs +++ b/Hyperbar.Widget.Windows/IServiceCollectionExtensions.cs @@ -30,8 +30,8 @@ public static class IServiceCollectionExtensions services.AddHandler(); services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddContentTemplate(); services.AddContentTemplate(); diff --git a/Hyperbar.Widget.Windows/WidgetResourceInitializer.cs b/Hyperbar.Widget.Windows/WidgetResourceInitializer.cs index 4c39836..c9fb0d3 100644 --- a/Hyperbar.Widget.Windows/WidgetResourceInitializer.cs +++ b/Hyperbar.Widget.Windows/WidgetResourceInitializer.cs @@ -4,7 +4,7 @@ using Windows.Storage; namespace Hyperbar.Widget.Windows; internal class WidgetResourceInitializer(IWidgetAssembly widgetAssembly) : - IInitialization + IInitializer { public async Task InitializeAsync() { diff --git a/Hyperbar.Widget.Windows/WidgetXamlMetadataInitializer.cs b/Hyperbar.Widget.Windows/WidgetXamlMetadataInitializer.cs index 7ab2ffd..8869282 100644 --- a/Hyperbar.Widget.Windows/WidgetXamlMetadataInitializer.cs +++ b/Hyperbar.Widget.Windows/WidgetXamlMetadataInitializer.cs @@ -4,7 +4,7 @@ namespace Hyperbar.Widget.Windows; public class WidgetXamlMetadataInitializer(IWidgetAssembly widgetAssembly, IList xamlMetadataProviders) : - IInitialization + IInitializer { public Task InitializeAsync() { diff --git a/Hyperbar.Widget/IServiceCollectionExtensions.cs b/Hyperbar.Widget/IServiceCollectionExtensions.cs index 81af667..f8903fc 100644 --- a/Hyperbar.Widget/IServiceCollectionExtensions.cs +++ b/Hyperbar.Widget/IServiceCollectionExtensions.cs @@ -7,7 +7,7 @@ public static class IServiceCollectionExtensions { public static IServiceCollection AddWidget(this IServiceCollection services) { - services.AddTransient(); + services.AddTransient(); services.AddTransient, WidgetFactory>(); services.AddHandler(); diff --git a/Hyperbar.Widget/WidgetExtensionInitializer.cs b/Hyperbar.Widget/WidgetExtensionInitializer.cs index e509c38..87954d5 100644 --- a/Hyperbar.Widget/WidgetExtensionInitializer.cs +++ b/Hyperbar.Widget/WidgetExtensionInitializer.cs @@ -1,7 +1,7 @@ namespace Hyperbar.Widget; public class WidgetExtensionInitializer(IMediator mediator) : - IInitialization + IInitializer { public async Task InitializeAsync() => await mediator.PublishAsync>(); diff --git a/Hyperbar.Widget/WidgetHost.cs b/Hyperbar.Widget/WidgetHost.cs index fb4095a..ad476d2 100644 --- a/Hyperbar.Widget/WidgetHost.cs +++ b/Hyperbar.Widget/WidgetHost.cs @@ -9,16 +9,17 @@ public sealed class WidgetHost : private readonly IServiceProvider services; private readonly IMediator mediator; private readonly IProxyService proxyMediator; + private readonly IEnumerable hostedServices; public WidgetHost(IServiceProvider services, IMediator mediator, - IProxyService proxyMediator) + IProxyService proxyMediator, + IEnumerable hostedServices) { this.services = services; this.mediator = mediator; this.proxyMediator = proxyMediator; - - mediator.Subscribe(this); + this.hostedServices = hostedServices; } public WidgetConfiguration Configuration => @@ -33,6 +34,11 @@ public sealed class WidgetHost : public async Task StartAsync(CancellationToken cancellationToken = default) { + foreach (IHostedService service in hostedServices) + { + await service.StartAsync(cancellationToken); + } + if (proxyMediator.Proxy is IMediator mediator) { await mediator.PublishAsync(new Started(this), @@ -43,8 +49,11 @@ public sealed class WidgetHost : cancellationToken); } - public Task StopAsync(CancellationToken cancellationToken = default) + public async Task StopAsync(CancellationToken cancellationToken = default) { - return Task.CompletedTask; + foreach(IHostedService service in hostedServices) + { + await service.StopAsync(cancellationToken); + } } } \ No newline at end of file diff --git a/Hyperbar.Widget/WidgetHostHandler.cs b/Hyperbar.Widget/WidgetHostHandler.cs index 74edc18..545c189 100644 --- a/Hyperbar.Widget/WidgetHostHandler.cs +++ b/Hyperbar.Widget/WidgetHostHandler.cs @@ -10,10 +10,10 @@ public class WidgetHostHandler : { if (notification.Value is IWidgetHost host) { - if (host.Services.GetServices() is - IEnumerable initializations) + if (host.Services.GetServices() is + IEnumerable initializations) { - foreach (IInitialization initialization in initializations) + foreach (IInitializer initialization in initializations) { await initialization.InitializeAsync(); } diff --git a/Hyperbar.Widget/WidgetMonitor.cs b/Hyperbar.Widget/WidgetMonitor.cs index 31902af..1db1649 100644 --- a/Hyperbar.Widget/WidgetMonitor.cs +++ b/Hyperbar.Widget/WidgetMonitor.cs @@ -1,7 +1,7 @@ namespace Hyperbar.Widget; public class WidgetMonitor : - IInitialization + IInitializer { public Task InitializeAsync() { diff --git a/Hyperbar.Widget/WidgetService.cs b/Hyperbar.Widget/WidgetService.cs index c45d899..7c984bb 100644 --- a/Hyperbar.Widget/WidgetService.cs +++ b/Hyperbar.Widget/WidgetService.cs @@ -5,16 +5,16 @@ namespace Hyperbar.Widget; public class WidgetService : IHostedService { - private readonly IEnumerable initializers; + private readonly IEnumerable initializers; - public WidgetService(IEnumerable initializers) + public WidgetService(IEnumerable initializers) { this.initializers = initializers; } public async Task StartAsync(CancellationToken cancellationToken) { - foreach (IInitialization initializer in initializers) + foreach (IInitializer initializer in initializers) { await initializer.InitializeAsync(); } diff --git a/Hyperbar.Windows/App.xaml.cs b/Hyperbar.Windows/App.xaml.cs index fddd845..68e13bd 100644 --- a/Hyperbar.Windows/App.xaml.cs +++ b/Hyperbar.Windows/App.xaml.cs @@ -46,8 +46,7 @@ public partial class App : args.Placement = DesktopBarPlacemenet.Top; }); - services.AddTransient(); - + services.AddTransient(); services.AddSingleton(); }) .Build(); diff --git a/Hyperbar.Windows/Lifecycles/AppInitializer.cs b/Hyperbar.Windows/Lifecycles/AppInitializer.cs index 2bac5d9..e8f9a97 100644 --- a/Hyperbar.Windows/Lifecycles/AppInitializer.cs +++ b/Hyperbar.Windows/Lifecycles/AppInitializer.cs @@ -9,7 +9,7 @@ public class AppInitializer([FromKeyedServices(nameof(WidgetViewModel))] WidgetB [FromKeyedServices(nameof(WidgetViewModel))] WidgetViewModel viewModel, DesktopBar desktopFlyout, AppConfiguration configuration) : - IInitialization + IInitializer { public Task InitializeAsync() { diff --git a/Hyperbar/Configuration/ConfigurationInitializer.cs b/Hyperbar/Configuration/ConfigurationInitializer.cs index 6b24928..58d368a 100644 --- a/Hyperbar/Configuration/ConfigurationInitializer.cs +++ b/Hyperbar/Configuration/ConfigurationInitializer.cs @@ -6,7 +6,7 @@ public class ConfigurationInitializer(IMediator mediator, IConfigurationWriter writer, IConfigurationFactory factory) : IConfigurationInitializer, - IInitialization + IInitializer where TConfiguration : class { diff --git a/Hyperbar/Configuration/IConfigurationMonitor.cs b/Hyperbar/Configuration/IConfigurationMonitor.cs index 8b1e573..d2fb2f1 100644 --- a/Hyperbar/Configuration/IConfigurationMonitor.cs +++ b/Hyperbar/Configuration/IConfigurationMonitor.cs @@ -2,6 +2,6 @@ namespace Hyperbar; public interface IConfigurationMonitor : - IInitialization + IInitializer where TConfiguration : class; \ No newline at end of file diff --git a/Hyperbar/Extensions/IServiceCollectionExtensions.cs b/Hyperbar/Extensions/IServiceCollectionExtensions.cs index cfd9afe..87bb10d 100644 --- a/Hyperbar/Extensions/IServiceCollectionExtensions.cs +++ b/Hyperbar/Extensions/IServiceCollectionExtensions.cs @@ -117,7 +117,7 @@ public static class IServiceCollectionExtensions services.AddTransient>(provider => new ConfigurationFactory(() => configuration ?? provider.GetRequiredService())); - services.AddTransient>(); + services.AddTransient>(); services.AddTransient, ConfigurationInitializer>(); services.AddTransient, WritableConfiguration>(); diff --git a/Hyperbar/Lifecycles/AppService.cs b/Hyperbar/Lifecycles/AppService.cs index 30c75cc..07d318a 100644 --- a/Hyperbar/Lifecycles/AppService.cs +++ b/Hyperbar/Lifecycles/AppService.cs @@ -2,12 +2,12 @@ namespace Hyperbar; -public class AppService(IEnumerable initializers) : +public class AppService(IEnumerable initializers) : IHostedService { public async Task StartAsync(CancellationToken cancellationToken) { - foreach (IInitialization initializer in initializers) + foreach (IInitializer initializer in initializers) { await initializer.InitializeAsync(); } diff --git a/Hyperbar/Lifecycles/IInitialization.cs b/Hyperbar/Lifecycles/IInitialization.cs index e5fb430..ddb9bc2 100644 --- a/Hyperbar/Lifecycles/IInitialization.cs +++ b/Hyperbar/Lifecycles/IInitialization.cs @@ -2,7 +2,7 @@ namespace Hyperbar; -public interface IInitialization2 +public interface IInitialization { ICommand Initialize { get; } diff --git a/Hyperbar/Lifecycles/IInitializer.cs b/Hyperbar/Lifecycles/IInitializer.cs index d39880f..a12d1ea 100644 --- a/Hyperbar/Lifecycles/IInitializer.cs +++ b/Hyperbar/Lifecycles/IInitializer.cs @@ -1,6 +1,6 @@ namespace Hyperbar; -public interface IInitialization +public interface IInitializer { Task InitializeAsync(); } \ No newline at end of file diff --git a/Hyperbar/Lifecycles/ObservableCollectionViewModel.cs b/Hyperbar/Lifecycles/ObservableCollectionViewModel.cs index d0129f9..29b7289 100644 --- a/Hyperbar/Lifecycles/ObservableCollectionViewModel.cs +++ b/Hyperbar/Lifecycles/ObservableCollectionViewModel.cs @@ -20,14 +20,17 @@ public partial class ObservableCollectionViewModel : INotificationHandler>, INotificationHandler>, INotificationHandler>, - IDisposable + IDisposable, + IInitialization where TItem : IDisposable { - private readonly ObservableCollection collection = new(); + private readonly ObservableCollection collection = []; + + private bool isInitialized; public ObservableCollectionViewModel(IServiceFactory serviceFactory, - IMediator mediator, + IMediator mediator, IDisposer disposer) { ServiceFactory = serviceFactory; @@ -268,11 +271,7 @@ public partial class ObservableCollectionViewModel : 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) => + public void Insert(int index, TItem item) => InsertItem(index, item); void IList.Insert(int index, @@ -284,22 +283,6 @@ public partial class ObservableCollectionViewModel : } } - public bool Replace(int index, TItem item) - { - if (index <= Count - 1) - { - RemoveItem(index); - } - else - { - index = Count; - } - - Insert(index, item); - - return true; - } - public bool Move(int index, TItem item) { int oldIndex = collection.IndexOf(item); @@ -314,6 +297,11 @@ public partial class ObservableCollectionViewModel : return true; } + public virtual Task OnInitializeAsync() + { + return Task.CompletedTask; + } + public bool Remove(TItem item) { int index = collection.IndexOf(item); @@ -339,7 +327,23 @@ public partial class ObservableCollectionViewModel : public void RemoveAt(int index) => RemoveItem(index); - protected virtual void ClearItems() => + public bool Replace(int index, TItem item) + { + if (index <= Count - 1) + { + RemoveItem(index); + } + else + { + index = Count; + } + + Insert(index, item); + + return true; + } + + protected virtual void ClearItems() => collection.Clear(); protected virtual void InsertItem(int index, @@ -357,15 +361,27 @@ public partial class ObservableCollectionViewModel : collection.Insert(index, value); } - protected virtual void RemoveItem(int index) => + protected virtual void RemoveItem(int index) => collection.RemoveAt(index); - protected virtual void SetItem(int index, TItem item) => + protected virtual void SetItem(int index, TItem item) => collection[index] = item; - private static bool IsCompatibleObject(object? value) => + private static bool IsCompatibleObject(object? value) => (value is TItem) || (value == null && default(TItem) == null); + public async Task InitializeAsync() + { + if (isInitialized) + { + return; + } + + isInitialized = true; + + await Mediator.PublishAsync>(); + await OnInitializeAsync(); + } private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args) => CollectionChanged?.Invoke(this, args); }