Beispiel #1
0
        public async Task LoadWeatherForModels(ObservableCollection <CityTableCellViewModel> citiesModels)
        {
            using (var httpClient = new HttpClient())
            {
                foreach (var cityModel in citiesModels)
                {
                    try
                    {
                        string url  = UrlResolver.ResolveWeatherApiUrl(cityModel.WeatherId);
                        string json = await httpClient.GetStringAsync(url);

                        var data         = JObject.Parse(json);
                        int temp         = data["main"]["temp"].Value <int>();
                        var selectedCity = Cities.First((item) => item.WeatherId == cityModel.WeatherId);
                        selectedCity.CurrentWeather = Convert.ToString(temp) + " °C";
                        selectedCity.WindDegree     = data["wind"]["deg"].Value <int>();
                        cityModel.Weather           = selectedCity.CurrentWeather;
                        var wind = WindDirection.GetWindDirection(selectedCity.WindDegree);
                        cityModel.WindDirectionImageSource = WindDirection.GetWindImageDirection(wind);
                        cityModel.UpdateWeather();
                    }
                    catch
                    {
                        if (cityModel.WindDirectionImageSource == null)
                        {
                            cityModel.WindDirectionImageSource = WindDirection.GetNotLoadedImageDirection();
                            cityModel.UpdateWeather();
                        }
                    }
                }
            }
            WeatherUpdated(this, new EventArgs());
        }