Skip to content

Commit

Permalink
MauiCalendar Handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Dec 19, 2023
1 parent a970bec commit e8d1272
Show file tree
Hide file tree
Showing 25 changed files with 388 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<NoWarn>$(NoWarn);NU1603</NoWarn>
<ImplicitUsings>enable</ImplicitUsings>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">16.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">16.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">23.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
Expand Down
22 changes: 22 additions & 0 deletions MauiBells/Calendar/CalendarHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace MauiBells.Calendar;

public partial class CalendarHandler
{
public static IPropertyMapper<ICalendarView, CalendarHandler> PropertyMapper = new PropertyMapper<ICalendarView, CalendarHandler>(ViewMapper)
{
[nameof(ICalendarView.FirstDayOfWeek)] = MapFirstDayOfWeek,
[nameof(ICalendarView.MinDate)] = MapMinDate,
[nameof(ICalendarView.MaxDate)] = MapMaxDate,
[nameof(ICalendarView.SelectedDate)] = MapSelectedDate,
};

public static CommandMapper<ICalendarView, CalendarHandler> CommandMapper = new(ViewCommandMapper);

public CalendarHandler(IPropertyMapper mapper, CommandMapper? commandMapper = null) : base(mapper, commandMapper)
{
}

public CalendarHandler() : this(PropertyMapper, CommandMapper)
{
}
}
51 changes: 51 additions & 0 deletions MauiBells/Calendar/CalendarView.cs
Original file line number Diff line number Diff line change
@@ -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<SelectedDateChangedEventArgs> SelectedDateChanged
{
add => calendarViewEventManager.AddEventHandler(value);
remove => calendarViewEventManager.RemoveEventHandler(value);
}

void ICalendarView.OnSelectedDateChanged(DateTimeOffset? selectedDate)
{
calendarViewEventManager.HandleEvent(this, new SelectedDateChangedEventArgs(selectedDate), nameof(SelectedDateChanged));
}
}
17 changes: 17 additions & 0 deletions MauiBells/Calendar/DateTimeOffsetStringConverter.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions MauiBells/Calendar/ICalendarView.cs
Original file line number Diff line number Diff line change
@@ -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);
}
6 changes: 6 additions & 0 deletions MauiBells/Calendar/SelectedDateChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MauiBells.Calendar;

public class SelectedDateChangedEventArgs(DateTimeOffset? selectedDate) : EventArgs
{
public DateTimeOffset? SelectedDate { get; } = selectedDate;
}
30 changes: 22 additions & 8 deletions MauiBells/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:calendar="clr-namespace:MauiBells.Calendar"
x:Class="MauiBells.MainPage">

<ContentPage.Resources>
Expand All @@ -16,34 +17,47 @@
Padding="30,0"
VerticalOptions="Center">

<calendar:CalendarView
x:Name="CalendarView"
HorizontalOptions="Center"
FirstDayOfWeek="Monday"
MinDate="1/1/2023"
MaxDate="12/31/2024"
SelectedDate="12/31/2023"
SelectedDateChanged="CalendarView_OnSelectedDateChanged">
</calendar:CalendarView>

<Label x:Name="SelectedDateLabel" />

<HorizontalStackLayout
HorizontalOptions="Center">
<Label x:Name="Year" Text="2022"/>
<Label Text="/"/>
<Label x:Name="Month" Text="12"/>
<Label Text="/"/>
<Label x:Name="Day" Text="31"/>
<Label Text=" "/>
<Label Text="T"/>
<Label x:Name="Hour" Text="12"/>
<Label Text=":"/>
<Label x:Name="Minute" Text="12"/>
<Label Text=":"/>
<Label x:Name="Second" Text="12"/>
</HorizontalStackLayout>

<Image
x:Name="Bell"
Source="bell.png"
HeightRequest="200"
HorizontalOptions="Center" />

<Label Text="Shake me!" />

<Image
Source="dotnet.png"
WidthRequest="200"
HeightRequest="200"
HorizontalOptions="Center" />

<Label Text="Shake me!" />

<Image
x:Name="Bell"
Source="bell.png"
HeightRequest="200"
HorizontalOptions="Center" />
</VerticalStackLayout>

</ScrollView>
Expand Down
7 changes: 7 additions & 0 deletions MauiBells/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace MauiBells;

using Calendar;
using Plugin.Maui.Audio;

public sealed partial class MainPage : ContentPage, IDisposable
Expand Down Expand Up @@ -66,6 +67,7 @@ private void ShakeDetected(object? sender, EventArgs e)

public void Dispose()
{
CalendarView.Handler?.DisconnectHandler();
Accelerometer.Default.ShakeDetected -= ShakeDetected;
Accelerometer.Default.ReadingChanged -= ReadingChanged;
Accelerometer.Default.Stop();
Expand All @@ -85,4 +87,9 @@ await Dispatcher.DispatchAsync(() =>
await label.RotateXTo(0, 500);
}
}

private void CalendarView_OnSelectedDateChanged(object? sender, SelectedDateChangedEventArgs e)
{
SelectedDateLabel.Text = $"Selected Date: {e.SelectedDate}";
}
}
8 changes: 7 additions & 1 deletion MauiBells/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
namespace MauiBells;

using Calendar;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>();
builder.UseMauiApp<App>()
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<CalendarView, CalendarHandler>();
});

return builder.Build();
}
Expand Down
56 changes: 56 additions & 0 deletions MauiBells/Platforms/Android/CalendarHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace MauiBells.Calendar;

using Microsoft.Maui.Handlers;
using Calendar = Android.Widget.CalendarView;

public partial class CalendarHandler : ViewHandler<ICalendarView, Calendar>
{
protected override Calendar CreatePlatformView()
{
return new Calendar(Context);
}
protected override void ConnectHandler(Calendar platformView)
{
base.ConnectHandler(platformView);
platformView.DateChange += PlatformView_SelectedDatesChanged;
}

private void PlatformView_SelectedDatesChanged(object? sender, Calendar.DateChangeEventArgs e)
{
PlatformView.DateChange -= PlatformView_SelectedDatesChanged;
VirtualView.SelectedDate = new DateTime(e.Year, e.Month + 1, e.DayOfMonth, 0,0,0);
VirtualView.OnSelectedDateChanged(VirtualView.SelectedDate);
PlatformView.DateChange += PlatformView_SelectedDatesChanged;
}

protected override void DisconnectHandler(Calendar platformView)
{
platformView.DateChange -= PlatformView_SelectedDatesChanged;
base.DisconnectHandler(platformView);
}

private static void MapFirstDayOfWeek(CalendarHandler handler, ICalendarView virtualView)
{
handler.PlatformView.FirstDayOfWeek = (int)virtualView.FirstDayOfWeek;
}

private static void MapMinDate(CalendarHandler handler, ICalendarView virtualView)
{
handler.PlatformView.MinDate = virtualView.MinDate.ToUnixTimeMilliseconds();
}

private static void MapMaxDate(CalendarHandler handler, ICalendarView virtualView)
{
handler.PlatformView.MaxDate = virtualView.MaxDate.ToUnixTimeMilliseconds();
}

private static void MapSelectedDate(CalendarHandler handler, ICalendarView virtualView)
{
if (virtualView.SelectedDate is null)
{
return;
}

handler.PlatformView.SetDate(virtualView.SelectedDate.Value.ToUnixTimeMilliseconds(), true, true);
}
}
6 changes: 3 additions & 3 deletions MauiBells/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Android.App;
using Android.Content.PM;
namespace MauiBells;

namespace MauiBells;
using Android.App;
using Android.Content.PM;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
Expand Down
6 changes: 3 additions & 3 deletions MauiBells/Platforms/Android/MainApplication.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Android.App;
using Android.Runtime;
namespace MauiBells;

namespace MauiBells;
using Android.App;
using Android.Runtime;

[Application]
public class MainApplication : MauiApplication
Expand Down
4 changes: 2 additions & 2 deletions MauiBells/Platforms/MacCatalyst/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Foundation;
namespace MauiBells;

namespace MauiBells;
using Foundation;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
Expand Down
31 changes: 31 additions & 0 deletions MauiBells/Platforms/MacCatalyst/CalendarHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace MauiBells.Calendar;

using Microsoft.Maui.Handlers;
using UIKit;

public partial class CalendarHandler : ViewHandler<ICalendarView, UICalendarView>
{
protected override UICalendarView CreatePlatformView()
{
return new UICalendarView();
}

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

}
private static void MapMinDate(CalendarHandler handler, ICalendarView virtualView)
{

}

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

}

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

}
}
4 changes: 2 additions & 2 deletions MauiBells/Platforms/MacCatalyst/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UIKit;
namespace MauiBells;

namespace MauiBells;
using UIKit;

public class Program
{
Expand Down
30 changes: 30 additions & 0 deletions MauiBells/Platforms/Tizen/CalendarHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace MauiBells.Calendar;

using Microsoft.Maui.Handlers;

public partial class CalendarHandler : ViewHandler<ICalendarView, CalendarView>
{
protected override CalendarView CreatePlatformView()
{
return new CalendarView();
}

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

}
private static void MapMinDate(CalendarHandler handler, ICalendarView virtualView)
{

}

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

}

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

}
}
Loading

0 comments on commit e8d1272

Please sign in to comment.