This commit is contained in:
TheXamlGuy
2024-07-17 21:43:58 +01:00
parent efd00ff81a
commit 93c7a43ab4
75 changed files with 231 additions and 183 deletions
+1 -1
View File
@@ -6,4 +6,4 @@ public record Activation
public static ActivationEventArgs<TSender> As<TSender>() =>
new();
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation;
public record ActivationBuilder(IActivation Value, object? Key = null);
public record ActivationBuilder(IActivation Value, object? Key = null);
+1
View File
@@ -193,6 +193,7 @@ public class Cache<TKey, TValue>(IComparer<TKey> comparer) :
value = default;
return false;
}
private class KeyValuePairComparer<TK, TV>(IComparer<TK> comparer) :
IComparer<KeyValuePair<TK, TV>>
{
+1 -1
View File
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation;
public record ChangedEventArgs<TSender>(TSender? Sender = default);
public record ChangedEventArgs<TSender>(TSender? Sender = default);
+1 -1
View File
@@ -5,4 +5,4 @@ public record Close
public static CloseEventArgs<TSender> As<TSender>(TSender sender) => new(sender);
public static CloseEventArgs<TSender> As<TSender>() where TSender : new() => new(new TSender());
}
}
+1 -2
View File
@@ -2,13 +2,12 @@
namespace Toolkit.Foundation;
public class Component :
IComponent
{
private readonly IComponentBuilder builder;
protected Component(IComponentBuilder builder) =>
protected Component(IComponentBuilder builder) =>
this.builder = builder;
public static TComponent? Create<TComponent>(IServiceProvider provider,
+2 -2
View File
@@ -3,7 +3,7 @@
public class ContentFactory(IServiceProvider provider,
IServiceFactory factory) : IContentFactory
{
public Task<object?> CreateAsync(IContentTemplateDescriptor descriptor,
public Task<object?> CreateAsync(IContentTemplateDescriptor descriptor,
object[] parameters)
{
object? content = parameters is { Length: > 0 }
@@ -24,4 +24,4 @@ public class ContentFactory(IServiceProvider provider,
return Task.FromResult<object?>(content);
}
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
namespace Toolkit.Foundation;
public record CreateEventArgs<TSender>(TSender? Sender = default,
public record CreateEventArgs<TSender>(TSender? Sender = default,
params object[] Parameters);
+1 -1
View File
@@ -7,4 +7,4 @@ public record Delete
public static DeleteEventArgs<TSender> As<TSender>() where TSender : new() =>
new(new TSender());
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation;
public record FileFilter(string Name, List<string> Extensions, bool AllowMultiple = false);
public record FileFilter(string Name, List<string> Extensions, bool AllowMultiple = false);
+1 -1
View File
@@ -3,4 +3,4 @@
public interface IClipboardWriter
{
Task Write<TContent>(TContent content);
}
}
@@ -4,4 +4,4 @@ public static class ICollectionSynchronizationExtensions
{
public static int IndexOf<TItem>(this ICollectionSynchronization<TItem> synchronization,
TItem item) => synchronization is IList<TItem> collection ? collection.IndexOf(item) : -1;
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Toolkit.Foundation;
public interface IComponentFactory
{
IComponentHost? Create<TComponent, TConfiguration>(string name,
TConfiguration? configuration = null,
TConfiguration? configuration = null,
Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent
where TConfiguration : ComponentConfiguration, new();
+2 -3
View File
@@ -1,9 +1,8 @@
namespace Toolkit.Foundation
namespace Toolkit.Foundation
{
public interface IContentFactory
{
Task<object?> CreateAsync(IContentTemplateDescriptor descriptor,
Task<object?> CreateAsync(IContentTemplateDescriptor descriptor,
object[] resolvedArguments);
}
}
+2 -2
View File
@@ -96,7 +96,7 @@ public static class IHostBuilderExtension
sections.Add(section);
}
}
foreach (string section in sections)
{
if (context.Properties.TryGetValue(section, out object? value))
@@ -112,7 +112,7 @@ public static class IHostBuilderExtension
configurations.Add(typeof(TConfiguration));
}
}
}
}
else
{
context.Properties.Add(section, new List<Type> { typeof(TConfiguration) });
+1 -1
View File
@@ -7,4 +7,4 @@ public interface IImageDescriptor
public int Width { get; }
public int Height { get; }
}
}
+4 -4
View File
@@ -2,8 +2,8 @@
public interface IImageReader
{
IImageDescriptor Get(Stream stream,
int width,
int height,
IImageDescriptor Get(Stream stream,
int width,
int height,
bool maintainAspectRatio = false);
}
}
+1 -1
View File
@@ -3,4 +3,4 @@
public interface IIndexed
{
public int Index { get; }
}
}
+1 -1
View File
@@ -3,4 +3,4 @@
public interface IInitialization
{
void Initialize();
}
}
+1 -1
View File
@@ -3,4 +3,4 @@
public interface IPrimaryConfirmation
{
Task<bool> ConfirmPrimary();
}
}
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public static class IServiceCollectionExtensions
{
public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services)
@@ -37,7 +38,7 @@ public static class IServiceCollectionExtensions
}
public static IServiceCollection AddHandler<THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null)
where THandler : IHandler
{
@@ -4,9 +4,9 @@ namespace Toolkit.Foundation;
public static class IServiceProviderExtensions
{
public static object GetRequiredKeyedService(this IServiceProvider provider,
Type serviceType,
Action<object> serviceDelegate,
public static object GetRequiredKeyedService(this IServiceProvider provider,
Type serviceType,
Action<object> serviceDelegate,
object? serviceKey)
{
object service = provider.GetRequiredKeyedService(serviceType, serviceKey);
@@ -14,4 +14,4 @@ public static class IServiceProviderExtensions
return service;
}
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ using System.Linq.Expressions;
namespace Toolkit.Foundation;
public interface IValidation :
public interface IValidation :
INotifyPropertyChanged
{
IReadOnlyDictionary<string, string> Errors { get; }
+1 -1
View File
@@ -8,4 +8,4 @@ public interface IValidatorCollection :
void Add(string key, Validator binder);
bool TryGet(string key, [MaybeNull] out Validator? value);
}
}
+5 -6
View File
@@ -30,7 +30,7 @@ public class Mediator(IHandlerProvider handlerProvider,
return default;
}
public async Task<object?> Handle(Type responseType,
public async Task<object?> Handle(Type responseType,
object message,
object? key = null,
CancellationToken cancellationToken = default)
@@ -57,7 +57,7 @@ public class Mediator(IHandlerProvider handlerProvider,
return default;
}
public async Task<List<object?>> HandleMany(Type responseType,
public async Task<List<object?>> HandleMany(Type responseType,
object message,
object? key = null,
CancellationToken cancellationToken = default)
@@ -97,7 +97,7 @@ public class Mediator(IHandlerProvider handlerProvider,
List<object?> handlers = GetHandlers(message, handlerType, key);
foreach (object? handler in handlers)
{
MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle",
MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle",
[messageType, typeof(CancellationToken)]);
if (handleMethod is not null)
@@ -127,7 +127,7 @@ public class Mediator(IHandlerProvider handlerProvider,
}
}
private List<object?> GetHandlers(object message,
private List<object?> GetHandlers(object message,
Type handlerWrapperType,
object? key)
{
@@ -151,7 +151,7 @@ public class Mediator(IHandlerProvider handlerProvider,
}
}
IEnumerable<object?> keyedServices = key is not null ? provider.GetKeyedServices(handlerWrapperType, key) :
IEnumerable<object?> keyedServices = key is not null ? provider.GetKeyedServices(handlerWrapperType, key) :
provider.GetServices(handlerWrapperType);
AddHandlers(keyedServices);
@@ -160,5 +160,4 @@ public class Mediator(IHandlerProvider handlerProvider,
return handlers.SelectMany(entry => entry.Value).ToList();
}
}
+1 -2
View File
@@ -2,5 +2,4 @@
public static class MediatorExtensions
{
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ public class NavigateBackHandler(IComponentScopeProvider provider) :
if (provider.Get(args.Scope ?? "Root")
is ComponentScopeDescriptor descriptor)
{
if (descriptor?.Services?.GetService<INavigation>() is
if (descriptor?.Services?.GetService<INavigation>() is
INavigation navigationScope)
{
navigationScope.Back(args.Context);
+1 -1
View File
@@ -27,7 +27,7 @@ public class Navigation(IServiceProvider provider,
{
currentSegmentIndex++;
if (provider.GetKeyedService<IContentTemplateDescriptor>(segment)
if (provider.GetKeyedService<IContentTemplateDescriptor>(segment)
is IContentTemplateDescriptor descriptor)
{
Dictionary<string, object>? arguments = parameters?.ToDictionary(x => x.Key, x => x.Value, StringComparer.InvariantCultureIgnoreCase) ?? [];
@@ -1,6 +1,5 @@
namespace Toolkit.Foundation;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class NotificationAttribute(Type type,
object key) : Attribute
+1 -1
View File
@@ -8,7 +8,7 @@ public static class ObjectExtensions
{
Type type = obj.GetType();
object? key = selector();
object? key = selector();
if (type.GetProperty($"{key}") is PropertyInfo property && property.GetValue(obj) is { } value)
{
return value;
+4 -7
View File
@@ -76,7 +76,6 @@ public partial class Observable(IServiceProvider provider,
public virtual void OnInitialize()
{
}
public virtual void Initialize()
@@ -129,10 +128,9 @@ public partial class Observable<TValue> :
protected virtual void OnValueChanged()
{
}
partial void OnValueChanged(TValue? value) => OnValueChanged();
private partial void OnValueChanged(TValue? value) => OnValueChanged();
}
public partial class Observable<TKey, TValue> :
@@ -148,8 +146,8 @@ public partial class Observable<TKey, TValue> :
public Observable(IServiceProvider provider,
IServiceFactory factory,
IMediator mediator,
IPublisher publisher,
ISubscriber subscriber,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer,
TKey key,
TValue? value = default) : base(provider, factory, mediator, publisher, subscriber, disposer)
@@ -160,8 +158,7 @@ public partial class Observable<TKey, TValue> :
protected virtual void OnValueChanged()
{
}
partial void OnValueChanged(TValue? value) => OnValueChanged();
private partial void OnValueChanged(TValue? value) => OnValueChanged();
}
+18 -22
View File
@@ -146,7 +146,7 @@ public partial class ObservableCollection<TItem> :
private Func<TItem>? defaultSelectionFactory;
public void SetSource(IList<TItem> source,
public void SetSource(IList<TItem> source,
Func<TItem>? defaultSelectionFactory)
{
foreach (TItem item in source)
@@ -167,7 +167,7 @@ public partial class ObservableCollection<TItem> :
}
}
private void SourceCollectionChanged(object? sender,
private void SourceCollectionChanged(object? sender,
NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
@@ -181,6 +181,7 @@ public partial class ObservableCollection<TItem> :
}
}
break;
case NotifyCollectionChangedAction.Remove:
if (args.OldItems is not null)
{
@@ -193,6 +194,7 @@ public partial class ObservableCollection<TItem> :
}
}
break;
case NotifyCollectionChangedAction.Reset:
Clear();
@@ -263,7 +265,6 @@ public partial class ObservableCollection<TItem> :
}
catch (InvalidCastException)
{
}
Add(item!);
@@ -296,7 +297,6 @@ public partial class ObservableCollection<TItem> :
Disposer.Dispose(item);
Disposer.Remove(this, item);
}
}
ClearItems();
@@ -351,7 +351,7 @@ public partial class ObservableCollection<TItem> :
Disposer.Dispose(this);
}
public void Activate(Func<ActivationBuilder> aggregateDelegate,
public void Activate(Func<ActivationBuilder> aggregateDelegate,
bool reset = false)
{
if (reset)
@@ -501,7 +501,6 @@ public partial class ObservableCollection<TItem> :
public virtual void OnInitialize()
{
}
public virtual void Initialize()
@@ -722,7 +721,7 @@ public partial class ObservableCollection<TItem> :
CollectionChanged?.Invoke(this, args);
}
partial void OnIsActivatedChanged(bool value)
private partial void OnIsActivatedChanged(bool value)
{
if (value)
{
@@ -734,7 +733,7 @@ public partial class ObservableCollection<TItem> :
}
}
partial void OnSelectedItemChanged(TItem? oldValue, TItem? newValue)
private partial void OnSelectedItemChanged(TItem? oldValue, TItem? newValue)
{
if (oldValue is ISelectable oldSelection)
{
@@ -749,10 +748,9 @@ public partial class ObservableCollection<TItem> :
Publisher.Publish(Selection.As(SelectedItem));
OnSelectedItemChanged();
}
protected virtual void OnSelectedItemChanged()
{
}
private void UpdateSelection(TItem item)
@@ -776,7 +774,7 @@ public partial class ObservableCollection<TValue, TViewModel>(IServiceProvider p
IServiceFactory factory,
IMediator mediator,
IPublisher publisher,
ISubscriber subscriber,
ISubscriber subscriber,
IDisposer disposer,
TValue value) : ObservableCollection<TViewModel>(provider, factory, mediator, publisher, subscriber, disposer)
where TViewModel : notnull, IDisposable
@@ -786,25 +784,24 @@ public partial class ObservableCollection<TValue, TViewModel>(IServiceProvider p
protected virtual void OnValueChanged()
{
}
partial void OnValueChanged(TValue value) => OnValueChanged();
private partial void OnValueChanged(TValue value) => OnValueChanged();
}
public partial class ObservableCollection<TViewModel, TKey, TValue> :
public partial class ObservableCollection<TViewModel, TKey, TValue> :
ObservableCollection<TViewModel>
where TViewModel : notnull, IDisposable
where TKey : notnull
{
{
[ObservableProperty]
private TKey key;
[ObservableProperty]
private TValue value;
public ObservableCollection(IServiceProvider provider,
IServiceFactory factory,
public ObservableCollection(IServiceProvider provider,
IServiceFactory factory,
IMediator mediator,
IPublisher publisher,
ISubscriber subscriber,
@@ -820,8 +817,8 @@ public partial class ObservableCollection<TViewModel, TKey, TValue> :
IServiceFactory factory,
IMediator mediator,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer,
ISubscriber subscriber,
IDisposer disposer,
IEnumerable<TViewModel> items,
TKey key,
TValue value) : base(provider, factory, mediator, publisher, subscriber, disposer, items)
@@ -832,13 +829,12 @@ public partial class ObservableCollection<TViewModel, TKey, TValue> :
protected virtual void OnValueChanged()
{
}
partial void OnValueChanged(TValue value) => OnValueChanged();
private partial void OnValueChanged(TValue value) => OnValueChanged();
}
public class ObservableCollection :
public class ObservableCollection :
ObservableCollection<IDisposable>
{
public ObservableCollection(IServiceProvider provider,
+1 -1
View File
@@ -5,4 +5,4 @@ public record Open
public static OpenEventArgs<TSender> As<TSender>(TSender sender) => new(sender);
public static OpenEventArgs<TSender> As<TSender>() where TSender : new() => new(new TSender());
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation;
public record OpenEventArgs<TSender>(TSender? Sender = default);
public record OpenEventArgs<TSender>(TSender? Sender = default);
+1 -1
View File
@@ -7,4 +7,4 @@ public class Read
public static ReadEventArgs<TSender> As<TSender>() where TSender : new() =>
new(new TSender());
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation;
public record ReadEventArgs<TSender>(TSender? Sender = default);
public record ReadEventArgs<TSender>(TSender? Sender = default);
+2 -1
View File
@@ -19,6 +19,7 @@ public static class TupleExtensions
(T3)Convert.ChangeType(parameters[2], typeof(T3)),
(T4)Convert.ChangeType(parameters[3], typeof(T4))
);
public static (T1, T2, T3, T4, T5) CreateValueTuple<T1, T2, T3, T4, T5>(this object[] parameters) => (
(T1)Convert.ChangeType(parameters[0], typeof(T1)),
(T2)Convert.ChangeType(parameters[1], typeof(T2)),
@@ -56,4 +57,4 @@ public static class TupleExtensions
(T7)Convert.ChangeType(parameters[6], typeof(T7)),
(T8)Convert.ChangeType(parameters[7], typeof(T8))
);
}
}
+8 -6
View File
@@ -9,15 +9,16 @@ public class Validation(IValidatorCollection validators) :
private readonly ValidationErrorCollection errors = [];
public event PropertyChangedEventHandler? PropertyChanged;
public IReadOnlyDictionary<string, string> Errors =>
public IReadOnlyDictionary<string, string> Errors =>
errors.AsReadOnly();
public bool HasErrors =>
public bool HasErrors =>
Errors.Count > 0;
internal IValidatorCollection Validators { get; } = validators;
public void Add<TProperty>(Expression<Func<TProperty>> property,
public void Add<TProperty>(Expression<Func<TProperty>> property,
ValidationRule[] rules,
ValidationTrigger trigger = ValidationTrigger.Deferred)
{
@@ -36,8 +37,7 @@ public class Validation(IValidatorCollection validators) :
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public async Task<bool> Validate<TProperty>(Expression<Func<TProperty>> property,
public async Task<bool> Validate<TProperty>(Expression<Func<TProperty>> property,
ValidationRule[] rules)
{
string? name = GetPropertyName(property);
@@ -109,11 +109,13 @@ public class Validation(IValidatorCollection validators) :
OnPropertyChanged(nameof(Errors), null, null);
}
}
private void Clear()
{
errors.Clear();
OnPropertyChanged(nameof(Errors), null, null);
}
private string GetPropertyName<T>(Expression<Func<T>> expression)
{
return expression.Body switch
@@ -123,4 +125,4 @@ public class Validation(IValidatorCollection validators) :
_ => string.Empty
};
}
}
}
+16 -16
View File
@@ -1,5 +1,5 @@
using System.Collections.Specialized;
using System.Collections;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
@@ -61,10 +61,10 @@ public class ValidationErrorCollection :
}
}
object? IDictionary.this[object key]
{
object? IDictionary.this[object key]
{
get => ((IDictionary)items)[key];
set => ((IDictionary)items)[key] = value;
set => ((IDictionary)items)[key] = value;
}
public void Add(string key, string value)
@@ -73,7 +73,7 @@ public class ValidationErrorCollection :
NotifyAdd(key, value);
}
void ICollection<KeyValuePair<string, string>>.Add(KeyValuePair<string, string> item) =>
void ICollection<KeyValuePair<string, string>>.Add(KeyValuePair<string, string> item) =>
Add(item.Key, item.Value);
void IDictionary.Add(object key, object? value) =>
@@ -93,24 +93,24 @@ public class ValidationErrorCollection :
bool ICollection<KeyValuePair<string, string>>.Contains(KeyValuePair<string, string> item) =>
items.Contains(item);
bool IDictionary.Contains(object key) =>
bool IDictionary.Contains(object key) =>
((IDictionary)items).Contains(key);
public bool ContainsKey(string key) => items.ContainsKey(key);
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex) =>
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex) =>
((IDictionary<string, string>)items).CopyTo(array, arrayIndex);
void ICollection.CopyTo(Array array, int index) =>
void ICollection.CopyTo(Array array, int index) =>
((ICollection)items).CopyTo(array, index);
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() =>
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() =>
items.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
IEnumerator IEnumerable.GetEnumerator() =>
items.GetEnumerator();
IDictionaryEnumerator IDictionary.GetEnumerator() =>
IDictionaryEnumerator IDictionary.GetEnumerator() =>
((IDictionary)items).GetEnumerator();
public bool Remove(string key)
@@ -131,10 +131,10 @@ public class ValidationErrorCollection :
}
}
public bool Contains(string key) =>
public bool Contains(string key) =>
items.ContainsKey(key);
bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, string> item) =>
bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, string> item) =>
Remove(item.Key);
void IDictionary.Remove(object key) => Remove((string)key);
@@ -146,7 +146,7 @@ public class ValidationErrorCollection :
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs($"Item[{key}]"));
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
new[] { new KeyValuePair<string, string>(key, value) }, -1));
}
}
}
+2 -2
View File
@@ -5,7 +5,7 @@ public class ValidationRule
private readonly Func<bool>? syncValidation;
private readonly Func<Task<bool>>? asyncValidation;
public ValidationRule(Func<bool> validation,
public ValidationRule(Func<bool> validation,
string message)
{
syncValidation = validation;
@@ -47,4 +47,4 @@ public class ValidationRule
}
public string Message { get; }
}
}
+1 -1
View File
@@ -4,4 +4,4 @@ public enum ValidationTrigger
{
Deferred,
Immediate
}
}
+1 -1
View File
@@ -19,4 +19,4 @@ public class Validator(string propertyName,
return (true, null);
}
}
}
+7 -7
View File
@@ -1,5 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Collections;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace Toolkit.Foundation;
@@ -10,15 +10,15 @@ public class ValidatorCollection :
public int Count => binders.Count;
public void Add(string key, Validator binder) =>
public void Add(string key, Validator binder) =>
binders.Add(key, binder);
public IEnumerator<Validator> GetEnumerator() =>
public IEnumerator<Validator> GetEnumerator() =>
binders.Select(x => x.Value).GetEnumerator();
public bool TryGet(string key, [MaybeNull] out Validator? value) =>
public bool TryGet(string key, [MaybeNull] out Validator? value) =>
binders.TryGetValue(key, out value);
IEnumerator IEnumerable.GetEnumerator() =>
IEnumerator IEnumerable.GetEnumerator() =>
binders.Select(x => x.Value).GetEnumerator();
}
}
+1 -1
View File
@@ -17,5 +17,5 @@ public partial class ValueViewModel<TValue>(IServiceProvider provider,
{
}
partial void OnValueChanged(TValue? value) => OnChanged(value);
private partial void OnValueChanged(TValue? value) => OnChanged(value);
}
+1 -1
View File
@@ -10,4 +10,4 @@ public class WriteClipboardHandler(IClipboardWriter clipboardWriter) :
await clipboardWriter.Write(clipboard.Value);
}
}
}
}