Skip to content

Commit e7a4978

Browse files
committed
Add property to automatically hide a day's events view if it has no events
1 parent 174ae7b commit e7a4978

File tree

6 files changed

+111
-10
lines changed

6 files changed

+111
-10
lines changed

XCalendar.Forms/Views/DayView.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
VerticalOptions="Center"
3737
VerticalTextAlignment="{Binding VerticalTextAlignment, Source={x:Reference DayView_Unique}}"/>
3838

39-
<ContentView Grid.Row="1" ControlTemplate="{Binding EventsTemplate, Source={x:Reference DayView_Unique}}">
39+
<ContentView x:Name="DayView_Unique_EventsView" Grid.Row="1" ControlTemplate="{Binding EventsTemplate, Source={x:Reference DayView_Unique}}">
4040
<StackLayout
4141
BindableLayout.ItemsSource="{Binding Events, Source={x:Reference DayView_Unique}}"
4242
HorizontalOptions="Center"

XCalendar.Forms/Views/DayView.xaml.cs

+55-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Specialized;
4+
using System.Linq;
35
using System.Windows.Input;
46
using Xamarin.Forms;
57
using Xamarin.Forms.Xaml;
@@ -155,6 +157,11 @@ public StackOrientation EventsOrientation
155157
get { return (StackOrientation)GetValue(EventsOrientationProperty); }
156158
set { SetValue(EventsOrientationProperty, value); }
157159
}
160+
public bool AutoEventsViewVisibility
161+
{
162+
get { return (bool)GetValue(AutoEventsViewVisibilityProperty); }
163+
set { SetValue(AutoEventsViewVisibilityProperty, value); }
164+
}
158165
public TextTransform TextTransform
159166
{
160167
get { return (TextTransform)GetValue(TextTransformProperty); }
@@ -269,14 +276,15 @@ public TextType TextType
269276
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(DayView), Label.TextColorProperty.DefaultValue);
270277
public static readonly BindableProperty VerticalTextAlignmentProperty = BindableProperty.Create(nameof(VerticalTextAlignment), typeof(TextAlignment), typeof(DayView), TextAlignment.Center);
271278
public static readonly BindableProperty AutoSetStyleBasedOnDayStateProperty = BindableProperty.Create(nameof(AutoSetStyleBasedOnDayState), typeof(bool), typeof(DayView), true, propertyChanged: AutoSetStyleBasedOnDayStatePropertyChanged);
272-
public static readonly BindableProperty EventsProperty = BindableProperty.Create(nameof(Events), typeof(IEnumerable<IEvent>), typeof(DayView));
273-
public static readonly BindableProperty EventsTemplateProperty = BindableProperty.Create(nameof(EventsTemplate), typeof(ControlTemplate), typeof(DayView));
274-
public static readonly BindableProperty EventTemplateProperty = BindableProperty.Create(nameof(EventTemplate), typeof(DataTemplate), typeof(DaysView));
279+
public static readonly BindableProperty EventsProperty = BindableProperty.Create(nameof(Events), typeof(IEnumerable<IEvent>), typeof(DayView), propertyChanged: EventsPropertyChanged);
280+
public static readonly BindableProperty EventsTemplateProperty = BindableProperty.Create(nameof(EventsTemplate), typeof(ControlTemplate), typeof(DayView), propertyChanged: EventsTemplatePropertyChanged);
281+
public static readonly BindableProperty EventTemplateProperty = BindableProperty.Create(nameof(EventTemplate), typeof(DataTemplate), typeof(DaysView), propertyChanged: EventTemplatePropertyChanged);
275282
public static readonly BindableProperty EventCornerRadiusProperty = BindableProperty.Create(nameof(EventCornerRadius), typeof(double), typeof(DayView), 100d);
276283
public static readonly BindableProperty EventWidthRequestProperty = BindableProperty.Create(nameof(EventWidthRequest), typeof(double), typeof(DayView), 8d);
277284
public static readonly BindableProperty EventHeightRequestProperty = BindableProperty.Create(nameof(EventHeightRequest), typeof(double), typeof(DayView), 8d);
278285
public static readonly BindableProperty EventsSpacingProperty = BindableProperty.Create(nameof(EventsSpacing), typeof(double), typeof(DayView), 2.5d);
279286
public static readonly BindableProperty EventsOrientationProperty = BindableProperty.Create(nameof(EventsOrientation), typeof(StackOrientation), typeof(DayView), StackOrientation.Horizontal);
287+
public static readonly BindableProperty AutoEventsViewVisibilityProperty = BindableProperty.Create(nameof(AutoEventsViewVisibilityProperty), typeof(bool), typeof(DayView), true, propertyChanged: AutoEventsViewVisibilityPropertyChanged);
280288

281289
#endregion
282290

@@ -352,6 +360,17 @@ public virtual void UpdateView()
352360
}
353361
}
354362
}
363+
private void UpdateEventsVisibility()
364+
{
365+
if (AutoEventsViewVisibility)
366+
{
367+
DayView_Unique_EventsView.IsVisible = Events?.Any() == true;
368+
}
369+
}
370+
private void Events_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
371+
{
372+
UpdateEventsVisibility();
373+
}
355374

356375
#region Bindable Properties Methods
357376
private static object CreateDefaultDayViewCurrentMonthStyle(BindableObject bindable)
@@ -481,6 +500,39 @@ private static void AutoSetStyleBasedOnDayStatePropertyChanged(BindableObject bi
481500
control.UpdateView();
482501
}
483502
}
503+
private static void EventsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
504+
{
505+
DayView control = (DayView)bindable;
506+
IEnumerable<IEvent> oldEvents = (IEnumerable<IEvent>)oldValue;
507+
IEnumerable<IEvent> newEvents = (IEnumerable<IEvent>)newValue;
508+
509+
if (oldEvents is INotifyCollectionChanged oldEventsAsCollectionChanged)
510+
{
511+
oldEventsAsCollectionChanged.CollectionChanged -= control.Events_CollectionChanged;
512+
}
513+
514+
if (newEvents is INotifyCollectionChanged newEventsAsCollectionChanged)
515+
{
516+
newEventsAsCollectionChanged.CollectionChanged += control.Events_CollectionChanged;
517+
}
518+
519+
control.UpdateEventsVisibility();
520+
}
521+
private static void EventTemplatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
522+
{
523+
DayView control = (DayView)bindable;
524+
control.UpdateEventsVisibility();
525+
}
526+
private static void EventsTemplatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
527+
{
528+
DayView control = (DayView)bindable;
529+
control.UpdateEventsVisibility();
530+
}
531+
private static void AutoEventsViewVisibilityPropertyChanged(BindableObject bindable, object oldValue, object newValue)
532+
{
533+
DayView control = (DayView)bindable;
534+
control.UpdateEventsVisibility();
535+
}
484536
private static object CoerceDayState(BindableObject bindable, object value)
485537
{
486538
DayView control = (DayView)bindable;

XCalendar.Maui/Views/DayView.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
VerticalOptions="Center"
3737
VerticalTextAlignment="{Binding VerticalTextAlignment, Source={x:Reference DayView_Unique}}"/>
3838

39-
<ContentView Grid.Row="1" ControlTemplate="{Binding EventsTemplate, Source={x:Reference DayView_Unique}}">
39+
<ContentView x:Name="DayView_Unique_EventsView" Grid.Row="1" ControlTemplate="{Binding EventsTemplate, Source={x:Reference DayView_Unique}}">
4040
<StackLayout
4141
BindableLayout.ItemsSource="{Binding Events, Source={x:Reference DayView_Unique}}"
4242
HorizontalOptions="Center"

XCalendar.Maui/Views/DayView.xaml.cs

+54-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Specialized;
12
using System.Windows.Input;
23
using XCalendar.Core.Enums;
34
using XCalendar.Core.Interfaces;
@@ -150,6 +151,11 @@ public StackOrientation EventsOrientation
150151
get { return (StackOrientation)GetValue(EventsOrientationProperty); }
151152
set { SetValue(EventsOrientationProperty, value); }
152153
}
154+
public bool AutoEventsViewVisibility
155+
{
156+
get { return (bool)GetValue(AutoEventsViewVisibilityProperty); }
157+
set { SetValue(AutoEventsViewVisibilityProperty, value); }
158+
}
153159
public TextTransform TextTransform
154160
{
155161
get { return (TextTransform)GetValue(TextTransformProperty); }
@@ -263,14 +269,15 @@ public TextType TextType
263269
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(DayView), Label.TextColorProperty.DefaultValue);
264270
public static readonly BindableProperty VerticalTextAlignmentProperty = BindableProperty.Create(nameof(VerticalTextAlignment), typeof(TextAlignment), typeof(DayView), TextAlignment.Center);
265271
public static readonly BindableProperty AutoSetStyleBasedOnDayStateProperty = BindableProperty.Create(nameof(AutoSetStyleBasedOnDayState), typeof(bool), typeof(DayView), true, propertyChanged: AutoSetStyleBasedOnDayStatePropertyChanged);
266-
public static readonly BindableProperty EventsProperty = BindableProperty.Create(nameof(Events), typeof(IEnumerable<IEvent>), typeof(DayView));
267-
public static readonly BindableProperty EventsTemplateProperty = BindableProperty.Create(nameof(EventsTemplate), typeof(ControlTemplate), typeof(DayView));
268-
public static readonly BindableProperty EventTemplateProperty = BindableProperty.Create(nameof(EventTemplate), typeof(DataTemplate), typeof(DaysView));
272+
public static readonly BindableProperty EventsProperty = BindableProperty.Create(nameof(Events), typeof(IEnumerable<IEvent>), typeof(DayView), propertyChanged: EventsPropertyChanged);
273+
public static readonly BindableProperty EventsTemplateProperty = BindableProperty.Create(nameof(EventsTemplate), typeof(ControlTemplate), typeof(DayView), propertyChanged: EventsTemplatePropertyChanged);
274+
public static readonly BindableProperty EventTemplateProperty = BindableProperty.Create(nameof(EventTemplate), typeof(DataTemplate), typeof(DaysView), propertyChanged: EventTemplatePropertyChanged);
269275
public static readonly BindableProperty EventCornerRadiusProperty = BindableProperty.Create(nameof(EventCornerRadius), typeof(double), typeof(DayView), 100d);
270276
public static readonly BindableProperty EventWidthRequestProperty = BindableProperty.Create(nameof(EventWidthRequest), typeof(double), typeof(DayView), 8d);
271277
public static readonly BindableProperty EventHeightRequestProperty = BindableProperty.Create(nameof(EventHeightRequest), typeof(double), typeof(DayView), 8d);
272278
public static readonly BindableProperty EventsSpacingProperty = BindableProperty.Create(nameof(EventsSpacing), typeof(double), typeof(DayView), 2.5d);
273279
public static readonly BindableProperty EventsOrientationProperty = BindableProperty.Create(nameof(EventsOrientation), typeof(StackOrientation), typeof(DayView), StackOrientation.Horizontal);
280+
public static readonly BindableProperty AutoEventsViewVisibilityProperty = BindableProperty.Create(nameof(AutoEventsViewVisibilityProperty), typeof(bool), typeof(DayView), true, propertyChanged: AutoEventsViewVisibilityPropertyChanged);
274281
#endregion
275282

276283
#endregion
@@ -345,6 +352,17 @@ public virtual void UpdateView()
345352
}
346353
}
347354
}
355+
private void UpdateEventsVisibility()
356+
{
357+
if (AutoEventsViewVisibility)
358+
{
359+
DayView_Unique_EventsView.IsVisible = Events?.Any() == true;
360+
}
361+
}
362+
private void Events_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
363+
{
364+
UpdateEventsVisibility();
365+
}
348366

349367
#region Bindable Properties Methods
350368
private static object CreateDefaultDayViewCurrentMonthStyle(BindableObject bindable)
@@ -474,6 +492,39 @@ private static void AutoSetStyleBasedOnDayStatePropertyChanged(BindableObject bi
474492
control.UpdateView();
475493
}
476494
}
495+
private static void EventsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
496+
{
497+
DayView control = (DayView)bindable;
498+
IEnumerable<IEvent> oldEvents = (IEnumerable<IEvent>)oldValue;
499+
IEnumerable<IEvent> newEvents = (IEnumerable<IEvent>)newValue;
500+
501+
if (oldEvents is INotifyCollectionChanged oldEventsAsCollectionChanged)
502+
{
503+
oldEventsAsCollectionChanged.CollectionChanged -= control.Events_CollectionChanged;
504+
}
505+
506+
if (newEvents is INotifyCollectionChanged newEventsAsCollectionChanged)
507+
{
508+
newEventsAsCollectionChanged.CollectionChanged += control.Events_CollectionChanged;
509+
}
510+
511+
control.UpdateEventsVisibility();
512+
}
513+
private static void EventTemplatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
514+
{
515+
DayView control = (DayView)bindable;
516+
control.UpdateEventsVisibility();
517+
}
518+
private static void EventsTemplatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
519+
{
520+
DayView control = (DayView)bindable;
521+
control.UpdateEventsVisibility();
522+
}
523+
private static void AutoEventsViewVisibilityPropertyChanged(BindableObject bindable, object oldValue, object newValue)
524+
{
525+
DayView control = (DayView)bindable;
526+
control.UpdateEventsVisibility();
527+
}
477528
private static object CoerceDayState(BindableObject bindable, object value)
478529
{
479530
DayView control = (DayView)bindable;

XCalendarFormsSample/XCalendarFormsSample/Views/PlaygroundPage.xaml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
x:Class="XCalendarFormsSample.Views.PlaygroundPage"
44
xmlns="http://xamarin.com/schemas/2014/forms"
55
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6-
xmlns:Models="clr-namespace:XCalendar.Forms.Models;assembly=XCalendar.Forms"
76
xmlns:System="clr-namespace:System;assembly=System.Runtime"
87
xmlns:ViewModels="clr-namespace:XCalendarFormsSample.ViewModels"
98
xmlns:xc="clr-namespace:XCalendar.Forms.Views;assembly=XCalendar.Forms"

XCalendarMauiSample/Views/PlaygroundPage.xaml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
x:Class="XCalendarMauiSample.Views.PlaygroundPage"
44
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
55
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6-
xmlns:Models="clr-namespace:XCalendar.Maui.Models;assembly=XCalendar.Maui"
76
xmlns:System="clr-namespace:System;assembly=System.Runtime"
87
xmlns:ViewModels="clr-namespace:XCalendarMauiSample.ViewModels"
98
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

0 commit comments

Comments
 (0)