This commit is contained in:
Dan Clark
2024-11-20 12:51:25 +00:00
parent 8963201480
commit 736fd2802b
17 changed files with 222 additions and 60 deletions
+3 -3
View File
@@ -19,7 +19,7 @@ public class ContentDialogHandler :
dialog.PrimaryButtonClick -= HandlePrimaryButtonClick; dialog.PrimaryButtonClick -= HandlePrimaryButtonClick;
if (dialog.DataContext is object content) if (dialog.DataContext is object content)
{ {
if (content is IPrimaryConfirmation primaryConfirmation) if (content is IAsyncPrimaryConfirmation primaryConfirmation)
{ {
Deferral deferral = args.GetDeferral(); Deferral deferral = args.GetDeferral();
if (!await primaryConfirmation.ConfirmPrimary()) if (!await primaryConfirmation.ConfirmPrimary())
@@ -39,7 +39,7 @@ public class ContentDialogHandler :
dialog.SecondaryButtonClick -= HandleSecondaryButtonClick; dialog.SecondaryButtonClick -= HandleSecondaryButtonClick;
if (dialog.DataContext is object content) if (dialog.DataContext is object content)
{ {
if (content is ISecondaryConfirmation secondaryConfirmation) if (content is IAsyncSecondaryConfirmation secondaryConfirmation)
{ {
Deferral deferral = args.GetDeferral(); Deferral deferral = args.GetDeferral();
if (!await secondaryConfirmation.ConfirmSecondary()) if (!await secondaryConfirmation.ConfirmSecondary())
@@ -63,7 +63,7 @@ public class ContentDialogHandler :
if (dialog.DataContext is object content) if (dialog.DataContext is object content)
{ {
bool cancelled = false; bool cancelled = false;
if (content is IConfirmation confirmation) if (content is IAsyncConfirmation confirmation)
{ {
Deferral deferral = args.GetDeferral(); Deferral deferral = args.GetDeferral();
if (!await confirmation.Confirm()) if (!await confirmation.Confirm())
+1 -1
View File
@@ -60,7 +60,7 @@ public class FrameHandler(ITransientNavigationStore<Frame> navigationStore) :
if (sender.DataContext is object content) if (sender.DataContext is object content)
{ {
if (content is IConfirmation confirmation && if (content is IAsyncConfirmation confirmation &&
!await confirmation.Confirm()) !await confirmation.Confirm())
{ {
args.Cancel = true; args.Cancel = true;
+1 -1
View File
@@ -26,7 +26,7 @@ public class TaskDialogHandler(ITopLevelProvider topLevelProvider) :
if (args.Result is TaskDialogResult result) if (args.Result is TaskDialogResult result)
{ {
if (result is TaskDialogResult.OK && content is if (result is TaskDialogResult.OK && content is
IPrimaryConfirmation primaryConfirmation) IAsyncPrimaryConfirmation primaryConfirmation)
{ {
Deferral deferral = args.GetDeferral(); Deferral deferral = args.GetDeferral();
if (!await primaryConfirmation.ConfirmPrimary()) if (!await primaryConfirmation.ConfirmPrimary())
@@ -7,15 +7,38 @@ public class AsyncHandlerInitialization<TMessage, TResponse, THandler>(IServiceP
IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse> IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class where TMessage : class
{ {
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, TResponse>>(provider, public void Initialize()
(provider, args) => args.Reply(provider.GetRequiredService<THandler>().Handle(args.Message, args.CancellationToken))); {
args.Reply(handler.Handle(args.Message, args.CancellationToken); if (!StrongReferenceMessenger.Default.IsRegistered<ResponseEventArgs<TMessage, TResponse>>(provider))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, TResponse>>(provider,
(provider, args) =>
{
foreach (IAsyncHandler<TMessage, TResponse> handler in provider.GetServices<IAsyncHandler<TMessage, TResponse>>())
{
args.Reply(handler.Handle(args.Message, args.CancellationToken));
}
});
}
}
} }
public class AsyncHandlerInitialization<TMessage, THandler>(IServiceProvider provider) : public class AsyncHandlerInitialization<TMessage, THandler>(IServiceProvider provider) :
IInitialization where THandler : class, IAsyncHandler<TMessage> IInitialization where THandler : class, IAsyncHandler<TMessage>
where TMessage : class where TMessage : class
{ {
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, Unit>>(provider, public void Initialize()
(provider, args) => provider.GetRequiredService<THandler>().Handle(args.Message, args.CancellationToken)); {
} if (!StrongReferenceMessenger.Default.IsRegistered<TMessage>(provider))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, Unit>>(provider,
(provider, args) =>
{
foreach (IAsyncHandler<TMessage> handler in provider.GetServices<IAsyncHandler<TMessage>>())
{
handler.Handle(args.Message, args.CancellationToken);
}
});
}
}
}
@@ -0,0 +1,44 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public class AsyncHandlerKeyedInitialization<TMessage, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IAsyncHandler<TMessage>
where TMessage : class
{
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<TMessage, string>(provider, key))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage, string>(provider, key,
(provider, args) =>
{
foreach (IAsyncHandler<TMessage> handler in provider.GetKeyedServices<IAsyncHandler<TMessage>>(key))
{
handler.Handle(args);
}
});
}
}
}
public class AsyncHandlerKeyedInitialization<TMessage, TResponse, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class
{
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<ResponseEventArgs<TMessage, TResponse>, string>(provider, key))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, ResponseEventArgs<TMessage, TResponse>, string>(provider, key,
(provider, args) =>
{
foreach (IAsyncHandler<TMessage, TResponse> handler in provider.GetKeyedServices<IAsyncHandler<TMessage, TResponse>>(key))
{
handler.Handle(args.Message);
}
});
}
}
}
+3 -3
View File
@@ -2,9 +2,9 @@
public record Create public record Create
{ {
public static CreateEventArgs<TSender> As<TSender>(TSender sender, params object[] parameters) => public static CreateEventArgs<TSender> As<TSender>(TSender sender) =>
new(sender, parameters); new(sender);
public static CreateEventArgs<TSender> As<TSender>(params object[] parameters) where TSender : new() => public static CreateEventArgs<TSender> As<TSender>(params object[] parameters) where TSender : new() =>
new(new TSender(), parameters); new(new TSender());
} }
+1 -2
View File
@@ -1,4 +1,3 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record CreateEventArgs<TSender>(TSender? Sender = default, public record CreateEventArgs<TSender>(TSender? Sender = default);
params object[] Parameters);
+28 -12
View File
@@ -7,22 +7,38 @@ public class HandlerInitialization<TMessage, TResponse, THandler>(IServiceProvid
IInitialization where THandler : class, IHandler<TMessage, TResponse> IInitialization where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class where TMessage : class
{ {
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, ResponseEventArgs<TMessage, TResponse>>(provider, public void Initialize()
(provider, args) => args.Reply(provider.GetRequiredService<THandler>().Handle(args.Message))); {
if (!StrongReferenceMessenger.Default.IsRegistered<ResponseEventArgs<TMessage, TResponse>>(provider))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, ResponseEventArgs<TMessage, TResponse>>(provider,
(provider, args) =>
{
foreach (IHandler<TMessage, TResponse> handler in provider.GetServices<IHandler<TMessage, TResponse>>())
{
handler.Handle(args.Message);
}
});
}
}
} }
public class HandlerInitialization<TMessage, THandler>(IServiceProvider provider) : public class HandlerInitialization<TMessage, THandler>(IServiceProvider provider) :
IInitialization where THandler : class, IHandler<TMessage> IInitialization where THandler : class, IHandler<TMessage>
where TMessage : class where TMessage : class
{ {
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage>(provider, public void Initialize()
(provider, args) => provider.GetRequiredService<THandler>().Handle(args)); {
if (!StrongReferenceMessenger.Default.IsRegistered<TMessage>(provider))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage>(provider,
(provider, args) =>
{
foreach (IHandler<TMessage> handler in provider.GetServices<IHandler<TMessage>>())
{
handler.Handle(args);
}
});
}
}
} }
public class HandlerKeyedInitialization<TMessage, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IHandler<TMessage>
where TMessage : class
{
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage, string>(provider, key,
(provider, args) => provider.GetRequiredKeyedService<THandler>(key).Handle(args));
}
@@ -0,0 +1,44 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public class HandlerKeyedInitialization<TMessage, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IHandler<TMessage>
where TMessage : class
{
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<TMessage, string>(provider, key))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage, string>(provider, key,
(provider, args) =>
{
foreach (IHandler<TMessage> handler in provider.GetKeyedServices<IHandler<TMessage>>(key))
{
handler.Handle(args);
}
});
}
}
}
public class HandlerKeyedInitialization<TMessage, TResponse, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class
{
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<ResponseEventArgs<TMessage, TResponse>, string>(provider, key))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, ResponseEventArgs<TMessage, TResponse>, string>(provider, key,
(provider, args) =>
{
foreach (IHandler<TMessage, TResponse> handler in provider.GetKeyedServices<IHandler<TMessage, TResponse>>(key))
{
handler.Handle(args.Message);
}
});
}
}
}
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public interface IConfirmation public interface IAsyncConfirmation
{ {
Task<bool> Confirm(); Task<bool> Confirm();
} }
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public interface IPrimaryConfirmation public interface IAsyncPrimaryConfirmation
{ {
Task<bool> ConfirmPrimary(); Task<bool> ConfirmPrimary();
} }
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public interface ISecondaryConfirmation public interface IAsyncSecondaryConfirmation
{ {
Task<bool> ConfirmSecondary(); Task<bool> ConfirmSecondary();
} }
-6
View File
@@ -1,6 +0,0 @@
namespace Toolkit.Foundation;
public interface IIndexed
{
public int Index { get; }
}
@@ -5,23 +5,51 @@ namespace Toolkit.Foundation;
public static class IServiceCollectionExtensions public static class IServiceCollectionExtensions
{ {
public static IServiceCollection AddAsyncHandler<TMessage, TResponse, THandler>(this IServiceCollection services, public static IServiceCollection AddAsyncHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient) string key)
where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class => AddAsyncHandler<TMessage, TResponse, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddAsyncHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null)
where THandler : class, IAsyncHandler<TMessage, TResponse> where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class where TMessage : class
{ {
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime)); if (key is { Length: > 0 })
services.AddInitialization<AsyncHandlerInitialization<TMessage, TResponse, THandler>>(); {
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage, TResponse>), key, typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerKeyedInitialization<TMessage, TResponse, IAsyncHandler<TMessage, TResponse>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage, TResponse>), typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerInitialization<TMessage, TResponse, IAsyncHandler<TMessage, TResponse>>>();
}
return services; return services;
} }
public static IServiceCollection AddAsyncHandler<TMessage, THandler>(this IServiceCollection services, public static IServiceCollection AddAsyncHandler<TMessage, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient) string key)
where THandler : class, IAsyncHandler<TMessage>
where TMessage : class => AddAsyncHandler<TMessage, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddAsyncHandler<TMessage, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null)
where THandler : class, IAsyncHandler<TMessage> where THandler : class, IAsyncHandler<TMessage>
where TMessage : class where TMessage : class
{ {
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime)); if (key is { Length: > 0 })
services.AddInitialization<AsyncHandlerInitialization<TMessage, THandler>>(); {
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage>), key, typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerInitialization<TMessage, IAsyncHandler<TMessage>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage>), typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerKeyedInitialization<TMessage, IAsyncHandler<TMessage>>>();
}
return services; return services;
} }
@@ -35,7 +63,7 @@ public static class IServiceCollectionExtensions
} }
public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services) public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services)
where TKey : notnull where TKey : notnull
where TValue : notnull where TValue : notnull
{ {
services.AddScoped<ICache<TKey, TValue>, Cache<TKey, TValue>>(); services.AddScoped<ICache<TKey, TValue>, Cache<TKey, TValue>>();
@@ -61,12 +89,26 @@ public static class IServiceCollectionExtensions
} }
public static IServiceCollection AddHandler<TMessage, TResponse, THandler>(this IServiceCollection services, public static IServiceCollection AddHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient) string key)
where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class => AddHandler<TMessage, TResponse, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null)
where THandler : class, IHandler<TMessage, TResponse> where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class where TMessage : class
{ {
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime)); if (key is { Length: > 0 })
services.AddInitialization<HandlerInitialization<TMessage, TResponse, THandler>>(); {
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage, TResponse>), key, typeof(THandler), lifetime));
services.AddInitialization<HandlerKeyedInitialization<TMessage, TResponse, IHandler<TMessage, TResponse>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage, TResponse>), typeof(THandler), lifetime));
services.AddInitialization<HandlerInitialization<TMessage, TResponse, IHandler<TMessage, TResponse>>>();
}
return services; return services;
} }
@@ -84,13 +126,13 @@ public static class IServiceCollectionExtensions
{ {
if (key is { Length: > 0}) if (key is { Length: > 0})
{ {
services.Add(new ServiceDescriptor(typeof(THandler), key, typeof(THandler), lifetime)); services.Add(new ServiceDescriptor(typeof(IHandler<TMessage>), key, typeof(THandler), lifetime));
services.AddInitialization<HandlerKeyedInitialization<TMessage, THandler>>(key); services.AddInitialization<HandlerKeyedInitialization<TMessage, IHandler<TMessage>>>(key);
} }
else else
{ {
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime)); services.Add(new ServiceDescriptor(typeof(IHandler<TMessage>), typeof(THandler), lifetime));
services.AddInitialization<HandlerInitialization<TMessage, THandler>>(); services.AddInitialization<HandlerInitialization<TMessage, IHandler<TMessage>>>();
} }
return services; return services;
@@ -147,7 +189,7 @@ public static class IServiceCollectionExtensions
public static IServiceCollection AddTemplate<TViewModel, TViewModelImplementation, TView>(this IServiceCollection services, public static IServiceCollection AddTemplate<TViewModel, TViewModelImplementation, TView>(this IServiceCollection services,
object? key = null, object? key = null,
ServiceLifetime serviceLifetime = ServiceLifetime.Transient, ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
params object[]? parameters) params object?[]? parameters)
{ {
Type viewModelType = typeof(TViewModel); Type viewModelType = typeof(TViewModel);
Type viewModelImplementationType = typeof(TViewModelImplementation); Type viewModelImplementationType = typeof(TViewModelImplementation);
@@ -190,7 +232,7 @@ public static class IServiceCollectionExtensions
Action<TValue?, TConfiguration> writeDelegate, Action<TValue?, TConfiguration> writeDelegate,
object? key = null, object? key = null,
ServiceLifetime serviceLifetime = ServiceLifetime.Transient, ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
params object[]? parameters) params object?[]? parameters)
{ {
parameters = [readDelegate, writeDelegate, .. parameters ?? Enumerable.Empty<object?>()]; parameters = [readDelegate, writeDelegate, .. parameters ?? Enumerable.Empty<object?>()];
return AddTemplate<TViewModel, TViewModel, TView>(services, key, serviceLifetime, parameters); return AddTemplate<TViewModel, TViewModel, TView>(services, key, serviceLifetime, parameters);
@@ -201,7 +243,7 @@ public static class IServiceCollectionExtensions
Action<TValue?, TConfiguration> writeDelegate, Action<TValue?, TConfiguration> writeDelegate,
object? key = null, object? key = null,
ServiceLifetime serviceLifetime = ServiceLifetime.Transient, ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
params object[]? parameters) params object?[]? parameters)
{ {
parameters = [readDelegate, writeDelegate, .. parameters ?? Enumerable.Empty<object?>()]; parameters = [readDelegate, writeDelegate, .. parameters ?? Enumerable.Empty<object?>()];
return AddTemplate<TViewModel, TViewModelImplementation, TView>(services, key, serviceLifetime, parameters); return AddTemplate<TViewModel, TViewModelImplementation, TView>(services, key, serviceLifetime, parameters);
+2 -2
View File
@@ -17,8 +17,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" /> <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" /> <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.241114003" />
<PackageReference Include="WinUIEx" Version="2.4.2" /> <PackageReference Include="WinUIEx" Version="2.5.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" /> <PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
</ItemGroup> </ItemGroup>
+3 -3
View File
@@ -30,7 +30,7 @@ public class ContentDialogHandler :
dialog.PrimaryButtonClick -= HandlePrimaryButtonClick; dialog.PrimaryButtonClick -= HandlePrimaryButtonClick;
if (dialog.DataContext is object content) if (dialog.DataContext is object content)
{ {
if (content is IPrimaryConfirmation primaryConfirmation) if (content is IAsyncPrimaryConfirmation primaryConfirmation)
{ {
ContentDialogButtonClickDeferral deferral = args.GetDeferral(); ContentDialogButtonClickDeferral deferral = args.GetDeferral();
if (!await primaryConfirmation.ConfirmPrimary()) if (!await primaryConfirmation.ConfirmPrimary())
@@ -50,7 +50,7 @@ public class ContentDialogHandler :
dialog.SecondaryButtonClick -= HandleSecondaryButtonClick; dialog.SecondaryButtonClick -= HandleSecondaryButtonClick;
if (dialog.DataContext is object content) if (dialog.DataContext is object content)
{ {
if (content is ISecondaryConfirmation secondaryConfirmation) if (content is IAsyncSecondaryConfirmation secondaryConfirmation)
{ {
ContentDialogButtonClickDeferral deferral = args.GetDeferral(); ContentDialogButtonClickDeferral deferral = args.GetDeferral();
if (!await secondaryConfirmation.ConfirmSecondary()) if (!await secondaryConfirmation.ConfirmSecondary())
@@ -73,7 +73,7 @@ public class ContentDialogHandler :
dialog.Closing -= HandleClosing; dialog.Closing -= HandleClosing;
if (dialog.DataContext is object content) if (dialog.DataContext is object content)
{ {
if (content is IConfirmation confirmation) if (content is IAsyncConfirmation confirmation)
{ {
ContentDialogClosingDeferral deferral = args.GetDeferral(); ContentDialogClosingDeferral deferral = args.GetDeferral();
if (!await confirmation.Confirm()) if (!await confirmation.Confirm())
+1 -1
View File
@@ -18,7 +18,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" /> <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" /> <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.241114003" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>