private async Task LoadLocations() { // Disable EditMode button EditButton.IsEnabled = false; // Lets load it up... var locations = await Settings.GetFavorites(); LocationPanels.Clear(); // Setup saved favorite locations await LoadGPSPanel(); foreach (LocationData location in locations) { var panel = new LocationPanelViewModel() { // Save index to tag (to easily retreive) LocationData = location }; LocationPanels.Add(panel); } foreach (LocationData location in locations) { var wLoader = new WeatherDataLoader(location, this, this); await wLoader.LoadWeatherData(false); } // Enable EditMode button EditButton.IsEnabled = true; }
private async void Location_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args) { LocationQueryViewModel query_vm = null; if (args.ChosenSuggestion != null) { // User selected an item from the suggestion list, take an action on it here. var theChosenOne = args.ChosenSuggestion as LocationQueryViewModel; if (!String.IsNullOrEmpty(theChosenOne.LocationQuery)) { query_vm = theChosenOne; } else { query_vm = new LocationQueryViewModel(); } } else if (!String.IsNullOrEmpty(args.QueryText)) { // Use args.QueryText to determine what to do. var result = (await wm.GetLocations(args.QueryText)).First(); if (result != null && !String.IsNullOrWhiteSpace(result.LocationQuery)) { sender.Text = result.LocationName; query_vm = result; } else { query_vm = new LocationQueryViewModel(); } } else if (String.IsNullOrWhiteSpace(args.QueryText)) { // Stop since there is no valid query return; } if (String.IsNullOrWhiteSpace(query_vm.LocationQuery)) { // Stop since there is no valid query return; } // Cancel other tasks cts.Cancel(); cts = new CancellationTokenSource(); var ctsToken = cts.Token; LoadingRing.IsActive = true; if (ctsToken.IsCancellationRequested) { LoadingRing.IsActive = false; return; } // Check if location already exists var locData = await Settings.GetLocationData(); if (locData.Exists(l => l.query == query_vm.LocationQuery)) { LoadingRing.IsActive = false; ShowAddLocationsPanel(false); return; } if (ctsToken.IsCancellationRequested) { LoadingRing.IsActive = false; return; } var location = new LocationData(query_vm); if (!location.IsValid()) { await Toast.ShowToastAsync(App.ResLoader.GetString("WError_NoWeather"), ToastDuration.Short); LoadingRing.IsActive = false; return; } var weather = await Settings.GetWeatherData(location.query); if (weather == null) { try { weather = await wm.GetWeather(location); } catch (WeatherException wEx) { weather = null; await Toast.ShowToastAsync(wEx.Message, ToastDuration.Short); } } if (weather == null) { LoadingRing.IsActive = false; return; } // We got our data so disable controls just in case sender.IsSuggestionListOpen = false; // Save data await Settings.AddLocation(location); if (wm.SupportsAlerts && weather.weather_alerts != null) { await Settings.SaveWeatherAlerts(location, weather.weather_alerts); } await Settings.SaveWeatherData(weather); var panelView = new LocationPanelViewModel(weather) { LocationData = location }; // Set properties if necessary if (EditMode) { panelView.EditMode = true; } // Add to collection LocationPanels.Add(panelView); // Hide add locations panel LoadingRing.IsActive = false; ShowAddLocationsPanel(false); }