Improve removal of items
This commit is contained in:
@@ -1,11 +1,35 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class ConfigurationChangedHandler :
|
||||
public class ConfigurationChangedHandler(IMediator mediator,
|
||||
PrimaryWidgetConfiguration configuration,
|
||||
IViewModelFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory,
|
||||
IViewModelCache<Guid, IWidgetComponentViewModel> cache) :
|
||||
INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>>
|
||||
{
|
||||
public ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
|
||||
public async ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
foreach (KeyValuePair<Guid, IWidgetComponentViewModel> cached in cache)
|
||||
{
|
||||
if (configuration.FirstOrDefault(x => x.Id == cached.Key) == null)
|
||||
{
|
||||
await mediator.PublishAsync(new Removed<IWidgetComponentViewModel>(cached.Value),
|
||||
cancellationToken);
|
||||
|
||||
cache.Remove(cached.Key);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (PrimaryCommandConfiguration item in configuration)
|
||||
{
|
||||
|
||||
//if (!cache.ContainsKey(item.Id))
|
||||
//{
|
||||
// factory.CreateAsync(item);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
|
||||
//}
|
||||
} }
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public class PrimaryWidgetProvider :
|
||||
{
|
||||
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
|
||||
services.AddConfiguration<PrimaryWidgetConfiguration>()
|
||||
.AddTransient<IViewModelCache<Guid, IWidgetComponentViewModel>, ViewModelCache<Guid, IWidgetComponentViewModel>>()
|
||||
.AddSingleton<IViewModelCache<Guid, IWidgetComponentViewModel>, ViewModelCache<Guid, IWidgetComponentViewModel>>()
|
||||
.AddTransient<IViewModelFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>, WidgetComponentViewModelFactory>()
|
||||
.AddTransient<IViewModelEnumerator<IWidgetComponentViewModel>, WidgetComponentViewModelEnumerator>()
|
||||
.AddWidgetTemplate<PrimaryWidgetViewModel>()
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
|
||||
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class PrimaryWidgetViewModel(ITemplateFactory templateFactory,
|
||||
IServiceFactory serviceFactory,
|
||||
|
||||
@@ -4,11 +4,11 @@ public class WidgetComponentViewModelEnumerator(PrimaryWidgetConfiguration confi
|
||||
IViewModelFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory) :
|
||||
IViewModelEnumerator<IWidgetComponentViewModel>
|
||||
{
|
||||
public async IAsyncEnumerable<IWidgetComponentViewModel?> Next()
|
||||
public IEnumerable<IWidgetComponentViewModel?> Next()
|
||||
{
|
||||
foreach (PrimaryCommandConfiguration item in configuration)
|
||||
{
|
||||
yield return await factory.CreateAsync(item);
|
||||
yield return factory.Create(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ public class WidgetComponentViewModelFactory(IServiceFactory service,
|
||||
IViewModelCache<Guid, IWidgetComponentViewModel> cache) :
|
||||
IViewModelFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>
|
||||
{
|
||||
public async ValueTask<IWidgetComponentViewModel?> CreateAsync(PrimaryCommandConfiguration value)
|
||||
public IWidgetComponentViewModel? Create(PrimaryCommandConfiguration value)
|
||||
{
|
||||
IWidgetComponentViewModel? viewModel = null;
|
||||
IWidgetComponentViewModel? viewModel = default;
|
||||
|
||||
if (value is KeyAcceleratorCommandConfiguration keyAcceleratorCommand)
|
||||
{
|
||||
@@ -31,6 +31,6 @@ public class WidgetComponentViewModelFactory(IServiceFactory service,
|
||||
cache.Add(value.Id, viewModel);
|
||||
}
|
||||
|
||||
return viewModel ?? default;
|
||||
return viewModel;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user