diff --git a/src/modules/awake/Awake/Core/Manager.cs b/src/modules/awake/Awake/Core/Manager.cs index 865457aae2..2136ca4b71 100644 --- a/src/modules/awake/Awake/Core/Manager.cs +++ b/src/modules/awake/Awake/Core/Manager.cs @@ -37,10 +37,15 @@ namespace Awake.Core private static CancellationTokenSource _tokenSource; + private static SettingsUtils? _moduleSettings; + + private static SettingsUtils? ModuleSettings { get => _moduleSettings; set => _moduleSettings = value; } + static Manager() { _tokenSource = new CancellationTokenSource(); _stateQueue = new BlockingCollection(); + ModuleSettings = new SettingsUtils(); } public static void StartMonitor() @@ -294,17 +299,28 @@ namespace Awake.Core public static void SetPassiveKeepAwakeMode(string moduleName) { + AwakeSettings currentSettings; + try { - SettingsUtils settingsUtils = new SettingsUtils(); - AwakeSettings settings = new AwakeSettings(); - - settings.Properties.Mode = AwakeMode.PASSIVE; - settingsUtils.SaveSettings(JsonSerializer.Serialize(settings), moduleName); + currentSettings = ModuleSettings!.GetSettings(moduleName); } catch (Exception ex) { - string? errorString = $"Failed to reset Awake mode: {ex.Message}"; + string? errorString = $"Failed to reset Awake mode GetSettings: {ex.Message}"; + Logger.LogError(errorString); + currentSettings = new AwakeSettings(); + } + + currentSettings.Properties.Mode = AwakeMode.PASSIVE; + + try + { + ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); + } + catch (Exception ex) + { + string? errorString = $"Failed to reset Awake mode SaveSettings: {ex.Message}"; Logger.LogError(errorString); } } diff --git a/src/modules/awake/Awake/Core/TrayMessageFilter.cs b/src/modules/awake/Awake/Core/TrayMessageFilter.cs index 617245705c..ecb3def337 100644 --- a/src/modules/awake/Awake/Core/TrayMessageFilter.cs +++ b/src/modules/awake/Awake/Core/TrayMessageFilter.cs @@ -9,6 +9,7 @@ using System.Text.Json; using System.Threading; using System.Windows.Forms; using Awake.Core.Models; +using ManagedCommon; using Microsoft.PowerToys.Settings.UI.Library; namespace Awake.Core @@ -83,14 +84,24 @@ namespace Awake.Core { currentSettings = ModuleSettings!.GetSettings(moduleName); } - catch (FileNotFoundException) + catch (Exception ex) { + string? errorString = $"Failed GetSettings: {ex.Message}"; + Logger.LogError(errorString); currentSettings = new AwakeSettings(); } currentSettings.Properties.KeepDisplayOn = !currentSettings.Properties.KeepDisplayOn; - ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); + try + { + ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); + } + catch (Exception ex) + { + string? errorString = $"Failed SaveSettings: {ex.Message}"; + Logger.LogError(errorString); + } } private static void TimedKeepAwakeCommandHandler(string moduleName, int seconds) @@ -103,8 +114,10 @@ namespace Awake.Core { currentSettings = ModuleSettings!.GetSettings(moduleName); } - catch (FileNotFoundException) + catch (Exception ex) { + string? errorString = $"Failed GetSettings: {ex.Message}"; + Logger.LogError(errorString); currentSettings = new AwakeSettings(); } @@ -112,7 +125,15 @@ namespace Awake.Core currentSettings.Properties.IntervalHours = (uint)timeSpan.Hours; currentSettings.Properties.IntervalMinutes = (uint)timeSpan.Minutes; - ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); + try + { + ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); + } + catch (Exception ex) + { + string? errorString = $"Failed SaveSettings: {ex.Message}"; + Logger.LogError(errorString); + } } private static void PassiveKeepAwakeCommandHandler(string moduleName) @@ -128,14 +149,24 @@ namespace Awake.Core { currentSettings = ModuleSettings!.GetSettings(moduleName); } - catch (FileNotFoundException) + catch (Exception ex) { + string? errorString = $"Failed GetSettings: {ex.Message}"; + Logger.LogError(errorString); currentSettings = new AwakeSettings(); } currentSettings.Properties.Mode = AwakeMode.INDEFINITE; - ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); + try + { + ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); + } + catch (Exception ex) + { + string? errorString = $"Failed SaveSettings: {ex.Message}"; + Logger.LogError(errorString); + } } } }