From f4fb8451cba99b41260ab00112dfa2d09c5121f8 Mon Sep 17 00:00:00 2001 From: ni4ka7a Date: Fri, 9 Nov 2018 15:04:25 +0200 Subject: [PATCH] Extract GetFirstDateToRender in the calendar model so it can be executed even before the calendar ViewModels are loaded --- .../Model/CalendarCenturyViewModel.cs | 2 +- .../Calendar/Model/CalendarDecadeViewModel.cs | 2 +- .../Input.UWP/Calendar/Model/CalendarModel.cs | 53 +++++++++++++++++++ .../Calendar/Model/CalendarMonthViewModel.cs | 12 +---- .../Model/CalendarMultiDayViewModel.cs | 19 +------ .../Calendar/Model/CalendarYearViewModel.cs | 4 +- 6 files changed, 59 insertions(+), 33 deletions(-) diff --git a/Controls/Input/Input.UWP/Calendar/Model/CalendarCenturyViewModel.cs b/Controls/Input/Input.UWP/Calendar/Model/CalendarCenturyViewModel.cs index ddbec6a..f25be4d 100644 --- a/Controls/Input/Input.UWP/Calendar/Model/CalendarCenturyViewModel.cs +++ b/Controls/Input/Input.UWP/Calendar/Model/CalendarCenturyViewModel.cs @@ -25,7 +25,7 @@ namespace Telerik.UI.Xaml.Controls.Input.Calendar internal override DateTime GetFirstDateToRender(DateTime date) { - return CalendarMathHelper.GetFirstDateOfCentury(date); + return this.Calendar.GetFirstDateToRenderForDisplayMode(date, CalendarDisplayMode.CenturyView); } internal override DateTime GetNextDateToRender(DateTime date) diff --git a/Controls/Input/Input.UWP/Calendar/Model/CalendarDecadeViewModel.cs b/Controls/Input/Input.UWP/Calendar/Model/CalendarDecadeViewModel.cs index 7cdf7c3..89e6d9f 100644 --- a/Controls/Input/Input.UWP/Calendar/Model/CalendarDecadeViewModel.cs +++ b/Controls/Input/Input.UWP/Calendar/Model/CalendarDecadeViewModel.cs @@ -25,7 +25,7 @@ namespace Telerik.UI.Xaml.Controls.Input.Calendar internal override DateTime GetFirstDateToRender(DateTime date) { - return CalendarMathHelper.GetFirstDateOfDecade(date); + return this.Calendar.GetFirstDateToRenderForDisplayMode(date, CalendarDisplayMode.DecadeView); } internal override DateTime GetNextDateToRender(DateTime date) diff --git a/Controls/Input/Input.UWP/Calendar/Model/CalendarModel.cs b/Controls/Input/Input.UWP/Calendar/Model/CalendarModel.cs index 8680517..b59ca8a 100644 --- a/Controls/Input/Input.UWP/Calendar/Model/CalendarModel.cs +++ b/Controls/Input/Input.UWP/Calendar/Model/CalendarModel.cs @@ -418,6 +418,59 @@ namespace Telerik.UI.Xaml.Controls.Input.Calendar return this.View.MeasureContent(owner, content); } + internal DateTime GetFirstDateToRenderForDisplayMode(DateTime date, CalendarDisplayMode displayMode) + { + DateTime firstDateToRender = date; + + if (displayMode == CalendarDisplayMode.MultiDayView) + { + DayOfWeek firstDayOfWeek = this.GetFirstDayOfWeek(); + DateTime firstDateOfCurrentWeek = CalendarMathHelper.GetFirstDayOfCurrentWeek(date, firstDayOfWeek); + + if (!(firstDateOfCurrentWeek.Date <= date.Date && firstDateOfCurrentWeek.AddDays(7).Date >= date.Date)) + { + firstDateToRender = firstDateOfCurrentWeek; + } + + if (!this.multiDayViewSettings.WeekendsVisible) + { + firstDateToRender = CalendarMathHelper.AddBusinessDays(date, -this.multiDayViewSettings.VisibleDays); + } + else + { + firstDateToRender = date.AddDays(-this.multiDayViewSettings.VisibleDays); + } + } + else if (displayMode == CalendarDisplayMode.MonthView) + { + DayOfWeek firstDayOfWeekToUse = this.GetFirstDayOfWeek(); + + DateTime monthStartDate = CalendarMathHelper.GetFirstDateOfMonth(date); + + int daysToSubtract = (int) monthStartDate.DayOfWeek - (int) firstDayOfWeekToUse; + if (daysToSubtract <= 0) + { + daysToSubtract += 7; + } + + firstDateToRender = monthStartDate.Date == DateTime.MinValue.Date ? monthStartDate : monthStartDate.AddDays(-daysToSubtract); + } + else if (displayMode == CalendarDisplayMode.YearView) + { + firstDateToRender = CalendarMathHelper.GetFirstDateOfYear(date); + } + else if (displayMode == CalendarDisplayMode.DecadeView) + { + firstDateToRender = CalendarMathHelper.GetFirstDateOfDecade(date); + } + else if (displayMode == CalendarDisplayMode.CenturyView) + { + firstDateToRender = CalendarMathHelper.GetFirstDateOfCentury(date); + } + + return firstDateToRender; + } + private void UpdateCurrentView() { switch (this.DisplayMode) diff --git a/Controls/Input/Input.UWP/Calendar/Model/CalendarMonthViewModel.cs b/Controls/Input/Input.UWP/Calendar/Model/CalendarMonthViewModel.cs index 1a8f820..de9eb5a 100644 --- a/Controls/Input/Input.UWP/Calendar/Model/CalendarMonthViewModel.cs +++ b/Controls/Input/Input.UWP/Calendar/Model/CalendarMonthViewModel.cs @@ -41,17 +41,7 @@ namespace Telerik.UI.Xaml.Controls.Input.Calendar internal override DateTime GetFirstDateToRender(DateTime date) { - DayOfWeek firstDayOfWeekToUse = this.Calendar.GetFirstDayOfWeek(); - - DateTime monthStartDate = CalendarMathHelper.GetFirstDateOfMonth(date); - - int daysToSubtract = (int)monthStartDate.DayOfWeek - (int)firstDayOfWeekToUse; - if (daysToSubtract <= 0) - { - daysToSubtract += 7; - } - - return monthStartDate.Date == DateTime.MinValue.Date ? monthStartDate : monthStartDate.AddDays(-daysToSubtract); + return this.Calendar.GetFirstDateToRenderForDisplayMode(date, CalendarDisplayMode.MonthView); } internal override DateTime GetNextDateToRender(DateTime dateToRender) diff --git a/Controls/Input/Input.UWP/Calendar/Model/CalendarMultiDayViewModel.cs b/Controls/Input/Input.UWP/Calendar/Model/CalendarMultiDayViewModel.cs index b86b42e..bd5205c 100644 --- a/Controls/Input/Input.UWP/Calendar/Model/CalendarMultiDayViewModel.cs +++ b/Controls/Input/Input.UWP/Calendar/Model/CalendarMultiDayViewModel.cs @@ -66,24 +66,7 @@ namespace Telerik.UI.Xaml.Controls.Input.Calendar internal override DateTime GetFirstDateToRender(DateTime date) { - DayOfWeek firstDayOfWeek = this.Calendar.GetFirstDayOfWeek(); - DateTime firstDateOfCurrentWeek = CalendarMathHelper.GetFirstDayOfCurrentWeek(date, firstDayOfWeek); - - if (!(firstDateOfCurrentWeek.Date <= date.Date && firstDateOfCurrentWeek.AddDays(7).Date >= date.Date)) - { - date = firstDateOfCurrentWeek; - } - - if (!this.Calendar.multiDayViewSettings.WeekendsVisible) - { - date = CalendarMathHelper.AddBusinessDays(date, -this.BufferItemsCount); - } - else - { - date = date.AddDays(-this.BufferItemsCount); - } - - return date; + return this.Calendar.GetFirstDateToRenderForDisplayMode(date, CalendarDisplayMode.MultiDayView); } internal override DateTime GetNextDateToRender(DateTime date) diff --git a/Controls/Input/Input.UWP/Calendar/Model/CalendarYearViewModel.cs b/Controls/Input/Input.UWP/Calendar/Model/CalendarYearViewModel.cs index 10594de..e74284f 100644 --- a/Controls/Input/Input.UWP/Calendar/Model/CalendarYearViewModel.cs +++ b/Controls/Input/Input.UWP/Calendar/Model/CalendarYearViewModel.cs @@ -24,8 +24,8 @@ namespace Telerik.UI.Xaml.Controls.Input.Calendar } internal override DateTime GetFirstDateToRender(DateTime date) - { - return CalendarMathHelper.GetFirstDateOfYear(date); + { + return this.Calendar.GetFirstDateToRenderForDisplayMode(date, CalendarDisplayMode.YearView); } internal override DateTime GetNextDateToRender(DateTime date)