Idle state loading for calendar.

This commit is contained in:
Burak Kaan Köse
2024-12-31 15:32:03 +01:00
parent 57d8fd7e10
commit 05ebfa68c9
4 changed files with 93 additions and 13 deletions

View File

@@ -195,6 +195,8 @@ namespace Wino.Calendar.ViewModels
private async Task InsertDayRangeModelAsync(DayRangeRenderModel dayRangeRenderModel, int index) private async Task InsertDayRangeModelAsync(DayRangeRenderModel dayRangeRenderModel, int index)
{ {
if (dayRangeRenderModel == null) return;
dayRangeRenderModel.CalendarDayEventCollectionUpdated -= EventsUpdatedInDayHeader; dayRangeRenderModel.CalendarDayEventCollectionUpdated -= EventsUpdatedInDayHeader;
dayRangeRenderModel.CalendarDayEventCollectionUpdated += EventsUpdatedInDayHeader; dayRangeRenderModel.CalendarDayEventCollectionUpdated += EventsUpdatedInDayHeader;
@@ -206,6 +208,8 @@ namespace Wino.Calendar.ViewModels
private async Task RemoveDayRangeModelAsync(DayRangeRenderModel dayRangeRenderModel) private async Task RemoveDayRangeModelAsync(DayRangeRenderModel dayRangeRenderModel)
{ {
if (dayRangeRenderModel == null) return;
dayRangeRenderModel.CalendarDayEventCollectionUpdated -= EventsUpdatedInDayHeader; dayRangeRenderModel.CalendarDayEventCollectionUpdated -= EventsUpdatedInDayHeader;
dayRangeRenderModel.UnregisterAll(); dayRangeRenderModel.UnregisterAll();
@@ -217,10 +221,18 @@ namespace Wino.Calendar.ViewModels
private async Task ClearDayRangeModelsAsync() private async Task ClearDayRangeModelsAsync()
{ {
while (DayRanges.Count > 0) // Unregister all events and clear the list directly.
foreach (var dayRangeModel in DayRanges)
{ {
await RemoveDayRangeModelAsync(DayRanges[0]); dayRangeModel.CalendarDayEventCollectionUpdated -= EventsUpdatedInDayHeader;
dayRangeModel.UnregisterAll();
} }
await ExecuteUIThread(() =>
{
DayRanges.Clear();
});
} }
private async Task RenderDatesAsync(CalendarInitInitiative calendarInitInitiative, private async Task RenderDatesAsync(CalendarInitInitiative calendarInitInitiative,

View File

@@ -10,6 +10,7 @@ namespace Wino.Calendar.Controls
public class WinoCalendarControl : Control public class WinoCalendarControl : Control
{ {
private const string PART_WinoFlipView = nameof(PART_WinoFlipView); private const string PART_WinoFlipView = nameof(PART_WinoFlipView);
private const string PART_IdleGrid = nameof(PART_IdleGrid);
public event EventHandler<TimelineCellSelectedArgs> TimelineCellSelected; public event EventHandler<TimelineCellSelectedArgs> TimelineCellSelected;
public event EventHandler<TimelineCellUnselectedArgs> TimelineCellUnselected; public event EventHandler<TimelineCellUnselectedArgs> TimelineCellUnselected;
@@ -20,6 +21,7 @@ namespace Wino.Calendar.Controls
public static readonly DependencyProperty SelectedFlipViewIndexProperty = DependencyProperty.Register(nameof(SelectedFlipViewIndex), typeof(int), typeof(WinoCalendarControl), new PropertyMetadata(-1)); public static readonly DependencyProperty SelectedFlipViewIndexProperty = DependencyProperty.Register(nameof(SelectedFlipViewIndex), typeof(int), typeof(WinoCalendarControl), new PropertyMetadata(-1));
public static readonly DependencyProperty SelectedFlipViewDayRangeProperty = DependencyProperty.Register(nameof(SelectedFlipViewDayRange), typeof(DayRangeRenderModel), typeof(WinoCalendarControl), new PropertyMetadata(null)); public static readonly DependencyProperty SelectedFlipViewDayRangeProperty = DependencyProperty.Register(nameof(SelectedFlipViewDayRange), typeof(DayRangeRenderModel), typeof(WinoCalendarControl), new PropertyMetadata(null));
public static readonly DependencyProperty ActiveCanvasProperty = DependencyProperty.Register(nameof(ActiveCanvas), typeof(WinoDayTimelineCanvas), typeof(WinoCalendarControl), new PropertyMetadata(null, new PropertyChangedCallback(OnActiveCanvasChanged))); public static readonly DependencyProperty ActiveCanvasProperty = DependencyProperty.Register(nameof(ActiveCanvas), typeof(WinoDayTimelineCanvas), typeof(WinoCalendarControl), new PropertyMetadata(null, new PropertyChangedCallback(OnActiveCanvasChanged)));
public static readonly DependencyProperty IsFlipIdleProperty = DependencyProperty.Register(nameof(IsFlipIdle), typeof(bool), typeof(WinoCalendarControl), new PropertyMetadata(true, new PropertyChangedCallback(OnIdleStateChanged)));
public DayRangeRenderModel SelectedFlipViewDayRange public DayRangeRenderModel SelectedFlipViewDayRange
{ {
@@ -33,6 +35,12 @@ namespace Wino.Calendar.Controls
set { SetValue(ActiveCanvasProperty, value); } set { SetValue(ActiveCanvasProperty, value); }
} }
public bool IsFlipIdle
{
get { return (bool)GetValue(IsFlipIdleProperty); }
set { SetValue(IsFlipIdleProperty, value); }
}
/// <summary> /// <summary>
/// Gets or sets the collection of day ranges to render. /// Gets or sets the collection of day ranges to render.
/// Each day range usually represents a week, but it may support other ranges. /// Each day range usually represents a week, but it may support other ranges.
@@ -52,6 +60,7 @@ namespace Wino.Calendar.Controls
#endregion #endregion
private WinoCalendarFlipView InternalFlipView; private WinoCalendarFlipView InternalFlipView;
private Grid IdleGrid;
public WinoCalendarControl() public WinoCalendarControl()
{ {
@@ -59,6 +68,14 @@ namespace Wino.Calendar.Controls
SizeChanged += CalendarSizeChanged; SizeChanged += CalendarSizeChanged;
} }
private static void OnIdleStateChanged(DependencyObject calendar, DependencyPropertyChangedEventArgs e)
{
if (calendar is WinoCalendarControl calendarControl)
{
calendarControl.UpdateIdleState();
}
}
private static void OnActiveCanvasChanged(DependencyObject calendar, DependencyPropertyChangedEventArgs e) private static void OnActiveCanvasChanged(DependencyObject calendar, DependencyPropertyChangedEventArgs e)
{ {
if (calendar is WinoCalendarControl calendarControl) if (calendar is WinoCalendarControl calendarControl)
@@ -120,6 +137,15 @@ namespace Wino.Calendar.Controls
base.OnApplyTemplate(); base.OnApplyTemplate();
InternalFlipView = GetTemplateChild(PART_WinoFlipView) as WinoCalendarFlipView; InternalFlipView = GetTemplateChild(PART_WinoFlipView) as WinoCalendarFlipView;
IdleGrid = GetTemplateChild(PART_IdleGrid) as Grid;
UpdateIdleState();
}
private void UpdateIdleState()
{
InternalFlipView.Visibility = IsFlipIdle ? Visibility.Collapsed : Visibility.Visible;
IdleGrid.Visibility = IsFlipIdle ? Visibility.Visible : Visibility.Collapsed;
} }
private void ActiveTimelineCellUnselected(object sender, TimelineCellUnselectedArgs e) private void ActiveTimelineCellUnselected(object sender, TimelineCellUnselectedArgs e)

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Specialized;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommunityToolkit.WinUI; using CommunityToolkit.WinUI;
@@ -11,17 +12,33 @@ namespace Wino.Calendar.Controls
{ {
public class WinoCalendarFlipView : CustomCalendarFlipView public class WinoCalendarFlipView : CustomCalendarFlipView
{ {
public static readonly DependencyProperty IsIdleProperty = DependencyProperty.Register(nameof(IsIdle), typeof(bool), typeof(WinoCalendarFlipView), new PropertyMetadata(true));
public static readonly DependencyProperty ActiveCanvasProperty = DependencyProperty.Register(nameof(ActiveCanvas), typeof(WinoDayTimelineCanvas), typeof(WinoCalendarFlipView), new PropertyMetadata(null));
public WinoDayTimelineCanvas ActiveCanvas public WinoDayTimelineCanvas ActiveCanvas
{ {
get { return (WinoDayTimelineCanvas)GetValue(ActiveCanvasProperty); } get { return (WinoDayTimelineCanvas)GetValue(ActiveCanvasProperty); }
set { SetValue(ActiveCanvasProperty, value); } set { SetValue(ActiveCanvasProperty, value); }
} }
public static readonly DependencyProperty ActiveCanvasProperty = DependencyProperty.Register(nameof(ActiveCanvas), typeof(WinoDayTimelineCanvas), typeof(WinoCalendarFlipView), new PropertyMetadata(null)); public bool IsIdle
{
get { return (bool)GetValue(IsIdleProperty); }
set { SetValue(IsIdleProperty, value); }
}
public WinoCalendarFlipView() public WinoCalendarFlipView()
{ {
RegisterPropertyChangedCallback(SelectedIndexProperty, new DependencyPropertyChangedCallback(OnSelectedIndexUpdated)); RegisterPropertyChangedCallback(SelectedIndexProperty, new DependencyPropertyChangedCallback(OnSelectedIndexUpdated));
RegisterPropertyChangedCallback(ItemsSourceProperty, new DependencyPropertyChangedCallback(OnItemsSourceChanged));
}
private static void OnItemsSourceChanged(DependencyObject d, DependencyProperty e)
{
if (d is WinoCalendarFlipView flipView)
{
flipView.RegisterItemsSourceChange();
}
} }
private static void OnSelectedIndexUpdated(DependencyObject d, DependencyProperty e) private static void OnSelectedIndexUpdated(DependencyObject d, DependencyProperty e)
@@ -32,6 +49,19 @@ namespace Wino.Calendar.Controls
} }
} }
private void RegisterItemsSourceChange()
{
if (GetItemsSource() is INotifyCollectionChanged notifyCollectionChanged)
{
notifyCollectionChanged.CollectionChanged += ItemsSourceUpdated;
}
}
private void ItemsSourceUpdated(object sender, NotifyCollectionChangedEventArgs e)
{
IsIdle = e.Action == NotifyCollectionChangedAction.Reset || e.Action == NotifyCollectionChangedAction.Replace;
}
public async void UpdateActiveCanvas() public async void UpdateActiveCanvas()
{ {
if (SelectedIndex < 0) if (SelectedIndex < 0)

View File

@@ -139,16 +139,28 @@
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
<ControlTemplate TargetType="controls:WinoCalendarControl"> <ControlTemplate TargetType="controls:WinoCalendarControl">
<Grid>
<controls:WinoCalendarFlipView <controls:WinoCalendarFlipView
x:Name="PART_WinoFlipView" x:Name="PART_WinoFlipView"
HorizontalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
ActiveCanvas="{x:Bind ActiveCanvas, Mode=TwoWay}" ActiveCanvas="{x:Bind ActiveCanvas, Mode=TwoWay}"
Background="Transparent" Background="Transparent"
IsIdle="{x:Bind IsFlipIdle, Mode=TwoWay}"
IsTabStop="False" IsTabStop="False"
ItemTemplate="{StaticResource FlipTemplate}" ItemTemplate="{StaticResource FlipTemplate}"
ItemsSource="{TemplateBinding DayRanges}" ItemsSource="{TemplateBinding DayRanges}"
SelectedIndex="{Binding SelectedFlipViewIndex, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay}" /> SelectedIndex="{Binding SelectedFlipViewIndex, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay}" />
<Grid x:Name="PART_IdleGrid" Visibility="Collapsed">
<muxc:ProgressRing
Width="50"
Height="50"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsActive="True" />
</Grid>
</Grid>
</ControlTemplate> </ControlTemplate>
</Setter.Value> </Setter.Value>
</Setter> </Setter>