Files
Wino-Mail/Wino.Calendar/Views/CalendarPage.xaml.cs

161 lines
5.6 KiB
C#
Raw Normal View History

using System;
using CommunityToolkit.Mvvm.Messaging;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Navigation;
using Wino.Calendar.Args;
using Wino.Calendar.Views.Abstract;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Models.Calendar;
using Wino.Messaging.Client.Calendar;
2025-05-18 14:06:25 +02:00
namespace Wino.Calendar.Views;
public sealed partial class CalendarPage : CalendarPageAbstract,
IRecipient<ScrollToDateMessage>,
IRecipient<ScrollToHourMessage>,
IRecipient<GoNextDateRequestedMessage>,
IRecipient<GoPreviousDateRequestedMessage>
{
2025-05-18 14:06:25 +02:00
private const int PopupDialogOffset = 12;
2025-01-01 19:17:54 +01:00
2025-05-18 14:06:25 +02:00
public CalendarPage()
{
InitializeComponent();
NavigationCacheMode = NavigationCacheMode.Enabled;
2025-05-18 14:06:25 +02:00
ViewModel.DetailsShowCalendarItemChanged += CalendarItemDetailContextChanged;
}
2025-05-18 14:06:25 +02:00
private void CalendarItemDetailContextChanged(object sender, EventArgs e)
{
if (ViewModel.DisplayDetailsCalendarItemViewModel != null)
{
2025-05-18 14:06:25 +02:00
var control = CalendarControl.GetCalendarItemControl(ViewModel.DisplayDetailsCalendarItemViewModel);
2025-05-18 14:06:25 +02:00
if (control != null)
{
EventDetailsPopup.PlacementTarget = control;
}
}
2025-05-18 14:06:25 +02:00
}
2025-05-18 14:06:25 +02:00
public void Receive(ScrollToHourMessage message) => CalendarControl.NavigateToHour(message.TimeSpan);
public void Receive(ScrollToDateMessage message) => CalendarControl.NavigateToDay(message.Date);
public void Receive(GoNextDateRequestedMessage message) => CalendarControl.GoNextRange();
public void Receive(GoPreviousDateRequestedMessage message) => CalendarControl.GoPreviousRange();
2025-05-18 14:06:25 +02:00
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
2025-05-18 14:06:25 +02:00
if (e.NavigationMode == NavigationMode.Back) return;
2025-05-18 14:06:25 +02:00
if (e.Parameter is CalendarPageNavigationArgs args)
{
if (args.RequestDefaultNavigation)
{
2025-05-18 14:06:25 +02:00
// Go today.
WeakReferenceMessenger.Default.Send(new LoadCalendarMessage(DateTime.Now.Date, CalendarInitInitiative.App));
}
else
{
// Go specified date.
WeakReferenceMessenger.Default.Send(new LoadCalendarMessage(args.NavigationDate, CalendarInitInitiative.User));
}
}
2025-05-18 14:06:25 +02:00
}
2025-05-18 14:06:25 +02:00
private void CellSelected(object sender, TimelineCellSelectedArgs e)
{
// Dismiss event details if exists and cancel the selection.
// This is to prevent the event details from being displayed when the user clicks somewhere else.
if (EventDetailsPopup.IsOpen)
{
2025-05-18 14:06:25 +02:00
CalendarControl.UnselectActiveTimelineCell();
ViewModel.DisplayDetailsCalendarItemViewModel = null;
2025-05-18 14:06:25 +02:00
return;
}
2025-05-18 14:06:25 +02:00
ViewModel.SelectedQuickEventDate = e.ClickedDate;
2025-05-18 14:06:25 +02:00
TeachingTipPositionerGrid.Width = e.CellSize.Width;
TeachingTipPositionerGrid.Height = e.CellSize.Height;
2025-05-18 14:06:25 +02:00
Canvas.SetLeft(TeachingTipPositionerGrid, e.PositionerPoint.X);
Canvas.SetTop(TeachingTipPositionerGrid, e.PositionerPoint.Y);
2025-05-18 14:06:25 +02:00
// Adjust the start and end time in the flyout.
var startTime = ViewModel.SelectedQuickEventDate.Value.TimeOfDay;
var endTime = startTime.Add(TimeSpan.FromMinutes(30));
2025-05-18 14:06:25 +02:00
ViewModel.SelectQuickEventTimeRange(startTime, endTime);
2025-01-01 17:28:29 +01:00
2025-05-18 14:06:25 +02:00
QuickEventPopupDialog.IsOpen = true;
}
2025-01-01 17:28:29 +01:00
2025-05-18 14:06:25 +02:00
private void CellUnselected(object sender, TimelineCellUnselectedArgs e)
{
QuickEventPopupDialog.IsOpen = false;
}
2025-05-18 14:06:25 +02:00
private void QuickEventAccountSelectorSelectionChanged(object sender, SelectionChangedEventArgs e)
{
QuickEventAccountSelectorFlyout.Hide();
}
2025-05-18 14:06:25 +02:00
private void QuickEventPopupClosed(object sender, object e)
{
// Reset the timeline selection when the tip is closed.
CalendarControl.ResetTimelineSelection();
}
2025-01-01 19:17:54 +01:00
2025-05-18 14:06:25 +02:00
private void PopupPlacementChanged(object sender, object e)
{
if (sender is Popup senderPopup)
{
2025-05-18 14:06:25 +02:00
// When the quick event Popup is positioned for different calendar types,
// we must adjust the offset to make sure the tip is not hidden and has nice
// spacing from the cell.
2025-05-18 14:06:25 +02:00
switch (senderPopup.ActualPlacement)
2025-01-01 19:17:54 +01:00
{
2025-05-18 14:06:25 +02:00
case PopupPlacementMode.Top:
senderPopup.VerticalOffset = PopupDialogOffset * -1;
break;
case PopupPlacementMode.Bottom:
senderPopup.VerticalOffset = PopupDialogOffset;
break;
case PopupPlacementMode.Left:
senderPopup.HorizontalOffset = PopupDialogOffset * -1;
break;
case PopupPlacementMode.Right:
senderPopup.HorizontalOffset = PopupDialogOffset;
break;
default:
break;
2025-01-01 19:17:54 +01:00
}
}
2025-05-18 14:06:25 +02:00
}
2025-05-18 14:06:25 +02:00
private void StartTimeDurationSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs args)
=> ViewModel.SelectedStartTimeString = args.Text;
2025-05-18 14:06:25 +02:00
private void EndTimeDurationSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs args)
=> ViewModel.SelectedEndTimeString = args.Text;
2025-05-18 14:06:25 +02:00
private void EventDetailsPopupClosed(object sender, object e)
{
ViewModel.DisplayDetailsCalendarItemViewModel = null;
}
private void CalendarScrolling(object sender, EventArgs e)
{
// In case of scrolling, we must dismiss the event details dialog.
ViewModel.DisplayDetailsCalendarItemViewModel = null;
}
}