public void OnWeatherLoaded(LocationData location, Weather weather)
        {
            AppCompatActivity?.RunOnUiThread(() =>
            {
                if (weather?.IsValid() == true)
                {
                    wm.UpdateWeather(weather);
                    weatherView.UpdateView(weather);
                    SetView(weatherView);

                    if (Settings.HomeData.Equals(location))
                    {
                        // Update widgets if they haven't been already
                        if (TimeSpan.FromTicks(DateTime.Now.Ticks - Settings.UpdateTime.Ticks).TotalMinutes > Settings.RefreshInterval)
                        {
                            WeatherWidgetService.EnqueueWork(App.Context, new Intent(App.Context, typeof(WeatherWidgetService))
                                                             .SetAction(WeatherWidgetService.ACTION_UPDATEWEATHER));
                        }

                        // Update ongoing notification if its not showing
                        if (Settings.OnGoingNotification && !Notifications.WeatherNotificationBuilder.IsShowing)
                        {
                            WeatherWidgetService.EnqueueWork(App.Context, new Intent(App.Context, typeof(WeatherWidgetService))
                                                             .SetAction(WeatherWidgetService.ACTION_REFRESHNOTIFICATION));
                        }
                    }

                    if (wm.SupportsAlerts && Settings.ShowAlerts &&
                        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));
                    }
                }

                refreshLayout.Refreshing = false;
            });
        }
Ejemplo n.º 2
0
        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();
            });
        }