Allow the Mediator to be subscribed to and therefore handled
This commit is contained in:
@@ -98,12 +98,13 @@ public partial class App : Application
|
|||||||
|
|
||||||
services.AddTemplate<ItemHeaderViewModel, ItemHeaderView>();
|
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.AddHandler<ItemActivatedHandler>();
|
||||||
|
|
||||||
services.AddScoped<IValueStore<Item>, ValueStore<Item>>();
|
|
||||||
services.AddHandler<ArchiveItemHandler>(ServiceLifetime.Scoped);
|
|
||||||
});
|
});
|
||||||
})!);
|
})!);
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
x:DataType="vm:ItemNavigationViewModel"
|
x:DataType="vm:ItemNavigationViewModel"
|
||||||
IsSelected="{Binding Selected}">
|
IsSelected="{Binding Selected}">
|
||||||
<Interaction.Behaviors>
|
<Interaction.Behaviors>
|
||||||
<EventTriggerBehavior EventName="Loaded">
|
<!--<EventTriggerBehavior EventName="Loaded">
|
||||||
<ConditionAction>
|
<ConditionAction>
|
||||||
<ConditionAction.Condition>
|
<ConditionAction.Condition>
|
||||||
<ConditionalExpression ForwardChaining="And">
|
<ConditionalExpression ForwardChaining="And">
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
</NavigateAction.ParameterBindings>
|
</NavigateAction.ParameterBindings>
|
||||||
</NavigateAction>
|
</NavigateAction>
|
||||||
</ConditionAction>
|
</ConditionAction>
|
||||||
</EventTriggerBehavior>
|
</EventTriggerBehavior>-->
|
||||||
<DataTriggerBehavior Binding="{Binding Selected}" Value="True">
|
<DataTriggerBehavior Binding="{Binding Selected}" Value="True">
|
||||||
<NavigateAction
|
<NavigateAction
|
||||||
Context="{Binding Named, StringFormat='{}{0}:ContentHeader'}"
|
Context="{Binding Named, StringFormat='{}{0}:ContentHeader'}"
|
||||||
|
|||||||
@@ -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
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,11 +11,18 @@ public partial class ItemHeaderViewModel(IServiceProvider provider,
|
|||||||
IDisposer disposer,
|
IDisposer disposer,
|
||||||
bool immutable,
|
bool immutable,
|
||||||
string? value = null) : ObservableViewModel<string, string>(provider, factory, mediator, publisher, subscriber, disposer, value),
|
string? value = null) : ObservableViewModel<string, string>(provider, factory, mediator, publisher, subscriber, disposer, value),
|
||||||
IItemViewModel
|
IHandler<ConfirmEventArgs<Item>, bool>
|
||||||
|
|
||||||
{
|
{
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private bool immutable = immutable;
|
private bool immutable = immutable;
|
||||||
|
|
||||||
|
public Task<bool> Handle(ConfirmEventArgs<Item> args,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return Task.FromResult(true);
|
||||||
|
}
|
||||||
|
|
||||||
public void Invoke(ItemConfiguration args) =>
|
public void Invoke(ItemConfiguration args) =>
|
||||||
args.Name = Value;
|
args.Name = Value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ using Toolkit.Foundation;
|
|||||||
namespace Bitvault;
|
namespace Bitvault;
|
||||||
|
|
||||||
public partial class ItemViewModel :
|
public partial class ItemViewModel :
|
||||||
ObservableCollectionViewModel<IItemViewModel>,
|
ObservableCollectionViewModel<IDisposable>
|
||||||
INotificationHandler<ConfirmEventArgs<Item>>
|
|
||||||
{
|
{
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private int? id;
|
private int? id;
|
||||||
@@ -37,15 +36,4 @@ public partial class ItemViewModel :
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IContentTemplate Template { get; set; }
|
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user