-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.xaml.cs
141 lines (118 loc) · 5.34 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System.Diagnostics;
using FluentDL.Activation;
using FluentDL.Contracts.Services;
using FluentDL.Core.Contracts.Services;
using FluentDL.Core.Services;
using FluentDL.Helpers;
using FluentDL.Models;
using FluentDL.Notifications;
using FluentDL.Services;
using FluentDL.ViewModels;
using FluentDL.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
namespace FluentDL;
// To learn more about WinUI 3, see https://docs.microsoft.com/windows/apps/winui/winui3/.
public partial class App : Application
{
// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
public IHost Host
{
get;
}
public static T GetService<T>()
where T : class
{
if ((App.Current as App)!.Host.Services.GetService(typeof(T)) is not T service)
{
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs.");
}
return service;
}
public static WindowEx MainWindow
{
get;
} = new MainWindow();
public static UIElement? AppTitlebar
{
get;
set;
}
public App()
{
InitializeComponent();
Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder().UseContentRoot(AppContext.BaseDirectory)
.ConfigureServices((context, services) =>
{
// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();
// Other Activation Handlers
services.AddTransient<IActivationHandler, AppNotificationActivationHandler>();
// Services
services.AddSingleton<IAppNotificationService, AppNotificationService>();
services.AddSingleton<ILocalSettingsService, LocalSettingsService>();
services.AddSingleton<IThemeSelectorService, ThemeSelectorService>();
services.AddTransient<INavigationViewService, NavigationViewService>();
services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
// Core Services
services.AddSingleton<ISampleDataService, SampleDataService>();
services.AddSingleton<IFileService, FileService>();
// Views and ViewModels
services.AddTransient<SettingsViewModel>();
services.AddTransient<SettingsPage>();
services.AddTransient<ContentGridDetailViewModel>();
services.AddTransient<ContentGridDetailPage>();
services.AddTransient<LocalExplorerViewModel>();
services.AddTransient<LocalExplorerPage>();
services.AddTransient<QueueViewModel>();
services.AddTransient<QueuePage>();
services.AddTransient<SearchViewModel>();
services.AddTransient<Search>();
services.AddTransient<ShellPage>();
services.AddTransient<ShellViewModel>();
// Configuration
services.Configure<LocalSettingsOptions>(
context.Configuration.GetSection(nameof(LocalSettingsOptions)));
}).Build();
App.GetService<IAppNotificationService>().Initialize();
UnhandledException += App_UnhandledException;
MainWindow.Closed += (sender, args) =>
{
};
}
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// TODO: Log and handle exceptions as appropriate.
Debug.WriteLine(e.Exception + ":" + e.Message);
}
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
await Task.Delay(500);
base.OnLaunched(args);
// App.GetService<IAppNotificationService>().Show(string.Format("AppNotificationSamplePayload".GetLocalized(), AppContext.BaseDirectory));
await App.GetService<IActivationService>().ActivateAsync(args);
try
{
// Fetch previous command list
await LocalCommands.Init();
// Initialize FFMpeg
await FFmpegRunner.Initialize();
// Initialize api objects
var localSettings = App.GetService<ILocalSettingsService>();
await SpotifyApi.Initialize(await localSettings.ReadSettingAsync<string>(SettingsViewModel.SpotifyClientId), await localSettings.ReadSettingAsync<string>(SettingsViewModel.SpotifyClientSecret));
await DeezerApi.InitDeezerClient(await localSettings.ReadSettingAsync<string>(SettingsViewModel.DeezerARL));
await QobuzApi.Initialize(await localSettings.ReadSettingAsync<string>(SettingsViewModel.QobuzId), await localSettings.ReadSettingAsync<string>(SettingsViewModel.QobuzToken));
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
}