Skip to content
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
23 changes: 16 additions & 7 deletions src/Core/src/Handlers/DatePicker/DatePickerHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ protected override MauiDatePicker CreatePlatformView()
HidePicker = HidePickerDialog
};

var date = VirtualView?.Date;

if (date != null)
_dialog = CreateDatePickerDialog(date.Value.Year, date.Value.Month, date.Value.Day);

return mauiDatePicker;
}

internal DatePickerDialog? DatePickerDialog { get { return _dialog; } }
internal DatePickerDialog? DatePickerDialog { get { return _dialog ??= CreateDefaultDatePickerDialog(); } }
Comment on lines -28 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just create the dialog in ShowPickerDialog like the time picker? I also see that in time picker, we always re-create the dialog:

void ShowPickerDialog(int hour, int minute)
{
	_dialog = CreateTimePickerDialog(hour, minute);
	_dialog.Show();
}

I see we do set some values in the mappers for the min/max dates, but I feel like that was wrong as we can totally set this in the setDateLater part of the ShowPickerDialog just like we set the actual date.

I think this whole setup should rather be a lazy loading thing and we set the min/max when the picker is about to appear.


protected override void ConnectHandler(MauiDatePicker platformView)
{
Expand Down Expand Up @@ -64,6 +59,16 @@ protected override void DisconnectHandler(MauiDatePicker platformView)
base.DisconnectHandler(platformView);
}

DatePickerDialog? CreateDefaultDatePickerDialog()
{
var date = VirtualView?.Date;
if (date != null)
{
return CreateDatePickerDialog(date.Value.Year, date.Value.Month, date.Value.Day);
}
return null;
}

protected virtual DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
{
var dialog = new DatePickerDialog(Context!, (o, e) =>
Expand All @@ -73,7 +78,11 @@ protected virtual DatePickerDialog CreateDatePickerDialog(int year, int month, i
VirtualView.Date = e.Date;
}
}, year, month, day);

if (VirtualView != null)
{
dialog.DatePicker.MaxDate = (long)VirtualView.MaximumDate.ToUniversalTimeNative().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
dialog.DatePicker.MinDate = (long)VirtualView.MinimumDate.ToUniversalTimeNative().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
}
Comment on lines +81 to +85
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, maybe no need to the setLater as you do it here. In this case, we can use a totally lazy creating.

return dialog;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Core/src/Platform/Android/DatePickerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ public static void UpdateMaximumDate(this MauiDatePicker platformDatePicker, IDa
}
}

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