From 22b038fdd92f4952f5870262c86b68aa1ab4a887 Mon Sep 17 00:00:00 2001 From: rampaa Date: Fri, 11 Oct 2024 04:36:38 +0300 Subject: [PATCH] Minor --- JL.Core/Config/ConfigDBManager.cs | 20 ++++++++++--- JL.Core/Config/CoreConfigManager.cs | 21 +------------- JL.Windows/ConfigManager.cs | 45 +++++------------------------ 3 files changed, 24 insertions(+), 62 deletions(-) diff --git a/JL.Core/Config/ConfigDBManager.cs b/JL.Core/Config/ConfigDBManager.cs index d884ad46..8b6422e4 100644 --- a/JL.Core/Config/ConfigDBManager.cs +++ b/JL.Core/Config/ConfigDBManager.cs @@ -172,7 +172,7 @@ FROM setting _ = command.ExecuteNonQuery(); } - public static T GetValueFromConfig(SqliteConnection connection, T variable, string configKey, TryParseHandler tryParseHandler) where T : struct + public static T GetValueFromConfig(SqliteConnection connection, T defaultValue, string configKey, TryParseHandler tryParseHandler) where T : struct { string? configValue = GetSettingValue(connection, configKey); if (configValue is not null && tryParseHandler(configValue, out T value)) @@ -182,14 +182,26 @@ public static T GetValueFromConfig(SqliteConnection connection, T variable, s if (configValue is null) { - InsertSetting(connection, configKey, Convert.ToString(variable, CultureInfo.InvariantCulture)!); + InsertSetting(connection, configKey, Convert.ToString(defaultValue, CultureInfo.InvariantCulture)!); } else { - UpdateSetting(connection, configKey, Convert.ToString(variable, CultureInfo.InvariantCulture)!); + UpdateSetting(connection, configKey, Convert.ToString(defaultValue, CultureInfo.InvariantCulture)!); } - return variable; + return defaultValue; + } + + public static string GetValueFromConfig(SqliteConnection connection, string defaultValue, string configKey) + { + string? configValue = GetSettingValue(connection, configKey); + if (configValue is not null) + { + return configValue; + } + + InsertSetting(connection, configKey, defaultValue); + return defaultValue; } public static T GetNumberWithDecimalPointFromConfig(SqliteConnection connection, T number, string configKey, TryParseHandlerWithCultureInfo tryParseHandler) where T : struct diff --git a/JL.Core/Config/CoreConfigManager.cs b/JL.Core/Config/CoreConfigManager.cs index 0b849c5c..98a76b5d 100644 --- a/JL.Core/Config/CoreConfigManager.cs +++ b/JL.Core/Config/CoreConfigManager.cs @@ -24,26 +24,7 @@ public static class CoreConfigManager public static void ApplyPreferences(SqliteConnection connection) { - { - string? minimumLogLevelStr = ConfigDBManager.GetSettingValue(connection, "MinimumLogLevel"); - if (minimumLogLevelStr is null) - { - ConfigDBManager.InsertSetting(connection, "MinimumLogLevel", "Error"); - } - else - { - Utils.s_loggingLevelSwitch.MinimumLevel = minimumLogLevelStr switch - { - "Fatal" => LogEventLevel.Fatal, - "Error" => LogEventLevel.Error, - "Warning" => LogEventLevel.Warning, - "Information" => LogEventLevel.Information, - "Debug" => LogEventLevel.Debug, - "Verbose" => LogEventLevel.Verbose, - _ => LogEventLevel.Error - }; - } - } + Utils.s_loggingLevelSwitch.MinimumLevel = ConfigDBManager.GetValueFromConfig(connection, LogEventLevel.Error, "MinimumLogLevel", Enum.TryParse); { string? ankiConnectUriStr = ConfigDBManager.GetSettingValue(connection, nameof(AnkiConnectUri)); diff --git a/JL.Windows/ConfigManager.cs b/JL.Windows/ConfigManager.cs index 24df380e..6db3ca7e 100644 --- a/JL.Windows/ConfigManager.cs +++ b/JL.Windows/ConfigManager.cs @@ -574,24 +574,12 @@ public static void ApplyPreferences() } { - string? mainWindowFontStr = ConfigDBManager.GetSettingValue(connection, "MainWindowFont"); - if (mainWindowFontStr is null) - { - ConfigDBManager.InsertSetting(connection, "MainWindowFont", "Meiryo"); - mainWindowFontStr = "Meiryo"; - } - + string mainWindowFontStr = ConfigDBManager.GetValueFromConfig(connection, "Meiryo", "MainWindowFont"); MainWindow.Instance.MainTextBox.FontFamily = new FontFamily(mainWindowFontStr); } { - string? popupPositionRelativeToCursorStr = ConfigDBManager.GetSettingValue(connection, "PopupPositionRelativeToCursor"); - if (popupPositionRelativeToCursorStr is null) - { - popupPositionRelativeToCursorStr = "BottomRight"; - ConfigDBManager.InsertSetting(connection, "PopupPositionRelativeToCursor", popupPositionRelativeToCursorStr); - } - + string popupPositionRelativeToCursorStr = ConfigDBManager.GetValueFromConfig(connection, "BottomRight", "PopupPositionRelativeToCursor"); switch (popupPositionRelativeToCursorStr) { case "TopLeft": @@ -622,13 +610,7 @@ public static void ApplyPreferences() } { - string? popupFlipStr = ConfigDBManager.GetSettingValue(connection, "PopupFlip"); - if (popupFlipStr is null) - { - popupFlipStr = "Both"; - ConfigDBManager.InsertSetting(connection, "PopupFlip", popupFlipStr); - } - + string popupFlipStr = ConfigDBManager.GetValueFromConfig(connection, "Both", "PopupFlip"); switch (popupFlipStr) { case "X": @@ -654,13 +636,7 @@ public static void ApplyPreferences() } { - string? lookupModeStr = ConfigDBManager.GetSettingValue(connection, "LookupMode"); - if (lookupModeStr is null) - { - lookupModeStr = "Hover"; - ConfigDBManager.InsertSetting(connection, "LookupMode", lookupModeStr); - } - + string lookupModeStr = ConfigDBManager.GetValueFromConfig(connection, "Hover", "LookupMode"); switch (lookupModeStr) { case "Hover": @@ -686,16 +662,9 @@ public static void ApplyPreferences() } { - string? popupFontStr = ConfigDBManager.GetSettingValue(connection, nameof(PopupFont)); - if (popupFontStr is null) - { - ConfigDBManager.InsertSetting(connection, nameof(PopupFont), PopupFont.Source); - } - else - { - PopupFont = new FontFamily(popupFontStr); - WindowsUtils.PopupFontTypeFace = new Typeface(popupFontStr); - } + string popupFontStr = ConfigDBManager.GetValueFromConfig(connection, PopupFont.Source, nameof(PopupFont)); + PopupFont = new FontFamily(popupFontStr); + WindowsUtils.PopupFontTypeFace = new Typeface(popupFontStr); } PopupWindow? currentPopupWindow = MainWindow.Instance.FirstPopupWindow;