From c797941786a6d6955423ce25997aa11ab46c9bc7 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Sun, 17 Aug 2014 09:51:15 +0200 Subject: [PATCH] WeatherInformation WP8: same code for settings Setting/Getting application settings using always the same class (instead of rewriting the same code) --- .../WeatherInformation/ViewModels/MainViewModel.cs | 55 ++-------------------- .../ViewModels/SelectedDateViewModel.cs | 9 +++- .../ViewModels/SettingsViewModel.cs | 2 - 3 files changed, 12 insertions(+), 54 deletions(-) diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs index 8cb7891..bca102b 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs @@ -11,18 +11,7 @@ namespace WeatherInformation.ViewModels { public class MainViewModel : INotifyPropertyChanged { - // The key names of _settings - // TODO: reuse settings object instead of using the same code here again... - private const string _temperatureUnitsSelectionSettingKeyName = "TemperatureUnitsSelection"; - private const string _forecastDayNumbersSelectionSelectionSettingKeyName = "ForecastDayNumbersSelection"; - - // The default value of _settings - // TODO: reuse settings object instead of using the same code here again... - private const int _temperatureUnitsSelectionSettingDefault = 0; - private const int _forecastDayNumbersSelectionSettingDefault = 0; - - // Settings - private readonly IsolatedStorageSettings _settings; + private readonly SettingsViewModel _settings; public MainViewModel() { @@ -30,7 +19,7 @@ namespace WeatherInformation.ViewModels this.CurrentItems = new ObservableCollection(); // Get the _settings for this application. - _settings = IsolatedStorageSettings.ApplicationSettings; + _settings = new SettingsViewModel(); } /// @@ -77,8 +66,8 @@ namespace WeatherInformation.ViewModels public void LoadData(WeatherData weatherData) { // TODO: there must be a better way than using the index value :( - int forecastDayNumbers = - GetValueOrDefault(_forecastDayNumbersSelectionSelectionSettingKeyName, _forecastDayNumbersSelectionSettingDefault); + + int forecastDayNumbers = _settings.ForecastDayNumbersSelectionSetting; int count; switch(forecastDayNumbers) { @@ -98,8 +87,7 @@ namespace WeatherInformation.ViewModels // TODO: there must be a better way than using the index value :( bool isFahrenheit = true; - int temperatureUnitsSelection = - GetValueOrDefault(_temperatureUnitsSelectionSettingKeyName, _temperatureUnitsSelectionSettingDefault); + int temperatureUnitsSelection = _settings.TemperaruteUnitsSelectionSetting; if (temperatureUnitsSelection != 0) { isFahrenheit = false; @@ -317,39 +305,6 @@ namespace WeatherInformation.ViewModels } - /// - /// Get the current value of the setting, or if it is not found, set the - /// setting to the default value. - /// - /// - /// - /// - /// - private T GetValueOrDefault(string Key, T defaultValue) - { - T value; - - // You need to add a check to DesignerProperties.IsInDesignTool to that code since accessing - // IsolatedStorageSettings in Visual Studio or Expression Blend is invalid. - // see: http://stackoverflow.com/questions/7294461/unable-to-determine-application-identity-of-the-caller - if (System.ComponentModel.DesignerProperties.IsInDesignTool) - { - return defaultValue; - } - - // If the key exists, retrieve the value. - if (_settings.Contains(Key)) - { - value = (T)_settings[Key]; - } - // Otherwise, use the default value. - else - { - value = defaultValue; - } - return value; - } - public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) { diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SelectedDateViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SelectedDateViewModel.cs index 1e27dee..36e1afe 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SelectedDateViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SelectedDateViewModel.cs @@ -15,6 +15,7 @@ namespace WeatherInformation.ViewModels { public class SelectedDateViewModel : INotifyPropertyChanged { + private readonly SettingsViewModel _settings = new SettingsViewModel(); [DataMember] public Int32 SelectedDateIndex { get; set; } @@ -66,8 +67,12 @@ namespace WeatherInformation.ViewModels this.SelectedDate = date.ToString("m", CultureInfo.InvariantCulture); NotifyPropertyChanged("SelectedDate"); - // TODO: units :( - bool isFahrenheit = false; + bool isFahrenheit = true; + int temperatureUnitsSelection = _settings.TemperaruteUnitsSelectionSetting; + if (temperatureUnitsSelection != 0) + { + isFahrenheit = false; + } double tempUnits = isFahrenheit ? 0 : 273.15; string symbol = isFahrenheit ? AppResources.TemperatureUnitsFahrenheitSymbol : AppResources.TemperatureUnitsCentigradeSymbol; diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs index c9f488f..cb1613e 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SettingsViewModel.cs @@ -12,8 +12,6 @@ using WeatherInformation.Resources; namespace WeatherInformation.ViewModels { - // TODO: to use this class or something like that in every place of this application where the settings must be retrieved!!! - // (do not copy-paste code!!!) // TODO: IMHO INotifyPropertyChanged does not do anything useful in this class... NotifyPropertyChanged has no effect :( public class SettingsViewModel : INotifyPropertyChanged { -- 2.1.4