2024-11-10 23:28:25 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
2024-11-30 23:05:07 +01:00
|
|
|
|
using Wino.Core.Domain.Collections;
|
2024-11-10 23:28:25 +01:00
|
|
|
|
using Wino.Core.Domain.Enums;
|
|
|
|
|
|
using Wino.Core.Domain.Interfaces;
|
|
|
|
|
|
using Wino.Core.Domain.Models.Calendar;
|
|
|
|
|
|
using Wino.Core.Domain.Models.Navigation;
|
2024-12-27 00:18:46 +01:00
|
|
|
|
using Wino.Core.Domain.Models.Synchronization;
|
2024-11-10 23:28:25 +01:00
|
|
|
|
using Wino.Core.Extensions;
|
|
|
|
|
|
using Wino.Core.ViewModels;
|
|
|
|
|
|
using Wino.Messaging.Client.Calendar;
|
|
|
|
|
|
using Wino.Messaging.Client.Navigation;
|
2024-12-27 00:18:46 +01:00
|
|
|
|
using Wino.Messaging.Server;
|
2024-11-10 23:28:25 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Wino.Calendar.ViewModels
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class AppShellViewModel : CalendarBaseViewModel,
|
|
|
|
|
|
IRecipient<VisibleDateRangeChangedMessage>,
|
|
|
|
|
|
IRecipient<CalendarEnableStatusChangedMessage>,
|
2024-12-28 16:39:43 +01:00
|
|
|
|
IRecipient<NavigateManageAccountsRequested>
|
2024-11-10 23:28:25 +01:00
|
|
|
|
{
|
|
|
|
|
|
public event EventHandler<CalendarDisplayType> DisplayTypeChanged;
|
|
|
|
|
|
public IPreferencesService PreferencesService { get; }
|
|
|
|
|
|
public IStatePersistanceService StatePersistenceService { get; }
|
|
|
|
|
|
public INavigationService NavigationService { get; }
|
|
|
|
|
|
public IWinoServerConnectionManager ServerConnectionManager { get; }
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2024-12-27 00:18:46 +01:00
|
|
|
|
private int _selectedMenuItemIndex = -1;
|
2024-11-10 23:28:25 +01:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private bool isCalendarEnabled;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the active connection status of the Wino server.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private WinoServerConnectionStatus activeConnectionStatus;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the display date of the calendar.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private DateTimeOffset _displayDate;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the highlighted range in the CalendarView and displayed date range in FlipView.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private DateRange highlightedDateRange;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private ObservableRangeCollection<string> dateNavigationHeaderItems = [];
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private int _selectedDateNavigationHeaderIndex;
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsVerticalCalendar => StatePersistenceService.CalendarDisplayType == CalendarDisplayType.Month;
|
|
|
|
|
|
|
|
|
|
|
|
public AppShellViewModel(IPreferencesService preferencesService,
|
|
|
|
|
|
IStatePersistanceService statePersistanceService,
|
|
|
|
|
|
INavigationService navigationService,
|
|
|
|
|
|
IWinoServerConnectionManager serverConnectionManager)
|
|
|
|
|
|
{
|
|
|
|
|
|
NavigationService = navigationService;
|
|
|
|
|
|
ServerConnectionManager = serverConnectionManager;
|
|
|
|
|
|
PreferencesService = preferencesService;
|
|
|
|
|
|
|
|
|
|
|
|
StatePersistenceService = statePersistanceService;
|
|
|
|
|
|
StatePersistenceService.StatePropertyChanged += PrefefencesChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void PrefefencesChanged(object sender, string e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (e == nameof(StatePersistenceService.CalendarDisplayType))
|
|
|
|
|
|
{
|
|
|
|
|
|
DisplayTypeChanged?.Invoke(this, StatePersistenceService.CalendarDisplayType);
|
|
|
|
|
|
OnPropertyChanged(nameof(IsVerticalCalendar));
|
|
|
|
|
|
|
|
|
|
|
|
// Change the calendar.
|
|
|
|
|
|
DateClicked(new CalendarViewDayClickedEventArgs(GetDisplayTypeSwitchDate()));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnNavigatedTo(NavigationMode mode, object parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnNavigatedTo(mode, parameters);
|
|
|
|
|
|
|
|
|
|
|
|
UpdateDateNavigationHeaderItems();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-28 16:39:43 +01:00
|
|
|
|
private void ForceNavigateCalendarDate()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedMenuItemIndex == -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
var args = new CalendarPageNavigationArgs()
|
|
|
|
|
|
{
|
|
|
|
|
|
NavigationDate = _navigationDate ?? DateTime.Now.Date
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Already on calendar. Just navigate.
|
|
|
|
|
|
NavigationService.Navigate(WinoPage.CalendarPage, args);
|
|
|
|
|
|
|
|
|
|
|
|
_navigationDate = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedMenuItemIndex = -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-27 00:18:46 +01:00
|
|
|
|
partial void OnSelectedMenuItemIndexChanged(int oldValue, int newValue)
|
2024-11-10 23:28:25 +01:00
|
|
|
|
{
|
2024-12-27 00:18:46 +01:00
|
|
|
|
switch (newValue)
|
2024-11-10 23:28:25 +01:00
|
|
|
|
{
|
2024-12-27 00:18:46 +01:00
|
|
|
|
case -1:
|
2024-12-28 16:39:43 +01:00
|
|
|
|
ForceNavigateCalendarDate();
|
2024-12-27 00:18:46 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
NavigationService.Navigate(WinoPage.AccountManagementPage);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
NavigationService.Navigate(WinoPage.SettingsPage);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
2024-11-10 23:28:25 +01:00
|
|
|
|
}
|
2024-12-27 00:18:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private void Sync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var t = new NewCalendarSynchronizationRequested(new CalendarSynchronizationOptions()
|
2024-11-10 23:28:25 +01:00
|
|
|
|
{
|
2024-12-27 00:18:46 +01:00
|
|
|
|
AccountId = Guid.Parse("52fae547-0740-4aa3-8d51-519bd31278ca"),
|
|
|
|
|
|
Type = CalendarSynchronizationType.CalendarMetadata
|
|
|
|
|
|
}, SynchronizationSource.Client);
|
|
|
|
|
|
|
|
|
|
|
|
Messenger.Send(t);
|
2024-11-10 23:28:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// When calendar type switches, we need to navigate to the most ideal date.
|
|
|
|
|
|
/// This method returns that date.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private DateTime GetDisplayTypeSwitchDate()
|
|
|
|
|
|
{
|
2024-11-30 12:47:24 +01:00
|
|
|
|
var settings = PreferencesService.GetCurrentCalendarSettings();
|
2024-11-10 23:28:25 +01:00
|
|
|
|
switch (StatePersistenceService.CalendarDisplayType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case CalendarDisplayType.Day:
|
|
|
|
|
|
if (HighlightedDateRange.IsInRange(DateTime.Now)) return DateTime.Now.Date;
|
|
|
|
|
|
|
|
|
|
|
|
return HighlightedDateRange.StartDate;
|
|
|
|
|
|
case CalendarDisplayType.Week:
|
2024-11-30 12:47:24 +01:00
|
|
|
|
if (HighlightedDateRange == null || HighlightedDateRange.IsInRange(DateTime.Now))
|
|
|
|
|
|
{
|
|
|
|
|
|
return DateTime.Now.Date.GetWeekStartDateForDate(settings.FirstDayOfWeek);
|
|
|
|
|
|
}
|
2024-11-10 23:28:25 +01:00
|
|
|
|
|
2024-11-30 12:47:24 +01:00
|
|
|
|
return HighlightedDateRange.StartDate.GetWeekStartDateForDate(settings.FirstDayOfWeek);
|
2024-11-10 23:28:25 +01:00
|
|
|
|
case CalendarDisplayType.WorkWeek:
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CalendarDisplayType.Month:
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CalendarDisplayType.Year:
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return DateTime.Today.Date;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-28 16:39:43 +01:00
|
|
|
|
private DateTime? _navigationDate;
|
|
|
|
|
|
|
2024-11-10 23:28:25 +01:00
|
|
|
|
public override void OnPageLoaded()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnPageLoaded();
|
|
|
|
|
|
|
2024-12-28 16:39:43 +01:00
|
|
|
|
TodayClicked();
|
2024-11-10 23:28:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Commands
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
2024-12-28 16:39:43 +01:00
|
|
|
|
private void TodayClicked()
|
|
|
|
|
|
{
|
|
|
|
|
|
_navigationDate = DateTime.Now.Date;
|
|
|
|
|
|
|
|
|
|
|
|
ForceNavigateCalendarDate();
|
|
|
|
|
|
}
|
2024-11-10 23:28:25 +01:00
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public void ManageAccounts() => NavigationService.Navigate(WinoPage.AccountManagementPage);
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private Task ReconnectServerAsync() => ServerConnectionManager.ConnectAsync();
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
2024-12-28 16:39:43 +01:00
|
|
|
|
private void DateClicked(CalendarViewDayClickedEventArgs clickedDateArgs)
|
|
|
|
|
|
{
|
|
|
|
|
|
_navigationDate = clickedDateArgs.ClickedDate;
|
|
|
|
|
|
|
|
|
|
|
|
ForceNavigateCalendarDate();
|
|
|
|
|
|
}
|
2024-11-10 23:28:25 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
public void Receive(VisibleDateRangeChangedMessage message) => HighlightedDateRange = message.DateRange;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the header navigation items based on visible date range and calendar type.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void UpdateDateNavigationHeaderItems()
|
|
|
|
|
|
{
|
|
|
|
|
|
DateNavigationHeaderItems.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: From settings
|
|
|
|
|
|
var testInfo = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
|
|
switch (StatePersistenceService.CalendarDisplayType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case CalendarDisplayType.Day:
|
|
|
|
|
|
case CalendarDisplayType.Week:
|
|
|
|
|
|
case CalendarDisplayType.WorkWeek:
|
|
|
|
|
|
case CalendarDisplayType.Month:
|
|
|
|
|
|
DateNavigationHeaderItems.ReplaceRange(testInfo.DateTimeFormat.MonthNames);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CalendarDisplayType.Year:
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SetDateNavigationHeaderItems();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
partial void OnHighlightedDateRangeChanged(DateRange value) => SetDateNavigationHeaderItems();
|
|
|
|
|
|
|
|
|
|
|
|
private void SetDateNavigationHeaderItems()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (HighlightedDateRange == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (DateNavigationHeaderItems.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
UpdateDateNavigationHeaderItems();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Year view
|
|
|
|
|
|
var monthIndex = HighlightedDateRange.GetMostVisibleMonthIndex();
|
|
|
|
|
|
|
|
|
|
|
|
SelectedDateNavigationHeaderIndex = Math.Max(monthIndex - 1, -1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async void Receive(CalendarEnableStatusChangedMessage message)
|
|
|
|
|
|
=> await ExecuteUIThread(() => IsCalendarEnabled = message.IsEnabled);
|
|
|
|
|
|
|
2024-12-27 00:18:46 +01:00
|
|
|
|
public void Receive(NavigateManageAccountsRequested message) => SelectedMenuItemIndex = 1;
|
|
|
|
|
|
|
2024-12-28 16:39:43 +01:00
|
|
|
|
//public void Receive(GoToCalendarDayMessage message) => SelectedMenuItemIndex = -1;
|
2024-11-10 23:28:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|