private async void PinButton_Click(object sender, RoutedEventArgs e) { var pinBtn = sender as AppBarButton; pinBtn.IsEnabled = false; if (SecondaryTileUtils.Exists(location.query)) { bool deleted = await new SecondaryTile( SecondaryTileUtils.GetTileId(location.query)).RequestDeleteAsync(); if (deleted) { SecondaryTileUtils.RemoveTileId(location.query); } SetPinButton(!deleted); GetPinBtn().IsEnabled = true; } else { // Initialize the tile with required arguments var tileID = DateTime.Now.Ticks.ToString(); var tile = new SecondaryTile( tileID, "SimpleWeather", "action=view-weather&query=" + location.query, new Uri("ms-appx:///Assets/Square150x150Logo.png"), TileSize.Default); // Enable wide and large tile sizes tile.VisualElements.Wide310x150Logo = new Uri("ms-appx:///Assets/Wide310x150Logo.png"); tile.VisualElements.Square310x310Logo = new Uri("ms-appx:///Assets/Square310x310Logo.png"); // Add a small size logo for better looking small tile tile.VisualElements.Square71x71Logo = new Uri("ms-appx:///Assets/Square71x71Logo.png"); // Add a unique corner logo for the secondary tile tile.VisualElements.Square44x44Logo = new Uri("ms-appx:///Assets/Square44x44Logo.png"); // Show the display name on all sizes tile.VisualElements.ShowNameOnSquare150x150Logo = true; tile.VisualElements.ShowNameOnWide310x150Logo = true; tile.VisualElements.ShowNameOnSquare310x310Logo = true; bool isPinned = await tile.RequestCreateAsync(); if (isPinned) { // Update tile with notifications SecondaryTileUtils.AddTileId(location.query, tileID); await WeatherTileCreator.TileUpdater(location); await tile.UpdateAsync(); } SetPinButton(isPinned); GetPinBtn().IsEnabled = true; } }
public void OnWeatherLoaded(LocationData location, Weather weather) { // Save index before update int index = TextForecastControl.SelectedIndex; if (weather?.IsValid() == true) { wm.UpdateWeather(weather); WeatherView.UpdateView(weather); if (wm.SupportsAlerts) { if (weather.weather_alerts != null && weather.weather_alerts.Count > 0) { // Alerts are posted to the user here. Set them as notified. Task.Run(async() => { await WeatherAlertHandler.SetasNotified(location, weather.weather_alerts); }); } // Show/Hide Alert panel if (WeatherView.Extras.Alerts.Count > 0) { FrameworkElement alertButton = AlertButton; if (alertButton == null) { alertButton = FindName(nameof(AlertButton)) as FrameworkElement; } ResizeAlertPanel(); alertButton.Visibility = Visibility.Visible; } else { FrameworkElement alertButton = AlertButton; if (alertButton != null) { alertButton.Visibility = Visibility.Collapsed; } } } else { FrameworkElement alertButton = AlertButton; if (alertButton != null) { alertButton.Visibility = Visibility.Collapsed; } } // Update home tile if it hasn't been already if (Settings.HomeData.Equals(location) && (TimeSpan.FromTicks(DateTime.Now.Ticks - Settings.UpdateTime.Ticks).TotalMinutes > Settings.RefreshInterval) || !WeatherTileCreator.TileUpdated) { Task.Run(async() => await WeatherUpdateBackgroundTask.RequestAppTrigger()); } else if (SecondaryTileUtils.Exists(location.query)) { WeatherTileCreator.TileUpdater(location, weather); } // Shell Shell.Instance.HamburgerButtonColor = WeatherView.PendingBackgroundColor; if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar")) { // Mobile StatusBar.GetForCurrentView().BackgroundColor = WeatherView.PendingBackgroundColor; } else { // Desktop var titlebar = ApplicationView.GetForCurrentView().TitleBar; titlebar.BackgroundColor = WeatherView.PendingBackgroundColor; titlebar.ButtonBackgroundColor = titlebar.BackgroundColor; } } // Set saved index from before update // Note: needed since ItemSource is cleared and index is reset if (index == 0) // Note: UWP Mobile Bug { TextForecastControl.SelectedIndex = index + 1; } TextForecastControl.SelectedIndex = index; if (WeatherView.Extras.HourlyForecast.Count >= 1) { HourlyForecastPanel.Visibility = Visibility.Visible; } else { HourlyForecastPanel.Visibility = Visibility.Collapsed; } if (WeatherView.Extras.TextForecast.Count >= 1) { ForecastSwitch.Visibility = Visibility.Visible; } else { ForecastSwitch.Visibility = Visibility.Collapsed; } if (!String.IsNullOrWhiteSpace(WeatherView.Extras.Chance)) { if (!Settings.API.Equals(WeatherAPI.MetNo)) { if (!DetailsWrapGrid.Children.Contains(PrecipitationPanel)) { DetailsWrapGrid.Children.Insert(0, PrecipitationPanel); ResizeDetailItems(); } PrecipitationPanel.Visibility = Visibility.Visible; } else { DetailsWrapGrid.Children.Remove(PrecipitationPanel); ResizeDetailItems(); } int precipCount = PrecipitationPanel.Children.Count; int atmosCount = AtmospherePanel.Children.Count; if (Settings.API.Equals(WeatherAPI.OpenWeatherMap) || Settings.API.Equals(WeatherAPI.MetNo)) { if (ChanceItem != null) { PrecipitationPanel.Children.Remove(ChanceItem); } FrameworkElement cloudinessItem = CloudinessItem; if (cloudinessItem == null) { cloudinessItem = FindName(nameof(CloudinessItem)) as FrameworkElement; } if (cloudinessItem != null && !AtmospherePanel.Children.Contains(cloudinessItem)) { AtmospherePanel.Children.Insert(2, cloudinessItem); } } else { FrameworkElement chanceItem = ChanceItem; if (chanceItem == null) { chanceItem = FindName(nameof(ChanceItem)) as FrameworkElement; } if (chanceItem != null && !PrecipitationPanel.Children.Contains(chanceItem)) { PrecipitationPanel.Children.Insert(2, chanceItem); } if (CloudinessItem != null) { AtmospherePanel.Children.Remove(CloudinessItem); } } if (precipCount != PrecipitationPanel.Children.Count || atmosCount != AtmospherePanel.Children.Count) { ResizeDetailItems(); } } else { DetailsWrapGrid.Children.Remove(PrecipitationPanel); if (CloudinessItem != null) { AtmospherePanel.Children.Remove(CloudinessItem); } ResizeDetailItems(); } LoadingRing.IsActive = false; }
public void Run(IBackgroundTaskInstance taskInstance) { // Get a deferral, to prevent the task from closing prematurely // while asynchronous code is still running. var deferral = taskInstance.GetDeferral(); taskInstance.Canceled += OnCanceled; Task.Run(async() => { try { Logger.WriteLine(LoggerLevel.Debug, "WeatherUpdateBackgroundTask: Run..."); if (Settings.WeatherLoaded) { Logger.WriteLine(LoggerLevel.Debug, "WeatherUpdateBackgroundTask: Getting weather data..."); // Retrieve weather data. var weather = await GetWeather(); if (cts.IsCancellationRequested) { return; } // Update the live tile with data. Logger.WriteLine(LoggerLevel.Debug, "WeatherUpdateBackgroundTask: Weather is NULL = " + (weather == null).ToString()); Logger.WriteLine(LoggerLevel.Debug, "WeatherUpdateBackgroundTask: Updating primary tile..."); if (weather != null) { WeatherTileCreator.TileUpdater(Settings.HomeData, weather); } if (cts.IsCancellationRequested) { return; } // Update secondary tiles var tiles = await SecondaryTile.FindAllAsync(); foreach (SecondaryTile tile in tiles) { Logger.WriteLine(LoggerLevel.Debug, "WeatherUpdateBackgroundTask: Updating secondary tile..."); var locations = await Settings.GetLocationData(); var location = locations.FirstOrDefault( loc => loc.query.Equals(SecondaryTileUtils.GetQueryFromId(tile.TileId))); Logger.WriteLine(LoggerLevel.Debug, "Location = " + location?.name); Logger.WriteLine(LoggerLevel.Debug, "TileID = " + tile.TileId); if (location != null) { await WeatherTileCreator.TileUpdater(location); } if (cts.IsCancellationRequested) { return; } } // Post alerts if setting is on Logger.WriteLine(LoggerLevel.Debug, "WeatherUpdateBackgroundTask: Posting alerts..."); if (Settings.ShowAlerts && wm.SupportsAlerts && weather != null) { await WeatherAlertHandler.PostAlerts(Settings.HomeData, weather.weather_alerts); } if (cts.IsCancellationRequested) { return; } // Set update time if (weather != null) { Settings.UpdateTime = DateTime.Now; } } Logger.WriteLine(LoggerLevel.Debug, "WeatherUpdateBackgroundTask: End of run..."); } catch (Exception ex) { Logger.WriteLine(LoggerLevel.Error, ex, "{0}: exception occurred...", taskName); } }).ContinueWith((t) => { // Inform the system that the task is finished. deferral.Complete(); cts.Dispose(); }); }