示例#1
0
        private async Task LoadWeatherData()
        {
            /*
             * If unable to retrieve saved data, data is old, or units don't match
             * Refresh weather data
             */

            Logger.WriteLine(LoggerLevel.Debug, "{0}: Loading weather data for {1}", TAG, location?.ToString());

            bool gotData = await LoadSavedWeatherData();

            if (!gotData)
            {
                Logger.WriteLine(LoggerLevel.Debug, "{0}: Saved weather data invalid for {1}", TAG, location?.ToString());
                Logger.WriteLine(LoggerLevel.Debug, "{0}: Retrieving data from weather provider", TAG);

                try
                {
                    if ((weather != null && weather.source != Settings.API) ||
                        (weather == null && location != null && location.source != Settings.API))
                    {
                        // Update location query and source for new API
                        string oldKey = location.query;

                        if (weather != null)
                        {
                            location.query = await wm.UpdateLocationQuery(weather);
                        }
                        else
                        {
                            location.query = await wm.UpdateLocationQuery(location);
                        }

                        location.source = Settings.API;

                        // Update database as well
#if !__ANDROID_WEAR__
                        if (location.locationType == LocationType.GPS)
                        {
                            Settings.SaveLastGPSLocData(location);
#if __ANDROID__
                            WearableDataListenerService.EnqueueWork(App.Context,
                                                                    new Android.Content.Intent(App.Context, typeof(WearableDataListenerService))
                                                                    .SetAction(WearableDataListenerService.ACTION_SENDLOCATIONUPDATE));
#endif
                        }
                        else
                        {
                            await Settings.UpdateLocationWithKey(location, oldKey);
                        }
#else
                        Settings.SaveHomeData(location);
#endif

#if WINDOWS_UWP
                        // Update tile id for location
                        if (SecondaryTileUtils.Exists(oldKey))
                        {
                            await SecondaryTileUtils.UpdateTileId(oldKey, location.query);
                        }
#elif __ANDROID__ && !__ANDROID_WEAR__
                        if (WidgetUtils.Exists(oldKey))
                        {
                            WidgetUtils.UpdateWidgetIds(oldKey, location);
                        }
#endif
                    }

                    await GetWeatherData();
                }
                catch (WeatherException wEx)
                {
                    errorCallback?.OnWeatherError(wEx);
                }
            }
        }