public void BuildGrid()
        {
            DateTime previousMonth;

            try
            {
                previousMonth = _currentMonth.AddMonths(-1);
            }
            catch
            {
                previousMonth = _currentMonth;
            }
            var daysInPreviousMonth = DateTime.DaysInMonth(previousMonth.Year, previousMonth.Month);
            var daysInMonth         = DateTime.DaysInMonth(_currentMonth.Year, _currentMonth.Month);

            weekdayOfFirst = (int)_currentMonth.DayOfWeek;
            var lead = daysInPreviousMonth - (weekdayOfFirst - 1);

            // build last month's days
            for (int i = 1; i <= weekdayOfFirst; i++)
            {
                var firstDayOfThisMonth = new DateTime(_currentMonth.Year, _currentMonth.Month, 1);
                var viewDay             = firstDayOfThisMonth.AddDays(-i);
                var dayView             = new CalendarDayView {
                    Frame = new CGRect((i - 1) * currentDayBlockWidth - 1, 0, currentDayBlockWidth + 1, 45), Text = lead.ToString(), Marked = _calendarMonthView.isDayMarker(viewDay)
                };
                AddSubview(dayView);
                _dayTiles.Add(dayView);
                lead++;
            }

            var position = weekdayOfFirst + 1;
            var line     = 0;

            // current month
            for (int i = 1; i <= daysInMonth; i++)
            {
                var viewDay = new DateTime(_currentMonth.Year, _currentMonth.Month, i);
                var dayView = new CalendarDayView {
                    Frame = new CGRect((position - 1) * currentDayBlockWidth - 1, line * 44, currentDayBlockWidth + 1, 45), Today = (_currentDay.Date == viewDay.Date), Text = i.ToString(), Active = true, Tag = i, Marked = _calendarMonthView.isDayMarker(viewDay), Selected = (SelectedDate.Day == i)
                };

                if (dayView.Selected)
                {
                    SelectedDayView = dayView;
                }

                AddSubview(dayView);
                _dayTiles.Add(dayView);

                position++;
                if (position > 7)
                {
                    position = 1;
                    line++;
                }
            }

            //next month
            if (position != 1)
            {
                int dayCounter = 1;
                for (int i = position; i < 8; i++)
                {
                    var lastDayOfThisMonth = new DateTime(_currentMonth.Year, _currentMonth.Month, daysInMonth);
                    var viewDay            = lastDayOfThisMonth.AddDays(dayCounter);
                    var dayView            = new CalendarDayView {
                        Frame = new CGRect((i - 1) * currentDayBlockWidth - 1, line * 44, currentDayBlockWidth + 1, 45), Text = dayCounter.ToString(), Marked = _calendarMonthView.isDayMarker(viewDay)
                    };
                    AddSubview(dayView);
                    _dayTiles.Add(dayView);
                    dayCounter++;
                }
            }

            Frame = new CGRect(Frame.Location, new CGSize(Frame.Width, (line + 1) * 44));

            Lines = (position == 1 ? line - 1 : line);

            if (SelectedDayView != null)
            {
                this.BringSubviewToFront(SelectedDayView);
            }
        }
        public void BuildGrid()
        {
            DateTime previousMonth;
            try {
                previousMonth = _currentMonth.AddMonths (-1);
            } catch {
                previousMonth = _currentMonth;
            }
            var daysInPreviousMonth = DateTime.DaysInMonth (previousMonth.Year, previousMonth.Month);
            var daysInMonth = DateTime.DaysInMonth (_currentMonth.Year, _currentMonth.Month);
            weekdayOfFirst = (int)_currentMonth.DayOfWeek;
            var lead = daysInPreviousMonth - (weekdayOfFirst - 1);

            // build last month's days
            for (int i = 1; i <= weekdayOfFirst; i++) {
                var viewDay = new DateTime (_currentMonth.Year, _currentMonth.Month, i);
                var dayView = new CalendarDayView { Frame = new RectangleF ((i - 1) * 46 - 1, 0, 47, 45), Text = lead.ToString (), Marked = _calendarMonthView.isDayMarker (viewDay) };
                AddSubview (dayView);
                _dayTiles.Add (dayView);
                lead++;
            }

            var position = weekdayOfFirst + 1;
            var line = 0;

            // current month
            for (int i = 1; i <= daysInMonth; i++) {
                var viewDay = new DateTime (_currentMonth.Year, _currentMonth.Month, i);
                var dayView = new CalendarDayView { Frame = new RectangleF ((position - 1) * 46 - 1, line * 44, 47, 45), Today = (_currentDay.Date == viewDay.Date), Text = i.ToString (), Active = true, Tag = i, Marked = _calendarMonthView.isDayMarker (viewDay), Selected = (SelectedDate.Day == i) };

                if (dayView.Selected)
                    SelectedDayView = dayView;

                AddSubview (dayView);
                _dayTiles.Add (dayView);

                position++;
                if (position > 7) {
                    position = 1;
                    line++;
                }
            }

            //next month
            if (position != 1) {
                int dayCounter = 1;
                for (int i = position; i < 8; i++) {
                    var viewDay = new DateTime (_currentMonth.Year, _currentMonth.Month, i);
                    var dayView = new CalendarDayView { Frame = new RectangleF ((i - 1) * 46 - 1, line * 44, 47, 45), Text = dayCounter.ToString (), Marked = _calendarMonthView.isDayMarker (viewDay) };
                    AddSubview (dayView);
                    _dayTiles.Add (dayView);
                    dayCounter++;
                }
            }

            Frame = new RectangleF (Frame.Location, new SizeF (Frame.Width, (line + 1) * 44));

            Lines = (position == 1 ? line - 1 : line);

            if (SelectedDayView != null)
                this.BringSubviewToFront (SelectedDayView);
        }
        private bool SelectDayView(UITouch touch)
        {
            var p = touch.LocationInView (this);

            int index = ((int)p.Y / 44) * 7 + ((int)p.X / 46);
            if (index < 0 || index >= _dayTiles.Count)
                return false;

            var newSelectedDayView = _dayTiles[index];
            if (newSelectedDayView == SelectedDayView)
                return false;

            if (!newSelectedDayView.Active && touch.Phase != UITouchPhase.Moved) {
                var day = int.Parse (newSelectedDayView.Text);
                if (day > 15)
                    _calendarMonthView.MoveCalendarMonths (false, true);
                else
                    _calendarMonthView.MoveCalendarMonths (true, true);
                return false;
            } else if (!newSelectedDayView.Active) {
                return false;
            }

            SelectedDayView.Selected = false;
            this.BringSubviewToFront (newSelectedDayView);
            newSelectedDayView.Selected = true;

            SelectedDayView = newSelectedDayView;
            SetNeedsDisplay ();
            return true;
        }
Example #4
0
        private bool SelectDayView(UITouch touch)
        {
            var p = touch.LocationInView (this);

            int index = ((int)p.Y / 44) * 7 + ((int)p.X / 46);
            if (index < 0 || index >= _dayTiles.Count) {
                return false;
            }

            // shortcut if they clicked on the already selected day view
            var newSelectedDayView = _dayTiles[index];
            if (newSelectedDayView == SelectedDayView) {
                return false;
            }

            // unselect the old day view
            if (SelectedDayView != null) {
                SelectedDayView.Selected = false;
            }

            // select the new day view
            SelectedDayView = newSelectedDayView;
            SelectedDate = _calendarMonthView.SelectedDate = SelectedDayView.Date;

            // if we are still in the same month, make it so.
            this.BringSubviewToFront (newSelectedDayView);
            newSelectedDayView.Selected = true;
            newSelectedDayView.SetNeedsDisplay();
            // SetNeedsDisplay ();

            // check to see if we are leaving the current month
            if (!newSelectedDayView.Active && touch.Phase != UITouchPhase.Moved) {
                if (newSelectedDayView.Date.Month < _currentMonth.Month) {
                    _calendarMonthView.MoveCalendarMonths (false, true);
                } else {
                    _calendarMonthView.MoveCalendarMonths (true, true);
                }
                return true;
            } else if (!newSelectedDayView.Active) {
                return false;
            }

            return true;
        }