diff --git a/Wallet.Avalonia/App.axaml.cs b/Wallet.Avalonia/App.axaml.cs index f87e394..9067b93 100644 --- a/Wallet.Avalonia/App.axaml.cs +++ b/Wallet.Avalonia/App.axaml.cs @@ -86,6 +86,8 @@ public partial class App : Application services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(provider => { IEnumerable> items = @@ -95,6 +97,8 @@ public partial class App : Application return new ItemConfigurationCollection(items.ToDictionary(x => x.Name, x => (Func)(() => x.Value))); }); + services.TryAddSingleton>, DecoratorService>>(); + services.TryAddSingleton, DecoratorService>(); services.TryAddSingleton, DecoratorService>(); @@ -107,7 +111,7 @@ public partial class App : Application } }); - services.AddHandler(); + services.AddHandler(); services.AddHandler(); services.AddHandler(); @@ -221,7 +225,7 @@ public partial class App : Application services.AddInitializer(); services.AddHandler(); - services.AddHandler(); + services.AddHandler(); services.AddTemplate("Main"); services.AddHandler(); diff --git a/Wallet.Avalonia/OpenWalletView.axaml b/Wallet.Avalonia/OpenWalletView.axaml index e246c30..7c35ec9 100644 --- a/Wallet.Avalonia/OpenWalletView.axaml +++ b/Wallet.Avalonia/OpenWalletView.axaml @@ -12,7 +12,8 @@ + DisplayName="{Binding Name}" + ProfilePicture="{Binding ImageDescriptor.Image}" /> + @@ -29,9 +30,13 @@ + - + \ No newline at end of file diff --git a/Wallet/CreateProfileImageHandler.cs b/Wallet/CreateProfileImageHandler.cs new file mode 100644 index 0000000..d155347 --- /dev/null +++ b/Wallet/CreateProfileImageHandler.cs @@ -0,0 +1,24 @@ +using Toolkit.Foundation; + +namespace Wallet; + +public class CreateProfileImageHandler(IFileProvider fileProvider, + IImageReader imageReader) : + IHandler, IImageDescriptor?> +{ + public async Task Handle(CreateEventArgs args, + CancellationToken cancellationToken) + { + if (await fileProvider.SelectFiles(new FileFilter("Image files", ["jpg", "jpeg", "png"])) + is { Count: 1 } files) + { + if (files.FirstOrDefault() is string file) + { + using FileStream stream = File.OpenRead(file); + return imageReader.Get(stream, 200, 200, true); + } + } + + return default; + } +} diff --git a/Wallet/CreateWalletViewModel.cs b/Wallet/CreateWalletViewModel.cs index 3169502..f5d775c 100644 --- a/Wallet/CreateWalletViewModel.cs +++ b/Wallet/CreateWalletViewModel.cs @@ -62,8 +62,8 @@ public partial class CreateWalletViewModel : } [RelayCommand] - public async Task Import() => ImageDescriptor = await Mediator.Handle, - IImageDescriptor>(Read.As()); + public async Task Import() => ImageDescriptor = await Mediator.Handle, + IImageDescriptor>(Create.As()); protected override void OnPropertyChanged(PropertyChangedEventArgs args) { diff --git a/Wallet/OpenWalletViewModel.cs b/Wallet/OpenWalletViewModel.cs index 01eceda..50e3e70 100644 --- a/Wallet/OpenWalletViewModel.cs +++ b/Wallet/OpenWalletViewModel.cs @@ -13,6 +13,9 @@ public partial class OpenWalletViewModel : Observable [ObservableProperty] private string? name; + [ObservableProperty] + private IImageDescriptor imageDescriptor; + [MaybeNull] [ObservableProperty] private string password; @@ -24,10 +27,13 @@ public partial class OpenWalletViewModel : Observable IPublisher publisher, ISubscriber subscriber, IDisposer disposer, - string name) : base(provider, factory, mediator, publisher, subscriber, disposer) + string name, + IImageDescriptor imageDescriptor) : base(provider, factory, mediator, publisher, subscriber, disposer) { this.validation = validation; + Name = name; + ImageDescriptor = imageDescriptor; } [RelayCommand] diff --git a/Wallet/ReadProfileImageHandler.cs b/Wallet/ReadProfileImageHandler.cs deleted file mode 100644 index b8d39ef..0000000 --- a/Wallet/ReadProfileImageHandler.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Toolkit.Foundation; - -namespace Wallet; - -public class ReadProfileImageHandler(IFileProvider fileProvider, - IImageReader imageReader) : - IHandler, IImageDescriptor?> -{ - public async Task Handle(ReadEventArgs args, - CancellationToken cancellationToken) - { - if (await fileProvider.SelectFiles(new FileFilter("Image files", ["jpg", "jpeg", "png"])) is { Count: 1 } files) - { - if (files.FirstOrDefault() is string file) - { - await using FileStream stream = File.OpenRead(file); - return await imageReader.Get(stream, 200, 200, true); - } - } - - return default; - } -} diff --git a/Wallet/SynchronizeMainViewModelHandler.cs b/Wallet/SynchronizeMainViewModelHandler.cs index 4fcc87f..a8501d7 100644 --- a/Wallet/SynchronizeMainViewModelHandler.cs +++ b/Wallet/SynchronizeMainViewModelHandler.cs @@ -15,11 +15,15 @@ public class SynchronizeMainViewModelHandler(IPublisher publisher, is IConfigurationDescriptor descriptor ? descriptor.Name : null)) { if (Wallet.Services.GetRequiredService>() - is IConfigurationDescriptor descriptor) + is IConfigurationDescriptor configuration) { if (Wallet.Services.GetRequiredService() is IServiceFactory factory) { - if (factory.Create(args => args.Initialize(), descriptor.Name, selected) + IDecoratorService> profileImageDecorator = + Wallet.Services.GetRequiredService>>(); + ProfileImage? profileImage = profileImageDecorator.Service; + + if (factory.Create(args => args.Initialize(), configuration.Name, profileImage?.Value, selected) is WalletNavigationViewModel viewModel) { publisher.Publish(Create.As(viewModel), diff --git a/Wallet/WalletCollectionInitializer.cs b/Wallet/WalletCollectionInitializer.cs index 8b93ea7..bf88978 100644 --- a/Wallet/WalletCollectionInitializer.cs +++ b/Wallet/WalletCollectionInitializer.cs @@ -8,7 +8,7 @@ public class WalletCollectionInitializer(IHostEnvironment environment, IWalletHostCollection Wallets) : IInitialization { - public async Task Initialize() + public void Initialize() { foreach (string wallet in Directory.EnumerateDirectories(Path.Combine(environment.ContentRootPath, "Wallet"))) { @@ -16,11 +16,11 @@ public class WalletCollectionInitializer(IHostEnvironment environment, string section = $"Wallet:{name}"; if (componentFactory.Create(section) + WalletConfiguration>(section, new WalletConfiguration()) is IComponentHost host) { Wallets.Add(host); - await host.StartAsync(); + host.Start(); } } } diff --git a/Wallet/WalletComponent.cs b/Wallet/WalletComponent.cs index 1d1bb0d..9acbd93 100644 --- a/Wallet/WalletComponent.cs +++ b/Wallet/WalletComponent.cs @@ -10,9 +10,10 @@ public class WalletComponent(IHostEnvironment environment, public override IComponentBuilder Configuring(string key, IComponentBuilder builder) { + string path = Path.Combine(environment.ContentRootPath, key.Replace(":", "\\")); builder.SetComponentConfiguration(args => { - args.ContentRoot = Path.Combine(environment.ContentRootPath, key.Replace(":", "\\")); + args.ContentRoot = Path.Combine(path); }); return base.Configuring(key, builder); diff --git a/Wallet/WalletNavigationViewModel.cs b/Wallet/WalletNavigationViewModel.cs index 059cc6c..efd7a18 100644 --- a/Wallet/WalletNavigationViewModel.cs +++ b/Wallet/WalletNavigationViewModel.cs @@ -27,6 +27,9 @@ public partial class WalletNavigationViewModel : [ObservableProperty] private bool isActivated; + [ObservableProperty] + private IImageDescriptor imageDescriptor; + public WalletNavigationViewModel(IServiceProvider provider, IServiceFactory factory, IMediator mediator, @@ -35,10 +38,12 @@ public partial class WalletNavigationViewModel : IDisposer disposer, IContentTemplate template, string name, + IImageDescriptor imageDescriptor, bool isSelected) : base(provider, factory, mediator, publisher, subscriber, disposer) { Template = template; Name = name; + ImageDescriptor = imageDescriptor; IsSelected = isSelected; } diff --git a/Wallet/WalletProfileImageInitializer.cs b/Wallet/WalletProfileImageInitializer.cs new file mode 100644 index 0000000..d41aabf --- /dev/null +++ b/Wallet/WalletProfileImageInitializer.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.Hosting; +using Toolkit.Foundation; + +namespace Wallet; + +public class WalletProfileImageInitializer(IHostEnvironment environment, + IImageReader reader, + IDecoratorService> profileImageDecorator) : + IInitialization +{ + public void Initialize() + { + string file = Path.Combine(environment.ContentRootPath, "Thumbnail.png"); + if (File.Exists(file)) + { + using FileStream stream = File.OpenRead(file); + IImageDescriptor imageDescriptor = reader.Get(stream, 200, 200); + + profileImageDecorator.Set(new ProfileImage(imageDescriptor)); + } + } +}