WeatherInformation WP8: MapPage improvements
[CSharpForFun/.git] / WindowsPhone / WP8 / WeatherInformation / WeatherInformation / ViewModels / SettingsViewModel.cs
1 using System;
2 using System.ComponentModel;
3 using System.IO.IsolatedStorage;
4 using WeatherInformation.Resources;
5
6 namespace WeatherInformation.ViewModels
7 {
8     // TODO: IMHO INotifyPropertyChanged does not do anything useful in this class... NotifyPropertyChanged has no effect :(
9     public class SettingsViewModel : INotifyPropertyChanged
10     {
11         // Settings
12         private readonly IsolatedStorageSettings _settings;
13
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";
20
21         // The default values
22         private const int _languageSelectionSettingDefault = 0;
23         private const int _temperatureUnitsSelectionSettingDefault = 0;
24         private const int _forecastDayNumbersSelectionSettingDefault = 0;
25         // Notifications off.
26         private const bool _tileNotificationSwitchSettingDefault = false;
27
28
29         public SettingsViewModel()
30         {
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)
35             {
36                 // Get the _settings for this application.
37                 _settings = IsolatedStorageSettings.ApplicationSettings;
38             }
39         }
40
41         /// <summary>
42         /// Property to get and set language selection Setting Key.
43         /// </summary>
44         public int LanguageSelectionSetting
45         {
46             get
47             {
48                 return GetValueOrDefault<int>(_languageSelectionSettingKeyName, _languageSelectionSettingDefault);
49             }
50             set
51             {
52                 if (AddOrUpdateValue(_languageSelectionSettingKeyName, value))
53                 {
54                     Save();
55                 }
56                 NotifyPropertyChanged(_languageSelectionSettingKeyName);
57             }
58         }
59
60         /// <summary>
61         /// Property to get and set temperature units selection Setting Key.
62         /// </summary>
63         public int TemperaruteUnitsSelectionSetting
64         {
65             get
66             {
67                 return GetValueOrDefault<int>(_temperatureUnitsSelectionSettingKeyName, _temperatureUnitsSelectionSettingDefault);
68             }
69             set
70             {
71                 if (AddOrUpdateValue(_temperatureUnitsSelectionSettingKeyName, value))
72                 {
73                     Save();
74                 }
75                 NotifyPropertyChanged(_temperatureUnitsSelectionSettingKeyName);
76             }
77         }
78
79         /// <summary>
80         /// Property to get and set forecast day numbers selection Setting Key.
81         /// </summary>
82         public int ForecastDayNumbersSelectionSetting
83         {
84             get
85             {
86                 return GetValueOrDefault<int>(_forecastDayNumbersSelectionSelectionSettingKeyName, _forecastDayNumbersSelectionSettingDefault);
87             }
88             set
89             {
90                 if (AddOrUpdateValue(_forecastDayNumbersSelectionSelectionSettingKeyName, value))
91                 {
92                     Save();
93                 }
94                 NotifyPropertyChanged(_forecastDayNumbersSelectionSelectionSettingKeyName);
95             }
96         }
97
98         /// <summary>
99         /// Turns on/off tile notification.
100         /// </summary>
101         public bool TileNotificationSwitchSetting
102         {
103             get
104             {
105                 bool value = GetValueOrDefault<bool>(_tileNotificationSwitchSettingKeyName, _tileNotificationSwitchSettingDefault);
106                 if (value)
107                 {
108                     TileNotificationSwitchContentSetting = AppResources.SettingsTileNotificationSwitchOn;
109                 }
110                 else
111                 {
112                     TileNotificationSwitchContentSetting = AppResources.SettingsTileNotificationSwitchOff;       
113                 }
114                 NotifyPropertyChanged(_tileNotificationSwitchContentSettingKeyName);
115                 return value;
116             }
117             set
118             {
119                 if (AddOrUpdateValue(_tileNotificationSwitchSettingKeyName, value))
120                 {
121                     Save();
122                 }
123                 if (value)
124                 {
125                     TileNotificationSwitchContentSetting = AppResources.SettingsTileNotificationSwitchOn;
126                 }
127                 else
128                 {
129                     TileNotificationSwitchContentSetting = AppResources.SettingsTileNotificationSwitchOff;
130                 }
131                 NotifyPropertyChanged(_tileNotificationSwitchSettingKeyName);
132                 NotifyPropertyChanged(_tileNotificationSwitchContentSettingKeyName);
133             }
134         }
135
136         /// <summary>
137         /// Changes content switch notification setting.
138         /// </summary>
139         public string TileNotificationSwitchContentSetting { get; private set; }
140
141
142         /// <summary>
143         /// Get the current value of the setting, or if it is not found, set the 
144         /// setting to the default value.
145         /// </summary>
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)
151         {
152             T value;
153
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)
158             {
159                 return defaultValue;
160             }
161
162             // If the key exists, retrieve the value.
163             if (_settings.Contains(Key))
164             {
165                 value = (T)_settings[Key];
166             }
167             // Otherwise, use the default value.
168             else
169             {
170                 value = defaultValue;
171             }
172             return value;
173         }
174
175         /// <summary>
176         /// Update a setting value for application. If the setting does not
177         /// exist, then add the setting.
178         /// </summary>
179         /// <param name="Key"></param>
180         /// <param name="value"></param>
181         /// <returns></returns>
182         private bool AddOrUpdateValue(string Key, Object value)
183         {
184             bool valueChanged = false;
185
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)
190             {
191                 return false;
192             }
193
194             // If the key exists
195             if (_settings.Contains(Key))
196             {
197                 // If the value has changed
198                 if (_settings[Key] != value)
199                 {
200                     // Store the new value
201                     _settings[Key] = value;
202                     valueChanged = true;
203                 }
204             }
205             // Otherwise create the key.
206             else
207             {
208                 _settings.Add(Key, value);
209                 valueChanged = true;
210             }
211             return valueChanged;
212         }
213
214         /// <summary>
215         /// Save the _settings.
216         /// </summary>
217         private void Save()
218         {
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)
223             {
224                 return;
225             }
226
227             _settings.Save();
228         }
229
230         public event PropertyChangedEventHandler PropertyChanged;
231         private void NotifyPropertyChanged(string propertyName)
232         {
233             PropertyChangedEventHandler handler = PropertyChanged;
234             if (null != handler)
235             {
236                 handler(this, new PropertyChangedEventArgs(propertyName));
237             }
238         }
239     }
240 }