Skip to content

Commit

Permalink
feat: Add calendar selection delegate for iOS and MacCatalyst platforms
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
VladislavAntonyuk committed Dec 24, 2023
1 parent e5e4a65 commit 18e62c3
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 14 deletions.
65 changes: 61 additions & 4 deletions MauiBells/Platforms/MacCatalyst/CalendarHandler.cs
Original file line number Diff line number Diff line change
@@ -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<ICalendarView, UICalendarView>
{
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;
}
}
13 changes: 7 additions & 6 deletions MauiBells/Platforms/Tizen/CalendarHandler.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
namespace MauiBells.Calendar;

using Microsoft.Maui.Handlers;
using Tizen.NUI.BaseComponents.View;

public partial class CalendarHandler : ViewHandler<ICalendarView, CalendarView>
public partial class CalendarHandler : ViewHandler<ICalendarView, Calendar>
{
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)
Expand All @@ -25,6 +26,6 @@ private static void MapMaxDate(CalendarHandler handler, ICalendarView virtualVie

private static void MapSelectedDate(CalendarHandler handler, ICalendarView virtualView)
{

}
}
66 changes: 62 additions & 4 deletions MauiBells/Platforms/iOS/CalendarHandler.cs
Original file line number Diff line number Diff line change
@@ -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<ICalendarView, UICalendarView>
{
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;
}
}

0 comments on commit 18e62c3

Please sign in to comment.