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

Improve JsonRPC plugin settings #2430

Merged
merged 8 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,15 @@ protected void ExecuteFlowLauncherAPI(string method, object[] parameters)

private async Task InitSettingAsync()
{
if (!File.Exists(SettingConfigurationPath))
return;
JsonRpcConfigurationModel configuration = null;
if (File.Exists(SettingConfigurationPath))
{
var deserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
configuration =
deserializer.Deserialize<JsonRpcConfigurationModel>(
await File.ReadAllTextAsync(SettingConfigurationPath));
}

var deserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var configuration =
deserializer.Deserialize<JsonRpcConfigurationModel>(
await File.ReadAllTextAsync(SettingConfigurationPath));

Settings ??= new JsonRPCPluginSettings
{
Expand Down
13 changes: 8 additions & 5 deletions Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Flow.Launcher.Core.Plugin
{
public class JsonRPCPluginSettings
{
public required JsonRpcConfigurationModel Configuration { get; init; }
public required JsonRpcConfigurationModel? Configuration { get; init; }

public required string SettingPath { get; init; }
public Dictionary<string, FrameworkElement> SettingControls { get; } = new();
Expand All @@ -37,6 +37,11 @@ public async Task InitializeAsync()
_storage = new JsonStorage<ConcurrentDictionary<string, object>>(SettingPath);
Settings = await _storage.LoadAsync();

if (Settings != null)
{
return;
}

foreach (var (type, attributes) in Configuration.Body)
{
if (attributes.Name == null)
Expand All @@ -59,10 +64,7 @@ public void UpdateSettings(IReadOnlyDictionary<string, object> settings)

foreach (var (key, value) in settings)
{
if (Settings.ContainsKey(key))
{
Settings[key] = value;
}
Settings[key] = value;

if (SettingControls.TryGetValue(key, out var control))
{
Expand All @@ -83,6 +85,7 @@ public void UpdateSettings(IReadOnlyDictionary<string, object> settings)
}
}
}
Save();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry another thoughts that why didn't we keep this consistent with other plugins. Save only be called when manually triggered or flow being closed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess just personal preference. It feels off to not save to file on change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah ok

}

public async Task SaveAsync()
Expand Down
Loading