From 79e5545fd3f4000bec39c31c4dbac9ae7963261f Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Thu, 22 Jul 2021 14:08:15 -0400 Subject: [PATCH] config bug fix --- FileManager/Configuration.cs | 18 ++++-- FileManager/PersistentDictionary.cs | 35 ++++++++---- LibationLauncher/LibationLauncher.csproj | 2 +- LibationLauncher/Program.cs | 7 ++- LibationWinForms/Form1.cs | 71 ++++++++++++++---------- 5 files changed, 86 insertions(+), 47 deletions(-) diff --git a/FileManager/Configuration.cs b/FileManager/Configuration.cs index 3cec142b..67080c76 100644 --- a/FileManager/Configuration.cs +++ b/FileManager/Configuration.cs @@ -48,7 +48,11 @@ namespace FileManager public object GetObject(string propertyName) => persistentDictionary.GetObject(propertyName); public void SetObject(string propertyName, object newValue) => persistentDictionary.SetNonString(propertyName, newValue); - public void SetWithJsonPath(string jsonPath, string propertyName, string newValue, bool suppressLogging = false) => persistentDictionary.SetWithJsonPath(jsonPath, propertyName, newValue, suppressLogging); + + /// WILL ONLY set if already present. WILL NOT create new + /// Value was changed + public bool SetWithJsonPath(string jsonPath, string propertyName, string newValue, bool suppressLogging = false) + => persistentDictionary.SetWithJsonPath(jsonPath, propertyName, newValue, suppressLogging); public string SettingsFilePath => Path.Combine(LibationFiles, "Settings.json"); @@ -219,7 +223,12 @@ namespace FileManager } set { - persistentDictionary.SetWithJsonPath("Serilog", "MinimumLevel", value.ToString()); + var valueWasChanged = persistentDictionary.SetWithJsonPath("Serilog", "MinimumLevel", value.ToString()); + if (!valueWasChanged) + { + Log.Logger.Information("LogLevel.set attempt. No change"); + return; + } configuration.Reload(); @@ -263,8 +272,9 @@ namespace FileManager // Config init in Program.ensureSerilogConfig() only happens when serilog setting is first created (prob on 1st run). // This Set() enforces current LibationFiles every time we restart Libation or redirect LibationFiles var logPath = Path.Combine(LibationFiles, "Log.log"); - SetWithJsonPath("Serilog.WriteTo[1].Args", "path", logPath, true); - configuration?.Reload(); + bool settingWasChanged = SetWithJsonPath("Serilog.WriteTo[1].Args", "path", logPath, true); + if (settingWasChanged) + configuration?.Reload(); return libationFilesPathCache; } diff --git a/FileManager/PersistentDictionary.cs b/FileManager/PersistentDictionary.cs index c57d645b..81ef971b 100644 --- a/FileManager/PersistentDictionary.cs +++ b/FileManager/PersistentDictionary.cs @@ -142,33 +142,44 @@ namespace FileManager catch { } } - public void SetWithJsonPath(string jsonPath, string propertyName, string newValue, bool suppressLogging = false) + /// WILL ONLY set if already present. WILL NOT create new + /// Value was changed + public bool SetWithJsonPath(string jsonPath, string propertyName, string newValue, bool suppressLogging = false) { if (IsReadOnly) - return; + return false; var path = $"{jsonPath}.{propertyName}"; { // only do this check in string cache, NOT object cache if (stringCache.ContainsKey(path) && stringCache[path] == newValue) - return; + return false; // set cache stringCache[path] = newValue; } - lock (locker) + try { - var jObject = readFile(); - var token = jObject.SelectToken(jsonPath); - var oldValue = token.Value(propertyName); + lock (locker) + { + var jObject = readFile(); + var token = jObject.SelectToken(jsonPath); + if (token is null || token[propertyName] is null) + return false; - if (oldValue == newValue) - return; + var oldValue = token.Value(propertyName); + if (oldValue == newValue) + return false; - token[propertyName] = newValue; - File.WriteAllText(Filepath, JsonConvert.SerializeObject(jObject, Formatting.Indented)); + token[propertyName] = newValue; + File.WriteAllText(Filepath, JsonConvert.SerializeObject(jObject, Formatting.Indented)); + } + } + catch (Exception exDebug) + { + return false; } if (!suppressLogging) @@ -180,6 +191,8 @@ namespace FileManager } catch { } } + + return true; } private static string formatValueForLog(string value) diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index 1f285814..cfaf96a6 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 5.3.4.1 + 5.3.5.1 diff --git a/LibationLauncher/Program.cs b/LibationLauncher/Program.cs index 5782cdf9..a1afc51a 100644 --- a/LibationLauncher/Program.cs +++ b/LibationLauncher/Program.cs @@ -351,7 +351,7 @@ Libation. try { // timed out - var latest = getLatestRelease(TimeSpan.FromSeconds(30)); + var latest = getLatestRelease(TimeSpan.FromSeconds(10)); if (latest is null) return; @@ -388,6 +388,11 @@ Libation. return; selectedPath = fileSelector.FileName; } + catch (AggregateException aggEx) + { + Log.Logger.Error(aggEx, "Checking for new version too often"); + return; + } catch (Exception ex) { MessageBoxAlertAdmin.Show("Error checking for update", "Error checking for update", ex); diff --git a/LibationWinForms/Form1.cs b/LibationWinForms/Form1.cs index a296b339..fe6d55a7 100644 --- a/LibationWinForms/Form1.cs +++ b/LibationWinForms/Form1.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using System.Windows.Forms; using ApplicationServices; using DataLayer; @@ -33,6 +34,31 @@ namespace LibationWinForms beginBookBackupsToolStripMenuItem_format = beginBookBackupsToolStripMenuItem.Text; beginPdfBackupsToolStripMenuItem_format = beginPdfBackupsToolStripMenuItem.Text; + + // after backing up formats: can set default/temp visible text + backupsCountsLbl.Text = "[Calculating backed up book quantities]"; + pdfsCountsLbl.Text = "[Calculating backed up PDFs]"; + setVisibleCount(null, 0); + + if (this.DesignMode) + return; + + // independent UI updates + this.Load += setBackupCountsAsync; + this.Load += (_, __) => RestoreSizeAndLocation(); + this.Load += (_, __) => RefreshImportMenu(); + + // start background service + this.Load += (_, __) => startBackgroundImageDownloader(); + } + + private static void startBackgroundImageDownloader() + { + // load default/missing cover images. this will also initiate the background image downloader + var format = System.Drawing.Imaging.ImageFormat.Jpeg; + PictureStorage.SetDefaultImage(PictureSize._80x80, Properties.Resources.default_cover_80x80.ToBytes(format)); + PictureStorage.SetDefaultImage(PictureSize._300x300, Properties.Resources.default_cover_300x300.ToBytes(format)); + PictureStorage.SetDefaultImage(PictureSize._500x500, Properties.Resources.default_cover_500x500.ToBytes(format)); } private void Form1_Load(object sender, EventArgs e) @@ -40,30 +66,13 @@ namespace LibationWinForms if (this.DesignMode) return; - RestoreSizeAndLocation(); - - // load default/missing cover images. this will also initiate the background image downloader - var format = System.Drawing.Imaging.ImageFormat.Jpeg; - PictureStorage.SetDefaultImage(PictureSize._80x80, Properties.Resources.default_cover_80x80.ToBytes(format)); - PictureStorage.SetDefaultImage(PictureSize._300x300, Properties.Resources.default_cover_300x300.ToBytes(format)); - PictureStorage.SetDefaultImage(PictureSize._500x500, Properties.Resources.default_cover_500x500.ToBytes(format)); - - RefreshImportMenu(); - - setVisibleCount(null, 0); - - reloadGrid(); - + reloadGrid(); + // also applies filter. ONLY call AFTER loading grid loadInitialQuickFilterState(); - - // init bottom counts - backupsCountsLbl.Text = "[Calculating backed up book quantities]"; - pdfsCountsLbl.Text = "[Calculating backed up PDFs]"; - setBackupCounts(null, null); } - private void Form1_FormClosing(object sender, FormClosingEventArgs e) + private void Form1_FormClosing(object sender, FormClosingEventArgs e) { SaveSizeAndLocation(); } @@ -161,14 +170,14 @@ namespace LibationWinForms { gridPanel.Controls.Remove(currProductsGrid); currProductsGrid.VisibleCountChanged -= setVisibleCount; - currProductsGrid.BackupCountsChanged -= setBackupCounts; + currProductsGrid.BackupCountsChanged -= setBackupCountsAsync; currProductsGrid.Dispose(); } currProductsGrid = new ProductsGrid { Dock = DockStyle.Fill }; currProductsGrid.VisibleCountChanged += setVisibleCount; - currProductsGrid.BackupCountsChanged += setBackupCounts; - gridPanel.Controls.Add(currProductsGrid); + currProductsGrid.BackupCountsChanged += setBackupCountsAsync; + gridPanel.UIThread(() => gridPanel.Controls.Add(currProductsGrid)); currProductsGrid.Display(); } ResumeLayout(); @@ -180,15 +189,17 @@ namespace LibationWinForms #endregion #region bottom: backup counts - private void setBackupCounts(object _, object __) + private async void setBackupCountsAsync(object _, object __) { - var books = DbContexts.GetContext() - .GetLibrary_Flat_NoTracking() - .Select(sp => sp.Book) - .ToList(); + await Task.Run(() => { + var books = DbContexts.GetContext() + .GetLibrary_Flat_NoTracking() + .Select(sp => sp.Book) + .ToList(); - setBookBackupCounts(books); - setPdfBackupCounts(books); + setBookBackupCounts(books); + setPdfBackupCounts(books); + }); } enum AudioFileState { full, aax, none } private void setBookBackupCounts(IEnumerable books)