diff --git a/SpotifySleepModeStopper/Contracts/ISettingsManager.cs b/SpotifySleepModeStopper/Contracts/ISettingsManager.cs index e25b967..6a44d75 100644 --- a/SpotifySleepModeStopper/Contracts/ISettingsManager.cs +++ b/SpotifySleepModeStopper/Contracts/ISettingsManager.cs @@ -2,9 +2,9 @@ namespace SpotifyTools.Contracts { - public interface ISettingsManager + public interface ISettingsManager where T : class { - AppSettings GetConfig(); - void SaveConfig(AppSettings settings); + T GetConfig(); + void SaveConfig(T settings); } } \ No newline at end of file diff --git a/SpotifySleepModeStopper/SpotifySaveModeStopperFacade.cs b/SpotifySleepModeStopper/SpotifySaveModeStopperFacade.cs index bed5fbb..866a16d 100644 --- a/SpotifySleepModeStopper/SpotifySaveModeStopperFacade.cs +++ b/SpotifySleepModeStopper/SpotifySaveModeStopperFacade.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using SpotifyTools.Contracts; using SpotifyTools.Tools; +using SpotifyTools.Tools.Model; namespace SpotifyTools { @@ -17,7 +18,7 @@ public class SpotifySaveModeStopperFacade : ISpotifySaveModeStopperFacade private readonly ISoundAnalyser _soundAnalyser; private readonly IAppStatusReporting _appState; private readonly IAutoStartManager _autoStartManager; - private readonly ISettingsManager _settingsManager; + private readonly ISettingsManager _settingsManager; private readonly IProcessAnalyser _processAnalyser; private bool _spotifyRunning; @@ -37,7 +38,7 @@ public class SpotifySaveModeStopperFacade : ISpotifySaveModeStopperFacade #region Ctor public SpotifySaveModeStopperFacade(IMessageDisplayer messageDisplayer, IPreventSleepScreen preventSleepScreen, - ISoundAnalyser soundAnalyser, IProcessAnalyser processAnalyser, IAppStatusReporting appState, IAutoStartManager autoStartManager, ISettingsManager settingsManager, int refreshRate = -1) + ISoundAnalyser soundAnalyser, IProcessAnalyser processAnalyser, IAppStatusReporting appState, IAutoStartManager autoStartManager, ISettingsManager settingsManager, int refreshRate = -1) { _messageDisplayer = messageDisplayer; _preventSleepScreen = preventSleepScreen; diff --git a/SpotifySleepModeStopper/Tools/SettingsManager.cs b/SpotifySleepModeStopper/Tools/SettingsManager.cs index 4815528..4dea054 100644 --- a/SpotifySleepModeStopper/Tools/SettingsManager.cs +++ b/SpotifySleepModeStopper/Tools/SettingsManager.cs @@ -5,22 +5,24 @@ namespace SpotifyTools.Tools { - public class SettingsManager : ISettingsManager + public class SettingsManager : ISettingsManager where T : class { private readonly string _fullPathSettingsFile; + private readonly T _defaultValue; #region Ctor - public SettingsManager(string appName, string fileName = "settings.config") + public SettingsManager(string appName, T defaultValue, string fileName = "settings.config") { var userAppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\" + appName; if (!Directory.Exists(userAppDataFolder)) Directory.CreateDirectory(userAppDataFolder); _fullPathSettingsFile = userAppDataFolder + @"\" + fileName; + _defaultValue = defaultValue; } #endregion - public AppSettings GetConfig() + public T GetConfig() { string configFile; @@ -30,17 +32,17 @@ public AppSettings GetConfig() try { configFile = File.ReadAllText(_fullPathSettingsFile); - return JsonSerializerHelper.Deserialize(configFile); + return JsonSerializerHelper.Deserialize(configFile); } catch (Exception) { configFile = ResetConfigFile(); } - return JsonSerializerHelper.Deserialize(configFile); + return JsonSerializerHelper.Deserialize(configFile); } - public void SaveConfig(AppSettings settings) + public void SaveConfig(T settings) { var serializedConfig = JsonSerializerHelper.Serialize(settings); @@ -49,10 +51,8 @@ public void SaveConfig(AppSettings settings) private string ResetConfigFile() { - var newSettings = new AppSettings() { IsScreenSleepEnabled = false }; - - SaveConfig(newSettings); - return JsonSerializerHelper.Serialize(newSettings); + SaveConfig(_defaultValue); + return JsonSerializerHelper.Serialize(_defaultValue); } } } diff --git a/SpotifySleepModeStopperGui/MainWindow.xaml.cs b/SpotifySleepModeStopperGui/MainWindow.xaml.cs index c48257d..d68e31f 100644 --- a/SpotifySleepModeStopperGui/MainWindow.xaml.cs +++ b/SpotifySleepModeStopperGui/MainWindow.xaml.cs @@ -13,6 +13,7 @@ using SpotifyTools.DomainLayer.MessageManagement; using SpotifyTools.DomainLayer.PowerManagement; using SpotifyTools.Tools; +using SpotifyTools.Tools.Model; namespace SpotifySleepModeStopperGui { @@ -30,11 +31,12 @@ public partial class MainWindow : Window private const string AppNotStartingOnStartupMess = "Auto-Start: Off"; private const string ScreenSleepEnabledMess = "Screen Sleep: On"; private const string ScreenSleepDisabledMess = "Screen Sleep: Off"; + private const string ExitMess = "Exit"; private readonly MenuItem _autoStartMenuItem; private readonly MenuItem _screenBehaviorMenuItem; - //private string _mess; + private const string AppName = "SpotifySleepModeStopper"; public MainWindow() { @@ -48,9 +50,9 @@ public MainWindow() var processAnalyser = new ProcessAnalyser(); var fullPath = Assembly.GetExecutingAssembly().Location; - var autoStartManager = new AutoStartManager("SpotifySleepModeStopper", fullPath); + var autoStartManager = new AutoStartManager(AppName, fullPath); - var settingsManager = new SettingsManager("SpotifySleepModeStopper"); + var settingsManager = new SettingsManager(AppName, new AppSettings {IsScreenSleepEnabled = false}); #endregion _facade = new SpotifySaveModeStopperFacade(messageDisplayer, powerHandler, soundAnalyser, processAnalyser, iconChanger, autoStartManager, settingsManager); @@ -79,7 +81,7 @@ public MainWindow() var contextMenu = new ContextMenu(); var exitMenuItem = new MenuItem(); exitMenuItem.Index = 2; - exitMenuItem.Text = "Exit"; + exitMenuItem.Text = ExitMess; exitMenuItem.Click += exit_Click; _screenBehaviorMenuItem = new MenuItem(); diff --git a/UnitTestProject/FacadeUnitTest.cs b/UnitTestProject/FacadeUnitTest.cs index 256b8b8..8e0ef89 100644 --- a/UnitTestProject/FacadeUnitTest.cs +++ b/UnitTestProject/FacadeUnitTest.cs @@ -25,7 +25,7 @@ public class FacadeUnitTest [TestMethod] public void CheckIfScreenSleepEnabledTestMethod() { - var settingsManagerStub = MockRepository.GenerateMock(); + var settingsManagerStub = MockRepository.GenerateMock>(); settingsManagerStub.Expect(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true }); var facade = new SpotifySaveModeStopperFacade(null, null, null, null, null, null, settingsManagerStub); @@ -37,7 +37,7 @@ public void CheckIfScreenSleepEnabledTestMethod() [TestMethod] public void CheckIfAutoStartEnabledTestMethod() { - var settingsManagerStub = MockRepository.GenerateStub(); + var settingsManagerStub = MockRepository.GenerateStub>(); settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true }); var autoStartManagerStub = MockRepository.GenerateMock(); @@ -54,7 +54,7 @@ public void ChangeScreenSleepSettingsTestMethod() { var saveConfigFired = new ManualResetEvent(false); - var settingsManagerStub = MockRepository.GenerateMock(); + var settingsManagerStub = MockRepository.GenerateMock>(); settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true }); settingsManagerStub.Expect(x => x.SaveConfig(new AppSettings())) .Constraints(Property.Value("IsScreenSleepEnabled", true)) @@ -72,7 +72,7 @@ public void ChangeAutoStartSettingsTestMethod() { var autoStartChangeFired = new ManualResetEvent(false); - var settingsManagerStub = MockRepository.GenerateStub(); + var settingsManagerStub = MockRepository.GenerateStub>(); settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true }); var autoStartManagerStub = MockRepository.GenerateMock(); @@ -94,7 +94,7 @@ public void ListeningTestMethod() var messageDisplayerStub = MockRepository.GenerateStub(); messageDisplayerStub.Stub(x => x.OutputMessage(Arg.Is.Anything)).Repeat.Any(); - var settingsManagerStub = MockRepository.GenerateStub(); + var settingsManagerStub = MockRepository.GenerateStub>(); settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true }); var processAnalyserStub = MockRepository.GenerateStub(); @@ -159,7 +159,7 @@ public void StopListeningTestMethod() var messageDisplayerStub = MockRepository.GenerateStub(); messageDisplayerStub.Stub(x => x.OutputMessage(Arg.Is.Anything)).Repeat.Any(); - var settingsManagerStub = MockRepository.GenerateStub(); + var settingsManagerStub = MockRepository.GenerateStub>(); settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true }); var processAnalyserStub = MockRepository.GenerateStub(); @@ -226,7 +226,7 @@ public void ChangeListeningTestMethod() var messageDisplayerStub = MockRepository.GenerateStub(); messageDisplayerStub.Stub(x => x.OutputMessage(Arg.Is.Anything)).Repeat.Any(); - var settingsManagerStub = MockRepository.GenerateStub(); + var settingsManagerStub = MockRepository.GenerateStub>(); settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true }); var processAnalyserStub = MockRepository.GenerateStub(); diff --git a/UnitTestProject/SettingsUnitTest.cs b/UnitTestProject/SettingsUnitTest.cs index 525f3c5..4fb4db8 100644 --- a/UnitTestProject/SettingsUnitTest.cs +++ b/UnitTestProject/SettingsUnitTest.cs @@ -22,7 +22,7 @@ public void FirstSettingsDeployementTestMethod() Assert.IsFalse(File.Exists(fullFileName)); - var settingsManager = new SettingsManager(AppName); + var settingsManager = new SettingsManager(AppName, new AppSettings {IsScreenSleepEnabled = false}); settingsManager.GetConfig(); //First get => Reset config Assert.IsTrue(File.Exists(fullFileName)); @@ -40,7 +40,7 @@ public void SaveSettingsTestMethod() var appSettings = new AppSettings() {IsScreenSleepEnabled = true}; - var settingsManager = new SettingsManager(AppName, configName); + var settingsManager = new SettingsManager(AppName, new AppSettings {IsScreenSleepEnabled = false}, configName); settingsManager.SaveConfig(appSettings); Assert.IsTrue(File.Exists(fullFileName)); @@ -70,7 +70,7 @@ public void LoadSettingsTestMethod() File.WriteAllText(fullFileName, configContentFalse); - var settingsManager = new SettingsManager(AppName, configName); + var settingsManager = new SettingsManager(AppName, new AppSettings { IsScreenSleepEnabled = false }, configName); var config = settingsManager.GetConfig(); Assert.IsFalse(config.IsScreenSleepEnabled); @@ -94,7 +94,7 @@ public void LoadCorruptSettingsTestMethod() File.WriteAllText(fullFileName, corruptConfigContent); - var settingsManager = new SettingsManager(AppName, configName); + var settingsManager = new SettingsManager(AppName, new AppSettings { IsScreenSleepEnabled = false }, configName); var config = settingsManager.GetConfig(); Assert.IsFalse(config.IsScreenSleepEnabled);