extend ConfigurationValueViewModel to write the value back to settings

This commit is contained in:
TheXamlGuy
2024-10-02 18:29:11 +01:00
parent b781f18758
commit 001fdb1404
5 changed files with 18 additions and 236 deletions
@@ -1,105 +0,0 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace Toolkit.Foundation;
public partial class ComponentConfigurationViewModel<TConfiguration, TValue, THeader, TDescription, TAction> :
ValueViewModel<TValue>,
IComponentConfigurationViewModel
where TConfiguration : class
{
public ComponentConfigurationViewModel(IServiceProvider provider,
IServiceFactory factory,
IMediator mediator,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer,
THeader header,
TDescription description,
TAction action) : base(provider, factory, mediator, publisher, subscriber, disposer)
{
}
}
public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TAction>(IServiceProvider provider,
IServiceFactory factory,
IMediator mediator,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer,
TAction action,
TConfiguration configuration,
Func<TConfiguration, TValue> valueDelegate,
object header,
object description) :
ValueViewModel<TValue>(provider, factory, mediator, publisher, subscriber, disposer),
IComponentConfigurationViewModel,
INotificationHandler<ChangedEventArgs<TConfiguration>>
where TConfiguration : class
{
[ObservableProperty]
private TAction action = action;
[ObservableProperty]
private object header = header;
[ObservableProperty]
private object description = description;
public override Task OnActivated()
{
Value = valueDelegate.Invoke(configuration);
return base.OnActivated();
}
public Task Handle(ChangedEventArgs<TConfiguration> args)
{
if (args.Sender is TConfiguration configuration)
{
Value = valueDelegate.Invoke(configuration);
}
return Task.CompletedTask;
}
}
public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TDescription, TAction>(IServiceProvider provider,
IServiceFactory factory,
IMediator mediator,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer,
TAction action,
TDescription description,
TConfiguration configuration,
Func<TConfiguration, TValue> valueDelegate,
object header) :
ValueViewModel<TValue>(provider, factory, mediator, publisher, subscriber, disposer),
IComponentConfigurationViewModel,
INotificationHandler<ChangedEventArgs<TConfiguration>>
where TConfiguration : class
{
[ObservableProperty]
private TAction action = action;
[ObservableProperty]
private object header = header;
[ObservableProperty]
private TDescription description = description;
public override Task OnActivated()
{
Value = valueDelegate.Invoke(configuration);
return base.OnActivated();
}
public Task Handle(ChangedEventArgs<TConfiguration> args)
{
if (args.Sender is TConfiguration configuration)
{
Value = valueDelegate.Invoke(configuration);
}
return Task.CompletedTask;
}
}
@@ -1,8 +0,0 @@
namespace Toolkit.Foundation;
public record ComponentContentConfiguration
{
// public string ContentRoot { get; set; } = "Local";
public string JsonFileName { get; set; } = "Settings2.json";
}
@@ -7,23 +7,28 @@ public partial class ConfigurationValueViewModel<TConfiguration, TValue>(IServic
ISubscriber subscriber,
IDisposer disposer,
TConfiguration configuration,
Func<TConfiguration, TValue> valueDelegate) :
IWritableConfiguration<TConfiguration> writer,
Func<TConfiguration, TValue?> read,
Action<TValue?, TConfiguration> write) :
ValueViewModel<TValue>(provider, factory, mediator, publisher, subscriber, disposer),
INotificationHandler<ChangedEventArgs<TConfiguration>>
where TConfiguration : class
{
private readonly TConfiguration configuration = configuration;
private readonly Func<TConfiguration, TValue> valueDelegate = valueDelegate;
public Task Handle(ChangedEventArgs<TConfiguration> args)
{
throw new NotImplementedException();
}
protected override void OnChanged(TValue? value)
{
writer.Write(args => write(value, args));
base.OnChanged(value);
}
public override Task OnActivated()
{
Value = valueDelegate(configuration);
Value = read(configuration);
return base.OnActivated();
}
}
@@ -105,6 +105,7 @@ public static class IServiceCollectionExtensions
services.AddTransient<IInitialization, TInitialization>();
return services;
}
public static IServiceCollection AddRange(this IServiceCollection services,
IServiceCollection fromServices)
{
@@ -166,22 +167,24 @@ public static class IServiceCollectionExtensions
}
public static IServiceCollection AddValueTemplate<TConfiguration, TValue, TViewModel, TView>(this IServiceCollection services,
Func<TConfiguration, TValue> valueDelegate,
Func<TConfiguration, TValue> readDelegate,
Action<TValue?, TConfiguration> writeDelegate,
object? key = null,
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
params object[]? parameters)
{
parameters = [valueDelegate, .. parameters ?? Enumerable.Empty<object?>()];
parameters = [readDelegate, writeDelegate, .. parameters ?? Enumerable.Empty<object?>()];
return AddTemplate<TViewModel, TViewModel, TView>(services, key, serviceLifetime, parameters);
}
public static IServiceCollection AddValueTemplate<TConfiguration, TValue, TViewModel, TViewModelImplementation, TView>(this IServiceCollection services,
Func<TConfiguration, TValue> valueDelegate,
Func<TConfiguration, TValue> readDelegate,
Action<TValue?, TConfiguration> writeDelegate,
object? key = null,
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
params object[]? parameters)
{
parameters = [valueDelegate, .. parameters ?? Enumerable.Empty<object?>()];
parameters = [readDelegate, writeDelegate, .. parameters ?? Enumerable.Empty<object?>()];
return AddTemplate<TViewModel, TViewModelImplementation, TView>(services, key, serviceLifetime, parameters);
}
}