2 using System.ComponentModel;
3 using System.IO.IsolatedStorage;
4 using WeatherInformation.Resources;
6 namespace WeatherInformation.ViewModels
8 // TODO: IMHO INotifyPropertyChanged does not do anything useful in this class... NotifyPropertyChanged has no effect :(
9 public class SettingsViewModel : INotifyPropertyChanged
12 private readonly IsolatedStorageSettings _settings;
14 // The key names of _settings
15 private const string _languageSelectionSettingKeyName = "LanguageSelection";
16 private const string _temperatureUnitsSelectionSettingKeyName = "TemperatureUnitsSelection";
17 private const string _forecastDayNumbersSelectionSelectionSettingKeyName = "ForecastDayNumbersSelection";
18 private const string _tileNotificationSwitchSettingKeyName = "TileNotificationSwitch";
19 private const string _tileNotificationSwitchContentSettingKeyName = "TileNotificationSwitchContentSetting";
22 private const int _languageSelectionSettingDefault = 0;
23 private const int _temperatureUnitsSelectionSettingDefault = 0;
24 private const int _forecastDayNumbersSelectionSettingDefault = 0;
26 private const bool _tileNotificationSwitchSettingDefault = false;
29 public SettingsViewModel()
31 // You need to add a check to DesignerProperties.IsInDesignTool to that code since accessing
32 // IsolatedStorageSettings in Visual Studio or Expression Blend is invalid.
33 // see: http://stackoverflow.com/questions/7294461/unable-to-determine-application-identity-of-the-caller
34 if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
36 // Get the _settings for this application.
37 _settings = IsolatedStorageSettings.ApplicationSettings;
42 /// Property to get and set language selection Setting Key.
44 public int LanguageSelectionSetting
48 return GetValueOrDefault<int>(_languageSelectionSettingKeyName, _languageSelectionSettingDefault);
52 if (AddOrUpdateValue(_languageSelectionSettingKeyName, value))
56 NotifyPropertyChanged(_languageSelectionSettingKeyName);
61 /// Property to get and set temperature units selection Setting Key.
63 public int TemperaruteUnitsSelectionSetting
67 return GetValueOrDefault<int>(_temperatureUnitsSelectionSettingKeyName, _temperatureUnitsSelectionSettingDefault);
71 if (AddOrUpdateValue(_temperatureUnitsSelectionSettingKeyName, value))
75 NotifyPropertyChanged(_temperatureUnitsSelectionSettingKeyName);
80 /// Property to get and set forecast day numbers selection Setting Key.
82 public int ForecastDayNumbersSelectionSetting
86 return GetValueOrDefault<int>(_forecastDayNumbersSelectionSelectionSettingKeyName, _forecastDayNumbersSelectionSettingDefault);
90 if (AddOrUpdateValue(_forecastDayNumbersSelectionSelectionSettingKeyName, value))
94 NotifyPropertyChanged(_forecastDayNumbersSelectionSelectionSettingKeyName);
99 /// Turns on/off tile notification.
101 public bool TileNotificationSwitchSetting
105 bool value = GetValueOrDefault<bool>(_tileNotificationSwitchSettingKeyName, _tileNotificationSwitchSettingDefault);
108 TileNotificationSwitchContentSetting = AppResources.SettingsTileNotificationSwitchOn;
112 TileNotificationSwitchContentSetting = AppResources.SettingsTileNotificationSwitchOff;
114 NotifyPropertyChanged(_tileNotificationSwitchContentSettingKeyName);
119 if (AddOrUpdateValue(_tileNotificationSwitchSettingKeyName, value))
125 TileNotificationSwitchContentSetting = AppResources.SettingsTileNotificationSwitchOn;
129 TileNotificationSwitchContentSetting = AppResources.SettingsTileNotificationSwitchOff;
131 NotifyPropertyChanged(_tileNotificationSwitchSettingKeyName);
132 NotifyPropertyChanged(_tileNotificationSwitchContentSettingKeyName);
137 /// Changes content switch notification setting.
139 public string TileNotificationSwitchContentSetting { get; private set; }
143 /// Get the current value of the setting, or if it is not found, set the
144 /// setting to the default value.
146 /// <typeparam name="T"></typeparam>
147 /// <param name="Key"></param>
148 /// <param name="defaultValue"></param>
149 /// <returns></returns>
150 private T GetValueOrDefault<T>(string Key, T defaultValue)
154 // You need to add a check to DesignerProperties.IsInDesignTool to that code since accessing
155 // IsolatedStorageSettings in Visual Studio or Expression Blend is invalid.
156 // see: http://stackoverflow.com/questions/7294461/unable-to-determine-application-identity-of-the-caller
157 if (System.ComponentModel.DesignerProperties.IsInDesignTool)
162 // If the key exists, retrieve the value.
163 if (_settings.Contains(Key))
165 value = (T)_settings[Key];
167 // Otherwise, use the default value.
170 value = defaultValue;
176 /// Update a setting value for application. If the setting does not
177 /// exist, then add the setting.
179 /// <param name="Key"></param>
180 /// <param name="value"></param>
181 /// <returns></returns>
182 private bool AddOrUpdateValue(string Key, Object value)
184 bool valueChanged = false;
186 // You need to add a check to DesignerProperties.IsInDesignTool to that code since accessing
187 // IsolatedStorageSettings in Visual Studio or Expression Blend is invalid.
188 // see: http://stackoverflow.com/questions/7294461/unable-to-determine-application-identity-of-the-caller
189 if (System.ComponentModel.DesignerProperties.IsInDesignTool)
195 if (_settings.Contains(Key))
197 // If the value has changed
198 if (_settings[Key] != value)
200 // Store the new value
201 _settings[Key] = value;
205 // Otherwise create the key.
208 _settings.Add(Key, value);
215 /// Save the _settings.
219 // You need to add a check to DesignerProperties.IsInDesignTool to that code since accessing
220 // IsolatedStorageSettings in Visual Studio or Expression Blend is invalid.
221 // see: http://stackoverflow.com/questions/7294461/unable-to-determine-application-identity-of-the-caller
222 if (System.ComponentModel.DesignerProperties.IsInDesignTool)
230 public event PropertyChangedEventHandler PropertyChanged;
231 private void NotifyPropertyChanged(string propertyName)
233 PropertyChangedEventHandler handler = PropertyChanged;
236 handler(this, new PropertyChangedEventArgs(propertyName));