Skip to content

Commit

Permalink
Merge pull request unoplatform#15234 from Youssef1313/calendarview-in…
Browse files Browse the repository at this point in the history
…finite-loop2

fix: Fix CalendarView infinite loop
  • Loading branch information
jeromelaban authored Feb 7, 2024
2 parents db94c92 + 62d492e commit 9bb910a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls;
[RunsOnUIThread]
public class Given_CalendarView
{
[TestMethod]
public async Task When_MinDate_Has_Different_Offset()
{
var calendarView = new CalendarView();
calendarView.MinDate = new DateTimeOffset(new DateTime(2010, 1, 1, 22, 0, 0), TimeSpan.Zero);
calendarView.MaxDate = new DateTimeOffset(new DateTime(2010, 1, 31), TimeSpan.FromHours(2));

await UITestHelper.Load(calendarView);
}

#if __WASM__
[TestMethod]
public async Task When_ItemCornerRadius()
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI.Tests/Windows_Globalization/When_Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void When_DateTimeOffset_Is_Next_Day_If_Converted_To_Utc()
var comparer = new DirectUI.DateComparer();
comparer.SetCalendarForComparison(calendar);
var result = comparer.CompareDay(offset, new DateTimeOffset(year: 2023, month: 5, day: 1, hour: 4, minute: 0, second: 0, TimeSpan.FromHours(0)));
Assert.AreEqual(0, result);
Assert.AreEqual(1, result);
}

[TestMethod]
Expand Down
9 changes: 8 additions & 1 deletion src/Uno.UI/DirectUI/DateComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ private int CompareDate(

global::System.Diagnostics.Debug.Assert(m_spCalendar is { });

long delta = lhs.ToUniversalTime().Ticks - rhs.ToUniversalTime().Ticks;
// Uno specific: In WinUI, wf::DateTime is a struct that only has "UniversalTime" field.
// So, we need to call ToUniversalTime here.
// Failure to do so can cause issues when comparing dates like 2024/01/01 10:00:00 PM UTC+2 and 2024/01/02 12:00:00 AM UTC
// Both dates should be equal, but getUnit will return 1 and 2 indicating that the first date is smaller.
lhs = lhs.ToUniversalTime();
rhs = rhs.ToUniversalTime();

long delta = lhs.Ticks - rhs.Ticks;
if (delta < 0)
{
delta = -delta;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ internal void ComputeSize()
{
int index = 0;

m_lastVisitedDateAndIndex.first = Owner.MinDate;
m_lastVisitedDateAndIndex.first = Owner.MinDate.ToUniversalTime();
m_lastVisitedDateAndIndex.second = 0;

global::System.Diagnostics.Debug.Assert(!Owner.DateComparer.LessThan(Owner.MaxDate, Owner.MinDate));
Expand Down Expand Up @@ -332,10 +332,10 @@ internal DateTime GetDateAt(uint index)

pCalendar.SetDateTime(m_lastVisitedDateAndIndex.first);
AddUnits((int)(index) - m_lastVisitedDateAndIndex.second);
date = pCalendar.GetDateTime();
date = pCalendar.GetDateTime().ToUniversalTime();
m_lastVisitedDateAndIndex.first = date;
m_lastVisitedDateAndIndex.second = (int)(index);
var pDate = date; // .ToUniversalTime() - UNO
var pDate = date;

return pDate;
}
Expand All @@ -350,7 +350,8 @@ internal DateTime GetDateAt(uint index)
internal int CalculateOffsetFromMinDate(DateTime date)
{
var pIndex = 0;
DateTime estimatedDate = m_lastVisitedDateAndIndex.first; //.ToUniversalTime(); - UNO
DateTime estimatedDate = m_lastVisitedDateAndIndex.first.ToUniversalTime();

var pCalendar = GetCalendar();
global::System.Diagnostics.Debug.Assert(m_lastVisitedDateAndIndex.second != -1);

Expand Down

0 comments on commit 9bb910a

Please sign in to comment.