using Bitvault.Data; using LinqKit; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Toolkit.Foundation; namespace Bitvault; public class ContainerViewModelHandler(IDbContextFactory dbContextFactory, IServiceProvider serviceProvider, ICache cache, IPublisher publisher) : INotificationHandler> { public async Task Handle(Enumerate args, CancellationToken cancellationToken = default) { if (args.Options is ContainerViewModelConfiguration configuration) { cache.Clear(); ExpressionStarter predicate = PredicateBuilder.New(true); if (configuration.Filter == "All") { predicate = predicate.And(x => x.State != 3); } if (configuration.Filter == "Starred") { predicate = predicate.And(x => x.State != 3 && x.State == 2); } if (configuration.Filter == "Archive") { predicate = predicate.And(x => x.State == 3); } var items = await Task.Run(async () => { using ContainerDbContext context = dbContextFactory.CreateDbContext(); return await context.Set().Where(predicate).Select(x => new { x.Id, x.Name }).OrderBy(x => x.Name).ToListAsync(); }, cancellationToken); bool selected = true; foreach (var item in items) { IServiceScope serviceScope = serviceProvider.CreateScope(); IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService(); if (serviceFactory.Create(item.Id, item.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); } selected = false; } } } }