extend ConfigurationValueViewModel to write the value back to settings
This commit is contained in:
@@ -9,119 +9,6 @@ namespace Toolkit.Avalonia;
|
|||||||
|
|
||||||
public static class IServiceCollectionExtensions
|
public static class IServiceCollectionExtensions
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddConfigurationTemplate<TConfiguration, TValue, THeader,
|
|
||||||
TDescription, TAction>(this IServiceCollection services,
|
|
||||||
params object[]? parameters)
|
|
||||||
where TConfiguration : class
|
|
||||||
where THeader : class
|
|
||||||
where TDescription : class
|
|
||||||
where TAction : class
|
|
||||||
{
|
|
||||||
Type viewModelType = typeof(ComponentConfigurationViewModel<TConfiguration, TValue, THeader, TDescription, TAction>);
|
|
||||||
Type viewType = typeof(Button);
|
|
||||||
|
|
||||||
object key = viewModelType.Name.Replace("ViewModel", "");
|
|
||||||
|
|
||||||
services.AddTransient<IComponentConfigurationViewModel,
|
|
||||||
ComponentConfigurationViewModel<TConfiguration, TValue, THeader, TDescription, TAction>>(provider =>
|
|
||||||
provider.GetRequiredService<IServiceFactory>()
|
|
||||||
.Create<ComponentConfigurationViewModel<TConfiguration, TValue, THeader, TDescription, TAction>>(parameters)!);
|
|
||||||
|
|
||||||
services.TryAddTransient(viewType);
|
|
||||||
|
|
||||||
services.AddKeyedTransient<IComponentConfigurationViewModel,
|
|
||||||
ComponentConfigurationViewModel<TConfiguration, TValue, THeader, TDescription, TAction>>(key, (provider, key) =>
|
|
||||||
provider.GetRequiredService<IServiceFactory>()
|
|
||||||
.Create<ComponentConfigurationViewModel<TConfiguration, TValue, THeader, TDescription, TAction>>(parameters)!);
|
|
||||||
|
|
||||||
services.TryAddKeyedTransient(viewType, key);
|
|
||||||
|
|
||||||
services.AddTransient<IContentTemplateDescriptor>(provider =>
|
|
||||||
new ContentTemplateDescriptor(key, viewModelType, viewType, parameters));
|
|
||||||
|
|
||||||
services.TryAddTransient<THeader>();
|
|
||||||
services.TryAddTransient<TDescription>();
|
|
||||||
services.TryAddTransient<TAction>();
|
|
||||||
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IServiceCollection AddConfigurationTemplate<TConfiguration, TValue, TAction>(this IServiceCollection services,
|
|
||||||
Func<TConfiguration, TValue> valueDelegate,
|
|
||||||
object header,
|
|
||||||
object description,
|
|
||||||
params object[]? parameters)
|
|
||||||
where TConfiguration : class
|
|
||||||
where TAction : class
|
|
||||||
{
|
|
||||||
Type viewModelType = typeof(ComponentConfigurationViewModel<TConfiguration, TValue, TAction>);
|
|
||||||
Type viewType = typeof(Button);
|
|
||||||
|
|
||||||
object key = viewModelType.Name.Replace("ViewModel", "");
|
|
||||||
|
|
||||||
parameters = [valueDelegate, header, description, .. parameters ?? Enumerable.Empty<object?>()];
|
|
||||||
|
|
||||||
services.AddTransient<IComponentConfigurationViewModel,
|
|
||||||
ComponentConfigurationViewModel<TConfiguration, TValue, TAction>>(provider =>
|
|
||||||
provider.GetRequiredService<IServiceFactory>()
|
|
||||||
.Create<ComponentConfigurationViewModel<TConfiguration, TValue, TAction>>(parameters)!);
|
|
||||||
|
|
||||||
services.TryAddTransient(viewType);
|
|
||||||
|
|
||||||
services.AddKeyedTransient<IComponentConfigurationViewModel,
|
|
||||||
ComponentConfigurationViewModel<TConfiguration, TValue, TAction>>(key, (provider, key) =>
|
|
||||||
provider.GetRequiredService<IServiceFactory>()
|
|
||||||
.Create<ComponentConfigurationViewModel<TConfiguration, TValue, TAction>>(parameters)!);
|
|
||||||
|
|
||||||
services.TryAddKeyedTransient(viewType, key);
|
|
||||||
|
|
||||||
services.AddTransient<IContentTemplateDescriptor>(provider =>
|
|
||||||
new ContentTemplateDescriptor(key, viewModelType, viewType, parameters));
|
|
||||||
|
|
||||||
services.TryAddTransient<TAction>();
|
|
||||||
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IServiceCollection AddConfigurationTemplate<TConfiguration, TValue,
|
|
||||||
TDescription, TAction>(this IServiceCollection services,
|
|
||||||
Func<TConfiguration, TValue> valueDelegate,
|
|
||||||
object description,
|
|
||||||
params object[]? parameters)
|
|
||||||
where TConfiguration : class
|
|
||||||
where TDescription : class
|
|
||||||
where TAction : class
|
|
||||||
{
|
|
||||||
Type viewModelType = typeof(ComponentConfigurationViewModel<TConfiguration, TValue, TDescription, TAction>);
|
|
||||||
Type viewType = typeof(Button);
|
|
||||||
|
|
||||||
object key = viewModelType.Name.Replace("ViewModel", "");
|
|
||||||
|
|
||||||
parameters = [valueDelegate, description, .. parameters ?? Enumerable.Empty<object?>()];
|
|
||||||
|
|
||||||
services.AddTransient<IComponentConfigurationViewModel,
|
|
||||||
ComponentConfigurationViewModel<TConfiguration, TValue, TDescription, TAction>>(provider =>
|
|
||||||
provider.GetRequiredService<IServiceFactory>()
|
|
||||||
.Create<ComponentConfigurationViewModel<TConfiguration, TValue, TDescription, TAction>>(parameters)!);
|
|
||||||
|
|
||||||
services.TryAddTransient(viewType);
|
|
||||||
|
|
||||||
services.AddKeyedTransient<IComponentConfigurationViewModel,
|
|
||||||
ComponentConfigurationViewModel<TConfiguration, TValue, TDescription, TAction>>(key, (provider, key) =>
|
|
||||||
provider.GetRequiredService<IServiceFactory>()
|
|
||||||
.Create<ComponentConfigurationViewModel<TConfiguration, TValue, TDescription, TAction>>(parameters)!);
|
|
||||||
|
|
||||||
services.TryAddKeyedTransient(viewType, key);
|
|
||||||
|
|
||||||
services.AddTransient<IContentTemplateDescriptor>(provider =>
|
|
||||||
new ContentTemplateDescriptor(key, viewModelType, viewType, parameters));
|
|
||||||
|
|
||||||
services.TryAddTransient<TDescription>();
|
|
||||||
services.TryAddTransient<TAction>();
|
|
||||||
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IServiceCollection AddAvalonia(this IServiceCollection services)
|
public static IServiceCollection AddAvalonia(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddTransient<ITopLevelProvider, TopLevelProvider>();
|
services.AddTransient<ITopLevelProvider, TopLevelProvider>();
|
||||||
|
|||||||
@@ -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,
|
ISubscriber subscriber,
|
||||||
IDisposer disposer,
|
IDisposer disposer,
|
||||||
TConfiguration configuration,
|
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),
|
ValueViewModel<TValue>(provider, factory, mediator, publisher, subscriber, disposer),
|
||||||
INotificationHandler<ChangedEventArgs<TConfiguration>>
|
INotificationHandler<ChangedEventArgs<TConfiguration>>
|
||||||
where TConfiguration : class
|
where TConfiguration : class
|
||||||
{
|
{
|
||||||
private readonly TConfiguration configuration = configuration;
|
|
||||||
|
|
||||||
private readonly Func<TConfiguration, TValue> valueDelegate = valueDelegate;
|
|
||||||
|
|
||||||
public Task Handle(ChangedEventArgs<TConfiguration> args)
|
public Task Handle(ChangedEventArgs<TConfiguration> args)
|
||||||
{
|
{
|
||||||
|
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void OnChanged(TValue? value)
|
||||||
|
{
|
||||||
|
writer.Write(args => write(value, args));
|
||||||
|
base.OnChanged(value);
|
||||||
|
}
|
||||||
|
|
||||||
public override Task OnActivated()
|
public override Task OnActivated()
|
||||||
{
|
{
|
||||||
Value = valueDelegate(configuration);
|
Value = read(configuration);
|
||||||
return base.OnActivated();
|
return base.OnActivated();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,6 +105,7 @@ public static class IServiceCollectionExtensions
|
|||||||
services.AddTransient<IInitialization, TInitialization>();
|
services.AddTransient<IInitialization, TInitialization>();
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddRange(this IServiceCollection services,
|
public static IServiceCollection AddRange(this IServiceCollection services,
|
||||||
IServiceCollection fromServices)
|
IServiceCollection fromServices)
|
||||||
{
|
{
|
||||||
@@ -166,22 +167,24 @@ public static class IServiceCollectionExtensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddValueTemplate<TConfiguration, TValue, TViewModel, TView>(this IServiceCollection services,
|
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,
|
object? key = null,
|
||||||
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
|
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
|
||||||
params object[]? parameters)
|
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);
|
return AddTemplate<TViewModel, TViewModel, TView>(services, key, serviceLifetime, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddValueTemplate<TConfiguration, TValue, TViewModel, TViewModelImplementation, TView>(this IServiceCollection services,
|
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,
|
object? key = null,
|
||||||
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
|
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
|
||||||
params object[]? parameters)
|
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);
|
return AddTemplate<TViewModel, TViewModelImplementation, TView>(services, key, serviceLifetime, parameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user