using Microsoft.Extensions.DependencyInjection; using Toolkit.Foundation; namespace Wallet; public class WalletActivatedHandler(IWalletHostCollection wallets, IPublisher publisher) : INotificationHandler>> { public Task Handle(ActivatedEventArgs> args) { if (args.Sender is Wallet wallet && wallet.Value is IComponentHost host) { List sortedWallets = [ .. wallets.OrderBy(wallet => { var descriptor = wallet.Services.GetRequiredService>(); return descriptor?.Name; }, Comparer.Create((x, y) => { bool xIsNumeric = int.TryParse(x, out int xNum); bool yIsNumeric = int.TryParse(y, out int yNum); return (xIsNumeric, yIsNumeric) switch { (true, true) => xNum.CompareTo(yNum), (true, false) => -1, (false, true) => 1, _ => string.Compare(x, y, StringComparison.Ordinal) }; })), ]; int index = sortedWallets.IndexOf(host); if (host.Services.GetRequiredService>() is ConfigurationDescriptor descriptor) { if (host.Services.GetRequiredService() is IServiceFactory serviceFactory) { IDecoratorService> profileImageDecorator = host.Services.GetRequiredService>>(); ProfileImage? profileImage = profileImageDecorator.Value; if (serviceFactory.Create(args => args.Initialize(), descriptor.Name, profileImage?.Value, false) is WalletNavigationViewModel viewModel) { publisher.Publish(Insert.As(index, viewModel), nameof(MainViewModel)); } } } } return Task.CompletedTask; } }