From f30ee9fe70cb39e51c8277ef5c1d1dfa16848bbb Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Tue, 23 Jul 2024 18:20:58 +0100 Subject: [PATCH] prevent dup wallets --- Wallet.Avalonia/CreateWalletView.axaml | 11 ++++-- Wallet/CreateWalletHandler.cs | 47 +++++++++++++++----------- Wallet/CreateWalletViewModel.cs | 13 +++++-- Wallet/OpenWalletViewModel.cs | 6 ++-- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/Wallet.Avalonia/CreateWalletView.axaml b/Wallet.Avalonia/CreateWalletView.axaml index 5ad551b..f778515 100644 --- a/Wallet.Avalonia/CreateWalletView.axaml +++ b/Wallet.Avalonia/CreateWalletView.axaml @@ -60,7 +60,7 @@ Width="400" Margin="0,-24,0,0" IsEnabled="{Binding !IsActive}" - Spacing="18"> + Spacing="16"> - + - + + >, bool> + IHandler>, Result> { - public async Task Handle(CreateEventArgs> args, + public async Task Handle(CreateEventArgs> args, CancellationToken cancellationToken) { - if (args.Sender is Wallet<(string, string, IImageDescriptor?)> wallet) + if (args.Sender is not Wallet<(string, string, IImageDescriptor?)> wallet) { - (string name, string password, IImageDescriptor? imageDescriptor) = wallet.Value; + return Result.Failure(Error.Failure); + } - if (name is { Length: > 0 } && - password is { Length: > 0 }) + (string name, string password, IImageDescriptor? imageDescriptor) = wallet.Value; + name = name.Trim(); + + if (Directory.Exists(Path.Combine(environment.ContentRootPath, "Wallet", name))) + { + return Result.Failure(Error.Duplicated); + } + + if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password)) + { + return Result.Failure(Error.Null); + } + + if (componentFactory.Create(name) is IComponentHost host) + { + IWalletFactory walletFactory = host.Services.GetRequiredService(); + if (await walletFactory.Create(name, password, imageDescriptor)) { - if (componentFactory.Create(name) is IComponentHost host) - { - IWalletFactory walletFactory = host.Services.GetRequiredService(); - if (await walletFactory.Create(name, password, imageDescriptor)) - { - wallets.Add(host); - host.Start(); + wallets.Add(host); + host.Start(); - publisher.Publish(Activated.As(new Wallet(host))); - return true; - } - } + publisher.Publish(Activated.As(new Wallet(host))); } } - return false; + return Result.Success(); } } \ No newline at end of file diff --git a/Wallet/CreateWalletViewModel.cs b/Wallet/CreateWalletViewModel.cs index c371165..5655090 100644 --- a/Wallet/CreateWalletViewModel.cs +++ b/Wallet/CreateWalletViewModel.cs @@ -50,8 +50,15 @@ public partial class CreateWalletViewModel : { using (await new ActivityLock(this)) { - IsConfirmed = await Mediator.Handle>, - bool>(Create.As(new Wallet<(string, string, IImageDescriptor?)>((Name, Password, ImageDescriptor)))); + if (await Mediator.Handle>, + Result>(Create.As(new Wallet<(string, string, IImageDescriptor?)>((Name, Password, ImageDescriptor)))) is Result result) + { + if (await Validation.Validate(() => Name, [new ValidationRule(() => result.Error != Error.Duplicated, + "This wallet name already exist.")])) + { + IsConfirmed = true; + } + } return IsConfirmed; } @@ -68,7 +75,7 @@ public partial class CreateWalletViewModel : { if (args.PropertyName is string name) { - _ = Validation.Validate(name); + Validation.Validate(name); } base.OnPropertyChanged(args); diff --git a/Wallet/OpenWalletViewModel.cs b/Wallet/OpenWalletViewModel.cs index f5e1ff3..3867111 100644 --- a/Wallet/OpenWalletViewModel.cs +++ b/Wallet/OpenWalletViewModel.cs @@ -42,9 +42,9 @@ public partial class OpenWalletViewModel : { using (await new ActivityLock(this)) { - if (await Validation.Validate(() => Password, [new ValidationRule(async () => - await Mediator.Handle>, bool>(Open.As(new Wallet(Password))), - "The password is incorrect, please try again.")])) + bool result = await Mediator.Handle>, bool>(Open.As(new Wallet(Password))); + if (await Validation.Validate(() => Password, [new ValidationRule(() => result, + "The password is incorrect, please try again.")])) { Publisher.Publish(Opened.As()); }