about summary refs log tree commit diff
path: root/MatrixRoomUtils.Desktop/App.axaml.cs
blob: e0b50a5bec6c83f6b689aff0004e3b68253f74a8 (plain) (blame)
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
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using LibMatrix.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sentry;

namespace MatrixRoomUtils.Desktop;

public partial class App : Application {
    public IHost host { get; set; }

    public override void Initialize() {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted() {
        host = Host.CreateDefaultBuilder().ConfigureServices((ctx, services) => {
            services.AddSingleton<MRUDesktopConfiguration>();
            services.AddSingleton<SentryService>();
            services.AddSingleton<TieredStorageService>(x =>
                new TieredStorageService(
                    cacheStorageProvider: new FileStorageProvider(x.GetService<MRUDesktopConfiguration>().CacheStoragePath),
                    dataStorageProvider: new FileStorageProvider(x.GetService<MRUDesktopConfiguration>().DataStoragePath)
                )
            );
            services.AddSingleton(new RoryLibMatrixConfiguration {
                AppName = "MatrixRoomUtils.Desktop"
            });
            services.AddRoryLibMatrixServices();
            // foreach (var commandClass in new ClassCollector<ICommand>().ResolveFromAllAccessibleAssemblies()) {
            // Console.WriteLine($"Adding command {commandClass.Name}");
            // services.AddScoped(typeof(ICommand), commandClass);
            // }
            services.AddSingleton<MRUStorageWrapper>();
            services.AddSingleton<MainWindow>();
            services.AddSingleton(this);
        }).UseConsoleLifetime().Build();

        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
            var scopeFac = host.Services.GetService<IServiceScopeFactory>();
            var scope = scopeFac.CreateScope();
            desktop.MainWindow = scope.ServiceProvider.GetRequiredService<MainWindow>();
        }
        base.OnFrameworkInitializationCompleted();
    }
}