protected virtual void UpdateWeatherPart(Context context, RemoteViews view, AppWidgetSettings settings, bool isDemoMode)
        {
            var weather = settings.Weather;
            var utcNow  = DateTime.UtcNow;

            if (isDemoMode && weather == null)
            {
                var weatherDataPoint = new WeatherDataPoint
                {
                    Date           = utcNow,
                    Temperature    = 0,
                    MinTemperature = -1,
                    MaxTemperature = 1,
                    WeatherCode    = WeatherCodes.ClearSky,
                    Condition      = context.GetString(Resource.String.DemoCondition)
                };

                weather = new WeatherForecast(providerId: settings.WeatherProviderId, latitude: null, longitude: null,
                                              units: AppSettings.Default.Units, languageCode: AppSettings.Default.Language, link: null,
                                              hasIcons: false)
                {
                    Currently = weatherDataPoint,
                    Daily     = new[] { weatherDataPoint }
                };
            }

            if (weather?.MaxPublishedDate == null)
            {
                return;
            }

            var actualUtcDate = utcNow > weather.MaxPublishedDate ? weather.MaxPublishedDate.Value : utcNow;
            var currently     = weather.FindActualCurrentlyAsync(actualUtcDate).Result;

            if (currently == null)
            {
                return;
            }

            var temperature = string.Empty;

            using (var weatherTools = new WeatherTools
            {
                ProviderUnits = weather.Units,
            })
            {
                var degree = weatherTools.DegreeString;
                if (currently.Temperature.HasValue)
                {
                    temperature = weatherTools.ConvertTemperatureToString(currently.Temperature.Value, "{0:f0}{1}",
                                                                          degree);
                }
                SetTextFormatted(view, Resource.Id.txtTemp, temperature,
                                 weatherTools.IsTemperatureAlerted(currently.Temperature));

                var minTempText = string.Empty;
                var maxTempText = string.Empty;
                var visibility  = ViewStates.Gone;
                var minTemp     = weatherTools
                                  .CalculateMinTemperatureAsync(actualUtcDate, weather, currently.MinTemperature).Result;
                var maxTemp = weatherTools
                              .CalculateMaxTemperatureAsync(actualUtcDate, weather, currently.MaxTemperature).Result;
                if (minTemp.HasValue && maxTemp.HasValue)
                {
                    minTempText = weatherTools.ConvertTemperatureToString(minTemp.Value, "{0:f0}{1}", degree);
                    maxTempText = weatherTools.ConvertTemperatureToString(maxTemp.Value, "{0:f0}{1}", degree);
                    visibility  = ViewStates.Visible;
                }
                SetTextFormatted(view, Resource.Id.txtLow, minTempText, weatherTools.IsTemperatureAlerted(minTemp));
                view.SetViewVisibility(Resource.Id.txtLowIndicator, visibility);
                SetTextFormatted(view, Resource.Id.txtHigh, maxTempText, weatherTools.IsTemperatureAlerted(maxTemp));
                view.SetViewVisibility(Resource.Id.txtHighIndicator, visibility);

                var conditionIconId = weatherTools.GetConditionIconId(WidgetTypes.AppWidget, settings.WidgetIconStyle,
                                                                      currently, true, true);
                view.SetImageViewResource(Resource.Id.imgCondition, conditionIconId);

                //"Небольшой снегопад "
                SetTextFormatted(view, Resource.Id.txtCondition,
                                 (currently.Condition ?? string.Empty).Trim().ToCapital(),
                                 weatherTools.IsConditionExtreme(currently.WeatherCode));
            }

            var locality = (settings.LocationAddress?.GetDisplayLocality() ?? string.Empty).Trim();

            if (string.IsNullOrEmpty(locality) && isDemoMode)
            {
                locality = context.GetString(settings.UseTrackCurrentLocation
                    ? Resource.String.CurrentLocationEmpty
                    : Resource.String.DemoLocality);
            }
            view.SetTextViewText(Resource.Id.txtLocation, locality);
        }
Exemplo n.º 2
0
        public void UpdateView(int?weatherProviderNameResourceId, double?locationLatitude, double?locationLongitude,
                               WeatherForecast weather, IWeatherDataPoint currently, WeatherDataPointDaily[] daily, out bool isAlerted)
        {
            isAlerted    = false;
            _weatherLink = null;

            if (View == null || Activity == null)
            {
                return;
            }

            if (_txtLogoWeather != null)
            {
                if (weatherProviderNameResourceId.HasValue)
                {
                    _txtLogoWeather.SetText(weatherProviderNameResourceId.Value);
                }
                else
                {
                    _txtLogoWeather.Text = string.Empty;
                }
            }

            if (_weatherTools == null)
            {
                _weatherTools = new WeatherTools();
            }

            if (weather != null)
            {
                _weatherTools.ProviderUnits = weather.Units;
            }

            if (_txtLogoWeather != null && !string.IsNullOrEmpty(weather?.Link))
            {
                _weatherLink = weather.Link;
                SubscribeEvents();
            }

            GetWeatherDate(currently?.Date, weather?.UpdatedDate, out string publishedDateText,
                           out string updatedDateText);

            float?locationDistance = null;

            if (locationLatitude.HasValue && locationLongitude.HasValue && weather?.Latitude != null &&
                weather.Longitude != null)
            {
                var distance = new float[1];
                Location.DistanceBetween(locationLatitude.Value, locationLongitude.Value,
                                         weather.Latitude.Value, weather.Longitude.Value, distance);
                locationDistance = Math.Abs(distance[0]);
            }

            UpdateTooltip(publishedDateText, updatedDateText, locationDistance,
                          weather == null ? null : AppSettings.Default.GetRequestCounter(weather.ProviderId)?.Count,
                          AppSettings.Default.UseGoogleMapsGeocodingApi
                    ? AppSettings.Default.GetRequestCounter(AppSettings.GoogleMapsGeocodingApiProviderId)?.Count
                    : null);

            Java.Lang.ICharSequence tempText = new SpannableString(string.Empty);
            var degree  = _weatherTools.DegreeString;
            var txtTemp = View.FindViewById <TextView>(Resource.Id.txtTemp);

            txtTemp.SetCompoundDrawables(null, null, null, null);

            if (currently?.Temperature != null)
            {
                tempText = _weatherTools.ConvertTemperatureToAlertedStyle(Activity, currently.Temperature.Value,
                                                                          "{0:f1}{1}{2}",
                                                                          degree,
                                                                          _weatherTools.TemperatureUnitString);

                if (_weatherTools.IsTemperatureAlerted(currently.Temperature.Value))
                {
                    isAlerted = true;
                    var drawable = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.alert, Activity.Theme);
                    var px       = (int)Resources.GetDimension(Resource.Dimension.alertImageDimen);
                    drawable.SetBounds(0, 0, px, px);
                    //blinkingAnimation.SetBounds(0, 0, blinkingAnimation.IntrinsicWidth, blinkingAnimation.IntrinsicHeight);
                    txtTemp.SetCompoundDrawables(null, null, drawable, null);
                }
            }
            txtTemp.TextFormatted = tempText;

            var imgCondition = View.FindViewById <ImageView>(Resource.Id.imgCondition);

            if (imgCondition != null)
            {
                if (currently == null)
                {
                    imgCondition.SetImageResource(Android.Resource.Color.Transparent);
                }
                else
                {
                    imgCondition.SetImageDrawable(
                        _weatherTools.GetConditionIcon(WidgetTypes.Widget, null, currently, true, true));
                }
            }

            var txtCondition = View.FindViewById <TextView>(Resource.Id.txtCondition);

            txtCondition.SetCompoundDrawables(null, null, null, null);

            var conditionText          = (currently?.Condition ?? string.Empty).Trim().ToCapital();
            var conditionTextFormatted = new SpannableString(conditionText);

            if (_weatherTools.IsConditionExtreme(currently?.WeatherCode))
            {
                isAlerted = true;
                var drawable = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.alert, Activity.Theme);
                var px       = (int)Resources.GetDimension(Resource.Dimension.alertImageDimen);
                drawable.SetBounds(0, 0, px, px);
                //blinkingAnimation.SetBounds(0, 0, blinkingAnimation.IntrinsicWidth, blinkingAnimation.IntrinsicHeight);
                txtCondition.SetCompoundDrawables(null, null, drawable, null);
                conditionTextFormatted.SetSpan(
                    new TextAppearanceSpan(Activity, Resource.Style.conditionAlertedTextStyle), 0, conditionText.Length,
                    SpanTypes.ExclusiveExclusive);
            }
            txtCondition.TextFormatted = conditionTextFormatted;

            var txtWind = View.FindViewById <TextView>(Resource.Id.txtWind);

            txtWind.Visibility = currently?.WindDirection != null || currently?.WindSpeed != null
                ? ViewStates.Visible
                : ViewStates.Gone;
            var imgWindDirection  = View.FindViewById <ImageView>(Resource.Id.imgWindDirection);
            var txtWindDirection  = View.FindViewById <TextView>(Resource.Id.txtWindDirection);
            var windDirectionText = string.Empty;

            if (currently?.WindDirection != null)
            {
                txtWind.Visibility          = ViewStates.Visible;
                imgWindDirection.Visibility = ViewStates.Visible;
                imgWindDirection.SetImageBitmap(_weatherTools.WindDirectionDrawable(currently.WindDirection.Value));
                windDirectionText = _weatherTools.WindDirectionToCardinal(currently.WindDirection.Value);
            }
            else
            {
                imgWindDirection.Visibility = ViewStates.Gone;
            }
            if (txtWindDirection != null)
            {
                txtWindDirection.Text = windDirectionText;
            }

            var txtWindSpeed  = View.FindViewById <TextView>(Resource.Id.txtWindSpeed);
            var windSpeedText = string.Empty;

            if (currently?.WindSpeed != null)
            {
                windSpeedText = _weatherTools.ConvertWindSpeedToString(currently.WindSpeed.Value, "{0:f1} {1}",
                                                                       _weatherTools.WindSpeedUnitString);
            }
            txtWindSpeed.Text = windSpeedText;

            var txtPressure  = View.FindViewById <TextView>(Resource.Id.txtPressure);
            var pressureText = string.Empty;

            if (currently?.Pressure != null)
            {
                pressureText = _weatherTools.ConvertPressureToString(currently.Pressure.Value, "{1} {0:f0} {2}",
                                                                     GetString(Resource.String.Pressure), _weatherTools.PressureUnitString);
            }
            txtPressure.Text = pressureText;

            var txtHumidity = View.FindViewById <TextView>(Resource.Id.txtHumidity);
            var humidity    = string.Empty;

            if (currently?.Humidity != null)
            {
                humidity = _weatherTools.Format("{0} {1:p0}", GetString(Resource.String.Humidity),
                                                currently.Humidity);
            }
            txtHumidity.Text = humidity;

            var txtApparentTemp = View.FindViewById <TextView>(Resource.Id.txtApparentTemp);
            var apparentTemp    = string.Empty;

            if (currently?.ApparentTemperature != null)
            {
                apparentTemp = _weatherTools.ConvertTemperatureToString(currently.ApparentTemperature.Value,
                                                                        "{1} {0:f0}{2}", GetString(Resource.String.ApparentTemperature), degree);
            }
            txtApparentTemp.Text = apparentTemp;

            var txtVisibility = View.FindViewById <TextView>(Resource.Id.txtVisibility);
            var valueText     = string.Empty;

            if (currently?.Visibility != null)
            {
                valueText = _weatherTools.ConvertVisibilityToString(currently.Visibility.Value, "{1} {0:f0} {2}",
                                                                    GetString(Resource.String.Visibility), _weatherTools.VisibilityUnitString);
            }
            else if (currently?.DewPoint != null)
            {
                valueText = _weatherTools.ConvertTemperatureToString(currently.DewPoint.Value, "{1} {0:f0}{2}",
                                                                     GetString(Resource.String.DewPoint), degree);
            }
            txtVisibility.Text = valueText;

            var txtSunInfo = View.FindViewById <TextView>(Resource.Id.txtSunInfo);
            var sunInfo    = string.Empty;

            if (currently?.Astronomy?.Sunrise != null && currently.Astronomy.Sunset.HasValue)
            {
                sunInfo = _weatherTools.Format("{0} {1:t} {2} {3:t}", GetString(Resource.String.Sunrise),
                                               currently.Astronomy.Sunrise.Value.ToLocalTime(), GetString(Resource.String.Sunset),
                                               currently.Astronomy.Sunset.Value.ToLocalTime());
            }

            var landOrientation = AppSettings.Default.LandOrientation;

            if (!landOrientation && string.IsNullOrEmpty(valueText))
            {
                txtVisibility.Text    = sunInfo;
                txtSunInfo.Visibility = ViewStates.Gone;
            }
            else
            {
                txtSunInfo.Text       = sunInfo;
                txtSunInfo.Visibility = ViewStates.Visible;
            }

            if (_gridWeatherByDay == null)
            {
                return;
            }

            LayoutManagerDispose();
            WeatherDailyItemAdapterDispose();

            if (landOrientation)
            {
                _layoutManager = new LinearLayoutManager(Activity, LinearLayoutManager.Horizontal, false);
                var scrollView         = View.FindViewById <ScrollView>(Resource.Id.viewScroll);
                var viewDetailsContent = View.FindViewById <View>(Resource.Id.viewDetailsContent);
                if (viewDetailsContent != null && viewDetailsContent.Handle != IntPtr.Zero)
                {
                    viewDetailsContent.Visibility = ViewStates.Gone;
                }
                _weatherDailyItemAdapter =
                    new WeatherDailyItemAdapterLand(daily, _weatherTools, viewDetailsContent, scrollView);
            }
            else
            {
                _layoutManager           = new LinearLayoutManager(Activity, LinearLayoutManager.Vertical, false);
                _weatherDailyItemAdapter = new WeatherDailyItemAdapter(daily, _weatherTools);
            }

            _gridWeatherByDay.SetLayoutManager(_layoutManager);
            _gridWeatherByDay.SetAdapter(_weatherDailyItemAdapter);
        }