Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of rendering DatePicker #24948

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Controls/src/Core/DatePicker/DatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class DatePicker : View, IFontElement, ITextElement, IElementConf
public static readonly BindableProperty DateProperty = BindableProperty.Create(nameof(Date), typeof(DateTime), typeof(DatePicker), default(DateTime), BindingMode.TwoWay,
coerceValue: CoerceDate,
propertyChanged: DatePropertyChanged,
defaultValueCreator: (bindable) => DateTime.Today);
defaultValueCreator: (bindable) => GetDefaultDate());

/// <summary>Bindable property for <see cref="MinimumDate"/>.</summary>
public static readonly BindableProperty MinimumDateProperty = BindableProperty.Create(nameof(MinimumDate), typeof(DateTime), typeof(DatePicker), new DateTime(1900, 1, 1),
Expand Down Expand Up @@ -152,6 +152,11 @@ void ITextElement.OnTextTransformChanged(TextTransform oldValue, TextTransform n
{
}

static DateTime GetDefaultDate()
{
return DateTimeOffset.Now.DateTime.Date;
}

/// <include file="../../docs/Microsoft.Maui.Controls/DatePicker.xml" path="//Member[@MemberName='UpdateFormsText']/Docs/*" />
public virtual string UpdateFormsText(string source, TextTransform textTransform)
=> TextTransformUtilites.GetTransformedText(source, textTransform);
Expand Down
18 changes: 16 additions & 2 deletions src/Core/src/Platform/Android/DatePickerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void UpdateMinimumDate(this MauiDatePicker platformDatePicker, IDa
{
if (datePickerDialog != null)
{
datePickerDialog.DatePicker.MinDate = (long)datePicker.MinimumDate.ToUniversalTime().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
datePickerDialog.DatePicker.MinDate = (long)datePicker.MinimumDate.ToUniversalTimeNative().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
}
}

Expand All @@ -49,10 +49,24 @@ public static void UpdateMaximumDate(this MauiDatePicker platformDatePicker, IDa
{
if (datePickerDialog != null)
{
datePickerDialog.DatePicker.MaxDate = (long)datePicker.MaximumDate.ToUniversalTime().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
datePickerDialog.DatePicker.MaxDate = (long)datePicker.MaximumDate.ToUniversalTimeNative().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
}
}

internal static DateTime ToUniversalTimeNative(this DateTime date)
{
if (date.Kind == DateTimeKind.Utc)
{
return date;
}
var timeZone = Java.Util.TimeZone.Default;
if (timeZone != null && date != DateTime.MaxValue && date != DateTime.MinValue)
{
return date.AddHours(-1 * (double)timeZone.RawOffset / 1000 / 60 / 60);
}
return date.ToUniversalTime();
}

internal static void SetText(this MauiDatePicker platformDatePicker, IDatePicker datePicker)
{
platformDatePicker.Text = datePicker.Date.ToString(datePicker.Format);
Expand Down
Loading