Allow the Mediator to be subscribed to and therefore handled

This commit is contained in:
TheXamlGuy
2024-05-15 19:28:07 +01:00
parent 98896267dc
commit 2c14001c26
7 changed files with 100 additions and 95 deletions
+4 -3
View File
@@ -98,12 +98,13 @@ public partial class App : Application
services.AddTemplate<ItemHeaderViewModel, ItemHeaderView>();
services.AddScoped<IValueStore<Item>, ValueStore<Item>>();
services.AddHandler<ConfirmItemHandler>(ServiceLifetime.Singleton);
services.AddHandler<ArchiveItemHandler>(ServiceLifetime.Scoped);
services.AddHandler<CreateItemHandler>(ServiceLifetime.Singleton);
services.AddHandler<ItemActivatedHandler>();
services.AddScoped<IValueStore<Item>, ValueStore<Item>>();
services.AddHandler<ArchiveItemHandler>(ServiceLifetime.Scoped);
});
})!);
+2 -2
View File
@@ -6,7 +6,7 @@
x:DataType="vm:ItemNavigationViewModel"
IsSelected="{Binding Selected}">
<Interaction.Behaviors>
<EventTriggerBehavior EventName="Loaded">
<!--<EventTriggerBehavior EventName="Loaded">
<ConditionAction>
<ConditionAction.Condition>
<ConditionalExpression ForwardChaining="And">
@@ -25,7 +25,7 @@
</NavigateAction.ParameterBindings>
</NavigateAction>
</ConditionAction>
</EventTriggerBehavior>
</EventTriggerBehavior>-->
<DataTriggerBehavior Binding="{Binding Selected}" Value="True">
<NavigateAction
Context="{Binding Named, StringFormat='{}{0}:ContentHeader'}"
+34
View File
@@ -0,0 +1,34 @@
using Bitvault.Data;
using Microsoft.EntityFrameworkCore;
using Toolkit.Foundation;
namespace Bitvault;
public class ArchiveItemHandler(IValueStore<Item> valueStore,
IDbContextFactory<ContainerDbContext> dbContextFactory) :
INotificationHandler<ArchiveEventArgs<Item>>
{
public async Task Handle(ArchiveEventArgs<Item> args)
{
try
{
if (valueStore.Value is Item item)
{
await Task.Run(async () =>
{
using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync();
if (await context.FindAsync<ItemEntry>(item.Id) is ItemEntry result)
{
result.State = 3;
await context.SaveChangesAsync();
}
});
}
}
catch
{
}
}
}
+51
View File
@@ -0,0 +1,51 @@
using Bitvault.Data;
using Microsoft.EntityFrameworkCore;
using Toolkit.Foundation;
namespace Bitvault;
public class ConfirmItemHandler(IMediator mediator,
IDbContextFactory<ContainerDbContext> dbContextFactory,
IPublisher publisher) :
INotificationHandler<ConfirmEventArgs<Item>>
{
public async Task<bool> Handle(ConfirmEventArgs<Item> args,
CancellationToken cancellationToken)
{
await mediator.Handle<ConfirmEventArgs<Item>, bool>(args);
//if (args.Value is ItemConfiguration configuration)
//{
// try
// {
// using ContainerDbContext context = dbContextFactory.CreateDbContext();
// EntityEntry<ItemEntry>? 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<Item> args)
{
await mediator.Handle<ConfirmEventArgs<Item>, bool>(args);
}
}
-76
View File
@@ -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<Item> valueStore,
IDbContextFactory<ContainerDbContext> dbContextFactory) :
INotificationHandler<ArchiveEventArgs<Item>>
{
public async Task Handle(ArchiveEventArgs<Item> args)
{
try
{
if (valueStore.Value is Item item)
{
await Task.Run(async () =>
{
using ContainerDbContext context = await dbContextFactory.CreateDbContextAsync();
if (await context.FindAsync<ItemEntry>(item.Id) is ItemEntry result)
{
result.State = 3;
await context.SaveChangesAsync();
}
});
}
}
catch
{
}
}
}
public class CreateItemHandler(IDbContextFactory<ContainerDbContext> dbContextFactory,
IPublisher publisher) :
IHandler<CreateEventArgs<ItemConfiguration>, bool>
{
public async Task<bool> Handle(CreateEventArgs<ItemConfiguration> args,
CancellationToken cancellationToken)
{
if (args.Value is ItemConfiguration configuration)
{
try
{
using ContainerDbContext context = dbContextFactory.CreateDbContext();
EntityEntry<ItemEntry>? 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;
}
}
+8 -1
View File
@@ -11,11 +11,18 @@ public partial class ItemHeaderViewModel(IServiceProvider provider,
IDisposer disposer,
bool immutable,
string? value = null) : ObservableViewModel<string, string>(provider, factory, mediator, publisher, subscriber, disposer, value),
IItemViewModel
IHandler<ConfirmEventArgs<Item>, bool>
{
[ObservableProperty]
private bool immutable = immutable;
public Task<bool> Handle(ConfirmEventArgs<Item> args,
CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
public void Invoke(ItemConfiguration args) =>
args.Name = Value;
}
+1 -13
View File
@@ -4,8 +4,7 @@ using Toolkit.Foundation;
namespace Bitvault;
public partial class ItemViewModel :
ObservableCollectionViewModel<IItemViewModel>,
INotificationHandler<ConfirmEventArgs<Item>>
ObservableCollectionViewModel<IDisposable>
{
[ObservableProperty]
private int? id;
@@ -37,15 +36,4 @@ public partial class ItemViewModel :
}
public IContentTemplate Template { get; set; }
public async Task Handle(ConfirmEventArgs<Item> args)
{
ItemConfiguration configuration = new();
foreach (IItemViewModel item in this)
{
item.Invoke(configuration);
}
await Mediator.Handle<CreateEventArgs<ItemConfiguration>, bool>(Create.As(configuration));
}
}