Support unarchiving
This commit is contained in:
@@ -30,7 +30,7 @@ public class AggerateContainerViewModelHandler(IMediator mediator,
|
||||
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
|
||||
IValueStore<Item> valueStore = serviceScope.ServiceProvider.GetRequiredService<IValueStore<Item>>();
|
||||
|
||||
if (serviceFactory.Create<ItemNavigationViewModel>(Id, Name, "Description " + 1, selected) is ItemNavigationViewModel viewModel)
|
||||
if (serviceFactory.Create<ItemNavigationViewModel>(Id, Name, "Description", selected, configuration.Filter == "Archive") is ItemNavigationViewModel viewModel)
|
||||
{
|
||||
Item item = new() { Id = Id, Name = Name };
|
||||
valueStore.Set(item);
|
||||
|
||||
+1
-1
@@ -7,4 +7,4 @@ public record Archive
|
||||
|
||||
public static ArchiveEventArgs<TValue> As<TValue>() where TValue : new() =>
|
||||
new(new TValue());
|
||||
}
|
||||
}
|
||||
@@ -14,3 +14,4 @@ public partial class ArchiveItemActionViewModel(IServiceProvider provider,
|
||||
[RelayCommand]
|
||||
public void Invoke() => Publisher.Publish(Archive.As<Item>());
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public class ArchiveItemHandler(IValueStore<Item> valueStore,
|
||||
|
||||
if (await context.FindAsync<ItemEntry>(item.Id) is ItemEntry result)
|
||||
{
|
||||
result.State = 3;
|
||||
result.State = 2;
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,83 +1,8 @@
|
||||
using Bitvault.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Toolkit.Foundation;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
public class EditItemHander(IDbContextFactory<ContainerDbContext> dbContextFactory) :
|
||||
IHandler<EditEventArgs<(int, ItemConfiguration)>, bool>
|
||||
{
|
||||
public async Task<bool> Handle(EditEventArgs<(int, 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)
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
|
||||
// }
|
||||
//}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateItemHander(IDbContextFactory<ContainerDbContext> dbContextFactory) :
|
||||
IHandler<CreateEventArgs<ItemConfiguration>, (bool, int)>
|
||||
{
|
||||
public async Task<(bool, int)> 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)
|
||||
{
|
||||
return (false, -1);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return (false, -1);
|
||||
}
|
||||
}
|
||||
|
||||
public class ConfirmItemHandler(IMediator mediator,
|
||||
IValueStore<Item> valueStore,
|
||||
IPublisher publisher) :
|
||||
public class ConfirmItemHandler(IMediator mediator) :
|
||||
INotificationHandler<ConfirmEventArgs<Item>>
|
||||
{
|
||||
public async Task Handle(ConfirmEventArgs<Item> args)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using Bitvault.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
public class CreateItemHander(IDbContextFactory<ContainerDbContext> dbContextFactory) :
|
||||
IHandler<CreateEventArgs<ItemConfiguration>, (bool, int)>
|
||||
{
|
||||
public async Task<(bool, int)> 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)
|
||||
{
|
||||
return (false, -1);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return (false, -1);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Toolkit.Foundation;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
@@ -7,4 +8,8 @@ public partial class DismissItemActionViewModel(IServiceProvider provider,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscription subscriber,
|
||||
IDisposer disposer) : Observable(provider, factory, mediator, publisher, subscriber, disposer);
|
||||
IDisposer disposer) : Observable(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
{
|
||||
[RelayCommand]
|
||||
public void Invoke() => Publisher.Publish(Cancel.As<Item>());
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Bitvault.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
public class EditItemHander(IDbContextFactory<ContainerDbContext> dbContextFactory) :
|
||||
IHandler<EditEventArgs<(int, ItemConfiguration)>, bool>
|
||||
{
|
||||
public async Task<bool> Handle(EditEventArgs<(int, 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)
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
|
||||
// }
|
||||
//}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -4,27 +4,3 @@ using Toolkit.Foundation;
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
public class ItemActivatedHandler(IServiceProvider serviceProvider,
|
||||
ICache<Item> cache,
|
||||
IPublisher publisher) :
|
||||
INotificationHandler<ActivatedEventArgs<Item>>
|
||||
{
|
||||
public Task Handle(ActivatedEventArgs<Item> args)
|
||||
{
|
||||
if (args.Value is Item item)
|
||||
{
|
||||
IServiceScope serviceScope = serviceProvider.CreateScope();
|
||||
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
|
||||
|
||||
cache.Add(item);
|
||||
int index = cache.IndexOf(item);
|
||||
|
||||
if (serviceFactory.Create<ItemNavigationViewModel>(item.Id, item.Name, "Description " + 1, true) is ItemNavigationViewModel viewModel)
|
||||
{
|
||||
publisher.Publish(Insert.As(index, viewModel), nameof(ContainerViewModel));
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -14,12 +14,17 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
|
||||
int id,
|
||||
string name,
|
||||
string description,
|
||||
bool selected) :
|
||||
bool selected,
|
||||
bool archived) :
|
||||
Observable(provider, factory, mediator, publisher, subscriber, disposer),
|
||||
INotificationHandler<ArchiveEventArgs<Item>>,
|
||||
INotificationHandler<UnarchiveEventArgs<Item>>,
|
||||
ISelectable,
|
||||
IRemovable
|
||||
{
|
||||
[ObservableProperty]
|
||||
private bool archived = archived;
|
||||
|
||||
[ObservableProperty]
|
||||
private string? description = description;
|
||||
|
||||
@@ -42,4 +47,10 @@ public partial class ItemNavigationViewModel(IServiceProvider provider,
|
||||
Dispose();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(UnarchiveEventArgs<Item> args)
|
||||
{
|
||||
Dispose();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,66 @@
|
||||
using Toolkit.Foundation;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
public partial class ItemViewModel :
|
||||
ObservableCollection<IDisposable>
|
||||
ObservableCollection<IDisposable>,
|
||||
INotificationHandler<EditEventArgs<Item>>,
|
||||
INotificationHandler<CancelEventArgs<Item>>
|
||||
{
|
||||
[ObservableProperty]
|
||||
private bool archived;
|
||||
|
||||
public ItemViewModel(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscription subscriber,
|
||||
IDisposer disposer,
|
||||
IContentTemplate template) : base(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
IContentTemplate template,
|
||||
bool archived = false) : base(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
{
|
||||
Template = template;
|
||||
Archived = archived;
|
||||
|
||||
if (!Archived)
|
||||
{
|
||||
Publisher.Publish(Notify.As(Factory.Create<CommandCollection>(new List<IDisposable>
|
||||
{
|
||||
Factory.Create<EditItemActionViewModel>(),
|
||||
Factory.Create<ArchiveItemActionViewModel>(),
|
||||
})));
|
||||
}
|
||||
else
|
||||
{
|
||||
Publisher.Publish(Notify.As(Factory.Create<CommandCollection>(new List<IDisposable>
|
||||
{
|
||||
Factory.Create<UnarchiveItemActionViewModel>(),
|
||||
})));
|
||||
}
|
||||
}
|
||||
|
||||
public IContentTemplate Template { get; set; }
|
||||
|
||||
public Task Handle(EditEventArgs<Item> args)
|
||||
{
|
||||
Publisher.Publish(Notify.As(Factory.Create<CommandCollection>(new List<IDisposable>
|
||||
{
|
||||
Factory.Create<ConfirmItemActionViewModel>(),
|
||||
Factory.Create<DismissItemActionViewModel>(),
|
||||
})));
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(CancelEventArgs<Item> args)
|
||||
{
|
||||
Publisher.Publish(Notify.As(Factory.Create<CommandCollection>(new List<IDisposable>
|
||||
{
|
||||
Factory.Create<EditItemActionViewModel>(),
|
||||
Factory.Create<ArchiveItemActionViewModel>(),
|
||||
})));
|
||||
}
|
||||
|
||||
public IContentTemplate Template { get; set; }
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,17 +20,17 @@ public class QueryContainerHandler(IDbContextFactory<ContainerDbContext> dbConte
|
||||
|
||||
if (queryConfiguration.Filter == "All")
|
||||
{
|
||||
predicate = predicate.And(x => x.State != 3);
|
||||
predicate = predicate.And(x => x.State != 2);
|
||||
}
|
||||
|
||||
if (queryConfiguration.Filter == "Starred")
|
||||
{
|
||||
predicate = predicate.And(x => x.State != 3 && x.State == 2);
|
||||
predicate = predicate.And(x => x.State != 2 && x.State == 1);
|
||||
}
|
||||
|
||||
if (queryConfiguration.Filter == "Archive")
|
||||
{
|
||||
predicate = predicate.And(x => x.State == 3);
|
||||
predicate = predicate.And(x => x.State == 2);
|
||||
}
|
||||
|
||||
if (queryConfiguration.Query is { Length: > 0} query)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Bitvault;
|
||||
|
||||
public record Unarchive
|
||||
{
|
||||
public static UnarchiveEventArgs<TValue> As<TValue>(TValue value) =>
|
||||
new(value);
|
||||
|
||||
public static UnarchiveEventArgs<TValue> As<TValue>() where TValue : new() =>
|
||||
new(new TValue());
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Bitvault;
|
||||
|
||||
public record UnarchiveEventArgs<TValue>(TValue Value);
|
||||
@@ -0,0 +1,17 @@
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
public partial class UnarchiveItemActionViewModel(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscription subscriber,
|
||||
IDisposer disposer) : Observable(provider, factory, mediator, publisher, subscriber, disposer),
|
||||
IRemovable
|
||||
{
|
||||
[RelayCommand]
|
||||
public void Invoke() => Publisher.Publish(Unarchive.As<Item>());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using Bitvault.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
public class UnarchiveItemHandler(IValueStore<Item> valueStore,
|
||||
IDbContextFactory<ContainerDbContext> dbContextFactory) :
|
||||
INotificationHandler<UnarchiveEventArgs<Item>>
|
||||
{
|
||||
public async Task Handle(UnarchiveEventArgs<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 = 0;
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user