Exemplo n.º 1
0
        protected void ChangeCalendar(CalandarChanges changes)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                OnStartRenderCalendar?.Invoke(this, EventArgs.Empty);
                Content = null;
                if (changes.HasFlag(CalandarChanges.StartDate))
                {
                    TitleLabel.Text = StartDate.ToString(TitleLabelFormat);
                    if (TitleLabels != null)
                    {
                        var tls = StartDate.AddMonths(1);
                        foreach (var tl in TitleLabels)
                        {
                            (tl as Label).Text = tls.ToString(TitleLabelFormat);
                            tls = tls.AddMonths(1);
                        }
                    }
                }

                var start        = CalendarStartDate(StartDate).Date;
                var beginOfMonth = false;
                var endOfMonth   = false;
                for (int i = 0; i < buttons.Count; i++)
                {
                    endOfMonth   |= beginOfMonth && start.Day == 1;
                    beginOfMonth |= start.Day == 1;

                    if (i < dayLabels.Count && WeekdaysShow && changes.HasFlag(CalandarChanges.StartDay))
                    {
                        SetWeekdayLabel(start, i);
                    }

                    ChangeWeekNumbers(start, i);

                    if (changes.HasFlag(CalandarChanges.All))
                    {
                        buttons[i].Text = string.Format("{0}", start.Day);
                    }
                    else
                    {
                        buttons[i].TextWithoutMeasure = string.Format("{0}", start.Day);
                    }
                    buttons[i].Date = start;

                    buttons[i].IsOutOfMonth = !(beginOfMonth && !endOfMonth);
                    buttons[i].IsEnabled    = ShowNumOfMonths == 1 || !buttons[i].IsOutOfMonth;

                    SpecialDate sd = null;
                    if (SpecialDates != null)
                    {
                        sd = SpecialDates.FirstOrDefault(s => s.Date.Date == start.Date);
                    }

                    SetButtonNormal(buttons[i]);

                    if ((MinDate.HasValue && start < MinDate) || (MaxDate.HasValue && start > MaxDate) || (DisableAllDates && sd == null))
                    {
                        SetButtonDisabled(buttons[i]);
                    }
                    else if (buttons[i].IsEnabled && SelectedDates.Select(d => d.Date).Contains(start.Date))
                    {
                        SetButtonSelected(buttons[i], sd);
                    }
                    else if (sd != null)
                    {
                        SetButtonSpecial(buttons[i], sd);
                    }

                    start = start.AddDays(1);
                    if (i != 0 && (i + 1) % 42 == 0)
                    {
                        beginOfMonth = false;
                        endOfMonth   = false;
                        start        = CalendarStartDate(start);
                    }
                }
                if (DisableDatesLimitToMaxMinRange)
                {
                    TitleLeftArrow.IsEnabled  = !(MinDate.HasValue && CalendarStartDate(StartDate).Date < MinDate);
                    TitleRightArrow.IsEnabled = !(MaxDate.HasValue && start > MaxDate);
                }
                Content = MainView;
                OnEndRenderCalendar?.Invoke(this, EventArgs.Empty);
            });
        }
        protected void ChangeCalendar(CalendarChanges changes)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                OnStartRenderCalendar?.Invoke(this, EventArgs.Empty);
                Content = null;
                if (changes.HasFlag(CalendarChanges.StartDate))
                {
                    TitleLabel.Text = StartDate.ToString(TitleLabelFormat);
                    if (TitleLabels != null)
                    {
                        var tls = StartDate.AddMonths(1);
                        foreach (var tl in TitleLabels)
                        {
                            (tl as Label).Text = tls.ToString(TitleLabelFormat);
                            tls = tls.AddMonths(1);
                        }
                    }
                }

                var start        = CalendarStartDate(StartDate).Date;
                var beginOfMonth = false;
                var endOfMonth   = false;
                var hideLastRow  = false;
                for (int i = 0; i < buttons.Count; i++)
                {
                    endOfMonth   |= beginOfMonth && start.Day == 1;
                    beginOfMonth |= start.Day == 1;

                    if (i < dayLabels.Count && WeekdaysShow && changes.HasFlag(CalendarChanges.StartDay))
                    {
                        if (WeekdaysFormat.ToLower() == "d1")
                        {
                            dayLabels[i].Text = start.ToString("ddd").Substring(0, 1);
                        }
                        else if (WeekdaysFormat.ToLower() == "d2")
                        {
                            dayLabels[i].Text = start.ToString("ddd").Substring(0, 2);
                        }
                        else
                        {
                            dayLabels[i].Text = start.ToString(WeekdaysFormat);
                        }
                    }

                    ChangeWeekNumbers(start, i);

                    if (changes.HasFlag(CalendarChanges.All))
                    {
                        buttons[i].Text = string.Format("{0}", start.Day);
                    }
                    else
                    {
                        buttons[i].TextWithoutMeasure = string.Format("{0}", start.Day);
                    }
                    buttons[i].Date = start;

                    buttons[i].IsOutOfMonth = !(beginOfMonth && !endOfMonth);
                    buttons[i].IsEnabled    = ShowNumOfMonths == 1 || !buttons[i].IsOutOfMonth;

                    if (i == 35 && buttons[i].IsOutOfMonth)
                    {
                        // this means that we are on the bottom row and none of the dates
                        // in it belong to the current month we should hide this row
                        hideLastRow           = true;
                        rows.Last().IsVisible = false;
                    }

                    if (i >= 35 && hideLastRow)
                    {
                        buttons[i].IsVisible = false;
                    }
                    else
                    {
                        if (buttons[i].IsOutOfMonth)
                        {
                            buttons[i].IsVisible = ShowOutOfMonthDates;
                        }
                        else
                        {
                            buttons[i].IsVisible = true;
                        }
                        rows.Last().IsVisible = true;
                    }

                    SpecialDate sd = null;
                    if (SpecialDates != null)
                    {
                        sd = SpecialDates.FirstOrDefault(s => s.Date.Date == start.Date);
                    }

                    SetButtonNormal(buttons[i]);

                    if ((MinDate.HasValue && start < MinDate) || (MaxDate.HasValue && start > MaxDate) || (DisableAllDates && sd == null))
                    {
                        SetButtonDisabled(buttons[i]);
                    }
                    else if (buttons[i].IsEnabled && SelectedDates.Select(d => d.Date).Contains(start.Date))
                    {
                        SetButtonSelected(buttons[i], sd);
                    }
                    else if (sd != null)
                    {
                        SetButtonSpecial(buttons[i], sd);
                    }

                    start = start.AddDays(1);
                    if (i != 0 && (i + 1) % 42 == 0)
                    {
                        beginOfMonth = false;
                        endOfMonth   = false;
                        start        = CalendarStartDate(start);
                    }
                }
                if (DisableDatesLimitToMaxMinRange)
                {
                    TitleLeftArrow.IsEnabled  = !(MinDate.HasValue && CalendarStartDate(StartDate).Date < MinDate);
                    TitleRightArrow.IsEnabled = !(MaxDate.HasValue && start > MaxDate);
                }
                Content = MainView;
                OnEndRenderCalendar?.Invoke(this, EventArgs.Empty);
            });
        }