Files
Wino-Mail/Wino.Mail.WinUI/Services/NavigationService.cs
T

281 lines
12 KiB
C#
Raw Normal View History

2025-11-15 14:52:01 +01:00
using System;
2025-09-29 11:16:14 +02:00
using System.Linq;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
2025-12-26 20:46:48 +01:00
using Wino.Calendar.Views;
2025-09-29 11:16:14 +02:00
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Navigation;
using Wino.Helpers;
using Wino.Mail.ViewModels.Data;
using Wino.Mail.ViewModels.Messages;
2025-12-26 20:46:48 +01:00
using Wino.Mail.WinUI;
using Wino.Mail.WinUI.Interfaces;
using Wino.Mail.WinUI.Services;
using Wino.Mail.WinUI.Views.Calendar;
2025-09-29 11:16:14 +02:00
using Wino.Messaging.Client.Mails;
using Wino.Messaging.Client.Navigation;
2025-09-29 11:16:14 +02:00
using Wino.Views;
using Wino.Views.Account;
2025-12-26 20:46:48 +01:00
using Wino.Views.Mail;
2025-09-29 11:16:14 +02:00
using Wino.Views.Settings;
namespace Wino.Services;
public class NavigationService : NavigationServiceBase, INavigationService
{
private readonly IStatePersistanceService _statePersistanceService;
private WinoPage[] _renderingPageTypes = new WinoPage[]
{
WinoPage.MailRenderingPage,
WinoPage.ComposePage
};
public NavigationService(IStatePersistanceService statePersistanceService)
{
_statePersistanceService = statePersistanceService;
}
2025-10-29 19:35:04 +01:00
public Type? GetPageType(WinoPage winoPage)
2025-09-29 11:16:14 +02:00
{
return winoPage switch
{
WinoPage.None => null,
WinoPage.IdlePage => typeof(IdlePage),
WinoPage.AccountDetailsPage => typeof(AccountDetailsPage),
WinoPage.MergedAccountDetailsPage => typeof(MergedAccountDetailsPage),
WinoPage.AccountManagementPage => typeof(AccountManagementPage),
WinoPage.ManageAccountsPage => typeof(ManageAccountsPage),
WinoPage.SignatureManagementPage => typeof(SignatureManagementPage),
WinoPage.AboutPage => typeof(AboutPage),
WinoPage.PersonalizationPage => typeof(PersonalizationPage),
WinoPage.MessageListPage => typeof(MessageListPage),
WinoPage.ReadComposePanePage => typeof(ReadComposePanePage),
WinoPage.MailRenderingPage => typeof(MailRenderingPage),
WinoPage.ComposePage => typeof(ComposePage),
WinoPage.MailListPage => typeof(MailListPage),
WinoPage.SettingsPage => typeof(SettingsPage),
WinoPage.WelcomePage => typeof(WelcomePage),
WinoPage.SettingOptionsPage => typeof(SettingOptionsPage),
WinoPage.AppPreferencesPage => typeof(AppPreferencesPage),
WinoPage.AliasManagementPage => typeof(AliasManagementPage),
WinoPage.LanguageTimePage => typeof(LanguageTimePage),
WinoPage.EditAccountDetailsPage => typeof(EditAccountDetailsPage),
2025-10-29 16:26:46 +01:00
WinoPage.KeyboardShortcutsPage => typeof(KeyboardShortcutsPage),
2025-10-29 19:35:04 +01:00
WinoPage.ContactsPage => typeof(ContactsPage),
2025-11-23 20:56:57 +01:00
WinoPage.SignatureAndEncryptionPage => typeof(SignatureAndEncryptionPage),
WinoPage.StoragePage => typeof(StoragePage),
2025-12-26 20:46:48 +01:00
WinoPage.CalendarPage => typeof(CalendarPage),
WinoPage.EventDetailsPage => typeof(EventDetailsPage),
2025-12-31 13:28:53 +01:00
WinoPage.CalendarSettingsPage => typeof(CalendarSettingsPage),
WinoPage.CalendarAccountSettingsPage => typeof(CalendarAccountSettingsPage),
2025-09-29 11:16:14 +02:00
_ => null,
};
}
public Frame GetCoreFrame(NavigationReferenceFrame frameType)
{
if (WinoApplication.MainWindow is not IWinoShellWindow shellWindow) throw new ArgumentException("MainWindow must implement IWinoShellWindow");
if (shellWindow.GetMainFrame() is not Frame mainFrame) throw new ArgumentException("MainFrame cannot be null.");
2025-12-26 20:46:48 +01:00
if (frameType == NavigationReferenceFrame.ShellFrame) return shellWindow.GetMainFrame();
2025-09-29 11:16:14 +02:00
return WinoVisualTreeHelper.GetChildObject<Frame>(mainFrame.Content as UIElement, frameType.ToString());
}
2025-12-26 20:46:48 +01:00
public bool ChangeApplicationMode(WinoApplicationMode mode)
{
var coreFrame = GetCoreFrame(NavigationReferenceFrame.ShellFrame);
if (coreFrame == null) return false;
// Update the application mode in state persistence service
_statePersistanceService.ApplicationMode = mode;
2025-12-27 19:16:24 +01:00
var targetPageType = mode == WinoApplicationMode.Mail ? typeof(MailAppShell) : typeof(CalendarAppShell);
var currentPageType = coreFrame.Content?.GetType();
var transitionInfo = GetNavigationTransitionInfo(NavigationTransitionType.DrillIn);
// If already on the target page, do nothing
if (currentPageType == targetPageType)
return true;
// Check if we can go back to the target page
if (coreFrame.CanGoBack && coreFrame.BackStack.Count > 0)
2025-12-26 20:46:48 +01:00
{
2025-12-27 19:16:24 +01:00
var previousPage = coreFrame.BackStack[coreFrame.BackStack.Count - 1];
if (previousPage.SourcePageType == targetPageType)
{
coreFrame.GoBack(transitionInfo);
return true;
}
2025-12-26 20:46:48 +01:00
}
2025-12-27 19:16:24 +01:00
// Check if we can go forward to the target page
if (coreFrame.CanGoForward && coreFrame.ForwardStack.Count > 0)
2025-12-26 20:46:48 +01:00
{
2025-12-27 19:16:24 +01:00
var nextPage = coreFrame.ForwardStack[coreFrame.ForwardStack.Count - 1];
if (nextPage.SourcePageType == targetPageType)
{
coreFrame.GoForward();
return true;
}
2025-12-26 20:46:48 +01:00
}
2025-12-27 19:16:24 +01:00
// Navigate to the target page only if it's not in the navigation stack
coreFrame.Navigate(targetPageType, null, transitionInfo);
2025-12-26 20:46:48 +01:00
return true;
}
2025-09-29 11:16:14 +02:00
public bool Navigate(WinoPage page,
2025-11-14 18:51:48 +01:00
object? parameter = null,
2025-12-26 20:46:48 +01:00
NavigationReferenceFrame frame = NavigationReferenceFrame.InnerShellFrame,
2025-09-29 11:16:14 +02:00
NavigationTransitionType transition = NavigationTransitionType.None)
{
var pageType = GetPageType(page);
var currentApplicationMode = GetCoreFrame(NavigationReferenceFrame.ShellFrame)?.Content?.GetType() == typeof(MailAppShell)
? WinoApplicationMode.Mail
: WinoApplicationMode.Calendar;
2025-09-29 11:16:14 +02:00
_statePersistanceService.IsReadingMail = _renderingPageTypes.Contains(page);
_statePersistanceService.IsEventDetailsVisible = page == WinoPage.EventDetailsPage;
2025-09-29 11:16:14 +02:00
Frame innerShellFrame = GetCoreFrame(NavigationReferenceFrame.InnerShellFrame);
if (innerShellFrame != null)
2025-09-29 11:16:14 +02:00
{
// Calendar navigations.
if (currentApplicationMode == WinoApplicationMode.Calendar)
2025-12-27 19:16:24 +01:00
{
return innerShellFrame.Navigate(pageType, parameter);
2025-12-27 19:16:24 +01:00
}
else
2025-09-29 11:16:14 +02:00
{
// Mail navigations.
var currentFrameType = GetCurrentFrameType(ref innerShellFrame);
bool isMailListingPageActive = currentFrameType != null && currentFrameType == typeof(MailListPage);
2025-09-29 11:16:14 +02:00
// Active page is mail list page and we are refreshing the folder.
if (isMailListingPageActive && currentFrameType == pageType && parameter is NavigateMailFolderEventArgs folderNavigationArgs)
{
// No need for new navigation, just refresh the folder.
WeakReferenceMessenger.Default.Send(new ActiveMailFolderChangedEvent(folderNavigationArgs.BaseFolderMenuItem, folderNavigationArgs.FolderInitLoadAwaitTask));
WeakReferenceMessenger.Default.Send(new DisposeRenderingFrameRequested());
2025-09-29 11:16:14 +02:00
return true;
}
2025-09-29 11:16:14 +02:00
var transitionInfo = GetNavigationTransitionInfo(transition);
2025-09-29 11:16:14 +02:00
// This page must be opened in the Frame placed in MailListingPage.
if (isMailListingPageActive && frame == NavigationReferenceFrame.RenderingFrame)
2025-09-29 11:16:14 +02:00
{
var listingFrame = GetCoreFrame(NavigationReferenceFrame.RenderingFrame);
if (listingFrame == null) return false;
// Active page is mail list page and we are opening a mail item.
// No navigation needed, just refresh the rendered mail item.
if (listingFrame.Content != null
&& listingFrame.Content.GetType() == GetPageType(WinoPage.MailRenderingPage)
&& parameter is MailItemViewModel mailItemViewModel
&& page != WinoPage.ComposePage)
{
WeakReferenceMessenger.Default.Send(new NewMailItemRenderingRequestedEvent(mailItemViewModel));
}
else if (listingFrame.Content != null
&& listingFrame.Content.GetType() == GetPageType(WinoPage.ComposePage)
&& page == WinoPage.ComposePage
&& parameter is MailItemViewModel composeDraftViewModel)
{
// ComposePage is already active and we're switching to another draft.
// Reuse existing ComposePage and WebView2 instead of navigating.
WeakReferenceMessenger.Default.Send(new NewComposeDraftItemRequestedEvent(composeDraftViewModel));
}
else if (listingFrame.Content != null
&& listingFrame.Content.GetType() == GetPageType(WinoPage.IdlePage)
&& pageType == typeof(IdlePage))
{
// Idle -> Idle navigation. Ignore.
return true;
}
else
{
listingFrame.Navigate(pageType, parameter, transitionInfo);
}
2025-09-29 11:16:14 +02:00
return true;
}
if ((currentFrameType != null && currentFrameType != pageType) || currentFrameType == null)
2025-09-29 11:16:14 +02:00
{
return innerShellFrame.Navigate(pageType, parameter, transitionInfo);
2025-09-29 11:16:14 +02:00
}
}
}
return false;
}
2025-09-29 11:16:14 +02:00
public void GoBack(Core.Domain.Enums.NavigationTransitionEffect slideEffect = Core.Domain.Enums.NavigationTransitionEffect.FromRight)
{
// Check if we're navigating within ManageAccountsPage (applies to both modes)
// Check if we're navigating within SettingsPage (applies to both modes)
if (_statePersistanceService.IsManageAccountsNavigating || _statePersistanceService.IsSettingsNavigating)
{
// Send message to ManageAccountsPage to go back within its AccountPagesFrame
WeakReferenceMessenger.Default.Send(new BackBreadcrumNavigationRequested(slideEffect));
return;
}
2026-01-06 17:34:06 +01:00
var innerShellFrame = GetCoreFrame(NavigationReferenceFrame.InnerShellFrame);
if (_statePersistanceService.ApplicationMode == WinoApplicationMode.Calendar && innerShellFrame?.CanGoBack == true)
{
2026-01-06 17:34:06 +01:00
innerShellFrame.GoBack();
2026-01-06 17:34:06 +01:00
// Calendar mode: Navigate back from EventDetailsPage
_statePersistanceService.IsEventDetailsVisible = false;
2025-09-29 11:16:14 +02:00
}
else
{
2026-01-06 17:34:06 +01:00
if (_statePersistanceService.IsReadingMail && _statePersistanceService.IsReaderNarrowed)
{
// Mail mode: Clear selections and dispose rendering frame
_statePersistanceService.IsReadingMail = false;
2025-09-29 11:16:14 +02:00
2026-01-06 17:34:06 +01:00
WeakReferenceMessenger.Default.Send(new ClearMailSelectionsRequested());
WeakReferenceMessenger.Default.Send(new DisposeRenderingFrameRequested());
}
else if (innerShellFrame != null && innerShellFrame.CanGoBack)
{
innerShellFrame.GoBack();
}
}
2025-09-29 11:16:14 +02:00
}
// Standalone EML viewer.
//public void NavigateRendering(MimeMessageInformation mimeMessageInformation, NavigationTransitionType transition = NavigationTransitionType.None)
//{
// if (mimeMessageInformation == null)
// throw new ArgumentException("MimeMessage cannot be null.");
// Navigate(WinoPage.MailRenderingPage, mimeMessageInformation, NavigationReferenceFrame.RenderingFrame, transition);
//}
//// Mail item view model clicked handler.
//public void NavigateRendering(IMailItem mailItem, NavigationTransitionType transition = NavigationTransitionType.None)
//{
// if (mailItem is MailItemViewModel mailItemViewModel)
// Navigate(WinoPage.MailRenderingPage, mailItemViewModel, NavigationReferenceFrame.RenderingFrame, transition);
// else
// throw new ArgumentException("MailItem must be of type MailItemViewModel.");
//}
//public void NavigateFolder(NavigateMailFolderEventArgs args)
// => Navigate(WinoPage.MailListPage, args, NavigationReferenceFrame.ShellFrame);
}