-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
100 lines (87 loc) · 3.88 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
using System;
using System.Diagnostics;
using System.Linq;
using wingman.Interfaces;
using wingman.Services;
using wingman.Updates;
using wingman.ViewModels;
using wingman.Views;
namespace wingman
{
public partial class App : Application, IDisposable
{
private readonly IHost _host;
private readonly AppUpdater _appUpdater;
public App()
{
InitializeComponent();
UnhandledException += App_UnhandledException;
_host = BuildHost();
Ioc.Default.ConfigureServices(_host.Services);
_appUpdater = new AppUpdater();
}
public void Dispose()
{
var serviceTypes = _host.Services.GetType().Assembly.GetTypes()
.Where(t => t.GetInterfaces().Contains(typeof(IDisposable)) && !t.IsInterface);
foreach (var serviceType in serviceTypes)
{
var serviceInstance = _host.Services.GetService(serviceType);
if (serviceInstance is IDisposable disposableService)
{
disposableService.Dispose();
}
}
Debug.WriteLine("Services disposed.");
_host.Dispose();
Debug.WriteLine("Host disposed.");
}
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
var Logger = Ioc.Default.GetRequiredService<ILoggingService>();
Logger.LogException(e.Exception.ToString());
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);
Ioc.Default.GetRequiredService<StatusWindow>(); // make sure events are initialized
Ioc.Default.GetRequiredService<IEventHandlerService>(); // make sure events are initialized
IAppActivationService appWindowService = Ioc.Default.GetRequiredService<IAppActivationService>();
appWindowService.Activate(args);
MainWindow mw = Ioc.Default.GetRequiredService<MainWindow>();
mw.SetApp(this);
_ = _appUpdater.CheckForUpdatesAsync(mw.Dispatcher);
}
private static IHost BuildHost() => Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
_ = services
// Services
.AddSingleton<IGlobalHotkeyService, GlobalHotkeyService>()
.AddSingleton<ILoggingService, LoggingService>()
.AddSingleton<IEventHandlerService, EventHandlerService>()
.AddSingleton<IWindowingService, WindowingService>()
.AddSingleton<IMicrophoneDeviceService, MicrophoneDeviceService>()
.AddSingleton<IEditorService, EditorService>()
.AddSingleton<IStdInService, StdInService>()
.AddSingleton<ISettingsService, SettingsService>()
.AddSingleton<IAppActivationService, AppActivationService>()
.AddScoped<IOpenAIAPIService, OpenAIAPIService>()
// ViewModels
.AddSingleton<AudioInputControlViewModel>()
.AddSingleton<OpenAIControlViewModel>()
.AddSingleton<MainWindowViewModel>()
.AddTransient<ModalControlViewModel>()
.AddSingleton<MainPageViewModel>()
.AddSingleton<FooterViewModel>()
// Views
.AddSingleton<StatusWindow>()
.AddSingleton<MainWindow>();
})
.Build();
}
}