From 6d40220412e6391391c1b00ad0850741b6e2a3dc Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sun, 28 Jan 2024 14:57:56 +0000 Subject: [PATCH] bunch ov fixes --- Hyperbar.UI.Windows/IWinRTObjectExtensions.cs | 40 ++++++--- .../{Foward.cs => Forward.cs} | 2 +- .../MediaButtonView.xaml.cs | 4 +- .../MediaButtonViewModel.cs | 54 ++++++++---- .../MediaController.cs | 83 ++++++++++--------- .../MediaControllerHandler.cs | 4 +- .../MediaControllerView.xaml.cs | 4 +- .../MediaControllerViewModel.cs | 23 +++-- .../MediaControllerWidgetView.xaml | 10 ++- .../MediaControllerWidgetView.xaml.cs | 71 +--------------- .../MediaInformationView.xaml | 2 +- .../MediaInformationView.xaml.cs | 3 +- .../Playback.cs | 3 - .../PlaybackButtonType.cs | 9 ++ .../PlaybackInformation.cs | 4 + .../PlaybackStatus.cs | 11 +++ .../{Backward.cs => Previous.cs} | 2 +- .../Hyperbar.Widget.Windows.csproj | 29 ------- .../WidgetResourceInitialization.cs | 35 ++++---- .../IServiceCollectionExtensions.cs | 1 - Hyperbar.Widget/WidgetBuilder.cs | 3 - Hyperbar.Widget/WidgetButtonViewModel.cs | 6 ++ Hyperbar.Widget/WidgetComponentViewModel.cs | 4 +- Hyperbar.Widget/WidgetExtensionHandler.cs | 1 - Hyperbar.Widget/WidgetService.cs | 17 ---- Hyperbar/Mediators/IMediator.cs | 6 -- Hyperbar/Mediators/Mediator.cs | 49 ++++++----- .../Mediators/NotificationHandlerAttribute.cs | 7 ++ 28 files changed, 238 insertions(+), 249 deletions(-) rename Hyperbar.Widget.MediaController.Windows/{Foward.cs => Forward.cs} (58%) delete mode 100644 Hyperbar.Widget.MediaController.Windows/Playback.cs create mode 100644 Hyperbar.Widget.MediaController.Windows/PlaybackButtonType.cs create mode 100644 Hyperbar.Widget.MediaController.Windows/PlaybackInformation.cs create mode 100644 Hyperbar.Widget.MediaController.Windows/PlaybackStatus.cs rename Hyperbar.Widget.MediaController.Windows/{Backward.cs => Previous.cs} (57%) delete mode 100644 Hyperbar.Widget/WidgetService.cs create mode 100644 Hyperbar/Mediators/NotificationHandlerAttribute.cs diff --git a/Hyperbar.UI.Windows/IWinRTObjectExtensions.cs b/Hyperbar.UI.Windows/IWinRTObjectExtensions.cs index cf3c5fb..d6fa743 100644 --- a/Hyperbar.UI.Windows/IWinRTObjectExtensions.cs +++ b/Hyperbar.UI.Windows/IWinRTObjectExtensions.cs @@ -1,24 +1,38 @@ -using System.Runtime.CompilerServices; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls.Primitives; +using System.Reflection; +using System.Runtime.CompilerServices; using WinRT; namespace Hyperbar.UI.Windows; public static class IWinRTObjectExtensions { - public static void InitializeComponent(this TComponent component, - ref bool loaded, - [CallerFilePath] string path = "") - where TComponent : - IWinRTObject + public static void InitializeComponent(this TComponent component, + ref bool loaded, [CallerFilePath] string path = "") + where TComponent : IWinRTObject { - if (loaded) + if (!loaded) { - return; + loaded = true; + + Type type = component.GetType(); + if (type.Assembly is Assembly assembly && Path.GetDirectoryName(assembly.Location) is string assemblyDirectory) + { + string resourceName = Path.GetFileNameWithoutExtension(path); + string[] pathParts = path.Split(Path.DirectorySeparatorChar)[..^1]; + + string? resourcePath = pathParts + .Reverse() + .Select(part => Path.Combine(assemblyDirectory, part, resourceName)) + .FirstOrDefault(File.Exists); + + if (resourcePath is not null) + { + Application.LoadComponent(component, new Uri($"ms-appx:///{resourcePath.Replace('\\', '/')}"), + ComponentResourceLocation.Nested); + } + } } - - loaded = true; - - //Uri resourceLocator = ApplicationExtensionHost.Current.LocateResource(component, callerFilePath); - //Application.LoadComponent(component, resourceLocator, ComponentResourceLocation.Nested); } } diff --git a/Hyperbar.Widget.MediaController.Windows/Foward.cs b/Hyperbar.Widget.MediaController.Windows/Forward.cs similarity index 58% rename from Hyperbar.Widget.MediaController.Windows/Foward.cs rename to Hyperbar.Widget.MediaController.Windows/Forward.cs index 0193bb7..a83d403 100644 --- a/Hyperbar.Widget.MediaController.Windows/Foward.cs +++ b/Hyperbar.Widget.MediaController.Windows/Forward.cs @@ -1,3 +1,3 @@ namespace Hyperbar.Widget.MediaController.Windows; -public record Foward : INotification; +public record Forward : INotification; diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonView.xaml.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonView.xaml.cs index 8a44060..8bbe98f 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaButtonView.xaml.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonView.xaml.cs @@ -1,9 +1,11 @@ using Microsoft.UI.Xaml.Controls; +using Hyperbar.UI.Windows; namespace Hyperbar.Widget.MediaController.Windows; public sealed partial class MediaButtonView : UserControl { - public MediaButtonView() => InitializeComponent(); + public MediaButtonView() => + this.InitializeComponent(ref _contentLoaded); } diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs index 15a32be..96210e4 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs @@ -1,21 +1,47 @@ using CommunityToolkit.Mvvm.Input; -using Hyperbar.Widget; -using System.Windows.Input; namespace Hyperbar.Widget.MediaController.Windows; -public class MediaButtonViewModel(IServiceFactory serviceFactory, - IMediator mediator, - IDisposer disposer, - ITemplateFactory templateFactory, - Guid guid = default, - string? text = null, - string? icon = null, - RelayCommand? command = null) : - WidgetButtonViewModel(serviceFactory, mediator, disposer, templateFactory, guid, text, icon, command), - IInitialization +public class MediaButtonViewModel : + WidgetButtonViewModel, + IInitialization, + INotificationHandler> { - public ICommand Initialize => new AsyncRelayCommand(InitializeAsync); + private readonly PlaybackButtonType buttonType; - public async Task InitializeAsync() => await Mediator.PublishAsync>(); + public MediaButtonViewModel(IServiceFactory serviceFactory, + IMediator mediator, + IDisposer disposer, + ITemplateFactory templateFactory, + PlaybackButtonType buttonType, + Guid guid = default, + string? text = null, + string? icon = null, + RelayCommand? command = null) : base (serviceFactory, mediator, disposer, templateFactory, guid, text, icon, command) + { + this.buttonType = buttonType; + mediator.Subscribe(this); + } + + public Task Handle(Changed notification, + CancellationToken cancellationToken) + { + if (notification.Value is PlaybackInformation information) + { + switch (buttonType) + { + case PlaybackButtonType.Play: + Visible = information.Status is PlaybackStatus.Playing; + break; + case PlaybackButtonType.Pause: + Visible = information.Status is PlaybackStatus.Paused; + break; + } + } + + return Task.CompletedTask; + } + + public override async Task InitializeAsync() => + await Mediator.PublishAsync>(); } diff --git a/Hyperbar.Widget.MediaController.Windows/MediaController.cs b/Hyperbar.Widget.MediaController.Windows/MediaController.cs index e2ad375..a0ab6f1 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaController.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaController.cs @@ -2,17 +2,17 @@ namespace Hyperbar.Widget.MediaController.Windows; -public class MediaController : +public class MediaController : INotificationHandler, INotificationHandler, - INotificationHandler>, + INotificationHandler>, INotificationHandler>, IDisposable { - private readonly IMediator mediator; - private readonly IDisposer disposer; - private readonly GlobalSystemMediaTransportControlsSession session; private readonly AsyncLock asyncLock = new(); + private readonly IDisposer disposer; + private readonly IMediator mediator; + private readonly GlobalSystemMediaTransportControlsSession session; public MediaController(IMediator mediator, IDisposer disposer, @@ -31,51 +31,46 @@ public class MediaController : public void Dispose() { + GC.SuppressFinalize(this); disposer.Dispose(this); } - public async Task Handle(Play notification, - CancellationToken cancellationToken) => + public async Task Handle(Play notification, + CancellationToken cancellationToken) + { await session.TryPlayAsync(); + await UpdateMediaPlaybackPropertiesAsync(); + } - public async Task Handle(Pause notification, - CancellationToken cancellationToken) => + public async Task Handle(Pause notification, + CancellationToken cancellationToken) + { await session.TryPauseAsync(); - - public async Task Handle(Request notification, - CancellationToken cancellationToken) - { - await mediator.PublishAsync(new Changed(), cancellationToken); + await UpdateMediaPlaybackPropertiesAsync(); } - public async Task Handle(Request _, - CancellationToken cancellationToken) - { - using (await asyncLock) - { - try - { - GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties = await session.TryGetMediaPropertiesAsync(); - await mediator.PublishAsync(new Changed(new MediaInformation(mediaProperties.Title, - mediaProperties.Subtitle)), cancellationToken); - } - catch - { + public async Task Handle(Request args, + CancellationToken cancellationToken) => await UpdateMediaPlaybackPropertiesAsync(); - } - } - } + public async Task Handle(Request args, + CancellationToken cancellationToken) => await UpdateMediaPropertiesAsync(); private async void OnMediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender, - MediaPropertiesChangedEventArgs args) + MediaPropertiesChangedEventArgs args) => await UpdateMediaPropertiesAsync(); + + private async void OnPlaybackInfoChanged(GlobalSystemMediaTransportControlsSession sender, + PlaybackInfoChangedEventArgs args) => await UpdateMediaPlaybackPropertiesAsync(); + + private async Task UpdateMediaPlaybackPropertiesAsync() { using (await asyncLock) { try { - GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties = await session.TryGetMediaPropertiesAsync(); - await mediator.PublishAsync(new Changed(new MediaInformation(mediaProperties.Title, - mediaProperties.Artist))); + GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo = session.GetPlaybackInfo(); + await mediator.PublishAsync(new Changed( + new PlaybackInformation((PlaybackStatus)playbackInfo.PlaybackStatus))); + } catch { @@ -84,9 +79,23 @@ public class MediaController : } } - private async void OnPlaybackInfoChanged(GlobalSystemMediaTransportControlsSession sender, - PlaybackInfoChangedEventArgs args) + private async Task UpdateMediaPropertiesAsync() { - await mediator.PublishAsync(new Changed()); + using (await asyncLock) + { + try + { + GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties = + await session.TryGetMediaPropertiesAsync(); + + await mediator.PublishAsync(new Changed(new MediaInformation(mediaProperties.Title, + mediaProperties.Artist))); + + } + catch + { + + } + } } } \ No newline at end of file diff --git a/Hyperbar.Widget.MediaController.Windows/MediaControllerHandler.cs b/Hyperbar.Widget.MediaController.Windows/MediaControllerHandler.cs index d163d2e..59b0072 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaControllerHandler.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaControllerHandler.cs @@ -22,8 +22,8 @@ public class MediaControllerHandler(IMediator mediator, if (factory.Create(mediaController) is MediaControllerViewModel mediaControllerViewModel) { cache.Add(mediaController, mediaControllerViewModel); - //await mediator.PublishAsync(new Created(mediaControllerViewModel), - // cancellationToken); + await mediator.PublishAsync(new Created(mediaControllerViewModel), + cancellationToken); } } } diff --git a/Hyperbar.Widget.MediaController.Windows/MediaControllerView.xaml.cs b/Hyperbar.Widget.MediaController.Windows/MediaControllerView.xaml.cs index 7a16b37..8ae2da7 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaControllerView.xaml.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaControllerView.xaml.cs @@ -1,9 +1,11 @@ using Microsoft.UI.Xaml.Controls; +using Hyperbar.UI.Windows; namespace Hyperbar.Widget.MediaController.Windows; public sealed partial class MediaControllerView : UserControl { - public MediaControllerView() => InitializeComponent(); + public MediaControllerView() => + this.InitializeComponent(ref _contentLoaded); } diff --git a/Hyperbar.Widget.MediaController.Windows/MediaControllerViewModel.cs b/Hyperbar.Widget.MediaController.Windows/MediaControllerViewModel.cs index 3e9abf6..c5b8c3c 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaControllerViewModel.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaControllerViewModel.cs @@ -1,5 +1,4 @@ using CommunityToolkit.Mvvm.Input; -using Hyperbar.Widget; namespace Hyperbar.Widget.MediaController.Windows; @@ -15,10 +14,24 @@ public class MediaControllerViewModel : TemplateFactory = templateFactory; Add(); - Add("Backward", "\uEB9E"); - Add("Play", "\uE768", new RelayCommand(async () => await mediator.PublishAsync())); - Add("Pause", "\uE769", new RelayCommand(async () => await mediator.PublishAsync())); - Add("Forward", "\uEB9D"); + + Add(PlaybackButtonType.Previous, + "Previous", "\uEB9E", + new RelayCommand(async () => await mediator.PublishAsync())); + + Add(PlaybackButtonType.Play, + "Play", "\uE768", + new RelayCommand(async () => await mediator.PublishAsync())); + + Add(PlaybackButtonType.Pause, + "Pause", "\uE769", + new RelayCommand(async () => await mediator.PublishAsync())); + + Add(PlaybackButtonType.Forward, + "Forward", "\uEB9D", + new RelayCommand(async () => await mediator.PublishAsync())); + + mediator.Subscribe(this); } public ITemplateFactory TemplateFactory { get; set; } diff --git a/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml b/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml index 2044035..590060e 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml +++ b/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml @@ -2,6 +2,12 @@ - + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:ui="using:Hyperbar.UI.Windows"> + diff --git a/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml.cs b/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml.cs index cbdbda4..2e6a3a8 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaControllerWidgetView.xaml.cs @@ -1,74 +1,11 @@ -using CustomExtensions.WinUI; using Microsoft.UI.Xaml.Controls; -using System.Reflection; -using System.Runtime.CompilerServices; +using Hyperbar.UI.Windows; + namespace Hyperbar.Widget.MediaController.Windows; public sealed partial class MediaControllerWidgetView : UserControl { - public MediaControllerWidgetView() - { - Foo(@"C:\Users\dan_c\AppData\Local\Packages\24ccddba-447f-4d37-891d-523e8d820f45_rmhrgjnfy8he0\LocalCache\Local\Hyperbar.Windows\Extensions\Hyperbar.Windows.MediaController\Hyperbar.Windows.MediaController.dll"); - LocateResourcePath(this); - } - - public Assembly ForeignAssembly { get; set; } - - private string ForeignAssemblyDir; - private string ForeignAssemblyName; - private bool? IsHotReloadAvailable; - private DisposableCollection Disposables = new(); - private bool IsDisposed; - - internal static readonly Assembly? EntryAssembly; - internal static readonly string HostingProcessDir; - - static MediaControllerWidgetView() - { - EntryAssembly = Assembly.GetEntryAssembly(); - HostingProcessDir = Path.GetDirectoryName(EntryAssembly.Location); - } - - public void Foo(string assemblyPath) - { - // TODO: For some reason WinUI gets very angry when loading via AssemblyLoadContext, - // even if using AssemblyLoadContext.Default which *should* have no difference than - // Assembly.LoadFrom(), but it does. - // - // ExtensionContext = new(assemblyPath); - // ForeignAssembly = ExtensionContext.LoadFromAssemblyPath(assemblyPath); - ForeignAssembly = Assembly.LoadFrom(assemblyPath); - ForeignAssemblyDir = Path.GetDirectoryName(ForeignAssembly.Location); - ForeignAssemblyName = ForeignAssembly.GetName().Name; - } - - private string LocateResourcePath(object component, [CallerFilePath] string callerFilePath = "") - { - if (component.GetType().Assembly != ForeignAssembly) - { - throw new InvalidProgramException(); - } - string resourceName = Path.GetFileName(callerFilePath)[..^3]; - - string[] pathParts = callerFilePath.Split('\\')[..^1]; - for (int i = pathParts.Length - 1; i > 1; i++) - { - string pathCandidate = Path.Join(pathParts[i..pathParts.Length].Append(resourceName).Prepend(ForeignAssemblyName).ToArray()); - FileInfo sourceResource = new(Path.Combine(ForeignAssemblyDir, pathCandidate)); - FileInfo colocatedResource = new(Path.Combine(HostingProcessDir, pathCandidate)); - if (colocatedResource.Exists) - { - return pathCandidate; - } - if (sourceResource.Exists) - { - return sourceResource.FullName; - } - - throw new FileNotFoundException("Could not find resource", resourceName); - } - - throw new FileNotFoundException("Could not find resource", resourceName); - } + public MediaControllerWidgetView() => + this.InitializeComponent(ref _contentLoaded); } \ No newline at end of file diff --git a/Hyperbar.Widget.MediaController.Windows/MediaInformationView.xaml b/Hyperbar.Widget.MediaController.Windows/MediaInformationView.xaml index 619cc27..645841d 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaInformationView.xaml +++ b/Hyperbar.Widget.MediaController.Windows/MediaInformationView.xaml @@ -9,7 +9,7 @@ - + this.InitializeComponent(ref _contentLoaded); + public MediaInformationView() => + this.InitializeComponent(ref _contentLoaded); } diff --git a/Hyperbar.Widget.MediaController.Windows/Playback.cs b/Hyperbar.Widget.MediaController.Windows/Playback.cs deleted file mode 100644 index a181650..0000000 --- a/Hyperbar.Widget.MediaController.Windows/Playback.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Hyperbar.Widget.MediaController.Windows; - -public record Playback : INotification; diff --git a/Hyperbar.Widget.MediaController.Windows/PlaybackButtonType.cs b/Hyperbar.Widget.MediaController.Windows/PlaybackButtonType.cs new file mode 100644 index 0000000..37a240f --- /dev/null +++ b/Hyperbar.Widget.MediaController.Windows/PlaybackButtonType.cs @@ -0,0 +1,9 @@ +namespace Hyperbar.Widget.MediaController.Windows; + +public enum PlaybackButtonType +{ + Previous, + Play, + Pause, + Forward +} diff --git a/Hyperbar.Widget.MediaController.Windows/PlaybackInformation.cs b/Hyperbar.Widget.MediaController.Windows/PlaybackInformation.cs new file mode 100644 index 0000000..2a15c24 --- /dev/null +++ b/Hyperbar.Widget.MediaController.Windows/PlaybackInformation.cs @@ -0,0 +1,4 @@ +namespace Hyperbar.Widget.MediaController.Windows; + +public record PlaybackInformation(PlaybackStatus Status) : + INotification; diff --git a/Hyperbar.Widget.MediaController.Windows/PlaybackStatus.cs b/Hyperbar.Widget.MediaController.Windows/PlaybackStatus.cs new file mode 100644 index 0000000..bf5e959 --- /dev/null +++ b/Hyperbar.Widget.MediaController.Windows/PlaybackStatus.cs @@ -0,0 +1,11 @@ +namespace Hyperbar.Widget.MediaController.Windows; + +public enum PlaybackStatus +{ + Closed, + Opened, + Changing, + Stopped, + Playing, + Paused +} \ No newline at end of file diff --git a/Hyperbar.Widget.MediaController.Windows/Backward.cs b/Hyperbar.Widget.MediaController.Windows/Previous.cs similarity index 57% rename from Hyperbar.Widget.MediaController.Windows/Backward.cs rename to Hyperbar.Widget.MediaController.Windows/Previous.cs index 96e67e5..4690f2c 100644 --- a/Hyperbar.Widget.MediaController.Windows/Backward.cs +++ b/Hyperbar.Widget.MediaController.Windows/Previous.cs @@ -1,3 +1,3 @@ namespace Hyperbar.Widget.MediaController.Windows; -public record Backward : INotification; +public record Previous : INotification; diff --git a/Hyperbar.Widget.Windows/Hyperbar.Widget.Windows.csproj b/Hyperbar.Widget.Windows/Hyperbar.Widget.Windows.csproj index 0692344..dd0738e 100644 --- a/Hyperbar.Widget.Windows/Hyperbar.Widget.Windows.csproj +++ b/Hyperbar.Widget.Windows/Hyperbar.Widget.Windows.csproj @@ -7,13 +7,6 @@ enable enable - - - - - - - @@ -25,26 +18,4 @@ - - - $(DefaultXamlRuntime) - Designer - - - $(DefaultXamlRuntime) - Designer - - - $(DefaultXamlRuntime) - Designer - - - $(DefaultXamlRuntime) - Designer - - - $(DefaultXamlRuntime) - Designer - - \ No newline at end of file diff --git a/Hyperbar.Widget.Windows/WidgetResourceInitialization.cs b/Hyperbar.Widget.Windows/WidgetResourceInitialization.cs index 68c33f0..3d8bbaa 100644 --- a/Hyperbar.Widget.Windows/WidgetResourceInitialization.cs +++ b/Hyperbar.Widget.Windows/WidgetResourceInitialization.cs @@ -1,5 +1,4 @@ -using System.Reflection; -using Windows.ApplicationModel.Resources.Core; +using Windows.ApplicationModel.Resources.Core; using Windows.Storage; namespace Hyperbar.Widget.Windows; @@ -9,27 +8,23 @@ internal class WidgetResourceInitialization(IWidgetAssembly widgetAssembly) : { public async Task InitializeAsync() { - if (widgetAssembly.Assembly is Assembly assembly) + string assemblyDirectory = Path.GetDirectoryName(widgetAssembly.Assembly.Location) ?? string.Empty; + string[] possibleFileNames = ["resources.pri", $"{widgetAssembly.Assembly.GetName().Name}.pri"]; + + FileInfo? resourceFileInfo = null; + foreach (string fileName in possibleFileNames) { - if (Path.GetDirectoryName(assembly.Location) is string assemblyDirectory) + resourceFileInfo = new FileInfo(Path.Combine(assemblyDirectory, fileName)); + if (resourceFileInfo.Exists) { - FileInfo resourceFileInfo = new(Path.Combine(assemblyDirectory, - "resources.pri")); - - if (!resourceFileInfo.Exists) - { - resourceFileInfo = new(Path.Combine(assemblyDirectory, - $"{assembly.GetName().Name}.pri")); - } - - if (!resourceFileInfo.Exists) - { - return; - } - - StorageFile file = await StorageFile.GetFileFromPathAsync(resourceFileInfo.FullName); - ResourceManager.Current.LoadPriFiles(new[] { file }); + break; } } + + if (resourceFileInfo?.Exists is true) + { + StorageFile file = await StorageFile.GetFileFromPathAsync(resourceFileInfo.FullName); + ResourceManager.Current.LoadPriFiles(new[] { file }); + } } } \ No newline at end of file diff --git a/Hyperbar.Widget/IServiceCollectionExtensions.cs b/Hyperbar.Widget/IServiceCollectionExtensions.cs index 9e2df37..7586a5b 100644 --- a/Hyperbar.Widget/IServiceCollectionExtensions.cs +++ b/Hyperbar.Widget/IServiceCollectionExtensions.cs @@ -12,7 +12,6 @@ public static class IServiceCollectionExtensions services.AddHandler(); services.AddHandler(); - services.AddHandler(); services.AddHandler(); return services; diff --git a/Hyperbar.Widget/WidgetBuilder.cs b/Hyperbar.Widget/WidgetBuilder.cs index c1004e2..410aa51 100644 --- a/Hyperbar.Widget/WidgetBuilder.cs +++ b/Hyperbar.Widget/WidgetBuilder.cs @@ -25,9 +25,6 @@ public class WidgetBuilder(TConfiguration configuration) : services.AddScoped(provider => new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!))); - - services.AddHostedService(); - services.AddScoped(); services.AddScoped(); diff --git a/Hyperbar.Widget/WidgetButtonViewModel.cs b/Hyperbar.Widget/WidgetButtonViewModel.cs index 61f947a..7721629 100644 --- a/Hyperbar.Widget/WidgetButtonViewModel.cs +++ b/Hyperbar.Widget/WidgetButtonViewModel.cs @@ -16,6 +16,9 @@ public partial class WidgetButtonViewModel(IServiceFactory serviceFactory, [ObservableProperty] private IRelayCommand? click = command; + [ObservableProperty] + private bool enabled; + [ObservableProperty] private string? icon = icon; @@ -24,4 +27,7 @@ public partial class WidgetButtonViewModel(IServiceFactory serviceFactory, [ObservableProperty] private string? text = text; + + [ObservableProperty] + private bool visible; } \ No newline at end of file diff --git a/Hyperbar.Widget/WidgetComponentViewModel.cs b/Hyperbar.Widget/WidgetComponentViewModel.cs index e07dc6a..798f298 100644 --- a/Hyperbar.Widget/WidgetComponentViewModel.cs +++ b/Hyperbar.Widget/WidgetComponentViewModel.cs @@ -1,6 +1,4 @@ -using CommunityToolkit.Mvvm.ComponentModel; - -namespace Hyperbar.Widget; +namespace Hyperbar.Widget; public partial class WidgetComponentViewModel : ObservableCollectionViewModel, diff --git a/Hyperbar.Widget/WidgetExtensionHandler.cs b/Hyperbar.Widget/WidgetExtensionHandler.cs index 52ecc97..db350a5 100644 --- a/Hyperbar.Widget/WidgetExtensionHandler.cs +++ b/Hyperbar.Widget/WidgetExtensionHandler.cs @@ -12,7 +12,6 @@ public class WidgetExtensionHandler(IProxyServiceCollection type if(notification.Value is WidgetExtension widgetExtension) { IWidgetBuilder builder = widgetExtension.Widget.Create(); - builder.ConfigureServices(args => { args.AddSingleton(widgetExtension.Assembly); diff --git a/Hyperbar.Widget/WidgetService.cs b/Hyperbar.Widget/WidgetService.cs deleted file mode 100644 index 0e69b2b..0000000 --- a/Hyperbar.Widget/WidgetService.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.Extensions.Hosting; - -namespace Hyperbar.Widget; - -public sealed class WidgetService(IEnumerable initializers) : - IHostedService -{ - public async Task StartAsync(CancellationToken cancellationToken) - { - foreach (IInitializer initializer in initializers) - { - await initializer.InitializeAsync(); - } - } - - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; -} \ No newline at end of file diff --git a/Hyperbar/Mediators/IMediator.cs b/Hyperbar/Mediators/IMediator.cs index 3370d01..9cf4034 100644 --- a/Hyperbar/Mediators/IMediator.cs +++ b/Hyperbar/Mediators/IMediator.cs @@ -1,11 +1,5 @@ namespace Hyperbar; -[AttributeUsage(AttributeTargets.Class)] -public class NotificationHandlerAttribute(object key) : Attribute -{ - public object Key { get; } = key; -} - public interface IMediator { Task PublishAsync(TNotification notification, diff --git a/Hyperbar/Mediators/Mediator.cs b/Hyperbar/Mediators/Mediator.cs index 7e63619..955ba63 100644 --- a/Hyperbar/Mediators/Mediator.cs +++ b/Hyperbar/Mediators/Mediator.cs @@ -107,31 +107,18 @@ public class Mediator(IServiceProvider provider, public void Subscribe(object handler) { Type handlerType = handler.GetType(); - object? key = null; + object? key = GetKeyFromHandler(handlerType, handler); - if (Attribute.GetCustomAttribute(handlerType, typeof(NotificationHandlerAttribute)) - is NotificationHandlerAttribute attribute) + foreach (Type interfaceType in GetNotificationHandlerInterfaces(handlerType)) { - if (handlerType.GetProperty($"{attribute.Key}") is PropertyInfo property) + if (interfaceType.GetGenericArguments().FirstOrDefault() + is Type argumentType) { - if (property.GetValue(handler, null) is { } value) + if (object.Equals(key, default)) { - key = value; - } - } - else - { - key = attribute.Key; - } - } - foreach (Type interfaceType in handlerType.GetInterfaces().Where(x => x.IsGenericType - && x.GetGenericTypeDefinition() == typeof(INotificationHandler<>))) - { - if (interfaceType.GetGenericArguments() is { Length: 1 } arguments) - { - key ??= arguments[0]; - subjects.AddOrUpdate(key, [handler], (value, collection) => + } + subjects.AddOrUpdate(key ?? argumentType, new List { handler }, (value, collection) => { collection.Add(handler); return collection; @@ -139,4 +126,26 @@ public class Mediator(IServiceProvider provider, } } } + + private object? GetKeyFromHandler(Type handlerType, object handler) + { + if (handlerType.GetCustomAttribute() is NotificationHandlerAttribute attribute) + { + if (handlerType.GetProperty($"{attribute.Key}") is PropertyInfo property + && property.GetValue(handler) is { } value) + { + return value; + } + else + { + return attribute.Key; + } + } + + return null; + } + + private IEnumerable GetNotificationHandlerInterfaces(Type handlerType) => handlerType.GetInterfaces() + .Where(interfaceType => interfaceType.IsGenericType && interfaceType + .GetGenericTypeDefinition() == typeof(INotificationHandler<>)); } \ No newline at end of file diff --git a/Hyperbar/Mediators/NotificationHandlerAttribute.cs b/Hyperbar/Mediators/NotificationHandlerAttribute.cs new file mode 100644 index 0000000..ff2b3d8 --- /dev/null +++ b/Hyperbar/Mediators/NotificationHandlerAttribute.cs @@ -0,0 +1,7 @@ +namespace Hyperbar; + +[AttributeUsage(AttributeTargets.Class, Inherited = false)] +public class NotificationHandlerAttribute(object key) : Attribute +{ + public object Key { get; } = key; +}