WeatherInformation WP8: MainPage with progress bar.
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 5 Oct 2014 22:29:25 +0000 (00:29 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 5 Oct 2014 22:29:25 +0000 (00:29 +0200)
WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml
WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml.cs
WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.Designer.cs
WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.resx
WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.xlf
WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.qps-ploc.xlf
WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.resx

index 5d73b4e..9e651b9 100644 (file)
             CurrentUICulture de la aplicación en tiempo de ejecución.
          -->
 
+        <TextBlock x:Name="NoDataAvailable" Text="{Binding LocalizedResources.MainPageRemoteDataError, Mode=OneWay, Source={StaticResource LocalizedStrings}}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle2Style}" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.ColumnSpan="3" Visibility="Collapsed"/>
+        <ProgressBar x:Name="ProgressBarRemoteData" IsIndeterminate="True" LargeChange="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Height="80" FontSize="20" FlowDirection="LeftToRight" UseLayoutRounding="True" IsEnabled="False" />
+
         <!--Control Pivot-->
         <phone:Pivot x:Name="TitleTextCityCountry" Title="{Binding TitleTextCityCountry}">
             <!--Elemento Pivot uno-->
-            <phone:PivotItem Header="{Binding LocalizedResources.MainPageForecastHeader, Mode=OneWay, Source={StaticResource LocalizedStrings}}">
+            <phone:PivotItem x:Name="ForecastData" Header="{Binding LocalizedResources.MainPageForecastHeader, Mode=OneWay, Source={StaticResource LocalizedStrings}}">
                 <phone:LongListSelector x:Name="ForecastItems" Margin="0,0,-12,0" ItemsSource="{Binding ForecastItems}" SelectionChanged="LongListSelector_SelectionChanged">
                     <phone:LongListSelector.ItemTemplate>
                         <DataTemplate>
@@ -80,7 +83,7 @@
             </phone:PivotItem>
 
             <!--Elemento Pivot dos-->
-            <phone:PivotItem Header="{Binding LocalizedResources.MainPageCurrentHeader, Mode=OneWay, Source={StaticResource LocalizedStrings}}">
+            <phone:PivotItem x:Name="CurrentData" Header="{Binding LocalizedResources.MainPageCurrentHeader, Mode=OneWay, Source={StaticResource LocalizedStrings}}">
                 <ScrollViewer HorizontalScrollBarVisibility="Auto">
                     <Grid>
                         <Grid.RowDefinitions>
index 90589a0..d87c6d1 100644 (file)
@@ -11,7 +11,6 @@ using WeatherInformation.Resources;
 using WeatherInformation.ViewModels;
 using System.Threading.Tasks;
 using WeatherInformation.Model.JsonDataParser;
-using Microsoft.Phone.Shell;
 
 namespace WeatherInformation
 {
@@ -37,8 +36,6 @@ namespace WeatherInformation
         {
             base.OnNavigatedTo(e);
 
-            CreateFlipTile();
-
             // If _isNewPageInstance is true, the page constuctor has been called, so
             // state may need to be restored.
             if (_isNewPageInstance)
@@ -65,9 +62,20 @@ namespace WeatherInformation
                 locationItem = db.Locations.Where(location => location.IsSelected).FirstOrDefault();
             }
 
+            // Empty UI
+            this.CurrentData.Visibility = Visibility.Collapsed;
+            this.ForecastData.Visibility = Visibility.Collapsed;
+            this.ProgressBarRemoteData.Visibility = Visibility.Collapsed;
+            this.NoDataAvailable.Visibility = Visibility.Collapsed;
+
             if (locationItem == null)
             {
                 // Nothing to do.
+                // Show error message.
+                this.CurrentData.Visibility = Visibility.Collapsed;
+                this.ForecastData.Visibility = Visibility.Collapsed;
+                this.ProgressBarRemoteData.Visibility = Visibility.Collapsed;
+                this.NoDataAvailable.Visibility = Visibility.Visible;
                 return;
             }
 
@@ -76,38 +84,68 @@ namespace WeatherInformation
             WeatherData weatherData = (Application.Current as WeatherInformation.App).ApplicationDataObject;
             if (!IsDataFresh(locationItem.LastRemoteDataUpdate) || weatherData == null)
             {
-                // Load remote data (aynchronous way by means of async/await)
-
                 // Gets the data from the web.
-                (Application.Current as WeatherInformation.App).ApplicationDataObject =
-                    await GetRemoteDataAsync(locationItem);
+                this.CurrentData.Visibility = Visibility.Collapsed;
+                this.ForecastData.Visibility = Visibility.Collapsed;
+                this.ProgressBarRemoteData.Visibility = Visibility.Visible;
+                this.NoDataAvailable.Visibility = Visibility.Collapsed;
 
-                using (var db = new LocationDataContext(LocationDataContext.DBConnectionString))
-                {
-                    locationItem = db.Locations.Where(location => location.IsSelected).FirstOrDefault();
-                    locationItem.LastRemoteDataUpdate = DateTime.UtcNow;
-                    db.SubmitChanges();
-                }
+                await GetRemoteDataAndUpdateUI(locationItem);
             }
+            else
+            {
+                this.CurrentData.Visibility = Visibility.Visible;
+                this.ForecastData.Visibility = Visibility.Visible;
+                this.ProgressBarRemoteData.Visibility = Visibility.Collapsed;
+                this.NoDataAvailable.Visibility = Visibility.Collapsed;
 
-            // Call UpdateUI on the UI thread.
-            // 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();
+                UpdateUI(weatherData);
+            }
         }
 
-        void UpdateUI()
+        private void UpdateUI(WeatherData weatherData)
         {
-            // Set the ApplicationData and ApplicationDataStatus members of the ViewModel
-            WeatherData weatherData = (Application.Current as WeatherInformation.App).ApplicationDataObject;
-
             if (weatherData != null)
             {
                 _mainViewModel.LoadData(weatherData);
             }
         }
 
+        async private Task GetRemoteDataAndUpdateUI(Location locationItem)
+        {
+            // Load remote data (aynchronous way by means of async/await)
+            try
+            {
+                // Without ConfigureAwait(false) await returns data on the calling thread.
+                WeatherData weatherData = await GetRemoteDataAsync(locationItem);
+
+                this.CurrentData.Visibility = Visibility.Visible;
+                this.ForecastData.Visibility = Visibility.Visible;
+                this.ProgressBarRemoteData.Visibility = Visibility.Collapsed;
+                this.NoDataAvailable.Visibility = Visibility.Collapsed;
+
+                UpdateUI(weatherData);
+
+                (Application.Current as WeatherInformation.App).ApplicationDataObject = weatherData;
+
+                using (var db = new LocationDataContext(LocationDataContext.DBConnectionString))
+                {
+                    locationItem = db.Locations.Where(location => location.IsSelected).FirstOrDefault();
+                    locationItem.LastRemoteDataUpdate = DateTime.UtcNow;
+                    db.SubmitChanges();
+                }
+            }
+            catch(Exception e)
+            {
+                // Empty UI and show error message
+                this.CurrentData.Visibility = Visibility.Collapsed;
+                this.ForecastData.Visibility = Visibility.Collapsed;
+                this.ProgressBarRemoteData.Visibility = Visibility.Collapsed;
+                this.NoDataAvailable.Visibility = Visibility.Visible;
+            }
+
+        }
+
         private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
             LongListSelector longListSelector = sender as LongListSelector;
@@ -174,22 +212,6 @@ namespace WeatherInformation
             NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative));
         }
 
-        private void CreateFlipTile()
-        {
-            ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault(
-                x => x.NavigationUri.ToString().Contains("flip"));
-            tile = ShellTile.ActiveTiles.First();
-            var activeTiles = ShellTile.ActiveTiles;
-
-            var tileData = new FlipTileData();
-            tileData.Title = "GUSTAVO RULES";
-            tileData.BackTitle = "Gustavo Rules Back";
-            tileData.BackContent = "Gustavo Back Content";
-            tileData.WideBackContent = "Gustavo Wid Back Content";
-            tile.Update(tileData);
-
-        }
-
         // Código de ejemplo para compilar una ApplicationBar traducida
         //private void BuildLocalizedApplicationBar()
         //{
index 1326219..b1df585 100644 (file)
@@ -295,6 +295,15 @@ namespace WeatherInformation.Resources {
         }
         
         /// <summary>
+        ///   Busca una cadena traducida similar a No data available.
+        /// </summary>
+        public static string MainPageRemoteDataError {
+            get {
+                return ResourceManager.GetString("MainPageRemoteDataError", resourceCulture);
+            }
+        }
+        
+        /// <summary>
         ///   Busca una cadena traducida similar a City, country.
         /// </summary>
         public static string MainPageTitle {
index 369b96c..9a72f3d 100644 (file)
     <value>Notificaciones</value>
     <comment>Settings page, switch notifications header</comment>
   </data>
+  <data name="MainPageRemoteDataError" xml:space="preserve">
+    <value>No hay datos disponibles</value>
+    <comment>Main pange, default message when there is no remote data</comment>
+  </data>
 </root>
\ No newline at end of file
index b4c6c13..cd0fd76 100644 (file)
           <target state="translated">Activado</target>
           <note from="MultilingualBuild" annotates="source" priority="2">Settings page, switch notifications on</note>
           </trans-unit>
+        <trans-unit id="Resx/MainPageRemoteDataError" translate="yes" xml:space="preserve">
+          <source>No data available</source>
+          <target state="translated">No hay datos disponibles</target>
+          <note from="MultilingualBuild" annotates="source" priority="2">Main pange, default message when there is no remote data</note>
+          </trans-unit>
         <trans-unit id="Resx/SettingsTileNotificationSwitchOff" translate="yes" xml:space="preserve">
           <source>Off</source>
           <target state="translated">Desactivado</target>
index 7b12b47..8ace91b 100644 (file)
           <target state="new">On</target>
           <note from="MultilingualBuild" annotates="source" priority="2">Settings page, switch notifications on</note>
         </trans-unit>
+        <trans-unit id="Resx/MainPageRemoteDataError" translate="yes" xml:space="preserve">
+          <source>No data available</source>
+          <target state="new">No data available</target>
+          <note from="MultilingualBuild" annotates="source" priority="2">Main pange, default message when there is no remote data</note>
+        </trans-unit>
         <trans-unit id="Resx/SettingsTileNotificationSwitchOff" translate="yes" xml:space="preserve">
           <source>Off</source>
           <target state="new">Off</target>
index ed45386..e51330b 100644 (file)
     <value>On</value>
     <comment>Settings page, switch notifications on</comment>
   </data>
+  <data name="MainPageRemoteDataError" xml:space="preserve">
+    <value>No data available</value>
+    <comment>Main pange, default message when there is no remote data</comment>
+  </data>
 </root>
\ No newline at end of file