コード例 #1
0
        protected override void OnBindContentViewHolder(RecyclerView.ViewHolder viewHolder, int position)
        {
            if (Daily == null)
            {
                return;
            }

            var holder          = (WeatherDailyItemViewHolder)viewHolder;
            var context         = holder.ItemView.Context;
            var item            = Daily[position];
            var isTodayPosition = false;

            var dateText = string.Empty;
            var lineBackgroundColorId = Resource.Color.lineBackgroundColor;
            var dateTextColorId       = Resource.Color.dateTextColor;
            var existsDate            = holder.Date != null;

            if (item.Date.HasValue)
            {
                var itemDate = item.Date.Value.ToLocalTime();
                isTodayPosition = itemDate.Date == DateTime.Today;
                if (existsDate)
                {
                    dateText = WeatherTools.Format("{0:ddd}, {1}", itemDate,
                                                   isTodayPosition
                            ? context.GetString(Resource.String.Today)
                            : WeatherTools.Format("{0:d MMMM}", itemDate));
                }

                var dayOfWeek = itemDate.DayOfWeek;
                if (dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday)
                {
                    dateTextColorId = Resource.Color.dateTextColorHighlight;
                }

                var dayOfWeekPrev = itemDate.AddDays(1).DayOfWeek;
                if (dayOfWeekPrev == WeatherTools.CultureInfo.DateTimeFormat.FirstDayOfWeek)
                {
                    lineBackgroundColorId = Resource.Color.lineBackgroundColorHighlight;
                }
            }

            if (existsDate)
            {
                holder.Date.Text = dateText;
                holder.Date.SetTextColor(new Color(ContextCompat.GetColor(context, dateTextColorId)));
            }

            holder.ViewLine?.SetBackgroundColor(new Color(ContextCompat.GetColor(context, lineBackgroundColorId)));

            Java.Lang.ICharSequence minTemperatureText = new SpannableString(string.Empty);
            //var mintemps = new List<double?> {item.MinTemperature};
            //if (isTodayPosition && Data.Currently?.MinTemperature != null)
            //    mintemps.Add(Data.Currently.MinTemperature);
            //var minTemp = WeatherTools.CalculateMinTemperature(item.Date, Data, mintemps.ToArray());
            var minTemp = item.MinTemperature;

            Java.Lang.ICharSequence maxTemperatureText = new SpannableString(string.Empty);
            //var maxtemps = new List<double?> {item.MaxTemperature};
            //if (isTodayPosition && Data.Currently?.MaxTemperature != null)
            //    mintemps.Add(Data.Currently.MaxTemperature);
            //var maxTemp = WeatherTools.CalculateMaxTemperature(item.Date, Data, maxtemps.ToArray());
            var maxTemp = item.MaxTemperature;

            if (minTemp.HasValue && maxTemp.HasValue)
            {
                var degree = WeatherTools.DegreeString;
                minTemperatureText = WeatherTools.ConvertTemperatureToAlertedStyle(context, minTemp.Value, "{0:f0}{1}",
                                                                                   degree);
                maxTemperatureText = WeatherTools.ConvertTemperatureToAlertedStyle(context, maxTemp.Value, "{0:f0}{1}",
                                                                                   degree);
            }
            if (holder.MinTemperature != null && holder.MaxTemperature != null)
            {
                holder.MinTemperature.TextFormatted = minTemperatureText;
                holder.MaxTemperature.TextFormatted = maxTemperatureText;
            }

            holder.ConditionImage?.SetImageDrawable(WeatherTools.GetConditionIcon(WidgetTypes.Item, null, item,
                                                                                  isTodayPosition, isTodayPosition));

            var visibility               = ViewStates.Gone;
            var winSpeedText             = string.Empty;
            var windDirectionText        = string.Empty;
            var existsWindDirectionImage = holder.WindDirectionImage != null;
            var existsWindDirection      = holder.WindDirection != null;

            if (item.WindDirection.HasValue)
            {
                if (existsWindDirectionImage)
                {
                    visibility = ViewStates.Visible;
                    holder.WindDirectionImage.SetImageBitmap(
                        WeatherTools.WindDirectionDrawable(item.WindDirection.Value));
                }

                if (existsWindDirection)
                {
                    windDirectionText = WeatherTools.WindDirectionToCardinal(item.WindDirection.Value);
                }
            }

            if (item.WindSpeed.HasValue)
            {
                winSpeedText = WeatherTools.ConvertWindSpeedToString(item.WindSpeed.Value, "{0:f0} {1}",
                                                                     WeatherTools.WindSpeedUnitString);
            }

            if (existsWindDirectionImage)
            {
                holder.WindDirectionImage.Visibility = visibility;
            }
            if (existsWindDirection)
            {
                holder.WindDirection.Text = windDirectionText;
            }
            if (holder.WindSpeed != null)
            {
                holder.WindSpeed.Text = winSpeedText;
            }
        }
コード例 #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);
        }