Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function specific settings overriding global ones #38

Merged
merged 13 commits into from
Jan 29, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using ModularToolManager.Services.Ui;
using ModularToolManager.ViewModels;
using ModularToolManager.Views;
using ModularToolManagerModel.DependencyInjection;
using ModularToolManagerModel.Services.Dependency;
using ModularToolManagerModel.Services.Functions;
using ModularToolManagerModel.Services.IO;
Expand Down Expand Up @@ -80,23 +81,20 @@ public static IServiceCollection AddViews(this IServiceCollection collection)
/// <returns>The extended collection</returns>
public static IServiceCollection AddServices(this IServiceCollection collection)
{
return collection.AddSingleton<IPathService, PathService>()
return collection.AddAllModelDepdencies()
.AddSingleton<IPathService, PathService>()
.AddSingleton<IStyleService, DefaultStyleService>()
.AddSingleton<IPluginTranslationService, PluginTranslationService>()
.AddSingleton<IFunctionSettingsService, FunctionSettingService>()
.AddSingleton<ILanguageService, ResourceCultureService>()
.AddSingleton<IWindowManagementService, WindowManagementService>()
.AddSingleton<IPluginService, PluginService>()
.AddSingleton<ISerializationOptionFactory<JsonSerializerOptions>, JsonSerializationOptionFactory>()
.AddSingleton<IFunctionService, SerializedFunctionService>()
.AddSingleton<IViewModelLocatorService, ViewModelLocator>()
.AddSingleton<IDependencyResolverService, MicrosoftDepdencyResolverService>()
.AddSingleton<ISettingsService, SerializedSettingsService>()
.AddTransient<ViewLocator>()
.AddTransient<ISerializeService, JsonSerializationService>()
.AddTransient<IImageService, ImageService>()
.AddTransient<IUrlOpenerService, UrlOpenerService>()
.AddTransient<IFileSystemService, FileSystemService>()
.AddTransient(typeof(IPluginLoggerService<>), typeof(LoggingPluginAdapter<>));
.AddSingleton<PluginSettingViewModelService>()
.AddTransient<IImageService, ImageService>();
}
}
95 changes: 0 additions & 95 deletions src/ModularToolManager/Models/PersistantPluginSetting.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/ModularToolManager/Models/PluginSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ public class PluginSettings
/// The settings to persist for the plugin
/// </summary>
[JsonPropertyName("settings")]
public List<PersistantPluginSetting>? Settings { get; set; }
public List<SettingModel>? Settings { get; set; }
}
3 changes: 0 additions & 3 deletions src/ModularToolManager/ModularToolManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,4 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Folder Include="Models\PluginSettings\" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using ModularToolManager.ViewModels;
using ModularToolManager.ViewModels.Settings;
using ModularToolManagerPlugin.Enums;
using ModularToolManagerPlugin.Models;

namespace ModularToolManager.Services.Ui;
internal class PluginSettingViewModelService
XanatosX marked this conversation as resolved.
Show resolved Hide resolved
{
public IPluginSettingModel? GetViewModel(SettingModel settingModel)
{
return settingModel.Type switch
{
SettingType.Boolean => new BoolPluginSettingViewModel(settingModel),
SettingType.String => new StringPluginSettingViewModel(settingModel),
SettingType.Float => new FloatPluginSettingViewModel(settingModel),
SettingType.Int => new IntPluginSettingViewModel(settingModel),
_ => null
};
}
}
98 changes: 97 additions & 1 deletion src/ModularToolManager/ViewModels/AddFunctionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
using CommunityToolkit.Mvvm.Messaging;
using ModularToolManager.Models;
using ModularToolManager.Models.Messages;
using ModularToolManager.Services.Settings;
using ModularToolManager.Services.Ui;
using ModularToolManager.ViewModels.Settings;
using ModularToolManagerModel.Services.Functions;
using ModularToolManagerModel.Services.Plugin;
using ModularToolManagerPlugin.Models;
using ModularToolManagerPlugin.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
Expand All @@ -30,12 +34,28 @@ internal partial class AddFunctionViewModel : ObservableValidator
/// Private list for all the function plugins
/// </summary>
private readonly List<FunctionPluginViewModel> functionPlugins;

/// <summary>
/// The service used to get plugins
/// </summary>
private readonly IPluginService? pluginService;

/// <summary>
/// The service used to get the function settings
/// </summary>
private readonly IFunctionSettingsService functionSettingsService;

/// <summary>
/// The function service to use
/// </summary>
private readonly IFunctionService? functionService;

/// <summary>
/// Service to use for opening windows or modals
/// </summary>
private readonly IWindowManagementService windowManagmentService;
private readonly ISettingsService settingsService;
private readonly PluginSettingViewModelService pluginSettingView;

/// <summary>
/// The display name of the function
Expand All @@ -62,6 +82,12 @@ internal partial class AddFunctionViewModel : ObservableValidator
[NotifyCanExecuteChangedFor(nameof(OpenFunctionPathCommand))]
private FunctionPluginViewModel? selectedFunctionPlugin;

/// <summary>
/// Are there any settings available for the plugin
/// </summary>
[ObservableProperty]
private bool settingsAvailable;

/// <summary>
/// The parameters for the function
/// </summary>
Expand All @@ -76,6 +102,15 @@ internal partial class AddFunctionViewModel : ObservableValidator
[NotifyCanExecuteChangedFor(nameof(OkCommand))]
private string? selectedPath;

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(PluginSettingsFound))]
private List<IPluginSettingModel>? pluginSettings;

/// <summary>
/// Should the settings part be visible
/// </summary>
public bool PluginSettingsFound => PluginSettings?.Any() ?? false;

/// <summary>
/// The identifier to use for the function
/// </summary>
Expand All @@ -86,20 +121,65 @@ internal partial class AddFunctionViewModel : ObservableValidator
/// </summary>
/// <param name="pluginService">The plugin service to use</param>
/// <param name="functionService">The function service to use</param>
public AddFunctionViewModel(IPluginService? pluginService, IFunctionService? functionService, IWindowManagementService windowManagmentService)
public AddFunctionViewModel(IPluginService? pluginService,
IFunctionService? functionService,
IFunctionSettingsService functionSettingsService,
IWindowManagementService windowManagmentService,
ISettingsService settingsService,
PluginSettingViewModelService pluginSettingView)
{
functionPlugins = new();
this.pluginService = pluginService;
this.functionService = functionService;
this.functionSettingsService = functionSettingsService;
this.windowManagmentService = windowManagmentService;
this.settingsService = settingsService;
this.pluginSettingView = pluginSettingView;
if (pluginService is not null)
{
functionPlugins.AddRange(pluginService!.GetAvailablePlugins()
.Select(plugin => new FunctionPluginViewModel(plugin)));
}

PropertyChanged += (_, e) =>
{
if (e.PropertyName == nameof(SelectedFunctionPlugin))
{
LoadPluginSettings();
}
};

InitialValueSet();
}

/// <summary>
/// Load all the possible settings for the plugin
/// </summary>
private void LoadPluginSettings()
{
if (SelectedFunctionPlugin is null || SelectedFunctionPlugin.Plugin is null)
{
return;
}
SelectedFunctionPlugin.Plugin.ResetSettings();
List<IPluginSettingModel> settings = functionSettingsService.GetPluginSettingsValues(SelectedFunctionPlugin.Plugin, false)
.OfType<SettingModel>()
.Select(pluginSetting => pluginSettingView.GetViewModel(pluginSetting))
.OfType<IPluginSettingModel>()
.ToList();
var appPluginSettings = settingsService.GetApplicationSettings().PluginSettings
.FirstOrDefault(pSettings => pSettings.Plugin == SelectedFunctionPlugin.Plugin);
if (appPluginSettings is not null)
{
foreach (var setting in appPluginSettings.Settings ?? Enumerable.Empty<SettingModel>())
{
var matchingSettingView = settings.FirstOrDefault(settingView => settingView.GetSettingsModel().Key == setting.Key);
matchingSettingView?.UpdateValue(setting.Value);
}
}
PluginSettings = settings;
}

/// <summary>
/// Load on a function by the identifer to allow editing of the function
/// </summary>
Expand All @@ -116,6 +196,15 @@ public bool LoadInFunction(string identifier)
DisplayName = function.DisplayName;
Description = function.Description;
SelectedFunctionPlugin = FunctionPlugins.FirstOrDefault(plugin => plugin.Plugin == function.Plugin);
foreach (var setting in PluginSettings ?? Enumerable.Empty<IPluginSettingModel>())
{
var settingToUpdate = function.Settings.FirstOrDefault(pSetting => pSetting.Key == setting.GetSettingsModel().Key);
if (settingToUpdate is not null)
{
setting.UpdateValue(settingToUpdate.Value);
}

}
FunctionParameters = function.Parameters;
SelectedPath = function.Path;

Expand Down Expand Up @@ -168,11 +257,17 @@ private FunctionModel CreateNewFunctionModel()
Description = Description,
Plugin = SelectedFunctionPlugin!.Plugin,
Parameters = FunctionParameters!,
Settings = GetPluginSettings(),
Path = SelectedPath!,
SortOrder = 0
};
}

private IEnumerable<SettingModel> GetPluginSettings()
{
return PluginSettings?.Select(model => model.GetSettingsModel()) ?? Enumerable.Empty<SettingModel>();
}

/// <summary>
/// Create a edited function model with given identifier
/// </summary>
Expand All @@ -186,6 +281,7 @@ private FunctionModel CreateEditedFunctionModel()
Description = Description,
Plugin = SelectedFunctionPlugin!.Plugin,
Parameters = FunctionParameters!,
Settings = GetPluginSettings(),
Path = SelectedPath!,
SortOrder = 0
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ public override SettingModel GetSettingsModel()
storedModel.SetValue(IsChecked);
return storedModel;
}

public override void UpdateValue(object? newData)
XanatosX marked this conversation as resolved.
Show resolved Hide resolved
{
if (newData is bool)
{
IsChecked = (bool)newData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public override SettingModel GetSettingsModel()
storedModel.SetValue(FloatNumber);
return storedModel;
}

public override void UpdateValue(object? newData)
XanatosX marked this conversation as resolved.
Show resolved Hide resolved
{
if (newData is float)
{
FloatNumber = (float)newData;
}
}
}
7 changes: 6 additions & 1 deletion src/ModularToolManager/ViewModels/FunctionButtonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,13 @@ private void ApplyPluginSettings(IFunctionPlugin? plugin)

List<SettingAttribute> pluginSettings = functionSettingsService.GetPluginSettings(plugin).ToList() ?? new();
var settings = settingsService.GetApplicationSettings().PluginSettings.FirstOrDefault(setting => setting?.Plugin?.GetType() == functionModel?.Plugin?.GetType());
foreach (var loadedPluginSetting in settings?.Settings?.Select(setting => setting.GetSettingModel()) ?? Enumerable.Empty<SettingModel>())
foreach (var loadedPluginSetting in settings?.Settings ?? Enumerable.Empty<SettingModel>())
{
var specificSettings = functionModel?.Settings.FirstOrDefault(setting => setting.Key == loadedPluginSetting.Key);
if (specificSettings is not null)
{
loadedPluginSetting.SetValue(specificSettings.Value);
}
var matchingAttribute = pluginSettings.FirstOrDefault(setting => setting.Key == loadedPluginSetting.Key);
if (matchingAttribute is null)
{
Expand Down
Loading