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

Use DateOnly / TimeOnly in DatePicker / TimePicker #40

Merged
merged 1 commit into from
Sep 18, 2022
Merged
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
4 changes: 2 additions & 2 deletions samples/ControlGallery/Views/SetValues/DatePickerPage.razor
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<ContentPage Title="DatePicker">
<VerticalStackLayout Margin="20">
<DatePicker @bind-Date="_date" />
<Label>Selected date: @_date.ToShortDateString()</Label>
<Label>Selected date: @_date</Label>
</VerticalStackLayout>
</ContentPage>

@code
{
private DateTime _date;
private DateOnly _date = DateOnly.FromDateTime(DateTime.Today);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

@code
{
private TimeSpan _time;
private TimeOnly _time = TimeOnly.FromDateTime(DateTime.Now);
}
64 changes: 64 additions & 0 deletions src/BlazorBindings.Maui/Elements/DatePicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Microsoft.AspNetCore.Components;
using System;
using MC = Microsoft.Maui.Controls;

namespace BlazorBindings.Maui.Elements
{
public partial class DatePicker
{
[Parameter] public DateOnly? Date { get; set; }
[Parameter] public EventCallback<DateOnly> DateChanged { get; set; }
[Parameter] public DateOnly? MaximumDate { get; set; }
[Parameter] public DateOnly? MinimumDate { get; set; }

protected override bool HandleAdditionalParameter(string name, object value)
{
switch (name)
{
case nameof(Date):
if (!Equals(Date, value))
{
Date = (DateOnly?)value;

NativeControl.Date = Date != null
? Date.Value.ToDateTime(TimeOnly.MinValue)
: (DateTime)MC.DatePicker.DateProperty.DefaultValue;
}
return true;
case nameof(MaximumDate):
if (!Equals(MaximumDate, value))
{
MaximumDate = (DateOnly?)value;

NativeControl.MaximumDate = MaximumDate != null
? MaximumDate.Value.ToDateTime(TimeOnly.MinValue)
: (DateTime)MC.DatePicker.MaximumDateProperty.DefaultValue;
}
return true;
case nameof(MinimumDate):
if (!Equals(MinimumDate, value))
{
MinimumDate = (DateOnly?)value;

NativeControl.MinimumDate = MinimumDate != null
? MinimumDate.Value.ToDateTime(TimeOnly.MinValue)
: (DateTime)MC.DatePicker.MinimumDateProperty.DefaultValue;
}
return true;
case nameof(DateChanged):
if (!Equals(DateChanged, value))
{
void NativeControlDateSelected(object sender, MC.DateChangedEventArgs e) => DateChanged.InvokeAsync(DateOnly.FromDateTime(NativeControl.Date));

DateChanged = (EventCallback<DateOnly>)value;
NativeControl.DateSelected -= NativeControlDateSelected;
NativeControl.DateSelected += NativeControlDateSelected;
}
return true;

default:
return base.HandleAdditionalParameter(name, value);
}
}
}
}
36 changes: 0 additions & 36 deletions src/BlazorBindings.Maui/Elements/DatePicker.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using Microsoft.Maui.Graphics;
using System;
using System.Threading.Tasks;

namespace BlazorBindings.Maui.Elements
Expand All @@ -22,16 +21,12 @@ static DatePicker()
}

[Parameter] public double? CharacterSpacing { get; set; }
[Parameter] public DateTime? Date { get; set; }
[Parameter] public MC.FontAttributes? FontAttributes { get; set; }
[Parameter] public bool? FontAutoScalingEnabled { get; set; }
[Parameter] public string FontFamily { get; set; }
[Parameter] public double? FontSize { get; set; }
[Parameter] public string Format { get; set; }
[Parameter] public DateTime? MaximumDate { get; set; }
[Parameter] public DateTime? MinimumDate { get; set; }
[Parameter] public Color TextColor { get; set; }
[Parameter] public EventCallback<DateTime> DateChanged { get; set; }

public new MC.DatePicker NativeControl => (MC.DatePicker)((Element)this).NativeControl;

Expand All @@ -48,13 +43,6 @@ protected override void HandleParameter(string name, object value)
NativeControl.CharacterSpacing = CharacterSpacing ?? (double)MC.DatePicker.CharacterSpacingProperty.DefaultValue;
}
break;
case nameof(Date):
if (!Equals(Date, value))
{
Date = (DateTime?)value;
NativeControl.Date = Date ?? (DateTime)MC.DatePicker.DateProperty.DefaultValue;
}
break;
case nameof(FontAttributes):
if (!Equals(FontAttributes, value))
{
Expand Down Expand Up @@ -90,37 +78,13 @@ protected override void HandleParameter(string name, object value)
NativeControl.Format = Format;
}
break;
case nameof(MaximumDate):
if (!Equals(MaximumDate, value))
{
MaximumDate = (DateTime?)value;
NativeControl.MaximumDate = MaximumDate ?? (DateTime)MC.DatePicker.MaximumDateProperty.DefaultValue;
}
break;
case nameof(MinimumDate):
if (!Equals(MinimumDate, value))
{
MinimumDate = (DateTime?)value;
NativeControl.MinimumDate = MinimumDate ?? (DateTime)MC.DatePicker.MinimumDateProperty.DefaultValue;
}
break;
case nameof(TextColor):
if (!Equals(TextColor, value))
{
TextColor = (Color)value;
NativeControl.TextColor = TextColor;
}
break;
case nameof(DateChanged):
if (!Equals(DateChanged, value))
{
void NativeControlDateSelected(object sender, MC.DateChangedEventArgs e) => DateChanged.InvokeAsync(NativeControl.Date);

DateChanged = (EventCallback<DateTime>)value;
NativeControl.DateSelected -= NativeControlDateSelected;
NativeControl.DateSelected += NativeControlDateSelected;
}
break;

default:
base.HandleParameter(name, value);
Expand Down
57 changes: 57 additions & 0 deletions src/BlazorBindings.Maui/Elements/TimePicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using Microsoft.Maui.Graphics;
using System;
using System.ComponentModel;
using System.Threading.Tasks;

namespace BlazorBindings.Maui.Elements
{
public partial class TimePicker
{
[Parameter] public TimeOnly? Time { get; set; }
[Parameter] public EventCallback<TimeOnly> TimeChanged { get; set; }

protected override bool HandleAdditionalParameter(string name, object value)
{
switch (name)
{
case nameof(Time):
if (!Equals(Time, value))
{
Time = (TimeOnly?)value;
NativeControl.Time = Time != null
? Time.Value.ToTimeSpan()
: (TimeSpan)MC.TimePicker.TimeProperty.DefaultValue;
}
return true;
case nameof(TimeChanged):
if (!Equals(TimeChanged, value))
{
void NativeControlPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(NativeControl.Time))
{
TimeChanged.InvokeAsync(TimeOnly.FromTimeSpan(NativeControl.Time));
}
}

TimeChanged = (EventCallback<TimeOnly>)value;
NativeControl.PropertyChanged -= NativeControlPropertyChanged;
NativeControl.PropertyChanged += NativeControlPropertyChanged;
}
return true;
}

return base.HandleAdditionalParameter(name, value);
}
}
}
27 changes: 0 additions & 27 deletions src/BlazorBindings.Maui/Elements/TimePicker.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using Microsoft.Maui.Graphics;
using System;
using System.ComponentModel;
using System.Threading.Tasks;

namespace BlazorBindings.Maui.Elements
Expand All @@ -29,8 +27,6 @@ static TimePicker()
[Parameter] public double? FontSize { get; set; }
[Parameter] public string Format { get; set; }
[Parameter] public Color TextColor { get; set; }
[Parameter] public TimeSpan? Time { get; set; }
[Parameter] public EventCallback<TimeSpan> TimeChanged { get; set; }

public new MC.TimePicker NativeControl => (MC.TimePicker)((Element)this).NativeControl;

Expand Down Expand Up @@ -89,29 +85,6 @@ protected override void HandleParameter(string name, object value)
NativeControl.TextColor = TextColor;
}
break;
case nameof(Time):
if (!Equals(Time, value))
{
Time = (TimeSpan?)value;
NativeControl.Time = Time ?? (TimeSpan)MC.TimePicker.TimeProperty.DefaultValue;
}
break;
case nameof(TimeChanged):
if (!Equals(TimeChanged, value))
{
void NativeControlPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(NativeControl.Time))
{
TimeChanged.InvokeAsync(NativeControl.Time);
}
}

TimeChanged = (EventCallback<TimeSpan>)value;
NativeControl.PropertyChanged -= NativeControlPropertyChanged;
NativeControl.PropertyChanged += NativeControlPropertyChanged;
}
break;

default:
base.HandleParameter(name, value);
Expand Down
6 changes: 3 additions & 3 deletions src/BlazorBindings.Maui/Properties/AttributeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
//[assembly: GenerateComponent(typeof(CollectionView))] // Manually written to use custom logic for generics and binding
[assembly: GenerateComponent(typeof(ContentPage))]
[assembly: GenerateComponent(typeof(ContentView))]
[assembly: GenerateComponent(typeof(DatePicker))]
[assembly: GenerateComponent(typeof(DatePicker),
Exclude = new[] { nameof(DatePicker.Date), nameof(DatePicker.DateSelected), nameof(DatePicker.MaximumDate), nameof(DatePicker.MinimumDate) })]
[assembly: GenerateComponent(typeof(Editor))]
[assembly: GenerateComponent(typeof(Entry))]
[assembly: GenerateComponent(typeof(FlexLayout))]
Expand Down Expand Up @@ -68,8 +69,7 @@
[assembly: GenerateComponent(typeof(TabbedPage))]
[assembly: GenerateComponent(typeof(TemplatedPage))]
[assembly: GenerateComponent(typeof(TemplatedView))]
[assembly: GenerateComponent(typeof(TimePicker),
PropertyChangedEvents = new[] { nameof(TimePicker.Time) })]
[assembly: GenerateComponent(typeof(TimePicker), Exclude = new[] { nameof(TimePicker.Time) })]
[assembly: GenerateComponent(typeof(ToolbarItem))]
[assembly: GenerateComponent(typeof(VerticalStackLayout))]
[assembly: GenerateComponent(typeof(View))]
Expand Down