More refactoring

This commit is contained in:
TheXamlGuy
2024-01-11 19:52:48 +00:00
parent 7ccfedb5e2
commit 814c806240
16 changed files with 125 additions and 78 deletions
@@ -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()
+6
View File
@@ -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);
}
}
-5
View File
@@ -1,5 +0,0 @@
using System.Collections;
namespace Hyperbar;
public record CollectionChanged<TCollection>(TCollection Items) : INotification where TCollection : IEnumerable;
+5
View File
@@ -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>>>;
+36 -15
View File
@@ -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) :