Files
Walleby/Wallet/CreateWalletHandler.cs
T
TheXamlGuy bc5023c8ac Bug fixes
2024-07-05 21:57:01 +01:00

37 lines
1.3 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Toolkit.Foundation;
namespace Wallet;
public class CreateWalletHandler(IWalletHostFactory componentFactory,
IPublisher publisher) :
IHandler<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>, bool>
{
public async Task<bool> Handle(CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>> args,
CancellationToken cancellationToken)
{
if (args.Sender is Wallet<(string, string, IImageDescriptor?)> wallet)
{
(string name, string password, IImageDescriptor? imageDescriptor) = wallet.Value;
if (name is { Length: > 0 } &&
password is { Length: > 0 })
{
if (componentFactory.Create(name) is IComponentHost host)
{
IWalletFactory walletFactory = host.Services.GetRequiredService<IWalletFactory>();
if (await walletFactory.Create(name, password, imageDescriptor))
{
host.Start();
publisher.Publish(Activated.As(new Wallet<IComponentHost>(host)));
return true;
}
}
}
}
return false;
}
}