diff --git a/JL.Windows/ConfigManager.cs b/JL.Windows/ConfigManager.cs index 288ee6b8..c0b5fa06 100644 --- a/JL.Windows/ConfigManager.cs +++ b/JL.Windows/ConfigManager.cs @@ -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); @@ -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(); @@ -1410,9 +1405,9 @@ public async Task SavePreferences(PreferencesWindow preferenceWindow) ConfigDBManager.UpdateSetting(connection, "MainWindowLeftPosition", (mainWindow.Left * dpi.DpiScaleX).ToString(CultureInfo.InvariantCulture)); - } - ApplyPreferences(); + ApplyPreferences(connection); + } if (preferenceWindow.SetAnkiConfig) { @@ -1420,11 +1415,8 @@ public async Task SavePreferences(PreferencesWindow preferenceWindow) } } - 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)); diff --git a/JL.Windows/GUI/MainWindow.xaml.cs b/JL.Windows/GUI/MainWindow.xaml.cs index f687e010..5a9dd075 100644 --- a/JL.Windows/GUI/MainWindow.xaml.cs +++ b/JL.Windows/GUI/MainWindow.xaml.cs @@ -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(); @@ -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(); @@ -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); } diff --git a/JL.Windows/GUI/PreferencesWindow.xaml.cs b/JL.Windows/GUI/PreferencesWindow.xaml.cs index 094aa7c9..b3c02fba 100644 --- a/JL.Windows/GUI/PreferencesWindow.xaml.cs +++ b/JL.Windows/GUI/PreferencesWindow.xaml.cs @@ -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(); }