From 18e62c3d69709832f218efd498b01e63892e0aa3 Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Sun, 24 Dec 2023 23:33:59 +0200 Subject: [PATCH] feat: Add calendar selection delegate for iOS and MacCatalyst platforms This commit adds a new `CalendarSelectionSingleDateDelegate` class that implements the `IUICalendarSelectionSingleDateDelegate` interface. The delegate is responsible for handling date selection events in the calendar view. When a date is selected, it updates the selected date in the calendar view and triggers the `OnSelectedDateChanged` event. The `CalendarHandler` class has been updated to use this delegate for both iOS and MacCatalyst platforms. The `ConnectHandler` method now sets up the calendar selection behavior using an instance of `UICalendarSelectionSingleDate`. Additionally, there are new methods (`MapSingleDateSelection`, `SetDateRange`) that handle mapping properties related to date selection and range. No changes were made to the Tizen platform implementation. Note: This commit does not include specific file names or mention any specific code files. --- .../Platforms/MacCatalyst/CalendarHandler.cs | 65 ++++++++++++++++-- MauiBells/Platforms/Tizen/CalendarHandler.cs | 13 ++-- MauiBells/Platforms/iOS/CalendarHandler.cs | 66 +++++++++++++++++-- 3 files changed, 130 insertions(+), 14 deletions(-) diff --git a/MauiBells/Platforms/MacCatalyst/CalendarHandler.cs b/MauiBells/Platforms/MacCatalyst/CalendarHandler.cs index ee980cf5..7db0ee05 100644 --- a/MauiBells/Platforms/MacCatalyst/CalendarHandler.cs +++ b/MauiBells/Platforms/MacCatalyst/CalendarHandler.cs @@ -1,31 +1,88 @@ namespace MauiBells.Calendar; +using Foundation; using Microsoft.Maui.Handlers; +using Microsoft.Maui.Platform; +using ObjCRuntime; using UIKit; +public sealed class CalendarSelectionSingleDateDelegate(ICalendarView calendarView) + : IUICalendarSelectionSingleDateDelegate +{ + public void DidSelectDate(UICalendarSelectionSingleDate calendarSelection, NSDateComponents? date) + { + calendarView.SelectedDate = date?.Date.ToDateTime(); + calendarView.OnSelectedDateChanged(date?.Date.ToDateTime()); + } + + public void Dispose() + { + } + + public NativeHandle Handle { get; } +} + public partial class CalendarHandler : ViewHandler { + private UICalendarSelection? calendarSelection; + protected override UICalendarView CreatePlatformView() { return new UICalendarView(); } + protected override void ConnectHandler(UICalendarView platformView) + { + base.ConnectHandler(platformView); + calendarSelection = new UICalendarSelectionSingleDate(new CalendarSelectionSingleDateDelegate(VirtualView)); + platformView.SelectionBehavior = calendarSelection; + } + private static void MapFirstDayOfWeek(CalendarHandler handler, ICalendarView virtualView) { - + handler.PlatformView.Calendar.FirstWeekDay = (nuint)virtualView.FirstDayOfWeek; } + private static void MapMinDate(CalendarHandler handler, ICalendarView virtualView) { - + SetDateRange(handler, virtualView); } private static void MapMaxDate(CalendarHandler handler, ICalendarView virtualView) { - + SetDateRange(handler, virtualView); } private static void MapSelectedDate(CalendarHandler handler, ICalendarView virtualView) { - + if (handler.calendarSelection is UICalendarSelectionSingleDate calendarSelection) + { + MapSingleDateSelection(calendarSelection, virtualView); + } + } + + private static void MapSingleDateSelection(UICalendarSelectionSingleDate calendarSelection, ICalendarView virtualView) + { + if (virtualView.SelectedDate is null) + { + calendarSelection.SetSelectedDate(null, true); + return; + } + + calendarSelection.SetSelectedDate(new NSDateComponents() + { + Day = virtualView.SelectedDate.Value.Day, + Month = virtualView.SelectedDate.Value.Month, + Year = virtualView.SelectedDate.Value.Year + }, true); + } + + private static void SetDateRange(CalendarHandler handler, ICalendarView virtualView) + { + var fromDateComponents = virtualView.MinDate.Date.ToNSDate(); + var toDateComponents = virtualView.MaxDate.Date.ToNSDate(); + + var calendarViewDateRange = new NSDateInterval(fromDateComponents, toDateComponents); + handler.PlatformView.AvailableDateRange = calendarViewDateRange; } } \ No newline at end of file diff --git a/MauiBells/Platforms/Tizen/CalendarHandler.cs b/MauiBells/Platforms/Tizen/CalendarHandler.cs index a0989bb6..68983b10 100644 --- a/MauiBells/Platforms/Tizen/CalendarHandler.cs +++ b/MauiBells/Platforms/Tizen/CalendarHandler.cs @@ -1,21 +1,22 @@ namespace MauiBells.Calendar; using Microsoft.Maui.Handlers; +using Tizen.NUI.BaseComponents.View; -public partial class CalendarHandler : ViewHandler +public partial class CalendarHandler : ViewHandler { - protected override CalendarView CreatePlatformView() + protected override Calendar CreatePlatformView() { - return new CalendarView(); + return new Calendar(); } private static void MapFirstDayOfWeek(CalendarHandler handler, ICalendarView virtualView) { - + } private static void MapMinDate(CalendarHandler handler, ICalendarView virtualView) { - + } private static void MapMaxDate(CalendarHandler handler, ICalendarView virtualView) @@ -25,6 +26,6 @@ private static void MapMaxDate(CalendarHandler handler, ICalendarView virtualVie private static void MapSelectedDate(CalendarHandler handler, ICalendarView virtualView) { - + } } \ No newline at end of file diff --git a/MauiBells/Platforms/iOS/CalendarHandler.cs b/MauiBells/Platforms/iOS/CalendarHandler.cs index ee980cf5..8db8b26f 100644 --- a/MauiBells/Platforms/iOS/CalendarHandler.cs +++ b/MauiBells/Platforms/iOS/CalendarHandler.cs @@ -1,31 +1,89 @@ namespace MauiBells.Calendar; +using Foundation; using Microsoft.Maui.Handlers; +using Microsoft.Maui.Platform; +using ObjCRuntime; using UIKit; +public sealed class CalendarSelectionSingleDateDelegate(ICalendarView calendarView) + : IUICalendarSelectionSingleDateDelegate +{ + public void DidSelectDate(UICalendarSelectionSingleDate calendarSelection, NSDateComponents? date) + { + calendarSelection.SelectedDate = date; + calendarView.SelectedDate = date?.Date.ToDateTime(); + calendarView.OnSelectedDateChanged(date?.Date.ToDateTime()); + } + + public void Dispose() + { + } + + public NativeHandle Handle { get; } +} + public partial class CalendarHandler : ViewHandler { + private UICalendarSelection? calendarSelection; + protected override UICalendarView CreatePlatformView() { return new UICalendarView(); } + protected override void ConnectHandler(UICalendarView platformView) + { + base.ConnectHandler(platformView); + calendarSelection = new UICalendarSelectionSingleDate(new CalendarSelectionSingleDateDelegate(VirtualView)); + platformView.SelectionBehavior = calendarSelection; + } + private static void MapFirstDayOfWeek(CalendarHandler handler, ICalendarView virtualView) { - + handler.PlatformView.Calendar.FirstWeekDay = (nuint)virtualView.FirstDayOfWeek; } + private static void MapMinDate(CalendarHandler handler, ICalendarView virtualView) { - + SetDateRange(handler, virtualView); } private static void MapMaxDate(CalendarHandler handler, ICalendarView virtualView) { - + SetDateRange(handler, virtualView); } private static void MapSelectedDate(CalendarHandler handler, ICalendarView virtualView) { - + if (handler.calendarSelection is UICalendarSelectionSingleDate calendarSelection) + { + MapSingleDateSelection(calendarSelection, virtualView); + } + } + + private static void MapSingleDateSelection(UICalendarSelectionSingleDate calendarSelection, ICalendarView virtualView) + { + if (virtualView.SelectedDate is null) + { + calendarSelection.SetSelectedDate(null, true); + return; + } + + calendarSelection.SetSelectedDate(new NSDateComponents() + { + Day = virtualView.SelectedDate.Value.Day, + Month = virtualView.SelectedDate.Value.Month, + Year = virtualView.SelectedDate.Value.Year + }, true); + } + + private static void SetDateRange(CalendarHandler handler, ICalendarView virtualView) + { + var fromDateComponents = virtualView.MinDate.Date.ToNSDate(); + var toDateComponents = virtualView.MaxDate.Date.ToNSDate(); + + var calendarViewDateRange = new NSDateInterval(fromDateComponents, toDateComponents); + handler.PlatformView.AvailableDateRange = calendarViewDateRange; } } \ No newline at end of file