Viewmodel caching WIP
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class ConfigurationChangedHandler :
|
||||
INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>>
|
||||
{
|
||||
public ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,10 @@ public class PrimaryWidgetProvider :
|
||||
{
|
||||
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
|
||||
services.AddConfiguration<PrimaryWidgetConfiguration>()
|
||||
.AddTransient<IViewModelEnumerator<IWidgetComponentViewModel>, WidgetComponentViewModelEnumerator>()
|
||||
.AddTransient<IViewModelCache<Guid, IWidgetComponentViewModel>, ViewModelCache<Guid, IWidgetComponentViewModel>>()
|
||||
.AddTransient<IViewModelFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>, WidgetComponentViewModelFactory>()
|
||||
.AddTransient<IViewModelEnumerator<IWidgetComponentViewModel>, WidgetComponentViewModelEnumerator>()
|
||||
.AddWidgetTemplate<PrimaryWidgetViewModel>()
|
||||
.AddNotificationPipeline<ConfigurationChanged<PrimaryWidgetConfiguration>,
|
||||
CollectionChanged<IEnumerable<IWidgetComponentViewModel>>>();
|
||||
.AddHandler<ConfigurationChangedHandler>();
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
|
||||
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class PrimaryWidgetViewModel(ITemplateFactory templateFactory,
|
||||
|
||||
+14
-6
@@ -3,26 +3,34 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class WidgetComponentViewModelFactory(IServiceFactory service,
|
||||
IMediator mediator) :
|
||||
IMediator mediator,
|
||||
IViewModelCache<Guid, IWidgetComponentViewModel> cache) :
|
||||
IViewModelFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>
|
||||
{
|
||||
public async ValueTask<IWidgetComponentViewModel?> CreateAsync(PrimaryCommandConfiguration value)
|
||||
{
|
||||
IWidgetComponentViewModel? viewModel = null;
|
||||
|
||||
if (value is KeyAcceleratorCommandConfiguration keyAcceleratorCommand)
|
||||
{
|
||||
return await ValueTask.FromResult(service.Create<WidgetButtonViewModel>(keyAcceleratorCommand.Id, keyAcceleratorCommand.Icon,
|
||||
viewModel = service.Create<WidgetButtonViewModel>(keyAcceleratorCommand.Id, keyAcceleratorCommand.Icon,
|
||||
new RelayCommand(async () => await mediator.SendAsync(new KeyAcceleratorRequest((VirtualKey)
|
||||
keyAcceleratorCommand.Key, keyAcceleratorCommand.Modifiers?
|
||||
.Select(modifier => (VirtualKey)modifier).ToArray())))));
|
||||
.Select(modifier => (VirtualKey)modifier).ToArray()))));
|
||||
}
|
||||
|
||||
if (value is ProcessCommandConfiguration commandConfiguration)
|
||||
{
|
||||
return await ValueTask.FromResult(service.Create<WidgetButtonViewModel>(commandConfiguration.Id,
|
||||
viewModel = service.Create<WidgetButtonViewModel>(commandConfiguration.Id,
|
||||
commandConfiguration.Icon, new RelayCommand(async () =>
|
||||
await mediator.SendAsync(new ProcessRequest(commandConfiguration.Path)))));
|
||||
await mediator.SendAsync(new ProcessRequest(commandConfiguration.Path))));
|
||||
}
|
||||
|
||||
return default;
|
||||
if (viewModel is not null)
|
||||
{
|
||||
cache.Add(value.Id, viewModel);
|
||||
}
|
||||
|
||||
return viewModel ?? default;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IViewModelFactory<TIn, TOut>
|
||||
{
|
||||
ValueTask<TOut> CreateAsync(TIn value);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IViewModelFactory<TParameter, TViewModel>
|
||||
{
|
||||
ValueTask<TViewModel> CreateAsync(TParameter value);
|
||||
}
|
||||
|
||||
public interface IViewModelCache<TKey, TViewModel>
|
||||
{
|
||||
void Add(TKey key, TViewModel value);
|
||||
|
||||
void Remove(TKey key);
|
||||
}
|
||||
|
||||
public class ViewModelCache<TKey, TViewModel> :
|
||||
IViewModelCache<TKey, TViewModel>
|
||||
{
|
||||
private readonly Dictionary<TKey, TViewModel> cache = [];
|
||||
|
||||
public void Add(TKey key,
|
||||
TViewModel value)
|
||||
{
|
||||
cache.TryAdd(key, value);
|
||||
}
|
||||
|
||||
public void Remove(TKey key)
|
||||
{
|
||||
cache.Remove(key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user