Skip to content

Commit

Permalink
feat: DatePicker now shows picker with today selected if no date is set
Browse files Browse the repository at this point in the history
implemented DatePicker.SelectedDate + DatePickerFlyout.DatePicked
  • Loading branch information
Xiaoy312 committed Mar 17, 2020
1 parent 8894e25 commit deef3dc
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,13 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.UI.Xaml.Controls
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || false
#if false || false || false || false || false
[global::Uno.NotImplemented]
#endif
public partial class DatePickedEventArgs : global::Windows.UI.Xaml.DependencyObject
public partial class DatePickedEventArgs : global::Windows.UI.Xaml.DependencyObject
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public global::System.DateTimeOffset NewDate
{
get
{
throw new global::System.NotImplementedException("The member DateTimeOffset DatePickedEventArgs.NewDate is not implemented in Uno.");
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public global::System.DateTimeOffset OldDate
{
get
{
throw new global::System.NotImplementedException("The member DateTimeOffset DatePickedEventArgs.OldDate is not implemented in Uno.");
}
}
#endif
// Skipping already declared property NewDate
// Skipping already declared property OldDate
// Skipping already declared method Windows.UI.Xaml.Controls.DatePickedEventArgs.DatePickedEventArgs()
// Forced skipping of method Windows.UI.Xaml.Controls.DatePickedEventArgs.DatePickedEventArgs()
// Forced skipping of method Windows.UI.Xaml.Controls.DatePickedEventArgs.OldDate.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,7 @@ public string CalendarIdentifier
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public global::System.DateTimeOffset? SelectedDate
{
get
{
return (global::System.DateTimeOffset?)this.GetValue(SelectedDateProperty);
}
set
{
this.SetValue(SelectedDateProperty, value);
}
}
#endif
// Skipping already declared property SelectedDate
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public static global::Windows.UI.Xaml.DependencyProperty CalendarIdentifierProperty { get; } =
Expand Down Expand Up @@ -185,16 +172,9 @@ public string CalendarIdentifier
"YearFormat", typeof(string),
typeof(global::Windows.UI.Xaml.Controls.DatePicker),
new FrameworkPropertyMetadata(default(string)));
#endif
#endif
// Skipping already declared property YearVisibleProperty
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public static global::Windows.UI.Xaml.DependencyProperty SelectedDateProperty { get; } =
Windows.UI.Xaml.DependencyProperty.Register(
"SelectedDate", typeof(global::System.DateTimeOffset?),
typeof(global::Windows.UI.Xaml.Controls.DatePicker),
new FrameworkPropertyMetadata(default(global::System.DateTimeOffset?)));
#endif
// Skipping already declared property SelectedDateProperty
// Skipping already declared method Windows.UI.Xaml.Controls.DatePicker.DatePicker()
// Forced skipping of method Windows.UI.Xaml.Controls.DatePicker.DatePicker()
// Forced skipping of method Windows.UI.Xaml.Controls.DatePicker.Header.get
Expand Down Expand Up @@ -247,21 +227,6 @@ public string CalendarIdentifier
// Forced skipping of method Windows.UI.Xaml.Controls.DatePicker.MaxYearProperty.get
// Forced skipping of method Windows.UI.Xaml.Controls.DatePicker.OrientationProperty.get
// Skipping already declared event Windows.UI.Xaml.Controls.DatePicker.DateChanged
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public event global::Windows.Foundation.TypedEventHandler<global::Windows.UI.Xaml.Controls.DatePicker, global::Windows.UI.Xaml.Controls.DatePickerSelectedValueChangedEventArgs> SelectedDateChanged
{
[global::Uno.NotImplemented]
add
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.UI.Xaml.Controls.DatePicker", "event TypedEventHandler<DatePicker, DatePickerSelectedValueChangedEventArgs> DatePicker.SelectedDateChanged");
}
[global::Uno.NotImplemented]
remove
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.UI.Xaml.Controls.DatePicker", "event TypedEventHandler<DatePicker, DatePickerSelectedValueChangedEventArgs> DatePicker.SelectedDateChanged");
}
}
#endif
// Skipping already declared event Windows.UI.Xaml.Controls.DatePicker.SelectedDateChanged
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public string DayFormat
// Forced skipping of method Windows.UI.Xaml.Controls.DatePickerFlyout.YearVisibleProperty.get
// Forced skipping of method Windows.UI.Xaml.Controls.DatePickerFlyout.MinYearProperty.get
// Forced skipping of method Windows.UI.Xaml.Controls.DatePickerFlyout.MaxYearProperty.get
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
#if false || false || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public event global::Windows.Foundation.TypedEventHandler<global::Windows.UI.Xaml.Controls.DatePickerFlyout, global::Windows.UI.Xaml.Controls.DatePickedEventArgs> DatePicked
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,13 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.UI.Xaml.Controls
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
#if false || false || false || false || false
[global::Uno.NotImplemented]
#endif
public partial class DatePickerSelectedValueChangedEventArgs
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public global::System.DateTimeOffset? NewDate
{
get
{
throw new global::System.NotImplementedException("The member DateTimeOffset? DatePickerSelectedValueChangedEventArgs.NewDate is not implemented in Uno.");
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
[global::Uno.NotImplemented]
public global::System.DateTimeOffset? OldDate
{
get
{
throw new global::System.NotImplementedException("The member DateTimeOffset? DatePickerSelectedValueChangedEventArgs.OldDate is not implemented in Uno.");
}
}
#endif
// Skipping already declared property NewDate
// Skipping already declared property OldDate
// Forced skipping of method Windows.UI.Xaml.Controls.DatePickerSelectedValueChangedEventArgs.OldDate.get
// Forced skipping of method Windows.UI.Xaml.Controls.DatePickerSelectedValueChangedEventArgs.NewDate.get
}
Expand Down
16 changes: 16 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/DatePicker/DatePickedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace Windows.UI.Xaml.Controls
{
public partial class DatePickedEventArgs
{
public DatePickedEventArgs(DateTimeOffset newDate, DateTimeOffset oldDate)
{
NewDate = newDate;
OldDate = oldDate;
}

public DateTimeOffset NewDate { get; }
public DateTimeOffset OldDate { get; }
}
}
58 changes: 47 additions & 11 deletions src/Uno.UI/UI/Xaml/Controls/DatePicker/DatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace Windows.UI.Xaml.Controls
public partial class DatePicker : Control
{
public event EventHandler<DatePickerValueChangedEventArgs> DateChanged;
public event EventHandler<DatePickerSelectedValueChangedEventArgs> SelectedDateChanged;

private static readonly DateTimeOffset UnsetDateValue = DateTimeOffset.MinValue;

#region DateProperty

Expand All @@ -27,11 +30,17 @@ public DateTimeOffset Date
//Set initial value of DatePicker to DateTimeOffset.MinValue to avoid 2 way binding issue where the DatePicker reset Date(DateTimeOffset.MinValue) after the initial binding value.
//We assume that this is the view model who will set the initial value just the time to fix #18331
public static readonly DependencyProperty DateProperty =
DependencyProperty.Register("Date", typeof(DateTimeOffset), typeof(DatePicker), new PropertyMetadata(DateTimeOffset.MinValue,
DependencyProperty.Register("Date", typeof(DateTimeOffset), typeof(DatePicker), new PropertyMetadata(UnsetDateValue,
(s, e) => ((DatePicker)s).OnDatePropertyChanged((DateTimeOffset)e.NewValue, (DateTimeOffset)e.OldValue)));

private void OnDatePropertyChanged(DateTimeOffset newValue, DateTimeOffset oldValue)
{
if ((SelectedDate != newValue) &&
!(newValue == UnsetDateValue && !SelectedDate.HasValue))
{
SelectedDate = newValue;
}

#if XAMARIN
UpdateDisplayedDate();
#endif
Expand All @@ -43,6 +52,33 @@ private void OnDatePropertyChanged(DateTimeOffset newValue, DateTimeOffset oldVa
partial void OnDateChangedPartial();
#endregion

#region Property: SelectedDate
public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register(
nameof(SelectedDate),
typeof(DateTimeOffset?),
typeof(DatePicker),
new PropertyMetadata(default(DateTimeOffset?), (s, e) => (s as DatePicker).OnSelectedDateChanged((DateTimeOffset?)e.NewValue, (DateTimeOffset?)e.OldValue)));

public DateTimeOffset? SelectedDate
{
get => (DateTimeOffset?)GetValue(SelectedDateProperty);
set => SetValue(SelectedDateProperty, value);
}

private void OnSelectedDateChanged(DateTimeOffset? newValue, DateTimeOffset? oldValue)
{
if (Date != (newValue ?? UnsetDateValue))
{
Date = newValue ?? UnsetDateValue;
}

OnSelectedDatePartial();
SelectedDateChanged?.Invoke(this, new DatePickerSelectedValueChangedEventArgs(newValue, oldValue));
}

partial void OnSelectedDatePartial();
#endregion

#region DayVisibleProperty
public bool DayVisible
{
Expand Down Expand Up @@ -171,6 +207,7 @@ public DatePicker()
#endregion

private Button _flyoutButton;
private DatePickerFlyout _flyout;

private TextBlock _dayTextBlock;
private TextBlock _monthTextBlock;
Expand Down Expand Up @@ -237,28 +274,27 @@ private void SetupFlyoutButton()
if (_flyoutButton != null)
{
#if __IOS__ || __ANDROID__
_flyoutButton.Flyout = new DatePickerFlyout()
_flyoutButton.Flyout = _flyout = new DatePickerFlyout()
{
#if __IOS__
Placement = FlyoutPlacement,
#endif
Date = Date,
MinYear = MinYear,
MaxYear = MaxYear
MaxYear = MaxYear,
};

BindToFlyout(nameof(Date));
_flyout.Opened += (s, e) => _flyout.Date = SelectedDate ?? DateTimeOffset.Now;
_flyout.DatePicked += (s, e) => Date = e.NewDate;

BindToFlyout(nameof(MinYear));
BindToFlyout(nameof(MaxYear));
_flyoutButton.Flyout.BindToEquivalentProperty(this, nameof(LightDismissOverlayMode));
_flyoutButton.Flyout.BindToEquivalentProperty(this, nameof(LightDismissOverlayBackground));
_flyout.BindToEquivalentProperty(this, nameof(LightDismissOverlayMode));
_flyout.BindToEquivalentProperty(this, nameof(LightDismissOverlayBackground));
#endif
}
}

private void BindToFlyout(string propertyName)
{
this.Binding(propertyName, propertyName, _flyoutButton.Flyout, BindingMode.TwoWay);
this.Binding(propertyName, propertyName, _flyout, BindingMode.TwoWay);
}

private void InitializeTextBlocks(IFrameworkElement container)
Expand All @@ -272,7 +308,7 @@ private void InitializeTextBlocks(IFrameworkElement container)
.Select(x => new { Column = x, grid.ColumnDefinitions.ElementAtOrDefault(x)?.Width })
.ToArray();

// re-position TextBlocks' Grid.Column and their respective ColumnDefinition.Width
// update TextBlocks' Grid.Column and their respective ColumnDefinition.Width
foreach (var item in items)
{
if (grid.ColumnDefinitions.ElementAtOrDefault(oldColumnInfos[item.NewIndex].Column) is ColumnDefinition definition &&
Expand Down
Loading

0 comments on commit deef3dc

Please sign in to comment.