示例#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);
            }
        }
        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);
        }
示例#3
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;
            }
        }
示例#4
0
 public WeatherDailyItemAdapter(WeatherDataPointDaily[] daily, WeatherTools weatherTools) : base(daily, weatherTools)
 {
 }
 protected WeatherDailyItemAdapterBase(WeatherDataPointDaily[] daily, WeatherTools weatherTools)
 {
     Daily = daily;
     _weakReferenceWeatherTools = new WeakReference <WeatherTools>(weatherTools);
 }
示例#6
0
        protected override void OnBindContentViewHolder(RecyclerView.ViewHolder viewHolder, int position)
        {
            if (Daily == null)
            {
                return;
            }

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

            var dayText  = string.Empty;
            var dateText = string.Empty;
            var lineBackgroundColorId = Resource.Color.lineBackgroundColor;
            var dateTextColorId       = Resource.Color.dateTextColor;
            var existsDay             = holder.Day != null;
            var existsDate            = holder.Date != null;
            var isTodayPosition       = false;

            if (item.Date.HasValue)
            {
                var itemDate = item.Date.Value.ToLocalTime();
                isTodayPosition = itemDate.Date == DateTime.Today;
                if (existsDay)
                {
                    dayText = WeatherTools.Format("{0:ddd}", itemDate);
                }

                if (existsDate)
                {
                    dateText = WeatherTools.Format("{0}",
                                                   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;
                }
            }

            var datecolor = new Color(ContextCompat.GetColor(context, dateTextColorId));

            if (existsDay)
            {
                holder.Day.Text = dayText;
                holder.Day.SetTextColor(datecolor);
            }
            if (existsDate)
            {
                holder.Date.Text = dateText;
                holder.Date.SetTextColor(datecolor);
            }

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

            var minMaxTemperatureText = string.Empty;

            if (item.MinTemperature.HasValue && item.MaxTemperature.HasValue)
            {
                minMaxTemperatureText = WeatherTools.ConvertTemperatureToString(item.MinTemperature.Value,
                                                                                item.MaxTemperature.Value, "{0:f0}{2}   {1:f0}{2}", WeatherTools.DegreeString);
            }
            if (holder.MinMaxTemperature != null)
            {
                holder.MinMaxTemperature.Text = minMaxTemperatureText;
            }

            holder.ConditionImage?.SetImageDrawable(WeatherTools.GetConditionIcon(WidgetTypes.Item, null, item,
                                                                                  isTodayPosition, isTodayPosition));
        }
示例#7
0
 public WeatherDailyItemAdapterLand(WeatherDataPointDaily[] daily, WeatherTools weatherTools,
                                    View viewDetailsContent, ScrollView scrollView) : base(daily, weatherTools)
 {
     _viewDetailsContent = new WeakReference <View>(viewDetailsContent);
     _scrollView         = new WeakReference <ScrollView>(scrollView);
 }
示例#8
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);
        }
示例#9
0
 private void WeatherToolsDispose()
 {
     _weatherTools?.Dispose();
     _weatherTools = null;
 }