From a3a930d44c1f8500d7c20f2fc64faf0695ce8c02 Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Fri, 21 Aug 2015 20:15:16 +0200 Subject: [PATCH] Fixed: Conversion settings binding (for encoding type) --- Application/FileConverter/ConversionPreset.cs | 54 +++++++++++++------ .../FileConverter/ConversionSettings.cs | 12 +++++ .../FileConverter/FileConverter.csproj | 1 + .../ConversionSettingsToString.cs | 21 ++++++-- .../FileConverter/Windows/SettingsWindow.xaml | 6 +-- 5 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 Application/FileConverter/ConversionSettings.cs diff --git a/Application/FileConverter/ConversionPreset.cs b/Application/FileConverter/ConversionPreset.cs index dc9da6d..4acee7b 100644 --- a/Application/FileConverter/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset.cs @@ -18,7 +18,7 @@ namespace FileConverter private string name; private OutputType outputType; private List inputTypes; - private Dictionary settings = new Dictionary(); + private ConversionSettings settings = new ConversionSettings(); public ConversionPreset() { @@ -61,6 +61,7 @@ namespace FileConverter set { outputType = value; + this.InitializeDefaultSettings(outputType); this.OnPropertyChanged(); } } @@ -80,8 +81,8 @@ namespace FileConverter } } - [XmlElement] - public ConversionSetting[] Settings + [XmlElement("Settings")] + public ConversionSetting[] XmlSerializableSettings { get { @@ -98,7 +99,6 @@ namespace FileConverter set { - this.settings.Clear(); if (value != null) { for (int index = 0; index < value.Length; index++) @@ -107,7 +107,15 @@ namespace FileConverter } } - this.OnPropertyChanged(); + this.OnPropertyChanged("Settings"); + } + } + + public IConversionSettings Settings + { + get + { + return this.settings; } } @@ -161,7 +169,7 @@ namespace FileConverter return this.settings[settingsKey]; } - return this.GetDefaultValue(settingsKey); + return null; } public T GetSettingsValue(string settingsKey) @@ -183,21 +191,33 @@ namespace FileConverter this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } - private string GetDefaultValue(string settingsKey) + private void InitializeDefaultSettings(OutputType outputType) { - if (this.OutputType == OutputType.Mp3) + if (outputType == OutputType.Mp3) { - switch (settingsKey) - { - case "Encoding": - return "VBR"; - - case "Bitrate": - return "190"; - } + this.InitializeSettingsValue("Encoding", "VBR"); + this.InitializeSettingsValue("Bitrate", "190"); } - return null; + this.OnPropertyChanged("Settings"); + } + + private void InitializeSettingsValue(string settingsKey, string value) + { + if (string.IsNullOrEmpty(settingsKey)) + { + throw new ArgumentNullException("key"); + } + + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentNullException("value"); + } + + if (!this.settings.ContainsKey(settingsKey)) + { + this.settings.Add(settingsKey, value); + } } private string Validate(string propertyName) diff --git a/Application/FileConverter/ConversionSettings.cs b/Application/FileConverter/ConversionSettings.cs new file mode 100644 index 0000000..a9dae02 --- /dev/null +++ b/Application/FileConverter/ConversionSettings.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; + +namespace FileConverter +{ + public interface IConversionSettings : IReadOnlyDictionary + { + } + + public class ConversionSettings : Dictionary, IConversionSettings + { + } +} \ No newline at end of file diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index b720fab..a0e96f1 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -78,6 +78,7 @@ + diff --git a/Application/FileConverter/ValueConverters/ConversionSettingsToString.cs b/Application/FileConverter/ValueConverters/ConversionSettingsToString.cs index d63c2e9..8cf883f 100644 --- a/Application/FileConverter/ValueConverters/ConversionSettingsToString.cs +++ b/Application/FileConverter/ValueConverters/ConversionSettingsToString.cs @@ -15,12 +15,12 @@ namespace FileConverter.ValueConverters return null; } - if (!(value is ConversionPreset)) + if (!(value is IConversionSettings)) { throw new ArgumentException("The value must be a conversion preset array."); } - ConversionPreset preset = (ConversionPreset)value; + IConversionSettings settings = (IConversionSettings)value; string parameterString = parameter as string; if (parameterString == null) @@ -29,13 +29,24 @@ namespace FileConverter.ValueConverters } string[] parameters = parameterString.Split(','); - if (parameters.Length != 1) + if (parameters.Length < 1 || parameters.Length > 2) { - throw new ArgumentException("The parameter format must be 'SettingsKey'."); + throw new ArgumentException("The parameter format must be 'SettingsKey[,DefaultValue]'."); } string key = parameters[0]; - return preset.GetSettingsValue(key); + string setting; + if (settings.TryGetValue(key, out setting)) + { + return setting; + } + + if (parameters.Length >= 2) + { + return parameters[1]; + } + + return null; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) diff --git a/Application/FileConverter/Windows/SettingsWindow.xaml b/Application/FileConverter/Windows/SettingsWindow.xaml index 650fcd4..eff917a 100644 --- a/Application/FileConverter/Windows/SettingsWindow.xaml +++ b/Application/FileConverter/Windows/SettingsWindow.xaml @@ -134,12 +134,12 @@