More refactoring
This commit is contained in:
@@ -6,5 +6,7 @@ namespace Hyperbar.Windows.Primary;
|
||||
[JsonDerivedType(typeof(ProcessCommandConfiguration), typeDiscriminator: "ProcessCommand")]
|
||||
public class PrimaryCommandConfiguration
|
||||
{
|
||||
public string? Icon { get; set; }
|
||||
public required string Id { get; set; }
|
||||
|
||||
public required string Icon { get; set; }
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
public class KeyAcceleratorCommandConfiguration :
|
||||
PrimaryCommandConfiguration
|
||||
{
|
||||
public int Key { get; set; }
|
||||
public required int Key { get; set; }
|
||||
|
||||
public int[]? Modifiers { get; set; }
|
||||
}
|
||||
|
||||
@@ -5,6 +5,6 @@ public class PrimaryWidgetConfiguration :
|
||||
{
|
||||
public static PrimaryWidgetConfiguration Defaults => new()
|
||||
{
|
||||
new KeyAcceleratorCommandConfiguration { Icon = "\uE720", Key = 91, Modifiers = [] }
|
||||
new KeyAcceleratorCommandConfiguration { Id = $"{Guid.NewGuid()}", Icon = "\uE720", Key = 91, Modifiers = [] }
|
||||
};
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class PrimaryWidgetConfigurationChangedHandler :
|
||||
INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>>
|
||||
{
|
||||
private readonly IMediator mediator;
|
||||
private readonly IEnumerable<IWidgetComponentViewModel> items;
|
||||
|
||||
public PrimaryWidgetConfigurationChangedHandler(IMediator mediator,
|
||||
IEnumerable<IWidgetComponentViewModel> items)
|
||||
{
|
||||
this.mediator = mediator;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public async ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await mediator.PublishAsync(new CollectionChanged<IEnumerable<IWidgetComponentViewModel>>(items),
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,11 @@ namespace Hyperbar.Windows.Primary;
|
||||
public class PrimaryWidgetProvider :
|
||||
IWidgetProvider
|
||||
{
|
||||
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
|
||||
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
|
||||
services.AddConfiguration<PrimaryWidgetConfiguration>()
|
||||
.AddHandler<WidgetComponentMapping>()
|
||||
.AddHandler<PrimaryWidgetConfigurationChangedHandler>()
|
||||
.AddWidgetTemplate<PrimaryWidgetViewModel>();
|
||||
.AddTransient<IFactory<IEnumerable<IWidgetComponentViewModel>>, WidgetComponentViewModelFactory>()
|
||||
.AddWidgetTemplate<PrimaryWidgetViewModel>()
|
||||
.AddNotificationPipeline<ConfigurationChanged<PrimaryWidgetConfiguration>,
|
||||
ValueChanging<IEnumerable<IWidgetComponentViewModel>>>();
|
||||
|
||||
}
|
||||
@@ -4,8 +4,8 @@ namespace Hyperbar.Windows.Primary;
|
||||
public class PrimaryWidgetViewModel(ITemplateFactory templateFactory,
|
||||
IServiceFactory serviceFactory,
|
||||
IMediator mediator,
|
||||
IEnumerable<IWidgetComponentViewModel> items) :
|
||||
ObservableCollectionViewModel<IWidgetComponentViewModel>(serviceFactory, mediator, items),
|
||||
IFactory<IEnumerable<IWidgetComponentViewModel>> factory) :
|
||||
ObservableCollectionViewModel<IWidgetComponentViewModel>(serviceFactory, mediator, factory),
|
||||
IWidgetViewModel,
|
||||
ITemplatedViewModel
|
||||
{
|
||||
|
||||
@@ -3,5 +3,5 @@
|
||||
public class ProcessCommandConfiguration :
|
||||
PrimaryCommandConfiguration
|
||||
{
|
||||
public string? Path { get; set; }
|
||||
public required string Path { get; set; }
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class WidgetComponentMapping(PrimaryWidgetConfiguration configuration,
|
||||
IServiceFactory service,
|
||||
IMediator mediator) :
|
||||
IMappingHandler<PrimaryWidgetConfiguration, IEnumerable<IWidgetComponentViewModel>>
|
||||
{
|
||||
public IEnumerable<IWidgetComponentViewModel> Handle()
|
||||
{
|
||||
foreach (var item in configuration)
|
||||
{
|
||||
if (item is KeyAcceleratorCommandConfiguration keyAcceleratorCommandConfiguration)
|
||||
{
|
||||
yield return service.Create<WidgetButtonViewModel>(keyAcceleratorCommandConfiguration.Icon, new Action(async () =>
|
||||
await mediator.SendAsync(new KeyAcceleratorRequest((VirtualKey)keyAcceleratorCommandConfiguration.Key,
|
||||
keyAcceleratorCommandConfiguration.Modifiers?.Select(modifier => (VirtualKey)modifier).ToArray()))));
|
||||
}
|
||||
|
||||
if (item is ProcessCommandConfiguration processCommandConfiguration)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class WidgetComponentViewModelFactory(PrimaryWidgetConfiguration configuration,
|
||||
IServiceFactory service,
|
||||
IMediator mediator) :
|
||||
IFactory<IEnumerable<IWidgetComponentViewModel>>
|
||||
{
|
||||
public IEnumerable<IWidgetComponentViewModel> Create()
|
||||
{
|
||||
foreach (PrimaryCommandConfiguration item in configuration)
|
||||
{
|
||||
if (item is KeyAcceleratorCommandConfiguration keyAcceleratorCommandConfiguration)
|
||||
{
|
||||
yield return service.Create<WidgetButtonViewModel>(keyAcceleratorCommandConfiguration.Icon, new Action(async () =>
|
||||
await mediator.SendAsync(new KeyAcceleratorRequest((VirtualKey)keyAcceleratorCommandConfiguration.Key,
|
||||
keyAcceleratorCommandConfiguration.Modifiers?.Select(modifier => (VirtualKey)modifier).ToArray()))));
|
||||
}
|
||||
|
||||
if (item is ProcessCommandConfiguration processCommandConfiguration)
|
||||
{
|
||||
yield return service.Create<WidgetButtonViewModel>(processCommandConfiguration.Icon, new Action(async () =>
|
||||
await mediator.SendAsync(new ProcessRequest(processCommandConfiguration.Path))));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,16 @@ public static class IServiceCollectionExtensions
|
||||
return services.AddConfiguration<TConfiguration>(typeof(TConfiguration).Name, "Settings.json", null);
|
||||
}
|
||||
|
||||
public static IServiceCollection AddNotificationPipeline<TFromNotification, TToNotification>(this IServiceCollection services)
|
||||
where TFromNotification :
|
||||
INotification
|
||||
where TToNotification :
|
||||
INotification, new()
|
||||
{
|
||||
return services.AddHandler<NotficationPipelineHandler<TFromNotification, TToNotification>>();
|
||||
}
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
TConfiguration? defaults = null)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IFactory<T>
|
||||
{
|
||||
T Create();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public class NotficationPipelineHandler<TFromNotification, ToNotification>(IMediator mediator) :
|
||||
INotificationHandler<TFromNotification>,
|
||||
IHandler
|
||||
where TFromNotification :
|
||||
INotification
|
||||
where ToNotification :
|
||||
INotification, new()
|
||||
{
|
||||
private readonly IMediator mediator = mediator;
|
||||
|
||||
public ValueTask Handle(TFromNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
return mediator.PublishAsync(new ToNotification(),
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public record CollectionChanged<TCollection>(TCollection Items) : INotification where TCollection : IEnumerable;
|
||||
@@ -0,0 +1,5 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public record ValueChanging<TValue> : INotification;
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IObservableCollectionViewModel<TItem> :
|
||||
IList<TItem>,
|
||||
IList,
|
||||
IReadOnlyList<TItem>,
|
||||
INotifyCollectionChanged,
|
||||
INotificationHandler<ValueChanging<IEnumerable<TItem>>>;
|
||||
@@ -5,13 +5,9 @@ using System.Collections.Specialized;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public partial class ObservableCollectionViewModel<TItem> :
|
||||
public partial class ObservableCollectionViewModel<TItem> :
|
||||
ObservableObject,
|
||||
IList<TItem>,
|
||||
IList,
|
||||
IReadOnlyList<TItem>,
|
||||
INotifyCollectionChanged,
|
||||
INotificationHandler<CollectionChanged<IEnumerable<TItem>>>
|
||||
IObservableCollectionViewModel<TItem>
|
||||
{
|
||||
public ObservableCollection<TItem> collection = [];
|
||||
private readonly SynchronizationContext? context;
|
||||
@@ -28,6 +24,23 @@ public partial class ObservableCollectionViewModel<TItem> :
|
||||
collection.CollectionChanged += OnCollectionChanged;
|
||||
}
|
||||
|
||||
public ObservableCollectionViewModel(IServiceFactory serviceFactory,
|
||||
IMediator mediator,
|
||||
IFactory<IEnumerable<TItem>> factory)
|
||||
{
|
||||
context = SynchronizationContext.Current;
|
||||
|
||||
this.serviceFactory = serviceFactory;
|
||||
mediator.Subscribe(this);
|
||||
|
||||
collection.CollectionChanged += OnCollectionChanged;
|
||||
|
||||
if (factory is not null && factory.Create() is { } items)
|
||||
{
|
||||
AddRange(factory.Create());
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollectionViewModel(IServiceFactory serviceFactory,
|
||||
IMediator mediator,
|
||||
IEnumerable<TItem> items)
|
||||
@@ -158,14 +171,14 @@ public partial class ObservableCollectionViewModel<TItem> :
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)collection).GetEnumerator();
|
||||
|
||||
public ValueTask Handle(CollectionChanged<IEnumerable<TItem>> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
context?.Post(state => Clear(), null);
|
||||
AddRange(notification.Items);
|
||||
//public ValueTask Handle(CollectionChanged<IEnumerable<TItem>> notification,
|
||||
// CancellationToken cancellationToken)
|
||||
//{
|
||||
// context?.Post(state => Clear(), null);
|
||||
// AddRange(notification.Items);
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
// return ValueTask.CompletedTask;
|
||||
//}
|
||||
|
||||
public int IndexOf(TItem item) => collection.IndexOf(item);
|
||||
|
||||
@@ -212,9 +225,17 @@ public partial class ObservableCollectionViewModel<TItem> :
|
||||
|
||||
protected virtual void SetItem(int index, TItem item) => collection[index] = item;
|
||||
|
||||
private static bool IsCompatibleObject(object? value) => (value is TItem) || (value == null && default(TItem) == null);
|
||||
private static bool IsCompatibleObject(object? value) => (value is TItem) ||
|
||||
(value == null && default(TItem) == null);
|
||||
|
||||
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args) => CollectionChanged?.Invoke(this, args);
|
||||
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args) =>
|
||||
CollectionChanged?.Invoke(this, args);
|
||||
|
||||
public ValueTask Handle(ValueChanging<IEnumerable<TItem>> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class ObservableCollectionViewModel(IServiceFactory serviceFactory, IMediator mediator) :
|
||||
|
||||
Reference in New Issue
Block a user