Improve Publisher
This commit is contained in:
@@ -98,8 +98,12 @@ public partial class App : Application
|
||||
|
||||
services.AddTemplate<ItemHeaderViewModel, ItemHeaderView>();
|
||||
|
||||
|
||||
services.AddHandler<CreateItemHandler>(ServiceLifetime.Singleton);
|
||||
services.AddHandler<ItemActivatedHandler>();
|
||||
|
||||
services.AddScoped<IValueStore<Item>, ValueStore<Item>>();
|
||||
services.AddHandler<ArchiveItemHandler>(ServiceLifetime.Scoped);
|
||||
});
|
||||
})!);
|
||||
|
||||
|
||||
@@ -9,16 +9,18 @@ namespace Bitvault.Avalonia;
|
||||
public class AppHandler(IPublisher publisher) :
|
||||
INotificationHandler<StartedEventArgs>
|
||||
{
|
||||
public async Task Handle(StartedEventArgs args, CancellationToken cancellationToken = default)
|
||||
public Task Handle(StartedEventArgs args)
|
||||
{
|
||||
if (Application.Current is Application application)
|
||||
{
|
||||
if (application.ApplicationLifetime is IApplicationLifetime lifetime)
|
||||
{
|
||||
await publisher.Publish(new NavigateEventArgs(lifetime is IClassicDesktopStyleApplicationLifetime ? "MainWindow" : "Main",
|
||||
publisher.Publish(new NavigateEventArgs(lifetime is IClassicDesktopStyleApplicationLifetime ? "MainWindow" : "Main",
|
||||
lifetime is IClassicDesktopStyleApplicationLifetime ? typeof(IClassicDesktopStyleApplicationLifetime) :
|
||||
typeof(ISingleViewApplicationLifetime)), cancellationToken);
|
||||
typeof(ISingleViewApplicationLifetime)));
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
VerticalAlignment="Center"
|
||||
Command="{Binding InvokeCommand}"
|
||||
Foreground="{DynamicResource IconForegroundBrush}"
|
||||
HotKey="Delete"
|
||||
ToolTip.Tip="Archive">
|
||||
<TextBlock
|
||||
VerticalAlignment="Center"
|
||||
|
||||
@@ -11,5 +11,5 @@ public partial class ArchiveItemActionViewModel(IServiceProvider provider,
|
||||
IDisposer disposer) : ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
{
|
||||
[RelayCommand]
|
||||
public async Task Invoke() => await Publisher.Publish(Archive.As<Item>());
|
||||
public void Invoke() => Publisher.Publish(Archive.As<Item>());
|
||||
}
|
||||
|
||||
@@ -12,5 +12,5 @@ public partial class ConfirmItemActionViewModel(IServiceProvider provider,
|
||||
{
|
||||
|
||||
[RelayCommand]
|
||||
public async Task Invoke() => await Publisher.Publish(Confirm.As<Item>());
|
||||
public void Invoke() => Publisher.Publish(Confirm.As<Item>());
|
||||
}
|
||||
|
||||
@@ -8,8 +8,7 @@ public class ContainerActivatedHandler(IContainerHostCollection containers,
|
||||
IPublisher publisher) :
|
||||
INotificationHandler<ActivatedEventArgs<IComponentHost>>
|
||||
{
|
||||
public async Task Handle(ActivatedEventArgs<IComponentHost> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
public Task Handle(ActivatedEventArgs<IComponentHost> args)
|
||||
{
|
||||
if (args.Value is IComponentHost container)
|
||||
{
|
||||
@@ -24,11 +23,13 @@ public class ContainerActivatedHandler(IContainerHostCollection containers,
|
||||
{
|
||||
if (serviceFactory.Create<ContainerNavigationViewModel>(configuration.Name) is ContainerNavigationViewModel viewModel)
|
||||
{
|
||||
await publisher.Publish(new InsertEventArgs<IMainNavigationViewModel>(index, viewModel),
|
||||
nameof(MainViewModel), cancellationToken);
|
||||
publisher.Publish(new InsertEventArgs<IMainNavigationViewModel>(index, viewModel),
|
||||
nameof(MainViewModel));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,7 @@ public partial class ContainerHeaderViewModel : ObservableCollectionViewModel<st
|
||||
|
||||
public IContentTemplate Template { get; set; }
|
||||
|
||||
public Task Handle(RequestEventArgs<Filter<string>> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
public Task Handle(RequestEventArgs<Filter<string>> args)
|
||||
{
|
||||
if (args.Value is Filter<string> filter)
|
||||
{
|
||||
|
||||
@@ -41,8 +41,7 @@ public partial class ContainerNavigationViewModel :
|
||||
|
||||
public IContentTemplate Template { get; set; }
|
||||
|
||||
public Task Handle(OpenedEventArgs<Container> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
public Task Handle(OpenedEventArgs<Container> args)
|
||||
{
|
||||
Add<AllNavigationViewModel>("All");
|
||||
Add<StarredNavigationViewModel>("Starred");
|
||||
@@ -53,8 +52,7 @@ public partial class ContainerNavigationViewModel :
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(ClosedEventArgs<Container> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
public Task Handle(ClosedEventArgs<Container> args)
|
||||
{
|
||||
Opened = true;
|
||||
Clear();
|
||||
@@ -62,11 +60,9 @@ public partial class ContainerNavigationViewModel :
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(DeactivatedEventArgs<Container> args,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
public Task Handle(DeactivatedEventArgs<Container> args) =>
|
||||
Task.FromResult(Activated = false);
|
||||
|
||||
public Task Handle(ActivatedEventArgs<Container> args,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
public Task Handle(ActivatedEventArgs<Container> args) =>
|
||||
Task.FromResult(Activated = true);
|
||||
}
|
||||
@@ -26,24 +26,25 @@ public partial class ContainerViewModel(IServiceProvider provider,
|
||||
|
||||
public override async Task OnActivated()
|
||||
{
|
||||
await Publisher.Publish(Activated.As<Container>());
|
||||
Publisher.Publish(Activated.As<Container>());
|
||||
await base.OnActivated();
|
||||
}
|
||||
|
||||
public override async Task OnDeactivated()
|
||||
{
|
||||
await Publisher.Publish(Deactivated.As<Container>());
|
||||
Publisher.Publish(Deactivated.As<Container>());
|
||||
await base.OnDeactivated();
|
||||
}
|
||||
|
||||
public async Task Handle(RequestEventArgs<Filter<string>> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
public Task Handle(RequestEventArgs<Filter<string>> args)
|
||||
{
|
||||
if (args.Value is Filter<string> filter)
|
||||
{
|
||||
Filter = filter.Value;
|
||||
await Enumerate();
|
||||
Enumerate();
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
protected override IEnumerate PrepareEnumeration(object? key) =>
|
||||
|
||||
@@ -12,8 +12,7 @@ public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbC
|
||||
IPublisher publisher) :
|
||||
INotificationHandler<Enumerate<ItemNavigationViewModel, ContainerViewModelConfiguration>>
|
||||
{
|
||||
public async Task Handle(Enumerate<ItemNavigationViewModel, ContainerViewModelConfiguration> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
public async Task Handle(Enumerate<ItemNavigationViewModel, ContainerViewModelConfiguration> args)
|
||||
{
|
||||
if (args.Options is ContainerViewModelConfiguration configuration)
|
||||
{
|
||||
@@ -36,7 +35,7 @@ public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbC
|
||||
predicate = predicate.And(x => x.State == 3);
|
||||
}
|
||||
|
||||
var items = await Task.Run(async () =>
|
||||
var results = await Task.Run(async () =>
|
||||
{
|
||||
using ContainerDbContext context = dbContextFactory.CreateDbContext();
|
||||
return await context.Set<ItemEntry>().Where(predicate).Select(x => new
|
||||
@@ -44,19 +43,21 @@ public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbC
|
||||
x.Id,
|
||||
x.Name
|
||||
}).OrderBy(x => x.Name).ToListAsync();
|
||||
|
||||
}, cancellationToken);
|
||||
});
|
||||
|
||||
bool selected = true;
|
||||
foreach (var item in items)
|
||||
foreach (var result in results)
|
||||
{
|
||||
IServiceScope serviceScope = serviceProvider.CreateScope();
|
||||
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
|
||||
IValueStore<Item> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item>>();
|
||||
|
||||
if (serviceFactory.Create<ItemNavigationViewModel>(item.Id, item.Name, "Description " + 1, selected) is ItemNavigationViewModel viewModel)
|
||||
if (serviceFactory.Create<ItemNavigationViewModel>(result.Id, result.Name, "Description " + 1, selected) is ItemNavigationViewModel viewModel)
|
||||
{
|
||||
cache.Add(new Item { Id = item.Id, Name = item.Name });
|
||||
await publisher.Publish(Create.As(viewModel), nameof(ContainerViewModel), cancellationToken);
|
||||
Item item = new() { Id = result.Id, Name = result.Name };
|
||||
valueStore.Set(item);
|
||||
|
||||
publisher.Publish(Create.As(viewModel), nameof(ContainerViewModel));
|
||||
}
|
||||
|
||||
selected = false;
|
||||
|
||||
@@ -33,7 +33,7 @@ public class CreateContainerHandler(IContainerFactory componentFactory,
|
||||
configuration.Write(args => args.Key = $"{Convert.ToBase64String(key.Salt)}:{Convert.ToBase64String(key.EncryptedKey)}:{Convert.ToBase64String(key.DecryptedKey)}");
|
||||
host.Start();
|
||||
|
||||
await publisher.Publish(Activated.As(host), cancellationToken);
|
||||
publisher.Publish(Activated.As(host), cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,41 @@
|
||||
using Bitvault.Data;
|
||||
using HarfBuzzSharp;
|
||||
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>
|
||||
@@ -30,7 +60,7 @@ public class CreateItemHandler(IDbContextFactory<ContainerDbContext> dbContextFa
|
||||
if (result is not null)
|
||||
{
|
||||
Item item = new() { Id = result.Entity.Id, Name = configuration.Name };
|
||||
await publisher.Publish(Activated.As(item), cancellationToken);
|
||||
publisher.Publish(Activated.As(item), cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -11,5 +11,5 @@ public partial class DeleteItemActionViewModel(IServiceProvider provider,
|
||||
IDisposer disposer) : ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
{
|
||||
[RelayCommand]
|
||||
public async Task Invoke() => await Publisher.Publish(Delete.As<Item>());
|
||||
public void Invoke() => Publisher.Publish(Delete.As<Item>());
|
||||
}
|
||||
|
||||
@@ -11,6 +11,6 @@ public partial class EditItemActionViewModel(IServiceProvider provider,
|
||||
IDisposer disposer) : ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
{
|
||||
[RelayCommand]
|
||||
public async Task Invoke() => await Publisher.Publish(Edit.As<Item>());
|
||||
public void Invoke() => Publisher.Publish(Edit.As<Item>());
|
||||
}
|
||||
|
||||
|
||||
@@ -29,14 +29,12 @@ public partial class FilterContainerNavigationViewModel : ObservableViewModel,
|
||||
Filter = filter;
|
||||
}
|
||||
|
||||
public Task Handle(DeactivatedEventArgs<Container> args,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
public Task Handle(DeactivatedEventArgs<Container> args) =>
|
||||
Task.FromResult(Activated = false);
|
||||
|
||||
public Task Handle(ActivatedEventArgs<Container> args,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
public Task Handle(ActivatedEventArgs<Container> args) =>
|
||||
Task.FromResult(Activated = true);
|
||||
|
||||
[RelayCommand]
|
||||
public async Task Invoke() => await Publisher.Publish(Request.As(new Filter<string>(Filter)), nameof(ContainerViewModel));
|
||||
public void Invoke() => Publisher.Publish(Request.As(new Filter<string>(Filter)), nameof(ContainerViewModel));
|
||||
}
|
||||
@@ -2,4 +2,4 @@
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
public interface IContainerComponent : IComponent;
|
||||
public interface IContainerComponent : IComponent;
|
||||
@@ -5,6 +5,5 @@ public record Item
|
||||
public int Id { get; init; }
|
||||
|
||||
public string? Name { get; init; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -9,8 +9,7 @@ public class ItemActivatedHandler(IServiceProvider serviceProvider,
|
||||
IPublisher publisher) :
|
||||
INotificationHandler<ActivatedEventArgs<Item>>
|
||||
{
|
||||
public async Task Handle(ActivatedEventArgs<Item> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
public Task Handle(ActivatedEventArgs<Item> args)
|
||||
{
|
||||
if (args.Value is Item item)
|
||||
{
|
||||
@@ -20,10 +19,12 @@ public class ItemActivatedHandler(IServiceProvider serviceProvider,
|
||||
cache.Add(item);
|
||||
int index = cache.IndexOf(item);
|
||||
|
||||
if (serviceFactory.Create<ItemNavigationViewModel>(item.Id, item.Name, "Description " + 1) is ItemNavigationViewModel viewModel)
|
||||
if (serviceFactory.Create<ItemNavigationViewModel>(item.Id, item.Name, "Description " + 1, true) is ItemNavigationViewModel viewModel)
|
||||
{
|
||||
await publisher.Publish(Insert.As(index, viewModel), nameof(ContainerViewModel), cancellationToken);
|
||||
publisher.Publish(Insert.As(index, viewModel), nameof(ContainerViewModel));
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -36,8 +36,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
|
||||
|
||||
public IContentTemplate Template { get; set; } = template;
|
||||
|
||||
public Task Handle(ArchiveEventArgs<Item> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
public Task Handle(ArchiveEventArgs<Item> args)
|
||||
{
|
||||
Dispose();
|
||||
return Task.CompletedTask;
|
||||
|
||||
@@ -38,7 +38,7 @@ public partial class ItemViewModel :
|
||||
|
||||
public IContentTemplate Template { get; set; }
|
||||
|
||||
public async Task Handle(ConfirmEventArgs<Item> args, CancellationToken cancellationToken = default)
|
||||
public async Task Handle(ConfirmEventArgs<Item> args)
|
||||
{
|
||||
ItemConfiguration configuration = new();
|
||||
foreach (IItemViewModel item in this)
|
||||
@@ -46,6 +46,6 @@ public partial class ItemViewModel :
|
||||
item.Invoke(configuration);
|
||||
}
|
||||
|
||||
await Mediator.Handle<CreateEventArgs<ItemConfiguration>, bool>(Create.As(configuration), cancellationToken);
|
||||
await Mediator.Handle<CreateEventArgs<ItemConfiguration>, bool>(Create.As(configuration));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,7 @@ public class MainViewModelHandler(IPublisher publisher,
|
||||
IContainerHostCollection containers) :
|
||||
INotificationHandler<EnumerateEventArgs<IMainNavigationViewModel>>
|
||||
{
|
||||
public async Task Handle(EnumerateEventArgs<IMainNavigationViewModel> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
public Task Handle(EnumerateEventArgs<IMainNavigationViewModel> args)
|
||||
{
|
||||
foreach (IComponentHost container in containers.OrderBy(x => x.GetConfiguration<ContainerConfiguration>()
|
||||
is ContainerConfiguration configuration ? configuration.Name : null))
|
||||
@@ -19,11 +18,13 @@ public class MainViewModelHandler(IPublisher publisher,
|
||||
{
|
||||
if (factory.Create<ContainerNavigationViewModel>(configuration.Name) is ContainerNavigationViewModel viewModel)
|
||||
{
|
||||
await publisher.Publish(new CreateEventArgs<IMainNavigationViewModel>(viewModel),
|
||||
nameof(MainViewModel), cancellationToken);
|
||||
publisher.Publish(new CreateEventArgs<IMainNavigationViewModel>(viewModel),
|
||||
nameof(MainViewModel));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public partial class OpenContainerViewModel(IServiceProvider provider,
|
||||
{
|
||||
if (await Mediator.Handle<ActivateEventArgs<Container>, bool>(Activate.As(new Container(Password))))
|
||||
{
|
||||
await Publisher.Publish(Opened.As<Container>());
|
||||
Publisher.Publish(Opened.As<Container>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user