From 72ace8aa6cd42f384a11ceaac23e07b5edc8b7d2 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Mon, 6 Oct 2014 00:29:25 +0200 Subject: [PATCH] WeatherInformation WP8: MainPage with progress bar. --- .../WeatherInformation/MainPage.xaml | 7 +- .../WeatherInformation/MainPage.xaml.cs | 98 +++++++++++++--------- .../Resources/AppResources.Designer.cs | 9 ++ .../Resources/AppResources.es.resx | 4 + .../Resources/AppResources.es.xlf | 5 ++ .../Resources/AppResources.qps-ploc.xlf | 5 ++ .../WeatherInformation/Resources/AppResources.resx | 4 + 7 files changed, 92 insertions(+), 40 deletions(-) diff --git a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml index 5d73b4e..9e651b9 100644 --- a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml +++ b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml @@ -46,10 +46,13 @@ CurrentUICulture de la aplicación en tiempo de ejecución. --> + + + - + @@ -80,7 +83,7 @@ - + diff --git a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml.cs b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml.cs index 90589a0..d87c6d1 100644 --- a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml.cs +++ b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/MainPage.xaml.cs @@ -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() //{ diff --git a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.Designer.cs b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.Designer.cs index 1326219..b1df585 100644 --- a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.Designer.cs +++ b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.Designer.cs @@ -295,6 +295,15 @@ namespace WeatherInformation.Resources { } /// + /// Busca una cadena traducida similar a No data available. + /// + public static string MainPageRemoteDataError { + get { + return ResourceManager.GetString("MainPageRemoteDataError", resourceCulture); + } + } + + /// /// Busca una cadena traducida similar a City, country. /// public static string MainPageTitle { diff --git a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.resx b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.resx index 369b96c..9a72f3d 100644 --- a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.resx +++ b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.resx @@ -279,4 +279,8 @@ Notificaciones Settings page, switch notifications header + + No hay datos disponibles + Main pange, default message when there is no remote data + \ No newline at end of file diff --git a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.xlf b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.xlf index b4c6c13..cd0fd76 100644 --- a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.xlf +++ b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.es.xlf @@ -172,6 +172,11 @@ Activado Settings page, switch notifications on + + No data available + No hay datos disponibles + Main pange, default message when there is no remote data + Off Desactivado diff --git a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.qps-ploc.xlf b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.qps-ploc.xlf index 7b12b47..8ace91b 100644 --- a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.qps-ploc.xlf +++ b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.qps-ploc.xlf @@ -157,6 +157,11 @@ On Settings page, switch notifications on + + No data available + No data available + Main pange, default message when there is no remote data + Off Off diff --git a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.resx b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.resx index ed45386..e51330b 100644 --- a/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.resx +++ b/WindowsPhone/WP8/WeatherInformation/WeatherInformation/Resources/AppResources.resx @@ -322,4 +322,8 @@ On Settings page, switch notifications on + + No data available + Main pange, default message when there is no remote data + \ No newline at end of file -- 2.1.4