From e8d1272921d49a5c59dc2d24ce9200ebb942f655 Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Wed, 20 Dec 2023 00:19:28 +0200 Subject: [PATCH] MauiCalendar Handlers --- Directory.Build.props | 4 +- MauiBells/Calendar/CalendarHandler.cs | 22 ++++++ MauiBells/Calendar/CalendarView.cs | 51 ++++++++++++++ .../Calendar/DateTimeOffsetStringConverter.cs | 17 +++++ MauiBells/Calendar/ICalendarView.cs | 10 +++ .../Calendar/SelectedDateChangedEventArgs.cs | 6 ++ MauiBells/MainPage.xaml | 30 +++++--- MauiBells/MainPage.xaml.cs | 7 ++ MauiBells/MauiProgram.cs | 8 ++- .../Platforms/Android/CalendarHandler.cs | 56 +++++++++++++++ MauiBells/Platforms/Android/MainActivity.cs | 6 +- .../Platforms/Android/MainApplication.cs | 6 +- .../Platforms/MacCatalyst/AppDelegate.cs | 4 +- .../Platforms/MacCatalyst/CalendarHandler.cs | 31 +++++++++ MauiBells/Platforms/MacCatalyst/Program.cs | 4 +- MauiBells/Platforms/Tizen/CalendarHandler.cs | 30 ++++++++ .../Platforms/Windows/CalendarHandler.cs | 68 +++++++++++++++++++ MauiBells/Platforms/iOS/AppDelegate.cs | 4 +- MauiBells/Platforms/iOS/CalendarHandler.cs | 31 +++++++++ MauiBells/Platforms/iOS/Program.cs | 4 +- MauiBells/README.md | 6 ++ MauiBells/README.mdpp | 6 ++ MauiBluetooth/MauiBluetooth.csproj | 15 ---- README.md | 2 +- README.mdpp | 2 +- 25 files changed, 388 insertions(+), 42 deletions(-) create mode 100644 MauiBells/Calendar/CalendarHandler.cs create mode 100644 MauiBells/Calendar/CalendarView.cs create mode 100644 MauiBells/Calendar/DateTimeOffsetStringConverter.cs create mode 100644 MauiBells/Calendar/ICalendarView.cs create mode 100644 MauiBells/Calendar/SelectedDateChangedEventArgs.cs create mode 100644 MauiBells/Platforms/Android/CalendarHandler.cs create mode 100644 MauiBells/Platforms/MacCatalyst/CalendarHandler.cs create mode 100644 MauiBells/Platforms/Tizen/CalendarHandler.cs create mode 100644 MauiBells/Platforms/Windows/CalendarHandler.cs create mode 100644 MauiBells/Platforms/iOS/CalendarHandler.cs diff --git a/Directory.Build.props b/Directory.Build.props index 0d831b6a..79bb1839 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -21,8 +21,8 @@ $(NoWarn);NU1603 enable - 15.0 - 15.0 + 16.0 + 16.0 23.0 10.0.17763.0 10.0.17763.0 diff --git a/MauiBells/Calendar/CalendarHandler.cs b/MauiBells/Calendar/CalendarHandler.cs new file mode 100644 index 00000000..e705640c --- /dev/null +++ b/MauiBells/Calendar/CalendarHandler.cs @@ -0,0 +1,22 @@ +namespace MauiBells.Calendar; + +public partial class CalendarHandler +{ + public static IPropertyMapper PropertyMapper = new PropertyMapper(ViewMapper) + { + [nameof(ICalendarView.FirstDayOfWeek)] = MapFirstDayOfWeek, + [nameof(ICalendarView.MinDate)] = MapMinDate, + [nameof(ICalendarView.MaxDate)] = MapMaxDate, + [nameof(ICalendarView.SelectedDate)] = MapSelectedDate, + }; + + public static CommandMapper CommandMapper = new(ViewCommandMapper); + + public CalendarHandler(IPropertyMapper mapper, CommandMapper? commandMapper = null) : base(mapper, commandMapper) + { + } + + public CalendarHandler() : this(PropertyMapper, CommandMapper) + { + } +} \ No newline at end of file diff --git a/MauiBells/Calendar/CalendarView.cs b/MauiBells/Calendar/CalendarView.cs new file mode 100644 index 00000000..f215bd3e --- /dev/null +++ b/MauiBells/Calendar/CalendarView.cs @@ -0,0 +1,51 @@ +namespace MauiBells.Calendar; + +using System.ComponentModel; + +public class CalendarView : View, ICalendarView +{ + readonly WeakEventManager calendarViewEventManager = new(); + + public static readonly BindableProperty FirstDayOfWeekProperty = BindableProperty.Create(nameof(FirstDayOfWeek), typeof(DayOfWeek), typeof(CalendarView), default(DayOfWeek)); + public static readonly BindableProperty MinDateProperty = BindableProperty.Create(nameof(MinDate), typeof(DateTimeOffset), typeof(CalendarView), DateTimeOffset.MinValue); + public static readonly BindableProperty MaxDateProperty = BindableProperty.Create(nameof(MaxDate), typeof(DateTimeOffset), typeof(CalendarView), DateTimeOffset.MaxValue); + public static readonly BindableProperty SelectedDateProperty = BindableProperty.Create(nameof(SelectedDate), typeof(DateTimeOffset?), typeof(CalendarView)); + + public DayOfWeek FirstDayOfWeek + { + get => (DayOfWeek)GetValue(FirstDayOfWeekProperty); + set => SetValue(FirstDayOfWeekProperty, value); + } + + [TypeConverter(typeof(DateTimeOffsetStringConverter))] + public DateTimeOffset MinDate + { + get => (DateTimeOffset)GetValue(MinDateProperty); + set => SetValue(MinDateProperty, value); + } + + [TypeConverter(typeof(DateTimeOffsetStringConverter))] + public DateTimeOffset MaxDate + { + get => (DateTimeOffset)GetValue(MaxDateProperty); + set => SetValue(MaxDateProperty, value); + } + + [TypeConverter(typeof(DateTimeOffsetStringConverter))] + public DateTimeOffset? SelectedDate + { + get => (DateTimeOffset?)GetValue(SelectedDateProperty); + set => SetValue(SelectedDateProperty, value); + } + + public event EventHandler SelectedDateChanged + { + add => calendarViewEventManager.AddEventHandler(value); + remove => calendarViewEventManager.RemoveEventHandler(value); + } + + void ICalendarView.OnSelectedDateChanged(DateTimeOffset? selectedDate) + { + calendarViewEventManager.HandleEvent(this, new SelectedDateChangedEventArgs(selectedDate), nameof(SelectedDateChanged)); + } +} \ No newline at end of file diff --git a/MauiBells/Calendar/DateTimeOffsetStringConverter.cs b/MauiBells/Calendar/DateTimeOffsetStringConverter.cs new file mode 100644 index 00000000..fefcade9 --- /dev/null +++ b/MauiBells/Calendar/DateTimeOffsetStringConverter.cs @@ -0,0 +1,17 @@ +namespace MauiBells.Calendar; + +using System.ComponentModel; +using System.Globalization; + +public class DateTimeOffsetStringConverter : TypeConverter +{ + public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) + { + if (value is string valueString) + { + return DateTimeOffset.Parse(valueString); + } + + return DateTimeOffset.MinValue; + } +} \ No newline at end of file diff --git a/MauiBells/Calendar/ICalendarView.cs b/MauiBells/Calendar/ICalendarView.cs new file mode 100644 index 00000000..74b4f280 --- /dev/null +++ b/MauiBells/Calendar/ICalendarView.cs @@ -0,0 +1,10 @@ +namespace MauiBells.Calendar; + +public interface ICalendarView : IView +{ + DayOfWeek FirstDayOfWeek { get; } + DateTimeOffset MinDate { get; } + DateTimeOffset MaxDate { get; } + DateTimeOffset? SelectedDate { get; set; } + void OnSelectedDateChanged(DateTimeOffset? selectedDate); +} \ No newline at end of file diff --git a/MauiBells/Calendar/SelectedDateChangedEventArgs.cs b/MauiBells/Calendar/SelectedDateChangedEventArgs.cs new file mode 100644 index 00000000..fd2311af --- /dev/null +++ b/MauiBells/Calendar/SelectedDateChangedEventArgs.cs @@ -0,0 +1,6 @@ +namespace MauiBells.Calendar; + +public class SelectedDateChangedEventArgs(DateTimeOffset? selectedDate) : EventArgs +{ + public DateTimeOffset? SelectedDate { get; } = selectedDate; +} \ No newline at end of file diff --git a/MauiBells/MainPage.xaml b/MauiBells/MainPage.xaml index b2b49ad8..b802fd61 100644 --- a/MauiBells/MainPage.xaml +++ b/MauiBells/MainPage.xaml @@ -1,6 +1,7 @@  @@ -16,6 +17,18 @@ Padding="30,0" VerticalOptions="Center"> + + + +