prevent dup wallets

This commit is contained in:
TheXamlGuy
2024-07-23 18:20:58 +01:00
parent 8e0f1e6715
commit f30ee9fe70
4 changed files with 49 additions and 28 deletions
+8 -3
View File
@@ -60,7 +60,7 @@
Width="400" Width="400"
Margin="0,-24,0,0" Margin="0,-24,0,0"
IsEnabled="{Binding !IsActive}" IsEnabled="{Binding !IsActive}"
Spacing="18"> Spacing="16">
<Grid HorizontalAlignment="Center"> <Grid HorizontalAlignment="Center">
<PersonPicture <PersonPicture
Width="{StaticResource PersonPictureSize}" Width="{StaticResource PersonPictureSize}"
@@ -100,16 +100,21 @@
</DropDownButton> </DropDownButton>
</Grid> </Grid>
<StackPanel> <StackPanel>
<TextBlock Margin="0,0,0,6" Text="Name" /> <TextBlock Margin="0,0,0,8" Text="Name" />
<TextBox Text="{Binding Name}" Watermark="e.g. Personal" /> <TextBox Text="{Binding Name}" Watermark="e.g. Personal" />
</StackPanel> </StackPanel>
<StackPanel> <StackPanel>
<TextBlock Margin="0,0,0,6" Text="Password" /> <TextBlock Margin="0,0,0,8" Text="Password" />
<TextBox <TextBox
Classes="revealPasswordButton" Classes="revealPasswordButton"
PasswordChar="&#x25CF;" PasswordChar="&#x25CF;"
Text="{Binding Password}" /> Text="{Binding Password}" />
</StackPanel> </StackPanel>
<InfoBar
IsClosable="False"
IsOpen="{Binding Validation.Errors[Name], Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
Message="{Binding Validation.Errors[Name]}"
Severity="Error" />
</StackPanel> </StackPanel>
<ProgressRing <ProgressRing
Width="48" Width="48"
+28 -19
View File
@@ -4,36 +4,45 @@ using Toolkit.Foundation;
namespace Wallet; namespace Wallet;
public class CreateWalletHandler(IWalletHostFactory componentFactory, public class CreateWalletHandler(IHostEnvironment environment,
IWalletHostFactory componentFactory,
IWalletHostCollection wallets, IWalletHostCollection wallets,
IPublisher publisher) : IPublisher publisher) :
IHandler<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>, bool> IHandler<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>, Result>
{ {
public async Task<bool> Handle(CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>> args, public async Task<Result> Handle(CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>> args,
CancellationToken cancellationToken) 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 } && (string name, string password, IImageDescriptor? imageDescriptor) = wallet.Value;
password is { Length: > 0 }) 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<IWalletFactory>();
if (await walletFactory.Create(name, password, imageDescriptor))
{ {
if (componentFactory.Create(name) is IComponentHost host) wallets.Add(host);
{ host.Start();
IWalletFactory walletFactory = host.Services.GetRequiredService<IWalletFactory>();
if (await walletFactory.Create(name, password, imageDescriptor))
{
wallets.Add(host);
host.Start();
publisher.Publish(Activated.As(new Wallet<IComponentHost>(host))); publisher.Publish(Activated.As(new Wallet<IComponentHost>(host)));
return true;
}
}
} }
} }
return false; return Result.Success();
} }
} }
+10 -3
View File
@@ -50,8 +50,15 @@ public partial class CreateWalletViewModel :
{ {
using (await new ActivityLock(this)) using (await new ActivityLock(this))
{ {
IsConfirmed = await Mediator.Handle<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>, if (await Mediator.Handle<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>,
bool>(Create.As(new Wallet<(string, string, IImageDescriptor?)>((Name, Password, ImageDescriptor)))); 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; return IsConfirmed;
} }
@@ -68,7 +75,7 @@ public partial class CreateWalletViewModel :
{ {
if (args.PropertyName is string name) if (args.PropertyName is string name)
{ {
_ = Validation.Validate(name); Validation.Validate(name);
} }
base.OnPropertyChanged(args); base.OnPropertyChanged(args);
+3 -3
View File
@@ -42,9 +42,9 @@ public partial class OpenWalletViewModel :
{ {
using (await new ActivityLock(this)) using (await new ActivityLock(this))
{ {
if (await Validation.Validate(() => Password, [new ValidationRule(async () => bool result = await Mediator.Handle<OpenEventArgs<Wallet<string>>, bool>(Open.As(new Wallet<string>(Password)));
await Mediator.Handle<OpenEventArgs<Wallet<string>>, bool>(Open.As(new Wallet<string>(Password))), if (await Validation.Validate(() => Password, [new ValidationRule(() => result,
"The password is incorrect, please try again.")])) "The password is incorrect, please try again.")]))
{ {
Publisher.Publish(Opened.As<Wallet>()); Publisher.Publish(Opened.As<Wallet>());
} }