switched to valuetuples for exhanging tokens

This commit is contained in:
TheXamlGuy
2024-05-27 20:43:18 +01:00
parent 1be1399511
commit 9535f75835
17 changed files with 85 additions and 94 deletions
+3 -1
View File
@@ -2,13 +2,15 @@
namespace Bitvault;
public class AggerateItemViewModelHandler(IMediator mediator,
public class AggerateItemViewModelHandler(IValueStore<Item<(Guid, string)>> valueStore,
IMediator mediator,
IServiceFactory serviceFactory,
IPublisher publisher) :
INotificationHandler<AggerateEventArgs<IItemEntryViewModel>>
{
public Task Handle(AggerateEventArgs<IItemEntryViewModel> args)
{
var d = valueStore;
//if (serviceFactory.Create<ItemHeaderViewModel>(false) is ItemHeaderViewModel viewModel)
//{
// publisher.Publish(Create.As<IItemEntryViewModel>(viewModel), nameof(ItemViewModel));
@@ -5,7 +5,7 @@ namespace Bitvault;
public class AggerateLockerItemViewModelHandler(IMediator mediator,
IServiceProvider serviceProvider,
ICache<Item> cache,
ICache<Item<(Guid, string)>> cache,
IPublisher publisher) :
INotificationHandler<AggerateEventArgs<ItemNavigationViewModel,
LockerViewModelConfiguration>>
@@ -29,11 +29,12 @@ public class AggerateLockerItemViewModelHandler(IMediator mediator,
{
IServiceScope serviceScope = serviceProvider.CreateScope();
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
IValueStore<Item> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item>>();
IValueStore<Item<(Guid, string)>> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item<(Guid, string)>>>();
if (serviceFactory.Create<ItemNavigationViewModel>(Id, Name, "Description", selected, Favourite, Archived) is ItemNavigationViewModel viewModel)
{
Item item = new() { Id = Id, Name = Name };
Item<(Guid, string)> item = new((Id, Name));
valueStore.Set(item);
cache.Add(item);
+8 -6
View File
@@ -2,21 +2,23 @@
namespace Bitvault;
public class ArchiveItemHandler(IValueStore<Item> valueStore,
ICache<Item> cache,
public class ArchiveItemHandler(IValueStore<Item<(Guid, string)>> valueStore,
ICache<Item<(Guid, string)>> cache,
IMediator mediator) :
INotificationHandler<ArchiveEventArgs<Item>>
INotificationHandler<ArchiveEventArgs<Item<(Guid, string)>>>
{
public async Task Handle(ArchiveEventArgs<Item> args)
public async Task Handle(ArchiveEventArgs<Item<(Guid, string)>> args)
{
try
{
if (valueStore.Value is Item item)
if (valueStore.Value is Item<(Guid, string)> item)
{
if (cache.Contains(item))
{
(Guid id, string name) = item.Value;
await mediator.Handle<UpdateEventArgs<(Guid, int)>,
bool>(new UpdateEventArgs<(Guid, int)>((item.Id, 2)));
bool>(new UpdateEventArgs<(Guid, int)>((id, 2)));
cache.Remove(item);
}
+10 -9
View File
@@ -2,32 +2,33 @@
namespace Bitvault;
public class ConfirmItemHandler(IValueStore<Item> valueStore,
public class ConfirmItemHandler(IValueStore<Item<(Guid, string)>> valueStore,
IMediator mediator,
IPublisher publisher) :
INotificationHandler<ConfirmEventArgs<Item>>
INotificationHandler<ConfirmEventArgs<Item<(Guid, string)>>>
{
public async Task Handle(ConfirmEventArgs<Item> args)
public async Task Handle(ConfirmEventArgs<Item<(Guid, string)>> args)
{
ItemHeaderConfiguration? configuration = await mediator.Handle<ConfirmEventArgs<Item>,
ItemHeaderConfiguration? configuration = await mediator.Handle<ConfirmEventArgs<Item<(Guid, string)>>,
ItemHeaderConfiguration>(args);
if (configuration is not null)
{
publisher.Publish(Notify.As(configuration));
if (valueStore?.Value is Item item)
if (valueStore?.Value is Item<(Guid, string)> item)
{
Guid id = item.Id;
(Guid id, string _) = item.Value;
string? name = configuration.Name;
Item newItem = new() { Id = id, Name = name };
Item<(Guid, string)> newItem = new((id, name));
publisher.Publish(Modified.As(item, newItem));
valueStore.Set(newItem);
await mediator.Handle<UpdateEventArgs<(Guid, ItemConfiguration)>, bool>(new UpdateEventArgs<(Guid,
ItemConfiguration)>((item.Id, new ItemConfiguration { Name = name })));
ItemConfiguration)>((id, new ItemConfiguration { Name = name })));
}
else
{
@@ -39,7 +40,7 @@ public class ConfirmItemHandler(IValueStore<Item> valueStore,
if (Success)
{
publisher.Publish(Created.As(new Item { Id = id, Name = name }));
publisher.Publish(Created.As(new Item<(Guid, string)>((id, name))));
}
}
}
+8 -6
View File
@@ -4,19 +4,21 @@ using Toolkit.Foundation;
namespace Bitvault;
public class CreatedItemHandler(IServiceProvider serviceProvider,
ICache<Item> cache,
ICache<Item<(Guid, string)>> cache,
IPublisher publisher) :
INotificationHandler<CreatedEventArgs<Item>>
INotificationHandler<CreatedEventArgs<Item<(Guid, string)>>>
{
public Task Handle(CreatedEventArgs<Item> args)
public Task Handle(CreatedEventArgs<Item<(Guid, string)>> args)
{
if (args.Value is Item item)
if (args.Value is Item<(Guid, string)> item)
{
(Guid id, string name) = item.Value;
IServiceScope serviceScope = serviceProvider.CreateScope();
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
IValueStore<Item> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item>>();
IValueStore<Item<(Guid, string)>> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item<(Guid, string)>>>();
if (serviceFactory.Create<ItemNavigationViewModel>(item.Id, item.Name, "Description", true)
if (serviceFactory.Create<ItemNavigationViewModel>(id, name, "Description", true)
is ItemNavigationViewModel viewModel)
{
cache.Add(item);
+6 -6
View File
@@ -2,18 +2,18 @@
namespace Bitvault;
public class FavouriteItemHandler(IValueStore<Item> valueStore,
public class FavouriteItemHandler(IValueStore<Item<(Guid, string)>> valueStore,
IMediator mediator) :
INotificationHandler<FavouriteEventArgs<Item>>
INotificationHandler<FavouriteEventArgs<Item<(Guid, string)>>>
{
public async Task Handle(FavouriteEventArgs<Item> args)
public async Task Handle(FavouriteEventArgs<Item<(Guid, string)>> args)
{
try
{
if (valueStore.Value is Item item)
if (valueStore.Value is Item<(Guid, string)> item)
{
await mediator.Handle<UpdateEventArgs<(Guid, int)>, bool>(new UpdateEventArgs<(Guid,
int)>((item.Id, 1)));
(Guid id, string name) = item.Value;
await mediator.Handle<UpdateEventArgs<(Guid, int)>, bool>(new UpdateEventArgs<(Guid, int)>((id, 1)));
}
}
catch
+2 -5
View File
@@ -1,8 +1,5 @@
namespace Bitvault;
public record Item
{
public Guid Id { get; init; }
public record Item<TValue>(TValue Value);
public string? Name { get; init; } = "";
}
public record Item;
@@ -9,7 +9,6 @@ public partial class ItemCategoryNavigationViewModel(IServiceProvider provider,
IPublisher publisher,
ISubscription subscriber,
IDisposer disposer,
NamedComponent named,
string name,
bool selected = false) :
Observable(provider, factory, mediator, publisher, subscriber, disposer),
@@ -19,9 +18,6 @@ public partial class ItemCategoryNavigationViewModel(IServiceProvider provider,
[ObservableProperty]
private string name = name;
[ObservableProperty]
private string named = $"{named}";
[ObservableProperty]
private bool selected = selected;
+2 -5
View File
@@ -2,8 +2,10 @@
namespace Bitvault;
[Aggerate(nameof(ItemContentViewModel))]
public partial class ItemContentViewModel :
ObservableCollection<IItemEntryViewModel>,
IItemEntryViewModel
{
public ItemContentViewModel(IServiceProvider provider,
@@ -15,11 +17,6 @@ public partial class ItemContentViewModel :
bool immutable = true) : base(provider, factory, mediator, publisher, subscriber, disposer)
{
Template = template;
if (!immutable)
{
Insert<AddItemNavigationViewModel>();
}
}
public IContentTemplate Template { get; set; }
+1 -1
View File
@@ -36,7 +36,7 @@ public partial class ItemHeaderViewModel : Observable<string, string>,
}
public Task<ItemHeaderConfiguration> Handle(ConfirmEventArgs<Item> args,
CancellationToken cancellationToken) => Task.FromResult(new ItemHeaderConfiguration { Name = Value });
CancellationToken cancellationToken) => Task.FromResult(new ItemHeaderConfiguration { Name = Value! });
public Task Handle(UpdateEventArgs<Item> args) =>
Task.FromResult(Immutable = false);
+7 -7
View File
@@ -5,20 +5,20 @@ namespace Bitvault;
public class ModifiedItemHandler(IServiceProvider serviceProvider,
IPublisher publisher) :
INotificationHandler<ModifiedEventArgs<Item>>
INotificationHandler<ModifiedEventArgs<Item<(Guid, string)>>>
{
public Task Handle(ModifiedEventArgs<Item> args)
public Task Handle(ModifiedEventArgs<Item<(Guid, string)>> args)
{
Item oldItem = args.OldView;
Item newItem = args.NewValue;
Item<(Guid, string)> oldItem = args.OldView;
Item<(Guid, string)> newItem = args.NewValue;
ICache<Item> cache = serviceProvider.GetRequiredService<ICache<Item>>();
if (cache.TryGetValue(oldItem, out Item? cachedItem))
ICache<Item<(Guid, string)>> cache = serviceProvider.GetRequiredService<ICache<Item<(Guid, string)>>>();
if (cache.TryGetValue(oldItem, out Item<(Guid, string)>? cachedItem))
{
if (cachedItem is not null)
{
IServiceScope serviceScope = serviceProvider.CreateScope();
IValueStore<Item> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item>>();
IValueStore<Item<(Guid, string)>> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item<(Guid, string)>>>();
int oldIndex = cache.IndexOf(cachedItem);
cache.Remove(cachedItem);
+6 -5
View File
@@ -4,21 +4,22 @@ using Toolkit.Foundation;
namespace Bitvault;
public class UnarchiveItemHandler(IValueStore<Item> valueStore,
public class UnarchiveItemHandler(IValueStore<Item<(Guid, string)>> valueStore,
IDbContextFactory<LockerContext> dbContextFactory) :
INotificationHandler<UnarchiveEventArgs<Item>>
INotificationHandler<UnarchiveEventArgs<Item<(Guid, string)>>>
{
public async Task Handle(UnarchiveEventArgs<Item> args)
public async Task Handle(UnarchiveEventArgs<Item<(Guid, string)>> args)
{
try
{
if (valueStore.Value is Item item)
if (valueStore.Value is Item<(Guid, string)> item)
{
(Guid id, string name) = item.Value;
await Task.Run(async () =>
{
using LockerContext context = await dbContextFactory.CreateDbContextAsync();
if (await context.FindAsync<ItemEntry>(item.Id) is ItemEntry result)
if (await context.FindAsync<ItemEntry>(id) is ItemEntry result)
{
result.State = 0;
await context.SaveChangesAsync();
+6 -6
View File
@@ -1,18 +1,18 @@
using Toolkit.Foundation;
namespace Bitvault;
public class UnfavouriteItemHandler(IValueStore<Item> valueStore,
public class UnfavouriteItemHandler(IValueStore<Item<(Guid, string)>> valueStore,
IMediator mediator) :
INotificationHandler<UnfavouriteEventArgs<Item>>
INotificationHandler<UnfavouriteEventArgs<Item<(Guid, string)>>>
{
public async Task Handle(UnfavouriteEventArgs<Item> args)
public async Task Handle(UnfavouriteEventArgs<Item<(Guid, string)>> args)
{
try
{
if (valueStore.Value is Item item)
if (valueStore.Value is Item<(Guid, string)> item)
{
await mediator.Handle<UpdateEventArgs<(Guid, int)>, bool>(new UpdateEventArgs<(Guid,
int)>((item.Id, 0)));
(Guid id, string name) = item.Value;
await mediator.Handle<UpdateEventArgs<(Guid, int)>, bool>(new UpdateEventArgs<(Guid, int)>((id, 0)));
}
}
catch