Fixed more edge cases

This commit is contained in:
TheXamlGuy
2024-05-16 21:41:48 +01:00
parent 8c0436644a
commit 7f3c4c53cc
16 changed files with 93 additions and 33 deletions
+1 -2
View File
@@ -32,9 +32,8 @@ public class ComponentBuilder :
services.AddScoped<SubscriptionCollection>(); services.AddScoped<SubscriptionCollection>();
services.AddTransient<IHandlerProvider, HandlerProvider>(); services.AddTransient<IHandlerProvider, HandlerProvider>();
services.AddTransient<ISubscription, Subscription>(); services.AddScoped<ISubscription, Subscription>();
services.AddTransient<ISubscription, Subscription>();
services.AddTransient<IPublisher, Publisher>(); services.AddTransient<IPublisher, Publisher>();
services.AddTransient<IMediator, Mediator>(); services.AddTransient<IMediator, Mediator>();
+1
View File
@@ -7,6 +7,7 @@ public class HandlerProvider(SubscriptionCollection subscriptions) :
object? key = null) object? key = null)
{ {
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{type}"; string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{type}";
var d = subscriptions;
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers)) if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
{ {
foreach (WeakReference weakRef in subscribers.ToArray()) foreach (WeakReference weakRef in subscribers.ToArray())
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IDisposerRequired
{
IDisposer Disposer { get; }
}
+5 -6
View File
@@ -1,9 +1,8 @@
namespace Toolkit.Foundation namespace Toolkit.Foundation;
public interface IHandlerProvider
{ {
public interface IHandlerProvider IEnumerable<object?> Get(Type type,
{ object? key = null);
IEnumerable<object?> Get(Type type,
object? key = null);
}
} }
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IMediatorRequired
{
IMediator Mediator { get; }
}
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IPublisherRequired
{
IPublisher Publisher { get; }
}
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IServiceFactoryRequired
{
IServiceFactory Factory { get; }
}
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IServiceProviderRequired
{
IServiceProvider Provider { get; }
}
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface ISubscriptionRequired
{
ISubscription Subscription { get; }
}
+2 -1
View File
@@ -40,6 +40,7 @@ public class Mediator(IHandlerProvider handlerProvider,
handlerList = []; handlerList = [];
handlers.Add(handlerType, handlerList); handlers.Add(handlerType, handlerList);
} }
handlerList.Add(handler); handlerList.Add(handler);
} }
} }
@@ -48,7 +49,7 @@ public class Mediator(IHandlerProvider handlerProvider,
{ {
foreach (object? handler in handlerEntry.Value) foreach (object? handler in handlerEntry.Value)
{ {
if (handler?.GetType().GetMethod("Handle") is MethodInfo handleMethod) if (handler?.GetType().GetMethod("Handle", [messageType, typeof(CancellationToken)]) is MethodInfo handleMethod)
{ {
return await (Task<TResponse?>)handleMethod.Invoke(handler, return await (Task<TResponse?>)handleMethod.Invoke(handler,
new object[] { message, cancellationToken })!; new object[] { message, cancellationToken })!;
+3 -4
View File
@@ -3,16 +3,15 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public class NavigateHandler(NamedComponent scope, public class NavigateHandler(NamedComponent scope,
IComponentScopeProvider componentScopeProvider, IComponentScopeProvider componentScopeProvider) :
IServiceProvider provider) :
INotificationHandler<NavigateEventArgs> INotificationHandler<NavigateEventArgs>
{ {
public Task Handle(NavigateEventArgs args) public Task Handle(NavigateEventArgs args)
{ {
INavigationScope? navigationScope; INavigationScope? navigationScope;
if (args.Scope == "self") if (args.Scope == "self" && args.Sender is IServiceProviderRequired requireServiceProvider)
{ {
navigationScope = provider.GetRequiredService<INavigationScope>(); navigationScope = requireServiceProvider.Provider.GetRequiredService<INavigationScope>();
} }
else else
{ {
+21 -19
View File
@@ -10,8 +10,11 @@ public class NavigationScope(IPublisher publisher,
IContentTemplateDescriptorProvider contentTemplateDescriptorProvider) : IContentTemplateDescriptorProvider contentTemplateDescriptorProvider) :
INavigationScope INavigationScope
{ {
public void Navigate(string route, object? sender = null, object? context = null, public void Navigate(string route,
EventHandler? navigated = null, object[]? parameters = null) object? sender = null,
object? context = null,
EventHandler? navigated = null,
object[]? parameters = null)
{ {
string[] segments = route.Split('/'); string[] segments = route.Split('/');
int segmentCount = segments.Length; int segmentCount = segments.Length;
@@ -41,26 +44,25 @@ public class NavigationScope(IPublisher publisher,
if (provider.GetRequiredKeyedService(descriptor.TemplateType, segment) is object view) if (provider.GetRequiredKeyedService(descriptor.TemplateType, segment) is object view)
{ {
if ((parameters is { Length: > 0 } if (context is not null)
{
if (navigationContextProvider.TryGet(context, out object? scopedContext))
{
context = scopedContext;
}
}
else
{
context = view;
}
if (context is not null)
{
if ((parameters is { Length: > 0 }
? factory.Create(descriptor.ContentType, parameters) ? factory.Create(descriptor.ContentType, parameters)
: provider.GetRequiredKeyedService(descriptor.ContentType, segment)) is object viewModel) : provider.GetRequiredKeyedService(descriptor.ContentType, segment)) is object viewModel)
{
if (context is not null)
{ {
if (navigationContextProvider.TryGet(context, out object? scopedContext)) if (navigationProvider.Get(context is Type type ? type : context.GetType()) is INavigation navigation)
{
context = scopedContext;
}
}
else
{
context = view;
}
if (context is not null)
{
if (navigationProvider.Get(context is Type type ? type : context.GetType())
is INavigation navigation)
{ {
Type navigateType = typeof(NavigateEventArgs<>).MakeGenericType(navigation.Type); Type navigateType = typeof(NavigateEventArgs<>).MakeGenericType(navigation.Type);
if (Activator.CreateInstance(navigateType, [context, view, viewModel, sender, parameters]) is object navigate) if (Activator.CreateInstance(navigateType, [context, view, viewModel, sender, parameters]) is object navigate)
@@ -19,6 +19,11 @@ public partial class ObservableCollectionViewModel<TViewModel> :
IList, IList,
IReadOnlyList<TViewModel>, IReadOnlyList<TViewModel>,
INotifyCollectionChanged, INotifyCollectionChanged,
IServiceProviderRequired,
IServiceFactoryRequired,
IMediatorRequired,
IPublisherRequired,
IDisposerRequired,
INotificationHandler<RemoveEventArgs<TViewModel>>, INotificationHandler<RemoveEventArgs<TViewModel>>,
INotificationHandler<CreateEventArgs<TViewModel>>, INotificationHandler<CreateEventArgs<TViewModel>>,
INotificationHandler<InsertEventArgs<TViewModel>>, INotificationHandler<InsertEventArgs<TViewModel>>,
+6 -1
View File
@@ -10,7 +10,12 @@ public partial class ObservableViewModel :
IDeactivating, IDeactivating,
IDeactivated, IDeactivated,
IDeactivatable, IDeactivatable,
IDisposable IDisposable,
IServiceProviderRequired,
IServiceFactoryRequired,
IMediatorRequired,
IPublisherRequired,
IDisposerRequired
{ {
[ObservableProperty] [ObservableProperty]
private bool isInitialized; private bool isInitialized;
+10
View File
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public record Validation
{
public static ValidationEventArgs<TValue> As<TValue>(TValue value) =>
new(value);
public static ValidationEventArgs<TValue> As<TValue>() where TValue : new() =>
new(new TValue());
}
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record ValidationEventArgs<TValue>(TValue Value);