Files
Wino-Mail/Wino.Calendar.ViewModels/EventDetailsPageViewModel.cs
T

468 lines
15 KiB
C#
Raw Normal View History

2025-01-14 00:53:54 +01:00
using System;
2026-01-01 10:07:56 +01:00
using System.Collections.Generic;
2026-01-01 15:02:40 +01:00
using System.Collections.ObjectModel;
using System.Diagnostics;
2026-01-01 15:02:40 +01:00
using System.Linq;
2025-01-14 00:53:54 +01:00
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Wino.Calendar.ViewModels.Data;
2026-01-01 10:07:56 +01:00
using Wino.Core.Domain;
2026-01-01 15:02:40 +01:00
using Wino.Core.Domain.Entities.Calendar;
2025-01-14 00:53:54 +01:00
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Calendar;
using Wino.Core.Domain.Models.Navigation;
using Wino.Core.ViewModels;
using Wino.Messaging.Client.Calendar;
2025-05-18 14:06:25 +02:00
namespace Wino.Calendar.ViewModels;
public partial class EventDetailsPageViewModel : CalendarBaseViewModel
2025-01-14 00:53:54 +01:00
{
2025-05-18 14:06:25 +02:00
private readonly ICalendarService _calendarService;
private readonly INativeAppService _nativeAppService;
private readonly IPreferencesService _preferencesService;
2026-01-01 10:07:56 +01:00
private readonly IMailDialogService _dialogService;
private readonly IWinoRequestDelegator _winoRequestDelegator;
private readonly INavigationService _navigationService;
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
public CalendarSettings CurrentSettings { get; }
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
#region Details
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(CanViewSeries))]
2026-01-01 10:07:56 +01:00
[NotifyPropertyChangedFor(nameof(CanEditSeries))]
2026-01-01 15:02:40 +01:00
[NotifyPropertyChangedFor(nameof(IsCurrentUserOrganizer))]
2026-01-03 19:33:36 +01:00
[NotifyPropertyChangedFor(nameof(CurrentRsvpText))]
[NotifyPropertyChangedFor(nameof(CurrentRsvpStatus))]
2026-01-01 15:02:40 +01:00
public partial CalendarItemViewModel CurrentEvent { get; set; }
2026-01-03 19:33:36 +01:00
2026-01-01 15:02:40 +01:00
[ObservableProperty]
public partial CalendarItemViewModel SeriesParent { get; set; }
2025-05-18 14:06:25 +02:00
[ObservableProperty]
2026-01-01 15:02:40 +01:00
public partial List<Reminder> Reminders { get; set; }
public ObservableCollection<ReminderOption> ReminderOptions { get; } = new ObservableCollection<ReminderOption>();
2026-01-01 10:07:56 +01:00
/// <summary>
/// Returns true if the event is part of a recurring series (as a child occurrence).
/// Used to enable "View Series" functionality.
/// </summary>
2025-05-18 14:06:25 +02:00
public bool CanViewSeries => CurrentEvent?.IsRecurringChild ?? false;
2025-01-14 00:53:54 +01:00
2026-01-01 10:07:56 +01:00
/// <summary>
/// Returns true if the "Edit Series" button should be visible.
/// Only visible for child occurrences of recurring events, not for master events or single events.
/// </summary>
public bool CanEditSeries => CurrentEvent?.IsRecurringChild ?? false;
2026-01-01 15:02:40 +01:00
/// <summary>
/// Returns true if the current user is the organizer of the event.
/// Used to determine if the user can invite attendees or modify the event.
/// </summary>
public bool IsCurrentUserOrganizer => CurrentEvent?.Attendees?.Any(a => a.IsOrganizer) ?? true;
2025-05-18 14:06:25 +02:00
#endregion
2025-01-14 00:53:54 +01:00
2026-01-01 10:07:56 +01:00
#region Show As Options
public List<CalendarItemShowAs> ShowAsOptions { get; } =
[
CalendarItemShowAs.Free,
CalendarItemShowAs.Tentative,
CalendarItemShowAs.Busy,
CalendarItemShowAs.OutOfOffice,
CalendarItemShowAs.WorkingElsewhere
];
[ObservableProperty]
public partial CalendarItemShowAs SelectedShowAs { get; set; } = CalendarItemShowAs.Busy;
#endregion
2026-01-03 19:33:36 +01:00
#region RSVP Panel
[ObservableProperty]
public partial bool IsRsvpPanelVisible { get; set; }
public bool IncludeRsvpMessage => !string.IsNullOrEmpty(RsvpMessage);
[ObservableProperty]
public partial string RsvpMessage { get; set; } = string.Empty;
public ObservableCollection<RsvpStatusOption> RsvpStatusOptions { get; } = new ObservableCollection<RsvpStatusOption>();
public CalendarItemStatus CurrentRsvpStatus
{
get
{
return CurrentEvent?.CalendarItem?.Status ?? CalendarItemStatus.NotResponded;
}
}
public string CurrentRsvpText
{
get
{
if (CurrentEvent?.CalendarItem == null) return Translator.CalendarEventResponse_Accept;
return CurrentEvent.CalendarItem.Status switch
{
CalendarItemStatus.Accepted => Translator.CalendarEventResponse_AcceptedResponse,
CalendarItemStatus.Tentative => Translator.CalendarEventResponse_TentativeResponse,
CalendarItemStatus.Cancelled => Translator.CalendarEventResponse_DeclinedResponse,
CalendarItemStatus.NotResponded => Translator.CalendarEventResponse_NotResponded,
_ => throw new NotImplementedException()
};
}
}
#endregion
2026-01-01 10:07:56 +01:00
public EventDetailsPageViewModel(ICalendarService calendarService,
INativeAppService nativeAppService,
IPreferencesService preferencesService,
IMailDialogService dialogService,
IWinoRequestDelegator winoRequestDelegator,
INavigationService navigationService)
2025-05-18 14:06:25 +02:00
{
_calendarService = calendarService;
_nativeAppService = nativeAppService;
_preferencesService = preferencesService;
2026-01-01 10:07:56 +01:00
_dialogService = dialogService;
_winoRequestDelegator = winoRequestDelegator;
_navigationService = navigationService;
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
CurrentSettings = _preferencesService.GetCurrentCalendarSettings();
2026-01-03 19:33:36 +01:00
// Initialize RSVP status options
RsvpStatusOptions.Add(new RsvpStatusOption(CalendarItemStatus.Accepted));
RsvpStatusOptions.Add(new RsvpStatusOption(CalendarItemStatus.Tentative));
RsvpStatusOptions.Add(new RsvpStatusOption(CalendarItemStatus.Cancelled));
2025-05-18 14:06:25 +02:00
}
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
public override async void OnNavigatedTo(NavigationMode mode, object parameters)
{
base.OnNavigatedTo(mode, parameters);
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
Messenger.Send(new DetailsPageStateChangedMessage(true));
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
if (parameters == null || parameters is not CalendarItemTarget args)
return;
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
await LoadCalendarItemTargetAsync(args);
}
2026-01-03 19:33:36 +01:00
protected override void OnCalendarItemDeleted(CalendarItem calendarItem)
{
base.OnCalendarItemDeleted(calendarItem);
// If the current event was deleted, navigate back
if (CurrentEvent?.CalendarItem?.Id == calendarItem.Id || CurrentEvent?.CalendarItem.RecurringCalendarItemId == calendarItem.Id)
{
_navigationService.GoBack();
}
}
2025-05-18 14:06:25 +02:00
private async Task LoadCalendarItemTargetAsync(CalendarItemTarget target)
{
try
{
2025-05-18 14:06:25 +02:00
var currentEventItem = await _calendarService.GetCalendarItemTargetAsync(target);
2025-05-18 14:06:25 +02:00
if (currentEventItem == null)
return;
2025-05-18 14:06:25 +02:00
CurrentEvent = new CalendarItemViewModel(currentEventItem);
2025-05-18 14:06:25 +02:00
var attendees = await _calendarService.GetAttendeesAsync(currentEventItem.EventTrackingId);
2025-05-18 14:06:25 +02:00
foreach (var item in attendees)
{
2025-05-18 14:06:25 +02:00
CurrentEvent.Attendees.Add(item);
}
2026-01-01 15:02:40 +01:00
// Load reminders for this calendar item
Reminders = await _calendarService.GetRemindersAsync(currentEventItem.EventTrackingId);
InitializeReminderOptions();
2025-01-14 00:53:54 +01:00
}
2025-05-18 14:06:25 +02:00
catch (Exception ex)
2025-01-14 00:53:54 +01:00
{
2025-05-18 14:06:25 +02:00
Debug.WriteLine(ex.Message);
2025-01-14 00:53:54 +01:00
}
2025-05-18 14:06:25 +02:00
}
2025-01-14 00:53:54 +01:00
2026-01-01 15:02:40 +01:00
private void InitializeReminderOptions()
2025-05-18 14:06:25 +02:00
{
2026-01-01 15:02:40 +01:00
ReminderOptions.Clear();
2025-01-14 00:53:54 +01:00
2026-01-01 15:02:40 +01:00
// Add predefined options from service
var predefinedMinutes = _calendarService.GetPredefinedReminderMinutes();
var predefinedOptions = predefinedMinutes.Select(m => new ReminderOption(m)).ToList();
// Add custom reminders from synced data
if (Reminders != null)
{
foreach (var reminder in Reminders)
{
// Convert seconds to minutes
var minutesDiff = (int)(reminder.DurationInSeconds / 60);
// Check if this is a custom value not in predefined list
if (!predefinedMinutes.Contains(minutesDiff))
{
predefinedOptions.Add(new ReminderOption(minutesDiff, isCustom: true));
}
}
}
// Sort by minutes descending and add to collection
foreach (var option in predefinedOptions.OrderByDescending(o => o.Minutes))
{
ReminderOptions.Add(option);
}
// Set selected state based on current reminders
if (Reminders != null)
{
foreach (var reminder in Reminders)
{
// Convert seconds to minutes
var minutesDiff = (int)(reminder.DurationInSeconds / 60);
var matchingOption = ReminderOptions.FirstOrDefault(o => o.Minutes == minutesDiff);
matchingOption?.IsSelected = true;
}
}
2025-05-18 14:06:25 +02:00
}
2025-01-14 00:53:54 +01:00
2026-01-01 15:02:40 +01:00
2025-05-18 14:06:25 +02:00
[RelayCommand]
private async Task SaveAsync()
{
2026-01-01 15:02:40 +01:00
if (CurrentEvent == null) return;
try
{
// Get selected reminder options
var selectedOptions = ReminderOptions.Where(o => o.IsSelected).ToList();
// Create separate Reminder entities for each selected option
var newReminders = new List<Reminder>();
foreach (var option in selectedOptions)
{
var durationInSeconds = option.Minutes * 60; // Convert minutes to seconds
newReminders.Add(new Reminder
{
Id = Guid.NewGuid(),
CalendarItemId = CurrentEvent.Id,
DurationInSeconds = durationInSeconds,
ReminderType = CalendarItemReminderType.Popup
});
}
// Save reminders to database
await _calendarService.SaveRemindersAsync(CurrentEvent.CalendarItem.EventTrackingId, newReminders);
Reminders = newReminders;
_navigationService.GoBack();
// TODO: Implement saving other event details
}
catch (Exception ex)
{
Debug.WriteLine($"Error saving event: {ex.Message}");
}
2025-05-18 14:06:25 +02:00
}
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
[RelayCommand]
private async Task DeleteAsync()
{
2026-01-01 10:07:56 +01:00
if (CurrentEvent == null) return;
// If the event is a master recurring event, ask for confirmation
if (CurrentEvent.IsRecurringParent)
{
var confirmed = await _dialogService.ShowConfirmationDialogAsync(
Translator.DialogMessage_DeleteRecurringSeriesMessage,
Translator.DialogMessage_DeleteRecurringSeriesTitle,
Translator.Buttons_Delete);
2025-01-14 00:53:54 +01:00
2026-01-01 10:07:56 +01:00
if (!confirmed) return;
}
try
{
var preparationRequest = new CalendarOperationPreparationRequest(
CalendarSynchronizerOperation.DeleteEvent,
CurrentEvent.CalendarItem,
null);
await _winoRequestDelegator.ExecuteAsync(preparationRequest);
// Navigate back after successful deletion
_navigationService.GoBack();
}
catch (Exception ex)
{
Debug.WriteLine($"Error deleting calendar event: {ex.Message}");
}
2025-05-18 14:06:25 +02:00
}
2025-01-14 00:53:54 +01:00
2025-05-18 14:06:25 +02:00
[RelayCommand]
2026-01-01 10:07:56 +01:00
private Task JoinOnlineAsync()
2025-05-18 14:06:25 +02:00
{
2026-01-01 10:07:56 +01:00
if (CurrentEvent == null || string.IsNullOrEmpty(CurrentEvent.CalendarItem.HtmlLink))
return Task.CompletedTask;
2025-05-18 14:06:25 +02:00
return _nativeAppService.LaunchUriAsync(new Uri(CurrentEvent.CalendarItem.HtmlLink));
}
[RelayCommand]
2026-01-03 19:33:36 +01:00
private void ToggleRsvpPanel()
{
IsRsvpPanelVisible = !IsRsvpPanelVisible;
if (IsRsvpPanelVisible && CurrentEvent?.CalendarItem != null)
{
// Initialize selection based on current status
foreach (var item in RsvpStatusOptions)
{
item.IsSelected = CurrentEvent?.CalendarItem?.Status == item.Status;
}
}
}
[RelayCommand]
private void CloseRsvpPanel()
{
IsRsvpPanelVisible = false;
RsvpMessage = string.Empty;
}
[RelayCommand]
private async Task SendRsvpResponse(AttendeeStatus status)
2025-05-18 14:06:25 +02:00
{
if (CurrentEvent == null) return;
2026-01-01 10:07:56 +01:00
2026-01-03 19:33:36 +01:00
try
{
// Get the optional response message if user wants to include it
var responseMessage = IncludeRsvpMessage ? RsvpMessage : null;
// Map status to operation
CalendarSynchronizerOperation operation = status switch
{
AttendeeStatus.Accepted => CalendarSynchronizerOperation.AcceptEvent,
AttendeeStatus.Tentative => CalendarSynchronizerOperation.TentativeEvent,
AttendeeStatus.Declined => CalendarSynchronizerOperation.DeclineEvent,
_ => throw new InvalidOperationException($"Invalid RSVP status: {status}")
};
// Create preparation request with the optional message
var preparationRequest = new CalendarOperationPreparationRequest(
operation,
CurrentEvent.CalendarItem,
null,
responseMessage);
await _winoRequestDelegator.ExecuteAsync(preparationRequest);
OnPropertyChanged(nameof(CurrentRsvpText));
OnPropertyChanged(nameof(CurrentRsvpStatus));
CloseRsvpPanel();
}
catch (Exception ex)
{
Debug.WriteLine($"Error sending RSVP response: {ex.Message}");
_dialogService.InfoBarMessage(
Translator.Info_AttachmentSaveFailedTitle,
ex.Message,
InfoBarMessageType.Error);
}
2025-01-14 00:53:54 +01:00
}
2026-01-01 15:02:40 +01:00
[RelayCommand]
private async Task ViewSeriesAsync()
{
if (CurrentEvent == null || !CurrentEvent.IsRecurringChild) return;
try
{
// Get the master event from the recurring series
var masterEventId = CurrentEvent.CalendarItem.RecurringCalendarItemId.Value;
var masterEvent = await _calendarService.GetCalendarItemAsync(masterEventId);
if (masterEvent == null) return;
// Load the master event without navigation
var target = new CalendarItemTarget(masterEvent, CalendarEventTargetType.Series);
await LoadCalendarItemTargetAsync(target);
}
catch (Exception ex)
{
Debug.WriteLine($"Error loading series: {ex.Message}");
}
}
}
public partial class ReminderOption : ObservableObject
{
public int Minutes { get; }
public bool IsCustom { get; }
[ObservableProperty]
public partial bool IsSelected { get; set; }
public string DisplayText
{
get
{
if (Minutes >= 60)
{
var hours = Minutes / 60;
return hours == 1 ? "1 Hour" : $"{hours} Hours";
}
return Minutes == 1 ? "1 Minute" : $"{Minutes} Minutes";
}
}
public ReminderOption(int minutes, bool isCustom = false)
{
Minutes = minutes;
IsCustom = isCustom;
}
2025-01-14 00:53:54 +01:00
}
2026-01-03 19:33:36 +01:00
public partial class RsvpStatusOption : ObservableObject
{
public CalendarItemStatus Status { get; }
public string StatusText
{
get
{
return Status switch
{
CalendarItemStatus.Accepted => Translator.CalendarEventResponse_Accept,
CalendarItemStatus.Tentative => Translator.CalendarEventResponse_Tentative,
CalendarItemStatus.Cancelled => Translator.CalendarEventResponse_Decline,
_ => Translator.CalendarEventResponse_Accept
};
}
}
[ObservableProperty]
public partial bool IsSelected { get; set; }
public RsvpStatusOption(CalendarItemStatus status)
{
Status = status;
}
}