using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Toolkit.Foundation; namespace Wallet; public class CreateWalletHandler(IWalletHostFactory componentFactory, IPublisher publisher) : IHandler>, bool> { public async Task Handle(CreateEventArgs> 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 factory = host.Services.GetRequiredService(); if (await factory.Create(name, password, imageDescriptor)) { host.Start(); publisher.Publish(Activated.As(new Wallet(host))); return true; } } } } return false; } }