Added DeepL API key setting

This commit is contained in:
Thorsten Sommer 2022-07-30 15:49:19 +02:00
parent d97b5cd0c1
commit 87c12de066
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 83 additions and 2 deletions

View File

@ -2,5 +2,6 @@
public static class SettingNames public static class SettingNames
{ {
public static readonly string DEEPL_API_KEY = "DeepL API Key";
public static readonly string DEEPL_MODE = "DeepL Mode"; public static readonly string DEEPL_MODE = "DeepL Mode";
} }

View File

@ -7,6 +7,8 @@ namespace Processor;
public static class AppSettings public static class AppSettings
{ {
#region DeepL Mode
public static async Task SetDeepLMode(SettingDeepL mode) public static async Task SetDeepLMode(SettingDeepL mode)
{ {
// Convert the enum to its int value: // Convert the enum to its int value:
@ -57,4 +59,58 @@ public static class AppSettings
return (SettingDeepL) setting.IntegerValue; 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<DataContext>();
// 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<string> GetDeepLAPIKey()
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// 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
} }

View File

@ -45,7 +45,7 @@ public partial class Setting : UserControl
Func<Control> SetupDataControl Func<Control> SetupDataControl
); );
private static async Task<Setting> ShowDeepLSettingAsync() private static async Task<Setting> ShowDeepLModeSettingAsync()
{ {
var currentSetting = await AppSettings.GetDeepLMode(); var currentSetting = await AppSettings.GetDeepLMode();
var settingData = new SettingUIData( var settingData = new SettingUIData(
@ -91,8 +91,32 @@ public partial class Setting : UserControl
}; };
} }
private static async Task<Setting> 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<Task<Setting>> GetAllSettings() public static IEnumerable<Task<Setting>> GetAllSettings()
{ {
yield return ShowDeepLSettingAsync(); yield return ShowDeepLAPIKeySettingAsync();
yield return ShowDeepLModeSettingAsync();
} }
} }