using System; using System.Text; using CommunityToolkit.Mvvm.Messaging; using Microsoft.Extensions.DependencyInjection; using Microsoft.Windows.AppLifecycle; using Wino.Core.Domain.Enums; using Wino.Core.Domain.Interfaces; using Wino.Core.WinUI; using Wino.Core.WinUI.Interfaces; using Wino.Mail.Services; using Wino.Mail.ViewModels; using Wino.Messaging.Server; using Wino.Services; namespace Wino.Mail.WinUI; public partial class App : WinoApplication, IRecipient { private ISynchronizationManager? _synchronizationManager; public App() { InitializeComponent(); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); RegisterRecipients(); } #region Dependency Injection private void RegisterUWPServices(IServiceCollection services) { services.AddSingleton(); services.AddSingleton(); services.AddTransient(); services.AddTransient(); services.AddSingleton(); } private void RegisterViewModels(IServiceCollection services) { services.AddSingleton(typeof(AppShellViewModel)); services.AddTransient(typeof(MailListPageViewModel)); services.AddTransient(typeof(MailRenderingPageViewModel)); services.AddTransient(typeof(AccountManagementViewModel)); services.AddTransient(typeof(WelcomePageViewModel)); services.AddTransient(typeof(ComposePageViewModel)); services.AddTransient(typeof(IdlePageViewModel)); services.AddTransient(typeof(EditAccountDetailsPageViewModel)); services.AddTransient(typeof(AccountDetailsPageViewModel)); services.AddTransient(typeof(SignatureManagementPageViewModel)); services.AddTransient(typeof(MessageListPageViewModel)); services.AddTransient(typeof(ReadComposePanePageViewModel)); services.AddTransient(typeof(MergedAccountDetailsPageViewModel)); services.AddTransient(typeof(LanguageTimePageViewModel)); services.AddTransient(typeof(AppPreferencesPageViewModel)); services.AddTransient(typeof(AliasManagementPageViewModel)); } #endregion public override IServiceProvider ConfigureServices() { var services = new ServiceCollection(); services.RegisterViewModelService(); services.RegisterSharedServices(); services.RegisterCoreUWPServices(); services.RegisterCoreViewModels(); RegisterUWPServices(services); RegisterViewModels(services); return services.BuildServiceProvider(); } private bool IsStartupTaskLaunch() => AppInstance.GetCurrent().GetActivatedEventArgs()?.Kind == ExtendedActivationKind.StartupTask; protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) { // TODO: Check app relaunch mutex before loading anything. // Initialize NewThemeService first to get backdrop settings before creating window var newThemeService = Services.GetRequiredService(); var configService = Services.GetRequiredService(); var nativeAppService = Services.GetRequiredService(); _synchronizationManager = Services.GetRequiredService(); // Load saved backdrop type before creating window var savedBackdropType = (WindowBackdropType)configService.Get("WindowBackdropTypeKey", (int)WindowBackdropType.Mica); MainWindow = new ShellWindow(); nativeAppService.GetCoreWindowHwnd = () => WinRT.Interop.WindowNative.GetWindowHandle(MainWindow); await InitializeServicesAsync(); if (MainWindow is not IWinoShellWindow shellWindow) throw new ArgumentException("MainWindow must implement IWinoShellWindow"); bool isStartupTaskLaunch = IsStartupTaskLaunch(); shellWindow.HandleAppActivation(args); // Do not actiavate window if launched from startup task. Keep running in the system tray. if (!isStartupTaskLaunch) { MainWindow.Activate(); } } private void RegisterRecipients() { WeakReferenceMessenger.Default.Register(this); } public void Receive(NewMailSynchronizationRequested message) { _synchronizationManager?.SynchronizeMailAsync(message.Options); } }