From b02839198d0d912fbe05a3a8efd4c729619c4bf6 Mon Sep 17 00:00:00 2001 From: Yanick Castonguay Date: Mon, 14 Oct 2013 23:35:34 -0400 Subject: [PATCH] WindowsStore: App configuration provider now working. Related to issue #424. --- .../{AppConfig.cs => AppConfigManager.cs} | 12 ++-- MPfm/MPfm.MVP/Config/AudioAppConfig.cs | 4 +- MPfm/MPfm.MVP/Config/ControlsAppConfig.cs | 2 +- MPfm/MPfm.MVP/Config/IAppConfig.cs | 28 ++++++++ .../Config/Providers/XmlAppConfigProvider.cs | 8 +-- MPfm/MPfm.MVP/Config/RootAppConfig.cs | 2 +- MPfm/MPfm.MVP/Config/TableViewAppConfig.cs | 2 +- .../Config/TableViewColumnAppConfig.cs | 2 +- MPfm/MPfm.MVP/Config/WindowAppConfig.cs | 2 +- MPfm/MPfm.MVP/Config/WindowsAppConfig.cs | 2 +- MPfm/MPfm.MVP/Helpers/ConfigurationHelper.cs | 36 +--------- MPfm/MPfm.MVP/MPfm.MVP.Android.csproj | 5 +- MPfm/MPfm.MVP/MPfm.MVP.WindowsPhone.csproj | 3 +- MPfm/MPfm.MVP/MPfm.MVP.WindowsStore.csproj | 3 +- MPfm/MPfm.MVP/MPfm.MVP.csproj | 7 +- MPfm/MPfm.MVP/MPfm.MVP.iOS.csproj | 3 +- .../Presenters/LibraryBrowserPresenter.cs | 2 +- .../Services/InitializationService.cs | 8 +-- .../WindowsStoreAppConfigProvider.cs | 69 ++++++++++++++++++- 19 files changed, 133 insertions(+), 67 deletions(-) rename MPfm/MPfm.MVP/Config/{AppConfig.cs => AppConfigManager.cs} (86%) create mode 100644 MPfm/MPfm.MVP/Config/IAppConfig.cs diff --git a/MPfm/MPfm.MVP/Config/AppConfig.cs b/MPfm/MPfm.MVP/Config/AppConfigManager.cs similarity index 86% rename from MPfm/MPfm.MVP/Config/AppConfig.cs rename to MPfm/MPfm.MVP/Config/AppConfigManager.cs index 7df76de2..c02fd549 100644 --- a/MPfm/MPfm.MVP/Config/AppConfig.cs +++ b/MPfm/MPfm.MVP/Config/AppConfigManager.cs @@ -25,30 +25,30 @@ namespace MPfm.MVP.Config /// /// Singleton containing all application settings. /// - public class AppConfig + public class AppConfigManager { private readonly IAppConfigProvider _provider; public RootAppConfig Root { get; private set; } #region Singleton - private static AppConfig instance; + private static AppConfigManager instance; /// - /// AppConfig instance. + /// AppConfigManager instance. /// - public static AppConfig Instance + public static AppConfigManager Instance { get { if(instance == null) - instance = new AppConfig(); + instance = new AppConfigManager(); return instance; } } #endregion - public AppConfig() + public AppConfigManager() { _provider = Bootstrapper.GetContainer().Resolve(); Root = new RootAppConfig(); diff --git a/MPfm/MPfm.MVP/Config/AudioAppConfig.cs b/MPfm/MPfm.MVP/Config/AudioAppConfig.cs index 67df2037..1a931c71 100644 --- a/MPfm/MPfm.MVP/Config/AudioAppConfig.cs +++ b/MPfm/MPfm.MVP/Config/AudioAppConfig.cs @@ -22,7 +22,7 @@ namespace MPfm.MVP.Config /// /// Class containing all audio settings for MPfm. /// - public class AudioAppConfig + public class AudioAppConfig : IAppConfig { public Device AudioDevice { get; set; } public int SampleRate { get; set; } @@ -38,6 +38,6 @@ public AudioAppConfig() SampleRate = 44100; Volume = 1; BufferSize = 100; - } + } } } diff --git a/MPfm/MPfm.MVP/Config/ControlsAppConfig.cs b/MPfm/MPfm.MVP/Config/ControlsAppConfig.cs index 6c91492e..313cef5d 100644 --- a/MPfm/MPfm.MVP/Config/ControlsAppConfig.cs +++ b/MPfm/MPfm.MVP/Config/ControlsAppConfig.cs @@ -20,7 +20,7 @@ namespace MPfm.MVP.Config /// /// Class containing all control settings for MPfm. /// - public class ControlsAppConfig + public class ControlsAppConfig : IAppConfig { public TableViewAppConfig TableViewAppSongBrowser { get; set; } public TableViewAppConfig TableViewAppPlaylistBrowser { get; set; } diff --git a/MPfm/MPfm.MVP/Config/IAppConfig.cs b/MPfm/MPfm.MVP/Config/IAppConfig.cs new file mode 100644 index 00000000..5a00eb51 --- /dev/null +++ b/MPfm/MPfm.MVP/Config/IAppConfig.cs @@ -0,0 +1,28 @@ +// Copyright © 2011-2013 Yanick Castonguay +// +// This file is part of MPfm. +// +// MPfm is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// MPfm is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with MPfm. If not, see . + +using MPfm.MVP.Bootstrap; +using MPfm.MVP.Config.Providers; +using MPfm.MVP.Helpers; +using MPfm.Sound.AudioFiles; + +namespace MPfm.MVP.Config +{ + public interface IAppConfig + { + } +} diff --git a/MPfm/MPfm.MVP/Config/Providers/XmlAppConfigProvider.cs b/MPfm/MPfm.MVP/Config/Providers/XmlAppConfigProvider.cs index c3c96444..321c8b5f 100644 --- a/MPfm/MPfm.MVP/Config/Providers/XmlAppConfigProvider.cs +++ b/MPfm/MPfm.MVP/Config/Providers/XmlAppConfigProvider.cs @@ -1,4 +1,4 @@ -// Copyright © 2011-2013 Yanick Castonguay +// Copyright © 2011-2013 Yanick Castonguay // // This file is part of MPfm. // @@ -28,7 +28,7 @@ public class XmlAppConfigProvider : IAppConfigProvider /// Loads application settings from file. /// /// Configuration file path - /// AppConfig object + /// AppConfigManager object public RootAppConfig Load(string filePath) { if (!File.Exists(filePath)) @@ -42,10 +42,10 @@ public RootAppConfig Load(string filePath) } /// - /// Saves AppConfig to file. + /// Saves AppConfigManager to file. /// /// Configuration file path - /// AppConfig object + /// AppConfigManager object public void Save(string filePath, RootAppConfig config) { XmlSerializer serializer = new XmlSerializer(typeof(RootAppConfig)); diff --git a/MPfm/MPfm.MVP/Config/RootAppConfig.cs b/MPfm/MPfm.MVP/Config/RootAppConfig.cs index 4dcf9c68..5645c672 100644 --- a/MPfm/MPfm.MVP/Config/RootAppConfig.cs +++ b/MPfm/MPfm.MVP/Config/RootAppConfig.cs @@ -22,7 +22,7 @@ namespace MPfm.MVP.Config { - public class RootAppConfig + public class RootAppConfig : IAppConfig { public bool IsFirstRun { get; set; } public bool ShowTooltips { get; set; } diff --git a/MPfm/MPfm.MVP/Config/TableViewAppConfig.cs b/MPfm/MPfm.MVP/Config/TableViewAppConfig.cs index 2a2057e1..a3e2e9f5 100644 --- a/MPfm/MPfm.MVP/Config/TableViewAppConfig.cs +++ b/MPfm/MPfm.MVP/Config/TableViewAppConfig.cs @@ -22,7 +22,7 @@ namespace MPfm.MVP.Config /// /// Class containing all table view settings for MPfm. /// - public class TableViewAppConfig + public class TableViewAppConfig : IAppConfig { public List Columns { get; private set; } diff --git a/MPfm/MPfm.MVP/Config/TableViewColumnAppConfig.cs b/MPfm/MPfm.MVP/Config/TableViewColumnAppConfig.cs index b302cb27..51c7f80e 100644 --- a/MPfm/MPfm.MVP/Config/TableViewColumnAppConfig.cs +++ b/MPfm/MPfm.MVP/Config/TableViewColumnAppConfig.cs @@ -20,7 +20,7 @@ namespace MPfm.MVP.Config /// /// Class containing settings for a single table view column for MPfm. /// - public class TableViewColumnAppConfig + public class TableViewColumnAppConfig : IAppConfig { public string FieldName { get; set; } public string Title { get; set; } diff --git a/MPfm/MPfm.MVP/Config/WindowAppConfig.cs b/MPfm/MPfm.MVP/Config/WindowAppConfig.cs index 5a9ae15b..57daa665 100644 --- a/MPfm/MPfm.MVP/Config/WindowAppConfig.cs +++ b/MPfm/MPfm.MVP/Config/WindowAppConfig.cs @@ -20,7 +20,7 @@ namespace MPfm.MVP.Config /// /// Class containing all settings for a single Window for MPfm. /// - public class WindowAppConfig + public class WindowAppConfig : IAppConfig { public string Title { get; set; } public float X { get; set; } diff --git a/MPfm/MPfm.MVP/Config/WindowsAppConfig.cs b/MPfm/MPfm.MVP/Config/WindowsAppConfig.cs index 799b8980..17e7905c 100644 --- a/MPfm/MPfm.MVP/Config/WindowsAppConfig.cs +++ b/MPfm/MPfm.MVP/Config/WindowsAppConfig.cs @@ -20,7 +20,7 @@ namespace MPfm.MVP.Config /// /// Class containing settings for all windows for MPfm. /// - public class WindowsAppConfig + public class WindowsAppConfig : IAppConfig { public WindowAppConfig MainWindowApp { get; set; } public WindowAppConfig PlaylistWindowApp { get; set; } diff --git a/MPfm/MPfm.MVP/Helpers/ConfigurationHelper.cs b/MPfm/MPfm.MVP/Helpers/ConfigurationHelper.cs index 1a4c04af..1bd66236 100644 --- a/MPfm/MPfm.MVP/Helpers/ConfigurationHelper.cs +++ b/MPfm/MPfm.MVP/Helpers/ConfigurationHelper.cs @@ -1,4 +1,4 @@ -// Copyright © 2011-2013 Yanick Castonguay +// Copyright © 2011-2013 Yanick Castonguay // // This file is part of MPfm. // @@ -64,39 +64,5 @@ static ConfigurationHelper() DatabaseFilePath = Path.Combine(HomeDirectory, "MPfm.Database.db"); LogFilePath = Path.Combine(HomeDirectory, "MPfm.Log.txt"); } - - /// - /// Loads AppConfig from file. - /// - /// Configuration file path - /// AppConfig object - public static AppConfig Load(string filePath) - { -#if WINDOWSSTORE - return new AppConfig(); -#else - XmlSerializer deserializer = new XmlSerializer(typeof(AppConfig)); - TextReader textReader = new StreamReader(filePath); - Object obj = deserializer.Deserialize(textReader); - AppConfig theme = (AppConfig)obj; - return theme; -#endif - } - - /// - /// Saves AppConfig to file. - /// - /// Configuration file path - /// AppConfig object - public static void Save(string filePath, AppConfig config) - { -#if !WINDOWSSTORE - XmlSerializer serializer = new XmlSerializer(typeof(AppConfig)); - TextWriter textWriter = new StreamWriter(filePath); - serializer.Serialize(textWriter, config); - textWriter.Dispose(); -#endif - } } } - diff --git a/MPfm/MPfm.MVP/MPfm.MVP.Android.csproj b/MPfm/MPfm.MVP/MPfm.MVP.Android.csproj index ab99920a..895a253c 100644 --- a/MPfm/MPfm.MVP/MPfm.MVP.Android.csproj +++ b/MPfm/MPfm.MVP/MPfm.MVP.Android.csproj @@ -127,6 +127,8 @@ + + @@ -194,10 +196,9 @@ - + - diff --git a/MPfm/MPfm.MVP/MPfm.MVP.WindowsPhone.csproj b/MPfm/MPfm.MVP/MPfm.MVP.WindowsPhone.csproj index 0a8fa6c5..d2d91488 100644 --- a/MPfm/MPfm.MVP/MPfm.MVP.WindowsPhone.csproj +++ b/MPfm/MPfm.MVP/MPfm.MVP.WindowsPhone.csproj @@ -199,9 +199,10 @@ + - + diff --git a/MPfm/MPfm.MVP/MPfm.MVP.WindowsStore.csproj b/MPfm/MPfm.MVP/MPfm.MVP.WindowsStore.csproj index 74fd3286..33e6d2e3 100644 --- a/MPfm/MPfm.MVP/MPfm.MVP.WindowsStore.csproj +++ b/MPfm/MPfm.MVP/MPfm.MVP.WindowsStore.csproj @@ -100,6 +100,7 @@ + @@ -168,7 +169,7 @@ - + diff --git a/MPfm/MPfm.MVP/MPfm.MVP.csproj b/MPfm/MPfm.MVP/MPfm.MVP.csproj index ad9d5f0c..4d429714 100644 --- a/MPfm/MPfm.MVP/MPfm.MVP.csproj +++ b/MPfm/MPfm.MVP/MPfm.MVP.csproj @@ -111,6 +111,8 @@ + + @@ -178,16 +180,15 @@ - + - - + diff --git a/MPfm/MPfm.MVP/MPfm.MVP.iOS.csproj b/MPfm/MPfm.MVP/MPfm.MVP.iOS.csproj index 538d3cd3..428730cc 100644 --- a/MPfm/MPfm.MVP/MPfm.MVP.iOS.csproj +++ b/MPfm/MPfm.MVP/MPfm.MVP.iOS.csproj @@ -238,7 +238,8 @@ - + + diff --git a/MPfm/MPfm.MVP/Presenters/LibraryBrowserPresenter.cs b/MPfm/MPfm.MVP/Presenters/LibraryBrowserPresenter.cs index bfaeeb78..830e3c6a 100644 --- a/MPfm/MPfm.MVP/Presenters/LibraryBrowserPresenter.cs +++ b/MPfm/MPfm.MVP/Presenters/LibraryBrowserPresenter.cs @@ -75,7 +75,7 @@ public void BindView(ILibraryBrowserView view) view.OnTreeNodeDoubleClicked = (entity) => { TreeNodeDoubleClicked(entity); }; // // Load configuration -// if (AppConfig.Instance.ShowTooltips) +// if (AppConfigManager.Instance.ShowTooltips) // Refresh view (first level nodes) view.RefreshLibraryBrowser(GetFirstLevelNodes()); diff --git a/MPfm/MPfm.MVP/Services/InitializationService.cs b/MPfm/MPfm.MVP/Services/InitializationService.cs index c38582da..047c8bf6 100644 --- a/MPfm/MPfm.MVP/Services/InitializationService.cs +++ b/MPfm/MPfm.MVP/Services/InitializationService.cs @@ -99,8 +99,8 @@ private void CreateTraceListener() private void CreateDirectories() { #if WINDOWSSTORE - var task = ApplicationData.Current.LocalFolder.CreateFolderAsync("PeakFiles", CreationCollisionOption.OpenIfExists); - var storageFolder = task.GetResults(); + //var task = ApplicationData.Current.LocalFolder.CreateFolderAsync("PeakFiles", CreationCollisionOption.OpenIfExists); + //var storageFolder = task.GetResults(); #elif WINDOWS_PHONE // #else @@ -115,9 +115,9 @@ private void CreateDirectories() private void LoadConfiguration() { Tracing.Log("InitializationService.CreateConfiguration -- Checking for configuration file..."); - AppConfig.Instance.Load(); + AppConfigManager.Instance.Load(); - //ConfigurationHelper.Save(ConfigurationHelper.ConfigurationFilePath, AppConfig.Instance); + //ConfigurationHelper.Save(ConfigurationHelper.ConfigurationFilePath, AppConfigManager.Instance); //EQPreset preset = EQPresetHelper.Load("/Users/animal/Documents/test.txt"); //EQPresetHelper.Save("/Users/animal/Documents/test.txt", new EQPreset()); } diff --git a/MPfm/MPfm.WindowsStore/Classes/Providers/WindowsStoreAppConfigProvider.cs b/MPfm/MPfm.WindowsStore/Classes/Providers/WindowsStoreAppConfigProvider.cs index c46b9c6b..f197860d 100644 --- a/MPfm/MPfm.WindowsStore/Classes/Providers/WindowsStoreAppConfigProvider.cs +++ b/MPfm/MPfm.WindowsStore/Classes/Providers/WindowsStoreAppConfigProvider.cs @@ -15,6 +15,9 @@ // You should have received a copy of the GNU General Public License // along with MPfm. If not, see . +using System.Diagnostics; +using System.Reflection; +using Windows.Storage; using MPfm.MVP.Config; using MPfm.MVP.Config.Providers; @@ -24,11 +27,75 @@ public class WindowsStoreAppConfigProvider : IAppConfigProvider { public RootAppConfig Load(string filePath) { - return new RootAppConfig(); + var config = new RootAppConfig(); + LoadRecursive(config, "Root."); + return config; } public void Save(string filePath, RootAppConfig config) { + SaveRecursive(config, "Root."); + } + + private void LoadRecursive(IAppConfig config, string keyPreset) + { + // Create map by scanning properties + var roamingSettings = ApplicationData.Current.RoamingSettings; + var propertyInfos = config.GetType().GetTypeInfo().DeclaredProperties; + foreach (PropertyInfo propertyInfo in propertyInfos) + { + var propertyType = propertyInfo.PropertyType; + string fullName = keyPreset + propertyInfo.Name; + //object value = propertyInfo.GetValue(config); + bool isAssignable = typeof(IAppConfig).GetTypeInfo().IsAssignableFrom(propertyType.GetTypeInfo()); + Debug.WriteLine("{0} - {1} - isAssignable: {2}", fullName, propertyInfo.PropertyType.Name, isAssignable); + + if (propertyType == typeof(int) || + propertyType == typeof(bool) || + propertyType == typeof(double) || + propertyType == typeof(float) || + propertyType == typeof(string)) + { + object value = roamingSettings.Values[fullName]; + propertyInfo.SetValue(config, value); + Debug.WriteLine("Load setting: {0} - {1} - {2}", fullName, propertyInfo.PropertyType.Name, value); + } + else if (typeof(IAppConfig).GetTypeInfo().IsAssignableFrom(propertyType.GetTypeInfo())) + { + var subConfig = (IAppConfig)propertyInfo.GetValue(config); + LoadRecursive(subConfig, keyPreset + propertyType.Name + "."); + } + } + } + + private void SaveRecursive(IAppConfig config, string keyPreset) + { + // Create map by scanning properties + var roamingSettings = ApplicationData.Current.RoamingSettings; + var propertyInfos = config.GetType().GetTypeInfo().DeclaredProperties; + foreach (PropertyInfo propertyInfo in propertyInfos) + { + var propertyType = propertyInfo.PropertyType; + string fullName = keyPreset + propertyInfo.Name; + object value = propertyInfo.GetValue(config); + bool isAssignable = typeof (IAppConfig).GetTypeInfo().IsAssignableFrom(propertyType.GetTypeInfo()); + Debug.WriteLine("{0} - {1} - isAssignable: {2}", fullName, propertyInfo.PropertyType.Name, isAssignable); + + if (propertyType == typeof (int) || + propertyType == typeof (bool) || + propertyType == typeof (double) || + propertyType == typeof (float) || + propertyType == typeof (string)) + { + Debug.WriteLine("Save setting: {0} - {1} - {2}", fullName, propertyInfo.PropertyType.Name, value); + roamingSettings.Values[fullName] = value; + } + else if (typeof (IAppConfig).GetTypeInfo().IsAssignableFrom(propertyType.GetTypeInfo())) + { + var subConfig = (IAppConfig)propertyInfo.GetValue(config); + SaveRecursive(subConfig, keyPreset + propertyType.Name + "."); + } + } } } }