Пример #1
0
        public async Task Load48HoursWeatherInfo()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                await GeolocationHelper.GetGeolocationInfo(geoModel);

                var result = await HttpConnection.Get48HoursWeatherInfoAsync(geoModel.Lat, geoModel.Lon, "apiKey");

                if (result.Hourly != null)
                {
                    foreach (var item in result.Hourly)
                    {
                        Hourly48ForecastList.Add(new Forecast48HoursModel
                        {
                            Dt                      = DateAndTimeHelper.ConvertToDateAndTime(item.Dt, DateAndTimeHelper.TypeForDateAndTimeFormat.TIME),
                            DateAndTime             = DateAndTimeHelper.ConvertToDateAndTime(item.Dt, DateAndTimeHelper.TypeForDateAndTimeFormat.DATE_AND_TIME_FOR_48FORECAST),
                            Temp                    = Math.Round(item.Temp.Value, 1).ToString(),
                            Icon                    = HttpConnection.GetIconForWeather(item.Weather.First().Icon),
                            WindDeg                 = WindHelper.DegreesToGeogrpahicalDirections(item.WindDeg.Value),
                            GroupName               = DateAndTimeHelper.ConvertToDateAndTime(item.Dt, DateAndTimeHelper.TypeForDateAndTimeFormat.COMMON_DATETIME),
                            ColorOfTemperatureLabel = item.Weather.First().Icon.Contains("n")?"#000000": "#008000",
                            Humidity                = item.Humidity,
                            WindSpeed               = WindHelper.MpsToKmph(item.WindSpeed.Value),
                            Rain                    = string.IsNullOrEmpty(item.Rain) ? "0" : item.Rain,
                            Clouds                  = item.Clouds,
                            Pressure                = item.Pressure,
                            Description             = item.Weather.First().Description,
                            FeelsTemp               = Math.Round(item.FeelsLike.Value, 1).ToString()
                        });
                    }

                    List <string> groupNamesList = new List <string>();

                    foreach (var item in Hourly48ForecastList)
                    {
                        groupNamesList.Add(item.GroupName);
                    }

                    var distinctGroupNamesList = groupNamesList.Distinct().ToList();

                    foreach (var item in distinctGroupNamesList)
                    {
                        GroupedHourly48ForecastList.Add(new GroupingForListClass <string, Forecast48HoursModel>(item, Hourly48ForecastList.Where(x => x.GroupName == item)));
                    }
                }
                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.", "OK");
            }
            finally
            {
                IsBusy = false;
            }
        }
Пример #2
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;
            }
        }