From bd22fcb9efee18c3bd1ac1672ee3c0fa12a3e41f Mon Sep 17 00:00:00 2001 From: TTLC198 Date: Mon, 4 Dec 2023 19:49:33 +0300 Subject: [PATCH] Fixed loading of the configuration file on first launch --- HSMonitor/Services/SettingsService.cs | 46 +++++++++++++++------------ HSMonitor/Utils/Bootstrapper.cs | 2 +- HSMonitor/Utils/Logger/FileLogger.cs | 20 +++++++----- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/HSMonitor/Services/SettingsService.cs b/HSMonitor/Services/SettingsService.cs index 5578ccc..e1697b5 100644 --- a/HSMonitor/Services/SettingsService.cs +++ b/HSMonitor/Services/SettingsService.cs @@ -34,7 +34,26 @@ public class SettingsService public event EventHandler? SettingsSaved; - public ApplicationSettings Settings { get; set; } = null!; + public ApplicationSettings Settings { get; set; } = DefaultSettings; + + private static readonly ApplicationSettings DefaultSettings = new ApplicationSettings() + { + LastSelectedPort = null, + LastSelectedBaudRate = 115200, + SendInterval = 1000, + DeviceDisplayBrightness = 100, + CpuId = "", + GpuId = "", + CpuCustomName = "", + GpuCustomName = "", + CpuCustomType = "", + GpuCustomType = "", + IsAutoDetectHardwareEnabled = true, + IsHiddenAutoStartEnabled = true, + IsAutoStartEnabled = false, + IsDeviceBackwardCompatibilityEnabled = false, + ApplicationCultureInfo = CultureInfo.InstalledUICulture.Name + }; public readonly string ConfigurationPath = Path.Combine(App.SettingsDirPath, "appsettings.json"); private readonly ILogger _logger; @@ -44,29 +63,11 @@ public SettingsService(IViewModelFactory viewModelFactory, DialogManager dialogM _viewModelFactory = viewModelFactory; _dialogManager = dialogManager; _logger = logger; - Load().Wait(); } public async Task Reset() { - Settings = new ApplicationSettings() - { - LastSelectedPort = null, - LastSelectedBaudRate = 115200, - SendInterval = 1000, - DeviceDisplayBrightness = 100, - CpuId = "", - GpuId = "", - CpuCustomName = "", - GpuCustomName = "", - CpuCustomType = "", - GpuCustomType = "", - IsAutoDetectHardwareEnabled = true, - IsHiddenAutoStartEnabled = true, - IsAutoStartEnabled = false, - IsDeviceBackwardCompatibilityEnabled = false, - ApplicationCultureInfo = CultureInfo.InstalledUICulture.Name - }; + Settings = DefaultSettings; await Save(); SettingsReset?.Invoke(this, EventArgs.Empty); } @@ -75,7 +76,10 @@ public async Task Load() { try { - var json = File.ReadAllText(ConfigurationPath); + if (!File.Exists(ConfigurationPath)) + Settings.JsonToFile(ConfigurationPath); + + var json = await File.ReadAllTextAsync(ConfigurationPath); Settings = JsonSerializer.Deserialize(json) ?? throw new InvalidOperationException(); Settings.IsAutoStartEnabled = _autoStartSwitch.IsSet; Settings.LastSelectedPort ??= SerialPort.GetPortNames().FirstOrDefault() ?? "COM1"; diff --git a/HSMonitor/Utils/Bootstrapper.cs b/HSMonitor/Utils/Bootstrapper.cs index f87deb2..ab3b64b 100644 --- a/HSMonitor/Utils/Bootstrapper.cs +++ b/HSMonitor/Utils/Bootstrapper.cs @@ -37,7 +37,7 @@ protected override void Launch() { GetInstance().HardwareInformationUpdate(this, EventArgs.Empty); _ = GetInstance().GetViewForDialogScreen(GetInstance()); - + GetInstance().Load(); base.Launch(); } diff --git a/HSMonitor/Utils/Logger/FileLogger.cs b/HSMonitor/Utils/Logger/FileLogger.cs index 30e14ed..5e18a77 100644 --- a/HSMonitor/Utils/Logger/FileLogger.cs +++ b/HSMonitor/Utils/Logger/FileLogger.cs @@ -15,6 +15,8 @@ public class FileLogger : ILogger public FileLogger() { _fullFilePath = Path.Combine(App.LogsDirPath, DateTime.Now.ToString("yyyy-MM-dd") + "_log.txt"); + if (!Directory.Exists(App.LogsDirPath)) + Directory.CreateDirectory(App.LogsDirPath); DeleteOldLogFiles(); } @@ -40,18 +42,20 @@ private void Log(string logLevel, Exception? exception = null, string? message = var n = Environment.NewLine; if (exception != null) message += $"{n} {exception.GetType()} - {exception.Message} - {exception.StackTrace}"; - Directory.CreateDirectory(App.LogsDirPath); + if (!Directory.Exists(App.LogsDirPath)) + Directory.CreateDirectory(App.LogsDirPath); File.AppendAllText(_fullFilePath, $"{DateTime.Now} | {logLevel} | {typeof(T)} - {message} {n}"); } } - private void DeleteOldLogFiles() + private static void DeleteOldLogFiles() { - Directory - .GetFiles(App.LogsDirPath) - .Select(f => new FileInfo(f)) - .Where(f => f.CreationTime < DateTime.Now.AddMonths(-3)) - .ToList() - .ForEach(f => f.Delete()); + if (Directory.Exists(App.LogsDirPath)) + Directory + .GetFiles(App.LogsDirPath) + .Select(f => new FileInfo(f)) + .Where(f => f.CreationTime < DateTime.Now.AddMonths(-3)) + .ToList() + .ForEach(f => f.Delete()); } } \ No newline at end of file