public static async Task <BarDataModel> RequestBarsAroundCoords(double latitude, double longitude, double radius) { var result = new BarDataModel(); var failedToConnectCounter = 0; var providerCount = _providerList.Count; foreach (IBeerable provider in _providerList) { try { var barsFromProvider = await CollectBarsFromProvider(provider, latitude, longitude, radius); if (barsFromProvider != null) { result.AddRange(barsFromProvider); } } catch (HttpRequestException) { if (++failedToConnectCounter == providerCount) { throw; } } catch (WebException) { Console.Write("Provider \"{0}\" is down\n", provider.ProviderName); // Provider is down, lets just ignore it } } var dbManager = new DatabaseManager(); dbManager.SaveBars(result); result.AddRange(dbManager.GetAllBarData(result.Select(x => x.BarId))); result.RemoveBarsOutsideRadius(radius); result.RemoveDuplicates(); return(result); }
private async void GoButton_Click(object sender, EventArgs e) { var latitude = GetLatitude(); var longitude = GetLongitude(); var radius = GetRadius(); var failedToConnectCounter = 0; var providerCount = _providerList.Count; try { var barsFromProvider = new List <BarData>(); var progressStep = 100 / providerCount; var result = new BarDataModel(); var currentProgressValue = 0; GoButton.Enabled = false; InitiateProgressBars(); UpdateProgressBars(currentProgressValue); foreach (IBeerable provider in _providerList) { try { barsFromProvider = await CollectBarsFromProvider(provider, latitude, longitude, radius); } catch (HttpRequestException) { if (++failedToConnectCounter == providerCount) { MessageBox.Show("Check your internet connection, it seems to be down."); } } catch (WebException) { MessageBox.Show("Failed connecting to: " + provider.ProviderName); } result.AddRange(barsFromProvider); currentProgressValue += progressStep; UpdateProgressBars(currentProgressValue); } result.RemoveDuplicates(); result.RemoveBarsOutsideRadius(radius); await Task.Run(() => result = (BarDataModel)WebApiAccess.GetAllBarData(result)); HideProgressBars(); // Display _barRating.BarsData = result; var currentLocation = GetCurrentLocation(); if (result != null) { foreach (var bar in _barRating.BarsData) { bar.DistanceToCurrentLocation = currentLocation.GetDistanceTo(new GeoCoordinate(bar.Latitude, bar.Longitude)); } SortList(CompareType.Distance); } } catch (ArgumentsForProvidersException) { MessageBox.Show("Please enter the required data correctly. Erroneus data is painted red."); } finally { HideProgressBars(); GoButton.Enabled = true; } }