WeatherInformation WP8: improvements in WeatherData object.
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 17 Aug 2014 10:11:24 +0000 (12:11 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 17 Aug 2014 10:11:24 +0000 (12:11 +0200)
WindowsPhone/WeatherInformation/WeatherInformation/App.xaml.cs
WindowsPhone/WeatherInformation/WeatherInformation/MainPage.xaml.cs
WindowsPhone/WeatherInformation/WeatherInformation/Model/Services/ServiceParser.cs
WindowsPhone/WeatherInformation/WeatherInformation/Model/WeatherData.cs
WindowsPhone/WeatherInformation/WeatherInformation/SelectedDatePage.xaml
WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/MainViewModel.cs
WindowsPhone/WeatherInformation/WeatherInformation/ViewModels/SelectedDateViewModel.cs

index 005e5e4..f567f68 100644 (file)
@@ -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;
index 26b6878..bddd5d3 100644 (file)
@@ -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;
         }
index b8543cb..29570a3 100644 (file)
@@ -28,24 +28,21 @@ namespace WeatherInformation.Model.Services
             return this._jsonParser.ParserWeatherData<ForecastWeather>(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);
         }
     }
 }
index 4eef16a..1e2e56f 100644 (file)
@@ -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; } }
     }
 }
index 37764ae..42302e1 100644 (file)
@@ -12,9 +12,6 @@
     shell:SystemTray.IsVisible="True"
     d:DataContext="{d:DesignData /SampleData/SelectedDateViewModelSampleData.xaml}">
 
-       <phone:PhoneApplicationPage.Resources>
-               <ViewModels:SelectedDateViewModel x:Key="SelectedDateViewModelDataSource" d:IsDataSource="True"/>
-       </phone:PhoneApplicationPage.Resources>
        <phone:PhoneApplicationPage.FontFamily>
                <StaticResource ResourceKey="PhoneFontFamilyNormal"/>
        </phone:PhoneApplicationPage.FontFamily>
index 951a59e..1c1b3e9 100644 (file)
@@ -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;
index 5510bd7..6cbe528 100644 (file)
@@ -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);