예제 #1
0
        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                //this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif
            Logger.WriteLine(LoggerLevel.Info, "Started logger...");

            var Dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;

            // App loaded for first time
            Initialize(e).ContinueWith(async(t) =>
            {
                if (!e.PrelaunchActivated)
                {
                    if (Settings.WeatherLoaded && !String.IsNullOrEmpty(e.TileId) && !e.TileId.Equals("App", StringComparison.OrdinalIgnoreCase))
                    {
                        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            if (RootFrame.Content == null)
                            {
                                RootFrame.Navigate(typeof(Shell), "suppressNavigate");
                            }
                        });

                        // Navigate to WeatherNow page for location
                        if (Shell.Instance != null)
                        {
                            var locData   = Task.Run(Settings.GetLocationData).Result;
                            var locations = new List <LocationData>(locData)
                            {
                                Settings.HomeData,
                            };
                            var location = locations.FirstOrDefault(loc => loc.query.Equals(SecondaryTileUtils.GetQueryFromId(e.TileId)));
                            if (location != null)
                            {
                                var isHome = location.Equals(Settings.HomeData);

                                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                                {
                                    Shell.Instance.AppFrame.Navigate(typeof(WeatherNow), location);
                                    Shell.Instance.AppFrame.BackStack.Clear();
                                    if (!isHome)
                                    {
                                        Shell.Instance.AppFrame.BackStack.Add(new PageStackEntry(typeof(WeatherNow), null, null));
                                        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
                                    }
                                    else
                                    {
                                        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
                                    }
                                });
                            }

                            // If Shell content is empty navigate to default page
                            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                if (Shell.Instance.AppFrame.CurrentSourcePageType == null)
                                {
                                    Shell.Instance.AppFrame.Navigate(typeof(WeatherNow), null);
                                }
                            });
                        }
                    }

                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        if (RootFrame.Content == null)
                        {
                            // When the navigation stack isn't restored navigate to the first page,
                            // configuring the new page by passing required information as a navigation
                            // parameter
                            if (Settings.WeatherLoaded)
                            {
                                RootFrame.Navigate(typeof(Shell), e.Arguments);
                            }
                            else
                            {
                                RootFrame.Navigate(typeof(SetupPage), e.Arguments);
                            }
                        }

                        // Ensure the current window is active
                        Window.Current.Activate();
                    });
                }
            });
        }
        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();
            });
        }