Skip to content

Commit

Permalink
Fixed loading of the configuration file on first launch
Browse files Browse the repository at this point in the history
  • Loading branch information
TTLC198 committed Dec 4, 2023
1 parent 1ddfc50 commit bd22fcb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
46 changes: 25 additions & 21 deletions HSMonitor/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SettingsService> _logger;
Expand All @@ -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);
}
Expand All @@ -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<ApplicationSettings>(json) ?? throw new InvalidOperationException();
Settings.IsAutoStartEnabled = _autoStartSwitch.IsSet;
Settings.LastSelectedPort ??= SerialPort.GetPortNames().FirstOrDefault() ?? "COM1";
Expand Down
2 changes: 1 addition & 1 deletion HSMonitor/Utils/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override void Launch()
{
GetInstance<HardwareMonitorService>().HardwareInformationUpdate(this, EventArgs.Empty);
_ = GetInstance<DialogManager>().GetViewForDialogScreen(GetInstance<SettingsViewModel>());

GetInstance<SettingsService>().Load();
base.Launch();
}

Expand Down
20 changes: 12 additions & 8 deletions HSMonitor/Utils/Logger/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class FileLogger<T> : ILogger<T>
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();
}

Expand All @@ -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());
}
}

0 comments on commit bd22fcb

Please sign in to comment.