From 33f1c0f91ffa9adef7a21b5a0ee72bb65038963a Mon Sep 17 00:00:00 2001 From: Me on Windows Date: Fri, 18 Jun 2021 22:20:39 +0000 Subject: [PATCH] Implement App config saves and restores --- QuickPlay/Configuration.cs | 40 ++++++++++++++++++++++++++++++++------ QuickPlay/Interfaces.cs | 2 +- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/QuickPlay/Configuration.cs b/QuickPlay/Configuration.cs index 01198f8..bc81adf 100644 --- a/QuickPlay/Configuration.cs +++ b/QuickPlay/Configuration.cs @@ -8,12 +8,19 @@ namespace QuickPlay /// Application (global) configuration data class /// [Serializable] - sealed class AppConfiguration + public sealed class AppConfiguration { // XXX: All the fields need to be checked in overriden Equals method // Also: sensible defaults should be provided in defaultConfiguration field public string playerConfigUrl; + // XXX: Any other fields of this model need [NonSerialized] attribute + [NonSerialized] + public static readonly string configFilePath = + Path.Combine( + System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), + "appConfig.xml" + ); public static AppConfiguration loadConfiguration() { var cfg = loadSavedConfiguration(); @@ -25,20 +32,41 @@ namespace QuickPlay } public static AppConfiguration loadSavedConfiguration() { - throw new NotImplementedException(); + var serializer = new System.Xml.Serialization.XmlSerializer(typeof(AppConfiguration)); + AppConfiguration result = null; + try + { + using (var stream = new System.IO.FileStream(configFilePath, System.IO.FileMode.Open)) + { + result = (AppConfiguration)serializer.Deserialize(stream); + + } + } catch (FileNotFoundException) + { + // This is fine, we will return null anyway. + } catch (InvalidCastException) { + // This is not so much fine, TODO log it. + // Still, we cannot supply config, so treat it as if it does not exist. + } + return result; } public void saveConfiguration() { - - + // First we save the config + var serializer = new System.Xml.Serialization.XmlSerializer(typeof(AppConfiguration)); + using (var stream = new System.IO.FileStream(configFilePath, System.IO.FileMode.Create)) + { + serializer.Serialize(stream, this); + } // Make sure that the configuration is same var newConfig = AppConfiguration.loadSavedConfiguration(); if (this != newConfig) throw new InvalidDataException("Saved configuration is different from the supplied one."); } - public static AppConfiguration defaultConfiguration = new AppConfiguration + [NonSerialized] + public static readonly AppConfiguration defaultConfiguration = new AppConfiguration { playerConfigUrl = "file:///dev/null", }; @@ -67,7 +95,7 @@ namespace QuickPlay /// /// Contains details about connection, configured songs &c. /// - class PlayerConfiguration + public class PlayerConfiguration { public static PlayerConfiguration FromFile(StreamReader reader) { diff --git a/QuickPlay/Interfaces.cs b/QuickPlay/Interfaces.cs index 0f3e44d..aa5194a 100644 --- a/QuickPlay/Interfaces.cs +++ b/QuickPlay/Interfaces.cs @@ -9,7 +9,7 @@ namespace QuickPlay /// /// That means that the interface may be extended in the future, which is sad. /// - interface IPlayer + public interface IPlayer { void Play(string identifier); float CurrentProgress { get; }