Files
Wino-Mail/Wino.Calendar/App.xaml.cs

160 lines
5.8 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Windows.ApplicationModel;
2024-04-18 01:44:37 +02:00
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.UI.Core.Preview;
using Wino.Activation;
using Wino.Calendar.Activation;
using Wino.Calendar.Services;
using Wino.Calendar.ViewModels;
using Wino.Calendar.ViewModels.Interfaces;
using Wino.Core.Domain;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Exceptions;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Synchronization;
using Wino.Core.UWP;
2024-12-28 20:42:03 +01:00
using Wino.Core.ViewModels;
using Wino.Messaging.Client.Connection;
using Wino.Messaging.Server;
using Wino.Services;
2024-04-18 01:44:37 +02:00
2025-03-15 15:23:26 +01:00
namespace Wino.Calendar;
public sealed partial class App : WinoApplication, IRecipient<NewCalendarSynchronizationRequested>
2024-04-18 01:44:37 +02:00
{
2025-03-15 15:23:26 +01:00
private BackgroundTaskDeferral connectionBackgroundTaskDeferral;
public App()
2024-04-18 01:44:37 +02:00
{
2025-03-15 15:23:26 +01:00
InitializeComponent();
WeakReferenceMessenger.Default.Register<NewCalendarSynchronizationRequested>(this);
}
2025-03-15 15:23:26 +01:00
public override IServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
2025-03-15 15:23:26 +01:00
services.RegisterSharedServices();
services.RegisterCalendarViewModelServices();
services.RegisterCoreUWPServices();
services.RegisterCoreViewModels();
2025-03-15 15:23:26 +01:00
RegisterUWPServices(services);
RegisterViewModels(services);
RegisterActivationHandlers(services);
2024-04-18 01:44:37 +02:00
2025-03-15 15:23:26 +01:00
return services.BuildServiceProvider();
}
2024-04-18 01:44:37 +02:00
2025-03-15 15:23:26 +01:00
#region Dependency Injection
2024-04-18 01:44:37 +02:00
2025-03-15 15:23:26 +01:00
private void RegisterActivationHandlers(IServiceCollection services)
{
//services.AddTransient<ProtocolActivationHandler>();
//services.AddTransient<ToastNotificationActivationHandler>();
//services.AddTransient<FileActivationHandler>();
}
2024-04-18 01:44:37 +02:00
2025-03-15 15:23:26 +01:00
private void RegisterUWPServices(IServiceCollection services)
{
services.AddSingleton<INavigationService, NavigationService>();
services.AddSingleton<ICalendarDialogService, DialogService>();
services.AddTransient<ISettingsBuilderService, SettingsBuilderService>();
services.AddTransient<IProviderService, ProviderService>();
services.AddSingleton<IAuthenticatorConfig, CalendarAuthenticatorConfig>();
services.AddSingleton<IAccountCalendarStateService, AccountCalendarStateService>();
}
2024-04-18 01:44:37 +02:00
2025-03-15 15:23:26 +01:00
private void RegisterViewModels(IServiceCollection services)
{
services.AddSingleton(typeof(AppShellViewModel));
services.AddSingleton(typeof(CalendarPageViewModel));
services.AddTransient(typeof(CalendarSettingsPageViewModel));
services.AddTransient(typeof(AccountManagementViewModel));
services.AddTransient(typeof(PersonalizationPageViewModel));
services.AddTransient(typeof(AccountDetailsPageViewModel));
services.AddTransient(typeof(EventDetailsPageViewModel));
}
2024-04-18 01:44:37 +02:00
2025-03-15 15:23:26 +01:00
#endregion
2024-04-18 01:44:37 +02:00
2025-03-15 15:23:26 +01:00
protected override void OnApplicationCloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
{
// TODO: Check server running.
}
2025-03-15 15:23:26 +01:00
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
LogActivation($"OnLaunched -> {args.GetType().Name}, Kind -> {args.Kind}, PreviousExecutionState -> {args.PreviousExecutionState}, IsPrelaunch -> {args.PrelaunchActivated}");
2025-03-15 15:23:26 +01:00
if (!args.PrelaunchActivated)
2024-04-18 01:44:37 +02:00
{
2025-03-15 15:23:26 +01:00
await ActivateWinoAsync(args);
2024-04-18 01:44:37 +02:00
}
2025-03-15 15:23:26 +01:00
}
2025-03-15 15:23:26 +01:00
protected override IEnumerable<ActivationHandler> GetActivationHandlers()
{
return null;
}
2025-03-15 15:23:26 +01:00
protected override ActivationHandler<IActivatedEventArgs> GetDefaultActivationHandler()
=> new DefaultActivationHandler();
2025-03-15 15:23:26 +01:00
protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
base.OnBackgroundActivated(args);
2025-03-15 15:23:26 +01:00
if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails appServiceTriggerDetails)
{
2025-03-15 15:23:26 +01:00
LogActivation("OnBackgroundActivated -> AppServiceTriggerDetails received.");
2025-03-15 15:23:26 +01:00
// Only accept connections from callers in the same package
if (appServiceTriggerDetails.CallerPackageFamilyName == Package.Current.Id.FamilyName)
{
2025-03-15 15:23:26 +01:00
// Connection established from the fulltrust process
2025-03-15 15:23:26 +01:00
connectionBackgroundTaskDeferral = args.TaskInstance.GetDeferral();
args.TaskInstance.Canceled += OnConnectionBackgroundTaskCanceled;
2025-03-15 15:23:26 +01:00
AppServiceConnectionManager.Connection = appServiceTriggerDetails.AppServiceConnection;
2025-03-15 15:23:26 +01:00
WeakReferenceMessenger.Default.Send(new WinoServerConnectionEstablished());
}
}
2025-03-15 15:23:26 +01:00
}
2025-03-15 15:23:26 +01:00
public void OnConnectionBackgroundTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
sender.Canceled -= OnConnectionBackgroundTaskCanceled;
2025-03-15 15:23:26 +01:00
Log.Information($"Server connection background task was canceled. Reason: {reason}");
2025-03-15 15:23:26 +01:00
connectionBackgroundTaskDeferral?.Complete();
connectionBackgroundTaskDeferral = null;
2025-03-15 15:23:26 +01:00
AppServiceConnectionManager.Connection = null;
}
2025-03-15 15:23:26 +01:00
public async void Receive(NewCalendarSynchronizationRequested message)
{
try
{
2025-03-15 15:23:26 +01:00
var synchronizationResultResponse = await AppServiceConnectionManager.GetResponseAsync<CalendarSynchronizationResult, NewCalendarSynchronizationRequested>(message);
synchronizationResultResponse.ThrowIfFailed();
}
catch (WinoServerException serverException)
{
var dialogService = Services.GetService<ICalendarDialogService>();
2025-03-15 15:23:26 +01:00
dialogService.InfoBarMessage(Translator.Info_SyncFailedTitle, serverException.Message, InfoBarMessageType.Error);
}
2024-04-18 01:44:37 +02:00
}
}