private async void FetchNewData() { var location = await Settings.GetFavoriteLocation(); if (location == null || string.IsNullOrEmpty(location.Id)) { var granted = await GetLocationPermission(); if (!granted) { ShowNoAccessView(); return; } ShowLoadingView(); var position = await GetPosition(); SetCurrentCity(); location = new LocationItem() { Latitude = position.Latitude, Longitude = position.Longitude }; } else { ShowLoadingView(); SetCurrentCity(); } await FetchCurrentForecast(location); ShowLocalizationSuccess(); TileDesigner.UpdatePrimary(_lastPosition); Settings.CacheForecastData(_pageDataSource.Forecast); Settings.CacheLocationAndTime( _currentTown, DateTime.Now.ToLocalTime().ToString("dddd HH:mm")); Timer deffered = null; deffered = new Timer(async(object state) => { await _UIDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (LoadingView.Visibility == Visibility.Collapsed) { deffered?.Dispose(); return; } HideLoadingView(); PopulateView(); PopulateCurrentLocationAndTime(); }); }, new AutoResetEvent(true), 3000, 2000); }
/// <summary> /// Find the LocationItem from storage and get new data for the specific location. /// If the item isn't found, abort and get new data for the current lcoation. /// (Handle no connectivity) /// </summary> /// <param name="locationId"></param> private async void FetchNewDataFromLocation(string locationId) { if (!NetworkInterface.GetIsNetworkAvailable()) { LoadCachedData(); return; } var locationsList = await Settings.GetSavedLocationAsync(); var locationName = locationId.Replace(".", ", "); LocationItem locationToFind = null; foreach (var location in locationsList) { if (location.Name == locationName) { locationToFind = location; break; } } if (locationToFind == null) { InitializePageData(); return; } ShowLoadingView(); await FetchCurrentForecast(locationToFind); HideLoadingView(); PopulateView(); PopulateCurrentLocationAndTime(); TownTextBlock.Text = locationToFind.Town; TileDesigner.UpdatePrimary(_lastPosition); Settings.CacheForecastData(_pageDataSource.Forecast); Settings.CacheLocationAndTime( _currentTown, DateTime.Now.ToLocalTime().ToString("dddd HH:mm")); }
public async void Run(IBackgroundTaskInstance taskInstance) { StartTask(taskInstance); string city = ""; LocationItem location = null; if (GetTaskType() == _GPSTaskTypeKey) { var position = await GetPosition(); if (position == null) { EndTask(); return; } var coord = position.Coordinate.Point.Position; location = new LocationItem() { Latitude = coord.Latitude, Longitude = coord.Longitude }; city = await GetCityName(coord); } else { location = Settings.GetFavoriteLocation(); city = location?.Town; } var forecast = await FetchCurrentWeather(location.Latitude, location.Longitude); TileDesigner.UpdatePrimary(forecast, city); LogTaskActivity(); EndTask(); }