예제 #1
0
        protected override void OnBindDetailsViewHolder(WeatherDailyItemViewHolderBase viewHolder, int position, WeatherDataPointHourly[] hourly)
        {
            if (viewHolder == null)
            {
                return;
            }

            base.OnBindDetailsViewHolder(viewHolder, position, hourly);

            var holder  = (WeatherDailyItemViewHolder)viewHolder;
            var context = holder.ItemView.Context;

            var pressureText = string.Empty;
            var minPressure  = hourly?.Min(p => p.Pressure);
            var maxPressure  = hourly?.Max(p => p.Pressure);

            if (minPressure.HasValue && maxPressure.HasValue)
            {
                if (hourly.Length == 1 || (int)minPressure.Value == (int)maxPressure.Value)
                {
                    pressureText = WeatherTools.ConvertPressureToString(minPressure.Value, "{1} {0:f0} {2}",
                                                                        context.GetString(Resource.String.Pressure), WeatherTools.PressureUnitString);
                }
                else
                {
                    pressureText = WeatherTools.ConvertPressureToString(minPressure.Value, maxPressure.Value,
                                                                        "{2} {0:f0}-{1:f0} {3}", context.GetString(Resource.String.Pressure),
                                                                        WeatherTools.PressureUnitString);
                }
            }
            if (holder.Pressure != null)
            {
                holder.Pressure.Text       = pressureText;
                holder.Pressure.Visibility = IsVisible(pressureText);
            }

            var humidityText = string.Empty;
            var minHumidity  = hourly?.Min(p => p.Humidity);
            var maxHumidity  = hourly?.Max(p => p.Humidity);

            if (minHumidity.HasValue && maxHumidity.HasValue)
            {
                var txthumidity = context.GetString(Resource.String.Humidity);
                humidityText = hourly.Length == 1 || (int)(minHumidity * 100) == (int)(maxHumidity * 100)
                    ? WeatherTools.Format("{0} {1:p0}", txthumidity, minHumidity)
                    : WeatherTools.Format("{0} {1:f0}-{2:p0}", txthumidity, minHumidity * 100, maxHumidity);
            }
            if (holder.Humidity != null)
            {
                holder.Humidity.Text       = humidityText;
                holder.Humidity.Visibility = IsVisible(humidityText);
            }
        }
예제 #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);
        }