Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Dec 8, 2024
1 parent 9332470 commit b58f86d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
30 changes: 11 additions & 19 deletions JL.Windows/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,28 +204,26 @@ private ConfigManager()

public static void ResetConfigs()
{
Instance.SaveBeforeClosing();
using SqliteConnection connection = ConfigDBManager.CreateReadWriteDBConnection();
Instance.SaveBeforeClosing(connection);
ConfigDBManager.DeleteAllSettingsFromProfile("MainWindowTopPosition", "MainWindowLeftPosition");

ConfigManager newInstance = new();
using (SqliteConnection connection = ConfigDBManager.CreateReadWriteDBConnection())
{
ConfigDBManager.InsertSetting(connection, nameof(Theme), newInstance.Theme.ToString());
ConfigDBManager.InsertSetting(connection, nameof(StripPunctuationBeforeCalculatingCharacterCount), newInstance.StripPunctuationBeforeCalculatingCharacterCount.ToString());
}
ConfigDBManager.InsertSetting(connection, nameof(Theme), newInstance.Theme.ToString());
ConfigDBManager.InsertSetting(connection, nameof(StripPunctuationBeforeCalculatingCharacterCount), newInstance.StripPunctuationBeforeCalculatingCharacterCount.ToString());

newInstance.Theme = Instance.Theme;
newInstance.StripPunctuationBeforeCalculatingCharacterCount = Instance.StripPunctuationBeforeCalculatingCharacterCount;

Instance = newInstance;
CoreConfigManager.CreateNewCoreConfigManager();
Instance.ApplyPreferences();
Instance.ApplyPreferences(connection);

ConfigDBManager.AnalyzeAndVacuum(connection);
}

public void ApplyPreferences()
public void ApplyPreferences(SqliteConnection connection)
{
using SqliteConnection connection = ConfigDBManager.CreateReadWriteDBConnection();

CoreConfigManager coreConfigManager = CoreConfigManager.Instance;
coreConfigManager.ApplyPreferences(connection);

Expand Down Expand Up @@ -742,10 +740,7 @@ public void ApplyPreferences()

public void LoadPreferenceWindow(PreferencesWindow preferenceWindow)
{
ConfigDBManager.CreateDB();

preferenceWindow.JLVersionTextBlock.Text = string.Create(CultureInfo.InvariantCulture, $"v{Utils.JLVersion}");

preferenceWindow.DisableHotkeysKeyGestureTextBox.Text = DisableHotkeysKeyGesture.ToFormattedString();
preferenceWindow.MiningModeKeyGestureTextBox.Text = MiningModeKeyGesture.ToFormattedString();
preferenceWindow.PlayAudioKeyGestureTextBox.Text = PlayAudioKeyGesture.ToFormattedString();
Expand Down Expand Up @@ -1410,21 +1405,18 @@ public async Task SavePreferences(PreferencesWindow preferenceWindow)

ConfigDBManager.UpdateSetting(connection, "MainWindowLeftPosition",
(mainWindow.Left * dpi.DpiScaleX).ToString(CultureInfo.InvariantCulture));
}

ApplyPreferences();
ApplyPreferences(connection);
}

if (preferenceWindow.SetAnkiConfig)
{
await preferenceWindow.SaveMiningSetup().ConfigureAwait(false);
}
}

public void SaveBeforeClosing()
public void SaveBeforeClosing(SqliteConnection connection)
{
ConfigDBManager.CreateDB();
using SqliteConnection connection = ConfigDBManager.CreateReadWriteDBConnection();

MainWindow mainWindow = MainWindow.Instance;
ConfigDBManager.UpdateSetting(connection, "MainWindowFontSize",
mainWindow.FontSizeSlider.Value.ToString(CultureInfo.InvariantCulture));
Expand Down
16 changes: 10 additions & 6 deletions JL.Windows/GUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ protected override async void OnSourceInitialized(EventArgs e)

ConfigDBManager.CreateDB();

SqliteConnection connection = ConfigDBManager.CreateReadWriteDBConnection();
await using (connection.ConfigureAwait(true))
SqliteConnection migrationConnection = ConfigDBManager.CreateReadWriteDBConnection();
await using (migrationConnection.ConfigureAwait(true))
{
await ConfigMigrationManager.MigrateConfig(connection).ConfigureAwait(true);
await ConfigMigrationManager.MigrateConfig(migrationConnection).ConfigureAwait(true);
}

SqliteConnection readOnlyConnection = ConfigDBManager.CreateReadOnlyDBConnection();
Expand All @@ -88,7 +88,11 @@ protected override async void OnSourceInitialized(EventArgs e)
}

ConfigManager configManager = ConfigManager.Instance;
configManager.ApplyPreferences();
SqliteConnection connection = ConfigDBManager.CreateReadWriteDBConnection();
await using (connection.ConfigureAwait(true))
{
configManager.ApplyPreferences(connection);
}

RegexReplacerUtils.PopulateRegexReplacements();

Expand Down Expand Up @@ -433,12 +437,12 @@ public async Task HandleAppClosing()
{
SystemEvents.DisplaySettingsChanged -= DisplaySettingsChanged;
MagpieUtils.UnmarkWindowAsMagpieToolWindow(WindowHandle);
ConfigManager.Instance.SaveBeforeClosing();
Stats.IncrementStat(StatType.Time, StatsUtils.StatsStopWatch.ElapsedTicks);

SqliteConnection connection = ConfigDBManager.CreateReadWriteDBConnection();
await using (connection.ConfigureAwait(false))
{
ConfigManager.Instance.SaveBeforeClosing(connection);
Stats.IncrementStat(StatType.Time, StatsUtils.StatsStopWatch.ElapsedTicks);
StatsDBUtils.UpdateLifetimeStats(connection);
StatsDBUtils.UpdateProfileLifetimeStats(connection);
}
Expand Down
14 changes: 7 additions & 7 deletions JL.Windows/GUI/PreferencesWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,14 @@ private void ProfileComboBox_SelectionChanged(object sender, SelectionChangedEve
ProfileDBUtils.UpdateCurrentProfile(connection);
Stats.ProfileLifetimeStats = StatsDBUtils.GetStatsFromDB(connection, ProfileUtils.CurrentProfileId)!;
StatsDBUtils.UpdateProfileLifetimeStats(connection);
}

ConfigManager configManager = ConfigManager.Instance;
Application.Current.Dispatcher.Invoke(() =>
{
configManager.ApplyPreferences();
configManager.LoadPreferenceWindow(this);
});
ConfigManager configManager = ConfigManager.Instance;
Application.Current.Dispatcher.Invoke(() =>
{
configManager.ApplyPreferences(connection);
configManager.LoadPreferenceWindow(this);
});
}

RegexReplacerUtils.PopulateRegexReplacements();
}
Expand Down

0 comments on commit b58f86d

Please sign in to comment.