Example #1
0
        public async Task SetLocation(string location, double latitude, double longitude, RemoteViews widgetView)
        {
            var weather = Settings.Settings.Instance.GetWeather(latitude, longitude);

            if (weather is null)
            {
                try
                {
                    weather = await OpenWeather.GetWeather(latitude, longitude, OpenWeather.Units.Metric);

                    Settings.Settings.Instance.UpdatePlace(latitude, longitude, weather);
                }
                catch (Exception)
                {
                    weather = Settings.Settings.Instance.GetWeatherFull(latitude, longitude);
                    if (weather is null)
                    {
                        return;
                    }
                }
            }
            widgetView.SetTextViewText(Resource.Id.Temperature, FormatTemperature(weather.Current.Temperature));
            widgetView.SetTextViewText(Resource.Id.Sunrise, string.Format(Resources.Translations.GetSunriseText(),
                                                                          UnixTimeStampToDateTimeConverter.Convert(weather.Current.Sunrise).ToString("t")));
            widgetView.SetTextViewText(Resource.Id.Sunset, string.Format(Resources.Translations.GetSunsetText(),
                                                                         UnixTimeStampToDateTimeConverter.Convert(weather.Current.Sunset).ToString("t")));
            widgetView.SetTextViewText(Resource.Id.Pressure, weather.Current.Pressure.ToString("F0") + "hPa");
            widgetView.SetTextViewText(Resource.Id.WindSpeed, FormatWindSpeed(weather.Current.WindSpeed));

            if (weather.Current.WeatherList.Count > 0)
            {
                SetWeatherIcon(widgetView, weather.Current.WeatherList[0].Icon);
                widgetView.SetTextViewText(Resource.Id.Description, weather.Current.WeatherList[0].Description.FirstCharToUpper());
            }
        }
Example #2
0
        public async void SetLocation(string location, double latitude, double longitude)
        {
            IsLoading        = true;
            CurrentLocation  = location;
            Latitude         = latitude;
            Longitude        = longitude;
            OlderDataVisible = false;

            var weather = Settings.Settings.Instance.GetWeather(latitude, longitude);

            if (weather is null)
            {
                try
                {
                    weather = await OpenWeather.GetWeather(latitude, longitude, OpenWeather.Units.Metric);

                    Settings.Settings.Instance.UpdatePlace(latitude, longitude, weather);
                }
                catch (Exception)
                {
                    weather = Settings.Settings.Instance.GetWeatherFull(latitude, longitude);
                    if (weather is null)
                    {
                        IsLoading = false;
                        return;
                    }

                    OlderDataVisible = true;
                    OlderDataText    = string.Format(Resources.AppTranslations.OldDataFrom,
                                                     Settings.Settings.Instance.GetWeatherRefreshTime(latitude, longitude)?.ToString("g"));
                }
            }


            DateText = DateTime.Now.ToString("d");
            if (weather.Current.WeatherList.Count > 0)
            {
                WeatherIcon        = StringToIconConverter.Convert(weather.Current.WeatherList[0].Icon);
                WeatherDescription = weather.Current.WeatherList[0].Description.FirstCharToUpper();
            }

            WeatherTemperature = FormatTemperature(weather.Current.Temperature);
            if (weather.Current.Rain != null)
            {
                var max = Math.Max(weather.Current.Rain.ThreeHours, weather.Current.Rain.OneHour);
                WeatherRain = max.ToString("F0") + "mm";
            }
            if (weather.Current.Snow != null)
            {
                var max = Math.Max(weather.Current.Snow.ThreeHours, weather.Current.Snow.OneHour);
                WeatherRain = max.ToString("F0") + "mm";
            }

            WeatherWind = FormatWindSpeed(weather.Current.WindSpeed);

            WeatherPressure = weather.Current.Pressure.ToString("F0") + "hPa";
            WeatherSunrise  = string.Format(AppTranslations.Sunrise,
                                            UnixTimeStampToDateTimeConverter.Convert(weather.Current.Sunrise).ToString("t"));
            WeatherSunset = string.Format(AppTranslations.Sunset,
                                          UnixTimeStampToDateTimeConverter.Convert(weather.Current.Sunset).ToString("t"));

            HoursForecastItems.Clear();
            foreach (var hourlyWeather in weather.Hourly)
            {
                var forecast = new HourForecast()
                {
                    Time = UnixTimeStampToDateTimeConverter.Convert(hourlyWeather.Datetime)
                           .ToString("t"),
                    Temperature = FormatTemperature(hourlyWeather.Temperature)
                };
                if (hourlyWeather.WeatherList.Count > 0)
                {
                    forecast.IconSource = StringToIconConverter.Convert(hourlyWeather.WeatherList[0].Icon);
                }

                HoursForecastItems.Add(forecast);
                if (HoursForecastItems.Count >= 24)
                {
                    break;
                }
            }

            DaysForecastItems.Clear();
            foreach (var dailyWeather in weather.Daily)
            {
                var forecast = new DayForecast()
                {
                    Temperature = FormatTemperature(dailyWeather.Temperature.Day),
                    Time        = UnixTimeStampToDateTimeConverter.Convert(dailyWeather.Datetime + weather.TimezoneOffset)
                                  .ToString("ddd"),
                    Rain = (dailyWeather.Rain + dailyWeather.Snow).ToString("F0") + "mm",
                };
                if (dailyWeather.WeatherList.Count > 0)
                {
                    forecast.IconSource = StringToIconConverter.Convert(dailyWeather.WeatherList[0].Icon);
                }

                DaysForecastItems.Add(forecast);

                if (DaysForecastItems.Count >= 7)
                {
                    break;
                }
            }

            RefreshFavoritePlaces();
            IsLoading = false;
        }