From 87c12de06636a593710654f8dad24eb13d733225 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 30 Jul 2022 15:49:19 +0200 Subject: [PATCH] Added DeepL API key setting --- .../DataModel/Database/SettingNames.cs | 1 + I18N Commander/Processor/AppSettings.cs | 56 +++++++++++++++++++ .../UI WinForms/Components/Setting.cs | 28 +++++++++- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/I18N Commander/DataModel/Database/SettingNames.cs b/I18N Commander/DataModel/Database/SettingNames.cs index b9fe29f..7a41b64 100644 --- a/I18N Commander/DataModel/Database/SettingNames.cs +++ b/I18N Commander/DataModel/Database/SettingNames.cs @@ -2,5 +2,6 @@ public static class SettingNames { + public static readonly string DEEPL_API_KEY = "DeepL API Key"; public static readonly string DEEPL_MODE = "DeepL Mode"; } \ No newline at end of file diff --git a/I18N Commander/Processor/AppSettings.cs b/I18N Commander/Processor/AppSettings.cs index ae6b7e9..b8b1f25 100644 --- a/I18N Commander/Processor/AppSettings.cs +++ b/I18N Commander/Processor/AppSettings.cs @@ -7,6 +7,8 @@ namespace Processor; public static class AppSettings { + #region DeepL Mode + public static async Task SetDeepLMode(SettingDeepL mode) { // Convert the enum to its int value: @@ -57,4 +59,58 @@ public static class AppSettings return (SettingDeepL) setting.IntegerValue; } + + #endregion + + #region DeepL API Key + + public static async Task SetDeepLAPIKey(string apiKey) + { + // Get the database: + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + + // Check, if the setting is already set: + if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_API_KEY) is {} existingSetting) + { + existingSetting.TextValue = apiKey; + await db.SaveChangesAsync(); + } + + // Does not exist, so create it: + else + { + var setting = new Setting + { + Code = SettingNames.DEEPL_API_KEY, + TextValue = apiKey, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + } + } + + public static async Task GetDeepLAPIKey() + { + // Get the database: + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + + // Check, if the setting is already set: + if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_API_KEY) is { } existingSetting) + return existingSetting.TextValue; + + // Does not exist, so create it: + var setting = new Setting + { + Code = SettingNames.DEEPL_API_KEY, + TextValue = string.Empty, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + + return setting.TextValue; + } + + #endregion } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/Setting.cs b/I18N Commander/UI WinForms/Components/Setting.cs index f0a1bd6..d4ca5a3 100644 --- a/I18N Commander/UI WinForms/Components/Setting.cs +++ b/I18N Commander/UI WinForms/Components/Setting.cs @@ -45,7 +45,7 @@ public partial class Setting : UserControl Func SetupDataControl ); - private static async Task ShowDeepLSettingAsync() + private static async Task ShowDeepLModeSettingAsync() { var currentSetting = await AppSettings.GetDeepLMode(); var settingData = new SettingUIData( @@ -91,8 +91,32 @@ public partial class Setting : UserControl }; } + private static async Task ShowDeepLAPIKeySettingAsync() + { + var currentSetting = await AppSettings.GetDeepLAPIKey(); + var settingData = new SettingUIData( + Icon: Icons.icons8_key_512, + SettingName: () => "DeepL API Key", + SettingExplanation: () => "The API key is required to use the DeepL translation service. You can find your API key on the DeepL website.", + SetupDataControl: () => + { + var textbox = new TextBox(); + textbox.Text = currentSetting; + textbox.TextChanged += async (sender, args) => await AppSettings.SetDeepLAPIKey(textbox.Text); + textbox.Dock = DockStyle.Fill; + return textbox; + } + ); + + return new Setting(settingData) + { + Dock = DockStyle.Top, + }; + } + public static IEnumerable> GetAllSettings() { - yield return ShowDeepLSettingAsync(); + yield return ShowDeepLAPIKeySettingAsync(); + yield return ShowDeepLModeSettingAsync(); } } \ No newline at end of file