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

This commit is contained in:
TheXamlGuy
2024-05-22 20:13:50 +01:00
parent 0d7cb192d5
commit 9c8c7bf889
9 changed files with 46 additions and 55 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ namespace Bitvault.Data;
public record ItemEntry public record ItemEntry
{ {
[Key] [Key]
public int Id { get; set; } public Guid Id { get; set; }
public required string Name { get; set; } public required string Name { get; set; }
@@ -18,13 +18,13 @@ public class AggerateContainerViewModelHandler(IMediator mediator,
bool selected = true; bool selected = true;
if (await mediator.Handle<RequestEventArgs<QueryContainerConfiguration>, if (await mediator.Handle<RequestEventArgs<QueryContainerConfiguration>,
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, Filter = configuration.Filter,
Query = configuration.Query 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(); IServiceScope serviceScope = serviceProvider.CreateScope();
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>(); IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
@@ -43,5 +43,7 @@ public class AggerateContainerViewModelHandler(IMediator mediator,
} }
} }
} }
var d = cache;
} }
} }
+14 -17
View File
@@ -14,37 +14,34 @@ public class ConfirmItemHandler(IValueStore<Item> valueStore,
if (configuration is not null) if (configuration is not null)
{ {
bool success = false; publisher.Publish(Notify.As(configuration));
if (valueStore?.Value is Item item) if (valueStore?.Value is Item item)
{ {
(bool Success, int Id, string Name) = await mediator.Handle<EditEventArgs<(int, ItemConfiguration)>, Guid id = item.Id;
(bool, int, string)>(new EditEventArgs<(int, ItemConfiguration)>((item.Id, new ItemConfiguration { Name = configuration.Name }))); string? name = configuration.Name;
if (Success) Item newItem = new() { Id = id, Name = name };
{
Item newItem = new() { Id = Id, Name = Name };
publisher.Publish(Modified.As(item, newItem)); publisher.Publish(Modified.As(item, newItem));
valueStore.Set(newItem); valueStore.Set(newItem);
success = true;
} await mediator.Handle<EditEventArgs<(Guid, ItemConfiguration)>, bool>(new EditEventArgs<(Guid,
ItemConfiguration)>((item.Id, new ItemConfiguration { Name = name })));
} }
else else
{ {
(bool Success, int Id, string Name) = await mediator.Handle<CreateEventArgs<ItemConfiguration>, Guid id = Guid.NewGuid();
(bool, int, string)>(new CreateEventArgs<ItemConfiguration>(new ItemConfiguration { Name = configuration.Name })); string? name = configuration.Name;
bool Success = await mediator.Handle<CreateEventArgs<(Guid,
ItemConfiguration)>, bool>(new CreateEventArgs<(Guid, ItemConfiguration)>((id, new ItemConfiguration { Name = name })));
if (Success) if (Success)
{ {
publisher.Publish(Created.As(new Item { Id = Id, Name = Name })); publisher.Publish(Created.As(new Item { Id = id, Name = name }));
success = true;
} }
} }
if (success)
{
publisher.Publish(Notify.As(configuration));
}
} }
} }
} }
+8 -6
View File
@@ -6,28 +6,30 @@ using Toolkit.Foundation;
namespace Bitvault; namespace Bitvault;
public class CreateItemHandler(IDbContextFactory<ContainerDbContext> dbContextFactory) : public class CreateItemHandler(IDbContextFactory<ContainerDbContext> dbContextFactory) :
IHandler<CreateEventArgs<ItemConfiguration>, (bool, int, string?)> IHandler<CreateEventArgs<(Guid, ItemConfiguration)>, bool>
{ {
public async Task<(bool, int, string?)> Handle(CreateEventArgs<ItemConfiguration> args, public async Task<bool> Handle(CreateEventArgs<(Guid, ItemConfiguration)> args,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
if (args.Value is ItemConfiguration configuration) if (args.Value is (Guid id, ItemConfiguration configuration))
{ {
try try
{ {
string? name = configuration.Name;
using ContainerDbContext context = dbContextFactory.CreateDbContext(); using ContainerDbContext context = dbContextFactory.CreateDbContext();
EntityEntry<ItemEntry>? result = null; EntityEntry<ItemEntry>? result = null;
await Task.Run(async () => 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); await context.SaveChangesAsync(cancellationToken);
}, cancellationToken); }, cancellationToken);
if (result is not null) if (result is not null)
{ {
return (true, result.Entity.Id, result.Entity.Name); return true;
} }
} }
catch catch
@@ -36,6 +38,6 @@ public class CreateItemHandler(IDbContextFactory<ContainerDbContext> dbContextFa
} }
} }
return (false, -1, ""); return false;
} }
} }
+8 -6
View File
@@ -5,15 +5,17 @@ using Toolkit.Foundation;
namespace Bitvault; namespace Bitvault;
public class EditItemHander(IDbContextFactory<ContainerDbContext> dbContextFactory) : public class EditItemHander(IDbContextFactory<ContainerDbContext> dbContextFactory) :
IHandler<EditEventArgs<(int, ItemConfiguration)>, (bool, int, string?)> IHandler<EditEventArgs<(Guid, ItemConfiguration)>, bool>
{ {
public async Task<(bool, int, string?)> Handle(EditEventArgs<(int, ItemConfiguration)> args, public async Task<bool> Handle(EditEventArgs<(Guid, ItemConfiguration)> args,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
if (args.Value is (int id, ItemConfiguration configuration)) if (args.Value is (Guid id, ItemConfiguration configuration))
{ {
try try
{ {
string? name = configuration.Name;
using ContainerDbContext context = dbContextFactory.CreateDbContext(); using ContainerDbContext context = dbContextFactory.CreateDbContext();
ItemEntry? result = null; ItemEntry? result = null;
@@ -23,7 +25,7 @@ public class EditItemHander(IDbContextFactory<ContainerDbContext> dbContextFacto
if (result is not null) if (result is not null)
{ {
result.Name = configuration.Name; result.Name = name;
await context.SaveChangesAsync(cancellationToken); await context.SaveChangesAsync(cancellationToken);
} }
@@ -31,7 +33,7 @@ public class EditItemHander(IDbContextFactory<ContainerDbContext> dbContextFacto
if (result is not null) if (result is not null)
{ {
return (true, result.Id, result.Name); return true;
} }
} }
catch catch
@@ -40,6 +42,6 @@ public class EditItemHander(IDbContextFactory<ContainerDbContext> dbContextFacto
} }
} }
return (false, -1, ""); return false;
} }
} }
+2 -4
View File
@@ -1,10 +1,8 @@
using System.Diagnostics.CodeAnalysis; namespace Bitvault;
namespace Bitvault;
public record Item public record Item
{ {
public int Id { get; init; } public Guid Id { get; init; }
public string Name { get; init; } = ""; public string Name { get; init; } = "";
} }
+2 -2
View File
@@ -11,7 +11,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
IDisposer disposer, IDisposer disposer,
IContentTemplate template, IContentTemplate template,
NamedComponent named, NamedComponent named,
int id, Guid id,
string? name = "", string? name = "",
string? description = "", string? description = "",
bool selected = false, bool selected = false,
@@ -36,7 +36,7 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
private bool favourite = favourite; private bool favourite = favourite;
[ObservableProperty] [ObservableProperty]
private int id = id; private Guid id = id;
[ObservableProperty] [ObservableProperty]
private string? name = name; private string? name = name;
+1 -1
View File
@@ -4,7 +4,6 @@ using Toolkit.Foundation;
namespace Bitvault; namespace Bitvault;
public class ModifiedItemHandler(IServiceProvider serviceProvider, public class ModifiedItemHandler(IServiceProvider serviceProvider,
ICache<Item> cache,
IPublisher publisher) : IPublisher publisher) :
INotificationHandler<ModifiedEventArgs<Item>> INotificationHandler<ModifiedEventArgs<Item>>
{ {
@@ -13,6 +12,7 @@ public class ModifiedItemHandler(IServiceProvider serviceProvider,
Item oldItem = args.OldView; Item oldItem = args.OldView;
Item newItem = args.NewValue; Item newItem = args.NewValue;
ICache<Item> cache = serviceProvider.GetRequiredService<ICache<Item>>();
if (cache.TryGetValue(oldItem, out Item? cachedItem)) if (cache.TryGetValue(oldItem, out Item? cachedItem))
{ {
if (cachedItem is not null) if (cachedItem is not null)
+3 -13
View File
@@ -5,23 +5,13 @@ using Toolkit.Foundation;
namespace Bitvault; namespace Bitvault;
public class QueryItemHandler(IDbContextFactory<ContainerDbContext> dbContextFactory) :
IHandler<RequestEventArgs<QueryItemConfiguration>, (int Id, string? Name)>
{
public Task<(int Id, string? Name)> Handle(RequestEventArgs<QueryItemConfiguration> args,
CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
public class QueryContainerHandler(IDbContextFactory<ContainerDbContext> dbContextFactory) : public class QueryContainerHandler(IDbContextFactory<ContainerDbContext> dbContextFactory) :
IHandler<RequestEventArgs<QueryContainerConfiguration>, IReadOnlyCollection<(int Id, string? Name, bool Favourite, bool Archived)>> IHandler<RequestEventArgs<QueryContainerConfiguration>, IReadOnlyCollection<(Guid Id, string? Name, bool Favourite, bool Archived)>>
{ {
public async Task<IReadOnlyCollection<(int Id, string Name, bool Favourite, bool Archived)>> Handle(RequestEventArgs<QueryContainerConfiguration> args, public async Task<IReadOnlyCollection<(Guid Id, string? Name, bool Favourite, bool Archived)>> Handle(RequestEventArgs<QueryContainerConfiguration> args,
CancellationToken cancellationToken) 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) if (args.Value is QueryContainerConfiguration queryConfiguration)
{ {