From 9c8c7bf88981ddbd67b44954d66dbac99033a5ca Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Wed, 22 May 2024 20:13:50 +0100 Subject: [PATCH] We are confidence enough that the data will be saved to the db, so lets change how we update the UI, so instead of wating on the db call to complete, we will just update the UI while the db call is in progress --- Bitvault.Data/ItemEntry.cs | 2 +- Bitvault/AggerateContainerViewModelHandler.cs | 8 +++-- Bitvault/ConfirmItemHandler.cs | 35 +++++++++---------- Bitvault/CreateItemHandler.cs | 14 ++++---- Bitvault/EditItemHander.cs | 14 ++++---- Bitvault/Item.cs | 6 ++-- Bitvault/ItemNavigationViewModel.cs | 4 +-- Bitvault/ModifiedItemHandler.cs | 2 +- Bitvault/QueryContainerHandler.cs | 16 ++------- 9 files changed, 46 insertions(+), 55 deletions(-) diff --git a/Bitvault.Data/ItemEntry.cs b/Bitvault.Data/ItemEntry.cs index 22b3af8..b106488 100644 --- a/Bitvault.Data/ItemEntry.cs +++ b/Bitvault.Data/ItemEntry.cs @@ -7,7 +7,7 @@ namespace Bitvault.Data; public record ItemEntry { [Key] - public int Id { get; set; } + public Guid Id { get; set; } public required string Name { get; set; } diff --git a/Bitvault/AggerateContainerViewModelHandler.cs b/Bitvault/AggerateContainerViewModelHandler.cs index 457f6b2..f14cbd4 100644 --- a/Bitvault/AggerateContainerViewModelHandler.cs +++ b/Bitvault/AggerateContainerViewModelHandler.cs @@ -18,13 +18,13 @@ public class AggerateContainerViewModelHandler(IMediator mediator, bool selected = true; if (await mediator.Handle, - IReadOnlyCollection<(int Id, string Name, bool Favourite, bool Archived)>>(Request.As(new QueryContainerConfiguration + IReadOnlyCollection<(Guid Id, string Name, bool Favourite, bool Archived)>>(Request.As(new QueryContainerConfiguration { Filter = configuration.Filter, Query = configuration.Query - })) is IReadOnlyCollection<(int Id, string Name, bool Favourite, bool Archived)> results) + })) is IReadOnlyCollection<(Guid Id, string Name, bool Favourite, bool Archived)> results) { - foreach ((int Id, string Name, bool Favourite, bool Archived) in results) + foreach ((Guid Id, string Name, bool Favourite, bool Archived) in results) { IServiceScope serviceScope = serviceProvider.CreateScope(); IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService(); @@ -43,5 +43,7 @@ public class AggerateContainerViewModelHandler(IMediator mediator, } } } + + var d = cache; } } diff --git a/Bitvault/ConfirmItemHandler.cs b/Bitvault/ConfirmItemHandler.cs index 77403de..6c395b2 100644 --- a/Bitvault/ConfirmItemHandler.cs +++ b/Bitvault/ConfirmItemHandler.cs @@ -14,37 +14,34 @@ public class ConfirmItemHandler(IValueStore valueStore, if (configuration is not null) { - bool success = false; + publisher.Publish(Notify.As(configuration)); + if (valueStore?.Value is Item item) { - (bool Success, int Id, string Name) = await mediator.Handle, - (bool, int, string)>(new EditEventArgs<(int, ItemConfiguration)>((item.Id, new ItemConfiguration { Name = configuration.Name }))); + Guid id = item.Id; + string? name = configuration.Name; - if (Success) - { - Item newItem = new() { Id = Id, Name = Name }; - publisher.Publish(Modified.As(item, newItem)); + Item newItem = new() { Id = id, Name = name }; + publisher.Publish(Modified.As(item, newItem)); - valueStore.Set(newItem); - success = true; - } + valueStore.Set(newItem); + + await mediator.Handle, bool>(new EditEventArgs<(Guid, + ItemConfiguration)>((item.Id, new ItemConfiguration { Name = name }))); } else { - (bool Success, int Id, string Name) = await mediator.Handle, - (bool, int, string)>(new CreateEventArgs(new ItemConfiguration { Name = configuration.Name })); + Guid id = Guid.NewGuid(); + string? name = configuration.Name; + + bool Success = await mediator.Handle, bool>(new CreateEventArgs<(Guid, ItemConfiguration)>((id, new ItemConfiguration { Name = name }))); if (Success) { - publisher.Publish(Created.As(new Item { Id = Id, Name = Name })); - success = true; + publisher.Publish(Created.As(new Item { Id = id, Name = name })); } } - - if (success) - { - publisher.Publish(Notify.As(configuration)); - } } } } diff --git a/Bitvault/CreateItemHandler.cs b/Bitvault/CreateItemHandler.cs index 551afe6..c26f79d 100644 --- a/Bitvault/CreateItemHandler.cs +++ b/Bitvault/CreateItemHandler.cs @@ -6,28 +6,30 @@ using Toolkit.Foundation; namespace Bitvault; public class CreateItemHandler(IDbContextFactory dbContextFactory) : - IHandler, (bool, int, string?)> + IHandler, bool> { - public async Task<(bool, int, string?)> Handle(CreateEventArgs args, + public async Task Handle(CreateEventArgs<(Guid, ItemConfiguration)> args, CancellationToken cancellationToken) { - if (args.Value is ItemConfiguration configuration) + if (args.Value is (Guid id, ItemConfiguration configuration)) { try { + string? name = configuration.Name; + using ContainerDbContext context = dbContextFactory.CreateDbContext(); EntityEntry? result = null; await Task.Run(async () => { - result = await context.AddAsync(new ItemEntry { Name = configuration.Name }, cancellationToken); + result = await context.AddAsync(new ItemEntry { Id = id, Name = name }, cancellationToken); await context.SaveChangesAsync(cancellationToken); }, cancellationToken); if (result is not null) { - return (true, result.Entity.Id, result.Entity.Name); + return true; } } catch @@ -36,6 +38,6 @@ public class CreateItemHandler(IDbContextFactory dbContextFa } } - return (false, -1, ""); + return false; } } diff --git a/Bitvault/EditItemHander.cs b/Bitvault/EditItemHander.cs index 6a72930..7f39e24 100644 --- a/Bitvault/EditItemHander.cs +++ b/Bitvault/EditItemHander.cs @@ -5,15 +5,17 @@ using Toolkit.Foundation; namespace Bitvault; public class EditItemHander(IDbContextFactory dbContextFactory) : - IHandler, (bool, int, string?)> + IHandler, bool> { - public async Task<(bool, int, string?)> Handle(EditEventArgs<(int, ItemConfiguration)> args, + public async Task Handle(EditEventArgs<(Guid, ItemConfiguration)> args, CancellationToken cancellationToken) { - if (args.Value is (int id, ItemConfiguration configuration)) + if (args.Value is (Guid id, ItemConfiguration configuration)) { try { + string? name = configuration.Name; + using ContainerDbContext context = dbContextFactory.CreateDbContext(); ItemEntry? result = null; @@ -23,7 +25,7 @@ public class EditItemHander(IDbContextFactory dbContextFacto if (result is not null) { - result.Name = configuration.Name; + result.Name = name; await context.SaveChangesAsync(cancellationToken); } @@ -31,7 +33,7 @@ public class EditItemHander(IDbContextFactory dbContextFacto if (result is not null) { - return (true, result.Id, result.Name); + return true; } } catch @@ -40,6 +42,6 @@ public class EditItemHander(IDbContextFactory dbContextFacto } } - return (false, -1, ""); + return false; } } diff --git a/Bitvault/Item.cs b/Bitvault/Item.cs index af5f6e6..03c4f25 100644 --- a/Bitvault/Item.cs +++ b/Bitvault/Item.cs @@ -1,10 +1,8 @@ -using System.Diagnostics.CodeAnalysis; - -namespace Bitvault; +namespace Bitvault; public record Item { - public int Id { get; init; } + public Guid Id { get; init; } public string Name { get; init; } = ""; } diff --git a/Bitvault/ItemNavigationViewModel.cs b/Bitvault/ItemNavigationViewModel.cs index 03f42e7..cb5bc61 100644 --- a/Bitvault/ItemNavigationViewModel.cs +++ b/Bitvault/ItemNavigationViewModel.cs @@ -11,7 +11,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider, IDisposer disposer, IContentTemplate template, NamedComponent named, - int id, + Guid id, string? name = "", string? description = "", bool selected = false, @@ -36,7 +36,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider, private bool favourite = favourite; [ObservableProperty] - private int id = id; + private Guid id = id; [ObservableProperty] private string? name = name; diff --git a/Bitvault/ModifiedItemHandler.cs b/Bitvault/ModifiedItemHandler.cs index 39bc09a..af0924e 100644 --- a/Bitvault/ModifiedItemHandler.cs +++ b/Bitvault/ModifiedItemHandler.cs @@ -4,7 +4,6 @@ using Toolkit.Foundation; namespace Bitvault; public class ModifiedItemHandler(IServiceProvider serviceProvider, - ICache cache, IPublisher publisher) : INotificationHandler> { @@ -13,6 +12,7 @@ public class ModifiedItemHandler(IServiceProvider serviceProvider, Item oldItem = args.OldView; Item newItem = args.NewValue; + ICache cache = serviceProvider.GetRequiredService>(); if (cache.TryGetValue(oldItem, out Item? cachedItem)) { if (cachedItem is not null) diff --git a/Bitvault/QueryContainerHandler.cs b/Bitvault/QueryContainerHandler.cs index b43bdd5..d22c622 100644 --- a/Bitvault/QueryContainerHandler.cs +++ b/Bitvault/QueryContainerHandler.cs @@ -5,23 +5,13 @@ using Toolkit.Foundation; namespace Bitvault; -public class QueryItemHandler(IDbContextFactory dbContextFactory) : - IHandler, (int Id, string? Name)> -{ - public Task<(int Id, string? Name)> Handle(RequestEventArgs args, - CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } -} - public class QueryContainerHandler(IDbContextFactory dbContextFactory) : - IHandler, IReadOnlyCollection<(int Id, string? Name, bool Favourite, bool Archived)>> + IHandler, IReadOnlyCollection<(Guid Id, string? Name, bool Favourite, bool Archived)>> { - public async Task> Handle(RequestEventArgs args, + public async Task> Handle(RequestEventArgs args, CancellationToken cancellationToken) { - List<(int Id, string Name, bool Favourite, bool Archived)> items = []; + List<(Guid Id, string Name, bool Favourite, bool Archived)> items = []; if (args.Value is QueryContainerConfiguration queryConfiguration) {