Beispiel #1
0
        public async Task LoadActualAndDailyWeatherInfoForSevenDays()
        {
            if (IsBusy)
            {
                return;
            }

            DailyForecastList.Clear();
            IsBusy = true;
            try
            {
                var result = await HttpConnection.GetActualWeatherInfoForLocationAsync(geoModel.Location, "apiKey");

                if (result != null)
                {
                    Location             = result.Name;
                    DescriptionOfWeather = result.Weather.First().Description;
                    DateAndTime          = DateAndTimeHelper.ConvertToDateAndTime(result.Dt, DateAndTimeHelper.TypeForDateAndTimeFormat.COMMON_DATETIME);
                    SunriseTime          = DateAndTimeHelper.ConvertToDateAndTime(result.Sys.Sunrise, DateAndTimeHelper.TypeForDateAndTimeFormat.TIME);
                    SunsetTime           = DateAndTimeHelper.ConvertToDateAndTime(result.Sys.Sunset, DateAndTimeHelper.TypeForDateAndTimeFormat.TIME);
                    Temperature          = Math.Round(result.Main.Temp.Value, 1).ToString();
                    FeelsLikeTemperature = Math.Round(result.Main.FeelsLike.Value, 1).ToString();
                    Humidity             = result.Main.Humidity;
                    WindSpeed            = WindHelper.MpsToKmph(result.Wind.Speed.Value);
                    Pressure             = result.Main.Pressure;
                    Clouds        = result.Clouds.All;
                    WindDirection = WindHelper.DegreesToGeogrpahicalDirections(result.Wind.Deg.Value);
                    Icon          = HttpConnection.GetIconForWeather(result.Weather.First().Icon);
                }
                else
                {
                    await App.Current.MainPage.DisplayAlert("Błąd", "Coś poszło nie tak.", "OK");
                }

                var resultForDailyWeatherInfo = await HttpConnection.GetDailyWeatherInfoForSevenDaysAsync(geoModel.Lat, geoModel.Lon, "apiKey");

                if (resultForDailyWeatherInfo.Daily != null)
                {
                    foreach (var item in resultForDailyWeatherInfo.Daily)
                    {
                        DailyForecastList.Add(new DailyForecastForSevenDaysModel
                        {
                            Dt           = DateAndTimeHelper.ConvertToDateAndTime(item.Dt, DateAndTimeHelper.TypeForDateAndTimeFormat.COMMON_DATETIME),
                            DayTemp      = Math.Round(item.Temp.Day.Value, 1).ToString(),
                            NightTemp    = Math.Round(item.Temp.Night.Value, 1).ToString(),
                            MinTemp      = Math.Round(item.Temp.Min.Value, 1).ToString(),
                            MaxTemp      = Math.Round(item.Temp.Max.Value, 1).ToString(),
                            FeelsTempDay = Math.Round(item.FeelsLike.Day.Value, 1).ToString(),
                            Humidity     = item.Humidity,
                            Sunrise      = DateAndTimeHelper.ConvertToDateAndTime(item.Sunrise, DateAndTimeHelper.TypeForDateAndTimeFormat.TIME),
                            Sunset       = DateAndTimeHelper.ConvertToDateAndTime(item.Sunset, DateAndTimeHelper.TypeForDateAndTimeFormat.TIME),
                            WindDeg      = WindHelper.DegreesToGeogrpahicalDirections(item.WindDeg.Value),
                            WindSpeed    = WindHelper.MpsToKmph(item.WindSpeed.Value),
                            Rain         = string.IsNullOrEmpty(item.Rain) ? "0" : item.Rain,
                            Icon         = HttpConnection.GetIconForWeather(item.Weather.First().Icon),
                            Clouds       = item.Clouds,
                            Pressure     = item.Pressure,
                            Description  = item.Weather.First().Description
                        });
                    }
                }
                else
                {
                    await App.Current.MainPage.DisplayAlert("Błąd", "Coś poszło nie tak.", "OK");
                }
            }

            catch (Exception ex)
            {
                await App.Current.MainPage.DisplayAlert("Błąd", "Coś poszło nie tak. Być może nie włączono Internetu.", "OK");

                System.Diagnostics.Process.GetCurrentProcess().CloseMainWindow();
            }
            finally
            {
                IsBusy = false;
            }
        }