From: gu.martinm@gmail.com Date: Sun, 17 Aug 2014 10:11:24 +0000 (+0200) Subject: WeatherInformation WP8: improvements in WeatherData object. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=6c61ff7fd43d35305af4eff066de3985f9281334;p=CSharpForFun%2F.git WeatherInformation WP8: improvements in WeatherData object. --- diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs index 005e5e4..f567f68 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs @@ -129,20 +129,20 @@ namespace WeatherInformation WeatherData weatherData = null; // Check to see if the key for the application state data is in the State dictionary. - string JSONRemoteForecastWeather = null; - string JSONRemoteCurrentWeather = null; + string jsonForecast = null; + string jsonCurrent = null; string city = null; string country = null; - if (PhoneApplicationService.Current.State.ContainsKey("JSONRemoteForecastWeather") && - PhoneApplicationService.Current.State.ContainsKey("JSONRemoteCurrentWeather") && + if (PhoneApplicationService.Current.State.ContainsKey("JSONForecast") && + PhoneApplicationService.Current.State.ContainsKey("JSONCurrent") && PhoneApplicationService.Current.State.ContainsKey("City") && PhoneApplicationService.Current.State.ContainsKey("Country")) { // If it exists, assign the data to the application member variable. - JSONRemoteForecastWeather = PhoneApplicationService.Current.State["JSONRemoteForecastWeather"] as string; + jsonForecast = PhoneApplicationService.Current.State["JSONForecast"] as string; // If it exists, assign the data to the application member variable. - JSONRemoteCurrentWeather = PhoneApplicationService.Current.State["JSONRemoteCurrentWeather"] as string; + jsonCurrent = PhoneApplicationService.Current.State["JSONCurrent"] as string; // If it exists, assign the data to the application member variable. city = PhoneApplicationService.Current.State["City"] as string; @@ -150,14 +150,12 @@ namespace WeatherInformation // If it exists, assign the data to the application member variable. country = PhoneApplicationService.Current.State["Country"] as string; } - - if (!string.IsNullOrEmpty(JSONRemoteCurrentWeather) && !string.IsNullOrEmpty(JSONRemoteForecastWeather) && + + if (!string.IsNullOrEmpty(jsonCurrent) && !string.IsNullOrEmpty(jsonForecast) && !string.IsNullOrEmpty(city) && !string.IsNullOrEmpty(country)) { var parser = new ServiceParser(new JsonParser()); - weatherData = parser.WeatherDataParser(JSONRemoteForecastWeather, JSONRemoteCurrentWeather); - weatherData.City = city; - weatherData.Country = country; + weatherData = parser.WeatherDataParser(jsonForecast, jsonCurrent, city, country); } ApplicationDataObject = weatherData; @@ -171,14 +169,14 @@ namespace WeatherInformation var weatherData = ApplicationDataObject; if (weatherData != null) { - if (!string.IsNullOrEmpty(weatherData.JSONRemoteCurrent) && - !string.IsNullOrEmpty(weatherData.JSONRemoteForecast)) + if (!string.IsNullOrEmpty(weatherData.JSONCurrent) && + !string.IsNullOrEmpty(weatherData.JSONForecast)) { // Store it in the State dictionary. - PhoneApplicationService.Current.State["JSONRemoteForecastWeather"] = weatherData.JSONRemoteForecast; + PhoneApplicationService.Current.State["JSONForecast"] = weatherData.JSONForecast; // Store it in the State dictionary. - PhoneApplicationService.Current.State["JSONRemoteCurrentWeather"] = weatherData.JSONRemoteCurrent; + PhoneApplicationService.Current.State["JSONCurrent"] = weatherData.JSONCurrent; // Store it in the State dictionary. PhoneApplicationService.Current.State["City"] = weatherData.City; diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/MainPage.xaml.cs b/WindowsPhone/WeatherInformation/WeatherInformation/MainPage.xaml.cs index 26b6878..bddd5d3 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/MainPage.xaml.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/MainPage.xaml.cs @@ -81,7 +81,7 @@ namespace WeatherInformation // Gets the data from the web. (Application.Current as WeatherInformation.App).ApplicationDataObject = - await GetRemoteDataAsync(locationItem).ConfigureAwait(false); + await GetRemoteDataAsync(locationItem); using (var db = new LocationDataContext(LocationDataContext.DBConnectionString)) { @@ -92,7 +92,10 @@ namespace WeatherInformation } // Call UpdateUI on the UI thread. - Dispatcher.BeginInvoke(() => UpdateUI()); + // Without ConfigureAwait(false) await returns data on the calling thread. In this case the calling one + // is the UI thread. So, I can save the call to Dispatcher.BeginInvoke. + //Dispatcher.BeginInvoke(() => UpdateUI()); + UpdateUI(); } void UpdateUI() @@ -131,17 +134,15 @@ namespace WeatherInformation string formattedForecastURL = String.Format( CultureInfo.InvariantCulture, AppResources.URIAPIOpenWeatherMapForecast, AppResources.APIVersionOpenWeatherMap, latitude, longitude, resultsNumber); - string JSONRemoteForecastWeather = await httpClient.GetWeatherDataAsync(formattedForecastURL).ConfigureAwait(false); + string jsonForecast = await httpClient.GetWeatherDataAsync(formattedForecastURL).ConfigureAwait(false); string formattedCurrentURL = String.Format( CultureInfo.InvariantCulture, AppResources.URIAPIOpenWeatherMapCurrent, AppResources.APIVersionOpenWeatherMap, latitude, longitude, resultsNumber); - string JSONRemoteCurrentWeather = await httpClient.GetWeatherDataAsync(formattedCurrentURL).ConfigureAwait(false); + string jsonCurrent = await httpClient.GetWeatherDataAsync(formattedCurrentURL).ConfigureAwait(false); var parser = new ServiceParser(new JsonParser()); - var weatherData = parser.WeatherDataParser(JSONRemoteForecastWeather, JSONRemoteCurrentWeather); - weatherData.City = locationItem.City; - weatherData.Country = locationItem.Country; + var weatherData = parser.WeatherDataParser(jsonForecast, jsonCurrent, locationItem.City, locationItem.Country); return weatherData; } diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/Model/Services/ServiceParser.cs b/WindowsPhone/WeatherInformation/WeatherInformation/Model/Services/ServiceParser.cs index b8543cb..29570a3 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/Model/Services/ServiceParser.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/Model/Services/ServiceParser.cs @@ -28,24 +28,21 @@ namespace WeatherInformation.Model.Services return this._jsonParser.ParserWeatherData(jsonData); } - public WeatherData WeatherDataParser(string JSONRemoteForecastWeather, string JSONRemoteCurrentWeather) + public WeatherData WeatherDataParser( + string JSONForecast, string JSONCurrent, string city, string country) { - if (string.IsNullOrEmpty(JSONRemoteForecastWeather)) + if (string.IsNullOrEmpty(JSONForecast)) { - throw new ArgumentException("Missing argument", "JSONRemoteForecastWeather"); + throw new ArgumentException("Missing argument", "JSONForecast"); } - if (string.IsNullOrEmpty(JSONRemoteCurrentWeather)) + if (string.IsNullOrEmpty(JSONCurrent)) { - throw new ArgumentException("Missing argument", "JSONRemoteCurrentWeather"); + throw new ArgumentException("Missing argument", "JSONCurrent"); } - return new WeatherData - { - JSONRemoteCurrent = JSONRemoteCurrentWeather, - JSONRemoteForecast = JSONRemoteForecastWeather, - RemoteCurrent = GetCurrentWeather(JSONRemoteCurrentWeather), - RemoteForecast = GetForecastWeather(JSONRemoteForecastWeather), - }; + var forecast = GetForecastWeather(JSONForecast); + var current = GetCurrentWeather(JSONCurrent); + return new WeatherData(forecast, JSONForecast, current, JSONCurrent, city, country); } } } diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/Model/WeatherData.cs b/WindowsPhone/WeatherInformation/WeatherInformation/Model/WeatherData.cs index 4eef16a..1e2e56f 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/Model/WeatherData.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/Model/WeatherData.cs @@ -10,31 +10,29 @@ namespace WeatherInformation.Model { public class WeatherData { - public ForecastWeather RemoteForecast - { - get; - set; - } - public string JSONRemoteForecast - { - get; - set; - } + private readonly ForecastWeather _forecast; + private readonly string _jsonForecast; + private readonly CurrentWeather _current; + private readonly string _jsonCurrent; + private readonly string _city; + private readonly string _country; - - public CurrentWeather RemoteCurrent - { - get; - set; - } - public string JSONRemoteCurrent + public WeatherData(ForecastWeather forecast, string jsonForecast, CurrentWeather current, + string jsonCurrent, string city, string country) { - get; - set; + _forecast = forecast; + _jsonForecast = jsonForecast; + _current = current; + _jsonCurrent = jsonCurrent; + _city = city; + _country = country; } - public string City { get; set;} - - public string Country { get; set; } + public ForecastWeather Forecast { get { return _forecast; } } + public string JSONForecast { get { return _jsonForecast; } } + public CurrentWeather Current { get { return _current; } } + public string JSONCurrent { get { return _jsonCurrent; } } + public string City { get { return _city; } } + public string Country { get { return _country; } } } } diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/SelectedDatePage.xaml b/WindowsPhone/WeatherInformation/WeatherInformation/SelectedDatePage.xaml index 37764ae..42302e1 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/SelectedDatePage.xaml +++ b/WindowsPhone/WeatherInformation/WeatherInformation/SelectedDatePage.xaml @@ -12,9 +12,6 @@ shell:SystemTray.IsVisible="True" d:DataContext="{d:DesignData /SampleData/SelectedDateViewModelSampleData.xaml}"> - - - diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs index 951a59e..1c1b3e9 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs @@ -96,7 +96,7 @@ namespace WeatherInformation.ViewModels string symbol = isFahrenheit ? AppResources.TemperatureUnitsFahrenheitSymbol : AppResources.TemperatureUnitsCentigradeSymbol; DateTime unixTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); - var remoteForecastWeatherData = weatherData.RemoteForecast; + var remoteForecastWeatherData = weatherData.Forecast; if (remoteForecastWeatherData != null) { this.ForecastItems.Clear(); @@ -148,7 +148,7 @@ namespace WeatherInformation.ViewModels // TODO: nullables? // TODO: nullables para distinguir cuando hay datos o no. Ahora me llega 0 si no datos (supongo) cuando double/integer - var remoteCurrentWeatherData = weatherData.RemoteCurrent; + var remoteCurrentWeatherData = weatherData.Current; if (remoteCurrentWeatherData != null) { string weatherImage; diff --git a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SelectedDateViewModel.cs b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SelectedDateViewModel.cs index 5510bd7..6cbe528 100644 --- a/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SelectedDateViewModel.cs +++ b/WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SelectedDateViewModel.cs @@ -61,7 +61,7 @@ namespace WeatherInformation.ViewModels public void LoadData(WeatherData weatherData, int index) { - var remoteForecastWeatherData = weatherData.RemoteForecast; + var remoteForecastWeatherData = weatherData.Forecast; WeatherInformation.Model.ForecastWeatherParser.List forecast = remoteForecastWeatherData.list[index]; DateTime unixTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);