From 2c14001c2627bc9c08c4951c9ef59c109cc4185c Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Wed, 15 May 2024 19:28:07 +0100 Subject: [PATCH] Allow the Mediator to be subscribed to and therefore handled --- Bitvault.Avalonia/App.axaml.cs | 7 +- Bitvault.Avalonia/ItemNavigationView.axaml | 4 +- Bitvault/ArchiveItemHandler.cs | 34 ++++++++++ Bitvault/ConfirmItemHandler.cs | 51 +++++++++++++++ Bitvault/CreateItemHandler.cs | 76 ---------------------- Bitvault/ItemHeaderViewModel.cs | 9 ++- Bitvault/ItemViewModel.cs | 14 +--- 7 files changed, 100 insertions(+), 95 deletions(-) create mode 100644 Bitvault/ArchiveItemHandler.cs create mode 100644 Bitvault/ConfirmItemHandler.cs delete mode 100644 Bitvault/CreateItemHandler.cs diff --git a/Bitvault.Avalonia/App.axaml.cs b/Bitvault.Avalonia/App.axaml.cs index bf0f88e..226e612 100644 --- a/Bitvault.Avalonia/App.axaml.cs +++ b/Bitvault.Avalonia/App.axaml.cs @@ -98,12 +98,13 @@ public partial class App : Application services.AddTemplate(); + services.AddScoped, ValueStore>(); + + services.AddHandler(ServiceLifetime.Singleton); + services.AddHandler(ServiceLifetime.Scoped); - services.AddHandler(ServiceLifetime.Singleton); services.AddHandler(); - services.AddScoped, ValueStore>(); - services.AddHandler(ServiceLifetime.Scoped); }); })!); diff --git a/Bitvault.Avalonia/ItemNavigationView.axaml b/Bitvault.Avalonia/ItemNavigationView.axaml index 8ad49aa..a4ee662 100644 --- a/Bitvault.Avalonia/ItemNavigationView.axaml +++ b/Bitvault.Avalonia/ItemNavigationView.axaml @@ -6,7 +6,7 @@ x:DataType="vm:ItemNavigationViewModel" IsSelected="{Binding Selected}"> - + valueStore, + IDbContextFactory dbContextFactory) : + INotificationHandler> +{ + public async Task Handle(ArchiveEventArgs args) + { + try + { + if (valueStore.Value is Item item) + { + await Task.Run(async () => + { + using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync(); + + if (await context.FindAsync(item.Id) is ItemEntry result) + { + result.State = 3; + await context.SaveChangesAsync(); + } + }); + } + } + catch + { + + } + } +} \ No newline at end of file diff --git a/Bitvault/ConfirmItemHandler.cs b/Bitvault/ConfirmItemHandler.cs new file mode 100644 index 0000000..ebdfc6d --- /dev/null +++ b/Bitvault/ConfirmItemHandler.cs @@ -0,0 +1,51 @@ +using Bitvault.Data; +using Microsoft.EntityFrameworkCore; +using Toolkit.Foundation; + +namespace Bitvault; + +public class ConfirmItemHandler(IMediator mediator, + IDbContextFactory dbContextFactory, + IPublisher publisher) : + INotificationHandler> +{ + public async Task Handle(ConfirmEventArgs args, + CancellationToken cancellationToken) + { + await mediator.Handle, bool>(args); + //if (args.Value is ItemConfiguration configuration) + //{ + // try + // { + // using ContainerDbContext context = dbContextFactory.CreateDbContext(); + // EntityEntry? result = null; + + // await Task.Run(async () => + // { + // result = await context.AddAsync(new ItemEntry { Name = configuration.Name }, cancellationToken); + // await context.SaveChangesAsync(cancellationToken); + + // }, cancellationToken); + + // if (result is not null) + // { + // Item item = new() { Id = result.Entity.Id, Name = configuration.Name }; + // publisher.Publish(Activated.As(item), cancellationToken); + + // return true; + // } + // } + // catch + // { + + // } + //} + + return false; + } + + public async Task Handle(ConfirmEventArgs args) + { + await mediator.Handle, bool>(args); + } +} diff --git a/Bitvault/CreateItemHandler.cs b/Bitvault/CreateItemHandler.cs deleted file mode 100644 index 9544240..0000000 --- a/Bitvault/CreateItemHandler.cs +++ /dev/null @@ -1,76 +0,0 @@ -using Bitvault.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; -using Microsoft.Extensions.Configuration; -using Toolkit.Foundation; - -namespace Bitvault; - -public class ArchiveItemHandler(IValueStore valueStore, - IDbContextFactory dbContextFactory) : - INotificationHandler> -{ - public async Task Handle(ArchiveEventArgs args) - { - try - { - if (valueStore.Value is Item item) - { - await Task.Run(async () => - { - using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync(); - - if (await context.FindAsync(item.Id) is ItemEntry result) - { - result.State = 3; - await context.SaveChangesAsync(); - } - }); - } - } - catch - { - - } - } -} - - -public class CreateItemHandler(IDbContextFactory dbContextFactory, - IPublisher publisher) : - IHandler, bool> -{ - public async Task Handle(CreateEventArgs args, - CancellationToken cancellationToken) - { - if (args.Value is ItemConfiguration configuration) - { - try - { - using ContainerDbContext context = dbContextFactory.CreateDbContext(); - EntityEntry? result = null; - - await Task.Run(async () => - { - result = await context.AddAsync(new ItemEntry { Name = configuration.Name }, cancellationToken); - await context.SaveChangesAsync(cancellationToken); - - }, cancellationToken); - - if (result is not null) - { - Item item = new() { Id = result.Entity.Id, Name = configuration.Name }; - publisher.Publish(Activated.As(item), cancellationToken); - - return true; - } - } - catch - { - - } - } - - return false; - } -} diff --git a/Bitvault/ItemHeaderViewModel.cs b/Bitvault/ItemHeaderViewModel.cs index d930887..7c2a821 100644 --- a/Bitvault/ItemHeaderViewModel.cs +++ b/Bitvault/ItemHeaderViewModel.cs @@ -11,11 +11,18 @@ public partial class ItemHeaderViewModel(IServiceProvider provider, IDisposer disposer, bool immutable, string? value = null) : ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer, value), - IItemViewModel + IHandler, bool> + { [ObservableProperty] private bool immutable = immutable; + public Task Handle(ConfirmEventArgs args, + CancellationToken cancellationToken) + { + return Task.FromResult(true); + } + public void Invoke(ItemConfiguration args) => args.Name = Value; } diff --git a/Bitvault/ItemViewModel.cs b/Bitvault/ItemViewModel.cs index 2377547..30fb146 100644 --- a/Bitvault/ItemViewModel.cs +++ b/Bitvault/ItemViewModel.cs @@ -4,8 +4,7 @@ using Toolkit.Foundation; namespace Bitvault; public partial class ItemViewModel : - ObservableCollectionViewModel, - INotificationHandler> + ObservableCollectionViewModel { [ObservableProperty] private int? id; @@ -37,15 +36,4 @@ public partial class ItemViewModel : } public IContentTemplate Template { get; set; } - - public async Task Handle(ConfirmEventArgs args) - { - ItemConfiguration configuration = new(); - foreach (IItemViewModel item in this) - { - item.Invoke(configuration); - } - - await Mediator.Handle, bool>(Create.As(configuration)); - } }