Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Oct 11, 2024
1 parent 1a230e9 commit 22b038f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 62 deletions.
20 changes: 16 additions & 4 deletions JL.Core/Config/ConfigDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ FROM setting
_ = command.ExecuteNonQuery();
}

public static T GetValueFromConfig<T>(SqliteConnection connection, T variable, string configKey, TryParseHandler<T> tryParseHandler) where T : struct
public static T GetValueFromConfig<T>(SqliteConnection connection, T defaultValue, string configKey, TryParseHandler<T> tryParseHandler) where T : struct
{
string? configValue = GetSettingValue(connection, configKey);
if (configValue is not null && tryParseHandler(configValue, out T value))
Expand All @@ -182,14 +182,26 @@ public static T GetValueFromConfig<T>(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<T>(SqliteConnection connection, T number, string configKey, TryParseHandlerWithCultureInfo<T> tryParseHandler) where T : struct
Expand Down
21 changes: 1 addition & 20 deletions JL.Core/Config/CoreConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
45 changes: 7 additions & 38 deletions JL.Windows/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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":
Expand All @@ -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":
Expand All @@ -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;
Expand Down

0 comments on commit 22b038f

Please sign in to comment.