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

Workaround for DatePicker and TimePicker controls not being able to set to value present in wrapped MAUI control #804

Merged
merged 1 commit into from
Oct 14, 2024
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
15 changes: 8 additions & 7 deletions src/UraniumUI.Material/Controls/DatePickerField.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Plainer.Maui.Controls;
using System.ComponentModel;
using UraniumUI.Controls;
using UraniumUI.Pages;
using UraniumUI.Resources;
using UraniumUI.Views;
Expand All @@ -10,8 +11,8 @@ namespace UraniumUI.Material.Controls;
[ContentProperty(nameof(Validations))]
public class DatePickerField : InputField
{
public DatePickerView DatePickerView => Content as DatePickerView;
public override View Content { get; set; } = new DatePickerView
public DatePickerWrappedView DatePickerView => Content as DatePickerWrappedView;
public override View Content { get; set; } = new DatePickerWrappedView
{
VerticalOptions = LayoutOptions.Center,
#if ANDROID
Expand Down Expand Up @@ -44,11 +45,11 @@ public DatePickerField()

UpdateClearIconState();

DatePickerView.SetBinding(DatePickerView.DateProperty, new Binding(nameof(Date), source: this));
DatePickerView.SetBinding(DatePickerView.IsEnabledProperty, new Binding(nameof(IsEnabled), source: this));
DatePickerView.SetBinding(DatePickerView.FontSizeProperty, new Binding(nameof(FontSize), source: this));
DatePickerView.SetBinding(DatePickerView.FontAutoScalingEnabledProperty, new Binding(nameof(FontAutoScalingEnabled), source: this));
DatePickerView.SetBinding(DatePickerView.FontFamilyProperty, new Binding(nameof(FontFamily), source: this));
DatePickerView.SetBinding(DatePicker.DateProperty, new Binding(nameof(Date), source: this));
DatePickerView.SetBinding(DatePicker.IsEnabledProperty, new Binding(nameof(IsEnabled), source: this));
DatePickerView.SetBinding(DatePicker.FontSizeProperty, new Binding(nameof(FontSize), source: this));
DatePickerView.SetBinding(DatePicker.FontAutoScalingEnabledProperty, new Binding(nameof(FontAutoScalingEnabled), source: this));
DatePickerView.SetBinding(DatePicker.FontFamilyProperty, new Binding(nameof(FontFamily), source: this));

#if MACCATALYST || IOS
labelTitle.InputTransparent = true;
Expand Down
7 changes: 4 additions & 3 deletions src/UraniumUI.Material/Controls/TimePickerField.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Plainer.Maui.Controls;
using System.ComponentModel;
using UraniumUI.Controls;
using UraniumUI.Pages;
using UraniumUI.Resources;
using UraniumUI.Views;
Expand All @@ -10,8 +11,8 @@ namespace UraniumUI.Material.Controls;
[ContentProperty(nameof(Validations))]
public class TimePickerField : InputField
{
public TimePickerView TimePickerView => Content as TimePickerView;
public override View Content { get; set; } = new TimePickerView
public TimePickerWrappedView TimePickerView => Content as TimePickerWrappedView;
public override View Content { get; set; } = new TimePickerWrappedView
{
VerticalOptions = LayoutOptions.Center,
#if WINDOWS
Expand Down Expand Up @@ -195,4 +196,4 @@ public override void ResetValidation()
typeof(bool), typeof(TimePickerField),
true,
propertyChanged: (bindable, oldValue, newValue) => (bindable as TimePickerField).OnAllowClearChanged());
}
}
27 changes: 27 additions & 0 deletions src/UraniumUI/Controls/DatePickerWrappedView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Plainer.Maui.Controls;

namespace UraniumUI.Controls;

/// <summary>
/// TODO Revisit when the underlying dotnet/maui issue is fixed: https://github.com/dotnet/maui/issues/13156
/// A workaround for abovementioned issue where the DateSelected event is not raised when the date is the same as the current date.
/// This "manually" raises the PropertyChanged event when the date is the same as the current date by briefly setting it to a different time before applying the actual value.
/// Alternatively, this workaround could be implemented in Plainer.Maui.Controls.DatePickerView directly.
/// </summary>
public class DatePickerWrappedView : DatePickerView, IDatePicker
{
DateTime IDatePicker.Date
{
get => Date;
set
{
if (value == Date)
{
Date += TimeSpan.FromDays(1);
}

Date = value;
OnPropertyChanged(nameof(Date));
}
}
}
25 changes: 25 additions & 0 deletions src/UraniumUI/Controls/TimePickerWrappedView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Plainer.Maui.Controls;

namespace UraniumUI.Controls;


/// <summary>
/// See explanation in <see cref="DatePickerWrappedView"/>
/// </summary>
public class TimePickerWrappedView : TimePickerView, ITimePicker
{
TimeSpan ITimePicker.Time
{
get => Time;
set
{
if (value == Time)
{
Time += TimeSpan.FromSeconds(1);
}

Time = value;
OnPropertyChanged(nameof(Time));
}
}
}