Skip to content

Commit

Permalink
Add property to automatically hide a day's events view if it has no e…
Browse files Browse the repository at this point in the history
…vents
  • Loading branch information
ME-MarvinE committed Mar 10, 2024
1 parent 174ae7b commit e7a4978
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 10 deletions.
2 changes: 1 addition & 1 deletion XCalendar.Forms/Views/DayView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
VerticalOptions="Center"
VerticalTextAlignment="{Binding VerticalTextAlignment, Source={x:Reference DayView_Unique}}"/>

<ContentView Grid.Row="1" ControlTemplate="{Binding EventsTemplate, Source={x:Reference DayView_Unique}}">
<ContentView x:Name="DayView_Unique_EventsView" Grid.Row="1" ControlTemplate="{Binding EventsTemplate, Source={x:Reference DayView_Unique}}">
<StackLayout
BindableLayout.ItemsSource="{Binding Events, Source={x:Reference DayView_Unique}}"
HorizontalOptions="Center"
Expand Down
58 changes: 55 additions & 3 deletions XCalendar.Forms/Views/DayView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
Expand Down Expand Up @@ -155,6 +157,11 @@ public StackOrientation EventsOrientation
get { return (StackOrientation)GetValue(EventsOrientationProperty); }
set { SetValue(EventsOrientationProperty, value); }
}
public bool AutoEventsViewVisibility
{
get { return (bool)GetValue(AutoEventsViewVisibilityProperty); }
set { SetValue(AutoEventsViewVisibilityProperty, value); }
}
public TextTransform TextTransform
{
get { return (TextTransform)GetValue(TextTransformProperty); }
Expand Down Expand Up @@ -269,14 +276,15 @@ public TextType TextType
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(DayView), Label.TextColorProperty.DefaultValue);
public static readonly BindableProperty VerticalTextAlignmentProperty = BindableProperty.Create(nameof(VerticalTextAlignment), typeof(TextAlignment), typeof(DayView), TextAlignment.Center);
public static readonly BindableProperty AutoSetStyleBasedOnDayStateProperty = BindableProperty.Create(nameof(AutoSetStyleBasedOnDayState), typeof(bool), typeof(DayView), true, propertyChanged: AutoSetStyleBasedOnDayStatePropertyChanged);
public static readonly BindableProperty EventsProperty = BindableProperty.Create(nameof(Events), typeof(IEnumerable<IEvent>), typeof(DayView));
public static readonly BindableProperty EventsTemplateProperty = BindableProperty.Create(nameof(EventsTemplate), typeof(ControlTemplate), typeof(DayView));
public static readonly BindableProperty EventTemplateProperty = BindableProperty.Create(nameof(EventTemplate), typeof(DataTemplate), typeof(DaysView));
public static readonly BindableProperty EventsProperty = BindableProperty.Create(nameof(Events), typeof(IEnumerable<IEvent>), typeof(DayView), propertyChanged: EventsPropertyChanged);
public static readonly BindableProperty EventsTemplateProperty = BindableProperty.Create(nameof(EventsTemplate), typeof(ControlTemplate), typeof(DayView), propertyChanged: EventsTemplatePropertyChanged);
public static readonly BindableProperty EventTemplateProperty = BindableProperty.Create(nameof(EventTemplate), typeof(DataTemplate), typeof(DaysView), propertyChanged: EventTemplatePropertyChanged);
public static readonly BindableProperty EventCornerRadiusProperty = BindableProperty.Create(nameof(EventCornerRadius), typeof(double), typeof(DayView), 100d);
public static readonly BindableProperty EventWidthRequestProperty = BindableProperty.Create(nameof(EventWidthRequest), typeof(double), typeof(DayView), 8d);
public static readonly BindableProperty EventHeightRequestProperty = BindableProperty.Create(nameof(EventHeightRequest), typeof(double), typeof(DayView), 8d);
public static readonly BindableProperty EventsSpacingProperty = BindableProperty.Create(nameof(EventsSpacing), typeof(double), typeof(DayView), 2.5d);
public static readonly BindableProperty EventsOrientationProperty = BindableProperty.Create(nameof(EventsOrientation), typeof(StackOrientation), typeof(DayView), StackOrientation.Horizontal);
public static readonly BindableProperty AutoEventsViewVisibilityProperty = BindableProperty.Create(nameof(AutoEventsViewVisibilityProperty), typeof(bool), typeof(DayView), true, propertyChanged: AutoEventsViewVisibilityPropertyChanged);

#endregion

Expand Down Expand Up @@ -352,6 +360,17 @@ public virtual void UpdateView()
}
}
}
private void UpdateEventsVisibility()
{
if (AutoEventsViewVisibility)
{
DayView_Unique_EventsView.IsVisible = Events?.Any() == true;
}
}
private void Events_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateEventsVisibility();
}

#region Bindable Properties Methods
private static object CreateDefaultDayViewCurrentMonthStyle(BindableObject bindable)
Expand Down Expand Up @@ -481,6 +500,39 @@ private static void AutoSetStyleBasedOnDayStatePropertyChanged(BindableObject bi
control.UpdateView();
}
}
private static void EventsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
DayView control = (DayView)bindable;
IEnumerable<IEvent> oldEvents = (IEnumerable<IEvent>)oldValue;
IEnumerable<IEvent> newEvents = (IEnumerable<IEvent>)newValue;

if (oldEvents is INotifyCollectionChanged oldEventsAsCollectionChanged)
{
oldEventsAsCollectionChanged.CollectionChanged -= control.Events_CollectionChanged;
}

if (newEvents is INotifyCollectionChanged newEventsAsCollectionChanged)
{
newEventsAsCollectionChanged.CollectionChanged += control.Events_CollectionChanged;
}

control.UpdateEventsVisibility();
}
private static void EventTemplatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
DayView control = (DayView)bindable;
control.UpdateEventsVisibility();
}
private static void EventsTemplatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
DayView control = (DayView)bindable;
control.UpdateEventsVisibility();
}
private static void AutoEventsViewVisibilityPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
DayView control = (DayView)bindable;
control.UpdateEventsVisibility();
}
private static object CoerceDayState(BindableObject bindable, object value)
{
DayView control = (DayView)bindable;
Expand Down
2 changes: 1 addition & 1 deletion XCalendar.Maui/Views/DayView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
VerticalOptions="Center"
VerticalTextAlignment="{Binding VerticalTextAlignment, Source={x:Reference DayView_Unique}}"/>

<ContentView Grid.Row="1" ControlTemplate="{Binding EventsTemplate, Source={x:Reference DayView_Unique}}">
<ContentView x:Name="DayView_Unique_EventsView" Grid.Row="1" ControlTemplate="{Binding EventsTemplate, Source={x:Reference DayView_Unique}}">
<StackLayout
BindableLayout.ItemsSource="{Binding Events, Source={x:Reference DayView_Unique}}"
HorizontalOptions="Center"
Expand Down
57 changes: 54 additions & 3 deletions XCalendar.Maui/Views/DayView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Specialized;
using System.Windows.Input;
using XCalendar.Core.Enums;
using XCalendar.Core.Interfaces;
Expand Down Expand Up @@ -150,6 +151,11 @@ public StackOrientation EventsOrientation
get { return (StackOrientation)GetValue(EventsOrientationProperty); }
set { SetValue(EventsOrientationProperty, value); }
}
public bool AutoEventsViewVisibility
{
get { return (bool)GetValue(AutoEventsViewVisibilityProperty); }
set { SetValue(AutoEventsViewVisibilityProperty, value); }
}
public TextTransform TextTransform
{
get { return (TextTransform)GetValue(TextTransformProperty); }
Expand Down Expand Up @@ -263,14 +269,15 @@ public TextType TextType
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(DayView), Label.TextColorProperty.DefaultValue);
public static readonly BindableProperty VerticalTextAlignmentProperty = BindableProperty.Create(nameof(VerticalTextAlignment), typeof(TextAlignment), typeof(DayView), TextAlignment.Center);
public static readonly BindableProperty AutoSetStyleBasedOnDayStateProperty = BindableProperty.Create(nameof(AutoSetStyleBasedOnDayState), typeof(bool), typeof(DayView), true, propertyChanged: AutoSetStyleBasedOnDayStatePropertyChanged);
public static readonly BindableProperty EventsProperty = BindableProperty.Create(nameof(Events), typeof(IEnumerable<IEvent>), typeof(DayView));
public static readonly BindableProperty EventsTemplateProperty = BindableProperty.Create(nameof(EventsTemplate), typeof(ControlTemplate), typeof(DayView));
public static readonly BindableProperty EventTemplateProperty = BindableProperty.Create(nameof(EventTemplate), typeof(DataTemplate), typeof(DaysView));
public static readonly BindableProperty EventsProperty = BindableProperty.Create(nameof(Events), typeof(IEnumerable<IEvent>), typeof(DayView), propertyChanged: EventsPropertyChanged);
public static readonly BindableProperty EventsTemplateProperty = BindableProperty.Create(nameof(EventsTemplate), typeof(ControlTemplate), typeof(DayView), propertyChanged: EventsTemplatePropertyChanged);
public static readonly BindableProperty EventTemplateProperty = BindableProperty.Create(nameof(EventTemplate), typeof(DataTemplate), typeof(DaysView), propertyChanged: EventTemplatePropertyChanged);
public static readonly BindableProperty EventCornerRadiusProperty = BindableProperty.Create(nameof(EventCornerRadius), typeof(double), typeof(DayView), 100d);
public static readonly BindableProperty EventWidthRequestProperty = BindableProperty.Create(nameof(EventWidthRequest), typeof(double), typeof(DayView), 8d);
public static readonly BindableProperty EventHeightRequestProperty = BindableProperty.Create(nameof(EventHeightRequest), typeof(double), typeof(DayView), 8d);
public static readonly BindableProperty EventsSpacingProperty = BindableProperty.Create(nameof(EventsSpacing), typeof(double), typeof(DayView), 2.5d);
public static readonly BindableProperty EventsOrientationProperty = BindableProperty.Create(nameof(EventsOrientation), typeof(StackOrientation), typeof(DayView), StackOrientation.Horizontal);
public static readonly BindableProperty AutoEventsViewVisibilityProperty = BindableProperty.Create(nameof(AutoEventsViewVisibilityProperty), typeof(bool), typeof(DayView), true, propertyChanged: AutoEventsViewVisibilityPropertyChanged);
#endregion

#endregion
Expand Down Expand Up @@ -345,6 +352,17 @@ public virtual void UpdateView()
}
}
}
private void UpdateEventsVisibility()
{
if (AutoEventsViewVisibility)
{
DayView_Unique_EventsView.IsVisible = Events?.Any() == true;
}
}
private void Events_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateEventsVisibility();
}

#region Bindable Properties Methods
private static object CreateDefaultDayViewCurrentMonthStyle(BindableObject bindable)
Expand Down Expand Up @@ -474,6 +492,39 @@ private static void AutoSetStyleBasedOnDayStatePropertyChanged(BindableObject bi
control.UpdateView();
}
}
private static void EventsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
DayView control = (DayView)bindable;
IEnumerable<IEvent> oldEvents = (IEnumerable<IEvent>)oldValue;
IEnumerable<IEvent> newEvents = (IEnumerable<IEvent>)newValue;

if (oldEvents is INotifyCollectionChanged oldEventsAsCollectionChanged)
{
oldEventsAsCollectionChanged.CollectionChanged -= control.Events_CollectionChanged;
}

if (newEvents is INotifyCollectionChanged newEventsAsCollectionChanged)
{
newEventsAsCollectionChanged.CollectionChanged += control.Events_CollectionChanged;
}

control.UpdateEventsVisibility();
}
private static void EventTemplatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
DayView control = (DayView)bindable;
control.UpdateEventsVisibility();
}
private static void EventsTemplatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
DayView control = (DayView)bindable;
control.UpdateEventsVisibility();
}
private static void AutoEventsViewVisibilityPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
DayView control = (DayView)bindable;
control.UpdateEventsVisibility();
}
private static object CoerceDayState(BindableObject bindable, object value)
{
DayView control = (DayView)bindable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
x:Class="XCalendarFormsSample.Views.PlaygroundPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Models="clr-namespace:XCalendar.Forms.Models;assembly=XCalendar.Forms"
xmlns:System="clr-namespace:System;assembly=System.Runtime"
xmlns:ViewModels="clr-namespace:XCalendarFormsSample.ViewModels"
xmlns:xc="clr-namespace:XCalendar.Forms.Views;assembly=XCalendar.Forms"
Expand Down
1 change: 0 additions & 1 deletion XCalendarMauiSample/Views/PlaygroundPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
x:Class="XCalendarMauiSample.Views.PlaygroundPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Models="clr-namespace:XCalendar.Maui.Models;assembly=XCalendar.Maui"
xmlns:System="clr-namespace:System;assembly=System.Runtime"
xmlns:ViewModels="clr-namespace:XCalendarMauiSample.ViewModels"
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Expand Down

0 comments on commit e7a4978

Please sign in to comment.