Handling of basic all-day events for calendar.

This commit is contained in:
Burak Kaan Köse
2024-12-29 13:06:35 +01:00
parent 979a3d8f1f
commit 8d8d7d0f8c
8 changed files with 165 additions and 12 deletions

View File

@@ -0,0 +1,54 @@
<UserControl
x:Class="Wino.Calendar.Controls.AllDayItemsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Wino.Calendar.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:domain="using:Wino.Core.Domain"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d">
<Grid>
<ScrollViewer x:Name="PART_AllDayItemsListScrollHost">
<ItemsControl
x:Name="PART_AllDayItemsList"
ItemTemplate="{x:Bind AllDayEventTemplate}"
ItemsSource="{x:Bind AllDayEvents, Mode=OneWay}" />
</ScrollViewer>
<Button
x:Name="AllDayItemsSummaryButton"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Visibility="Collapsed">
<Button.Flyout>
<Flyout Placement="Bottom">
<ScrollViewer>
<ItemsControl ItemTemplate="{x:Bind AllDayEventTemplate}" ItemsSource="{x:Bind AllDayEvents, Mode=OneWay}" />
</ScrollViewer>
</Flyout>
</Button.Flyout>
</Button>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ViewStates">
<VisualState x:Name="FullView" />
<VisualState x:Name="SummaryView">
<VisualState.Setters>
<Setter Target="PART_AllDayItemsListScrollHost.Visibility" Value="Collapsed" />
<Setter Target="PART_AllDayItemsList.Visibility" Value="Collapsed" />
<Setter Target="AllDayItemsSummaryButton.Visibility" Value="Visible" />
<Setter Target="AllDayItemsSummaryButton.Content">
<Setter.Value>
<TextBlock>
<Run Text="{x:Bind AllDayEvents.Count, Mode=OneWay, TargetNullValue='0'}" /> <Run Text="{x:Bind domain:Translator.CalendarAllDayEventSummary}" />
</TextBlock>
</Setter.Value>
</Setter>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</UserControl>

View File

@@ -0,0 +1,90 @@
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Wino.Core.Domain.Interfaces;
namespace Wino.Calendar.Controls
{
public sealed partial class AllDayItemsControl : UserControl
{
private const string STATE_SummaryView = "SummaryView";
private const string STATE_FullView = "FullView";
#region Dependency Properties
public static readonly DependencyProperty AllDayEventsProperty = DependencyProperty.Register(nameof(AllDayEvents), typeof(ObservableCollection<ICalendarItem>), typeof(AllDayItemsControl), new PropertyMetadata(null, new PropertyChangedCallback(OnAllDayEventsChanged)));
public static readonly DependencyProperty AllDayEventTemplateProperty = DependencyProperty.Register(nameof(AllDayEventTemplate), typeof(DataTemplate), typeof(AllDayItemsControl), new PropertyMetadata(null));
public DataTemplate AllDayEventTemplate
{
get { return (DataTemplate)GetValue(AllDayEventTemplateProperty); }
set { SetValue(AllDayEventTemplateProperty, value); }
}
public ObservableCollection<ICalendarItem> AllDayEvents
{
get { return (ObservableCollection<ICalendarItem>)GetValue(AllDayEventsProperty); }
set { SetValue(AllDayEventsProperty, value); }
}
#endregion
public AllDayItemsControl()
{
InitializeComponent();
}
private static void OnAllDayEventsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is AllDayItemsControl control)
{
if (e.OldValue != null && e.OldValue is ObservableCollection<ICalendarItem> oldCollection)
{
control.UnregisterEventCollectionChanged(oldCollection);
}
if (e.NewValue != null && e.NewValue is ObservableCollection<ICalendarItem> newCollection)
{
control.RegisterEventCollectionChanged(newCollection);
}
control.UpdateCollectionVisuals();
}
}
private void RegisterEventCollectionChanged(ObservableCollection<ICalendarItem> collection)
{
collection.CollectionChanged += EventsCollectionUpdated;
}
private void UnregisterEventCollectionChanged(ObservableCollection<ICalendarItem> collection)
{
collection.CollectionChanged -= EventsCollectionUpdated;
}
private void EventsCollectionUpdated(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
UpdateCollectionVisuals();
}
private void UpdateCollectionVisuals()
{
if (AllDayEvents == null) return;
if (AllDayEvents.Count > 1)
{
// Summarize
VisualStateManager.GoToState(this, STATE_SummaryView, false);
// AllDayItemsSummaryButton.Content = $"{AllDayEvents.Count} {Translator.CalendarAllDayEventSummary}";
}
else
{
// Full view.
VisualStateManager.GoToState(this, STATE_FullView, false);
}
}
}
}