Wire up the disposer for cleaning up unused objects, i.e disposing a VM will remove it from the view

This commit is contained in:
TheXamlGuy
2024-01-12 21:05:42 +00:00
parent 814c806240
commit 2a773f26db
37 changed files with 323 additions and 206 deletions
@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="System.Runtime.Caching" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Hyperbar\Hyperbar.csproj" />
@@ -6,7 +6,7 @@ namespace Hyperbar.Windows.Primary;
[JsonDerivedType(typeof(ProcessCommandConfiguration), typeDiscriminator: "ProcessCommand")]
public class PrimaryCommandConfiguration
{
public required string Id { get; set; }
public required Guid Id { get; set; }
public required string Icon { get; set; }
}
@@ -5,6 +5,6 @@ public class PrimaryWidgetConfiguration :
{
public static PrimaryWidgetConfiguration Defaults => new()
{
new KeyAcceleratorCommandConfiguration { Id = $"{Guid.NewGuid()}", Icon = "\uE720", Key = 91, Modifiers = [] }
new KeyAcceleratorCommandConfiguration { Id = Guid.NewGuid(), Icon = "\uE720", Key = 91, Modifiers = [] }
};
}
@@ -8,9 +8,10 @@ public class PrimaryWidgetProvider :
{
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
services.AddConfiguration<PrimaryWidgetConfiguration>()
.AddTransient<IFactory<IEnumerable<IWidgetComponentViewModel>>, WidgetComponentViewModelFactory>()
.AddTransient<IViewModelEnumerator<IWidgetComponentViewModel>, WidgetComponentViewModelEnumerator>()
.AddTransient<IViewModelFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>, WidgetComponentViewModelFactory>()
.AddWidgetTemplate<PrimaryWidgetViewModel>()
.AddNotificationPipeline<ConfigurationChanged<PrimaryWidgetConfiguration>,
ValueChanging<IEnumerable<IWidgetComponentViewModel>>>();
CollectionChanged<IEnumerable<IWidgetComponentViewModel>>>();
}
@@ -4,8 +4,9 @@ namespace Hyperbar.Windows.Primary;
public class PrimaryWidgetViewModel(ITemplateFactory templateFactory,
IServiceFactory serviceFactory,
IMediator mediator,
IFactory<IEnumerable<IWidgetComponentViewModel>> factory) :
ObservableCollectionViewModel<IWidgetComponentViewModel>(serviceFactory, mediator, factory),
IDisposer disposer,
IViewModelEnumerator<IWidgetComponentViewModel> enumerator) :
ObservableCollectionViewModel<IWidgetComponentViewModel>(serviceFactory, mediator, disposer, enumerator),
IWidgetViewModel,
ITemplatedViewModel
{
@@ -0,0 +1,28 @@
using CommunityToolkit.Mvvm.Input;
namespace Hyperbar.Windows.Primary;
public class WidgetComponentViewModelFactory(IServiceFactory service,
IMediator mediator) :
IViewModelFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>
{
public async ValueTask<IWidgetComponentViewModel?> CreateAsync(PrimaryCommandConfiguration value)
{
if (value is KeyAcceleratorCommandConfiguration keyAcceleratorCommand)
{
return await ValueTask.FromResult(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())))));
}
if (value is ProcessCommandConfiguration commandConfiguration)
{
return await ValueTask.FromResult(service.Create<WidgetButtonViewModel>(commandConfiguration.Id,
commandConfiguration.Icon, new RelayCommand(async () =>
await mediator.SendAsync(new ProcessRequest(commandConfiguration.Path)))));
}
return default;
}
}
@@ -0,0 +1,14 @@
namespace Hyperbar.Windows.Primary;
public class WidgetComponentViewModelEnumerator(PrimaryWidgetConfiguration configuration,
IViewModelFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory) :
IViewModelEnumerator<IWidgetComponentViewModel>
{
public async IAsyncEnumerable<IWidgetComponentViewModel?> Next()
{
foreach (PrimaryCommandConfiguration item in configuration)
{
yield return await factory.CreateAsync(item);
}
}
}
@@ -1,26 +0,0 @@
namespace Hyperbar.Windows.Primary;
public class WidgetComponentViewModelFactory(PrimaryWidgetConfiguration configuration,
IServiceFactory service,
IMediator mediator) :
IFactory<IEnumerable<IWidgetComponentViewModel>>
{
public IEnumerable<IWidgetComponentViewModel> Create()
{
foreach (PrimaryCommandConfiguration item in configuration)
{
if (item is KeyAcceleratorCommandConfiguration keyAcceleratorCommandConfiguration)
{
yield return service.Create<WidgetButtonViewModel>(keyAcceleratorCommandConfiguration.Icon, new Action(async () =>
await mediator.SendAsync(new KeyAcceleratorRequest((VirtualKey)keyAcceleratorCommandConfiguration.Key,
keyAcceleratorCommandConfiguration.Modifiers?.Select(modifier => (VirtualKey)modifier).ToArray()))));
}
if (item is ProcessCommandConfiguration processCommandConfiguration)
{
yield return service.Create<WidgetButtonViewModel>(processCommandConfiguration.Icon, new Action(async () =>
await mediator.SendAsync(new ProcessRequest(processCommandConfiguration.Path))));
}
}
}
}