-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.axaml.cs
More file actions
122 lines (102 loc) · 3.58 KB
/
App.axaml.cs
File metadata and controls
122 lines (102 loc) · 3.58 KB
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
using System;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using BetterRaid.Services;
using BetterRaid.ViewModels;
using BetterRaid.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace BetterRaid;
public class App : Application
{
private ServiceProvider? _serviceProvider;
private ILogger<App>? _logger;
public override void Initialize()
{
_serviceProvider = InitializeServices();
_logger = _serviceProvider.GetRequiredService<ILogger<App>>();
if (TryLoadDatabase() == false)
{
_logger?.LogError("Failed to load or initialize database");
Environment.Exit(1);
}
AvaloniaXamlLoader.Load(_serviceProvider, this);
}
private bool TryLoadDatabase()
{
if (_serviceProvider == null)
{
throw new FieldAccessException($"\"{nameof(_serviceProvider)}\" was null");
}
var db = _serviceProvider.GetRequiredService<IDatabaseService>();
try
{
db.LoadOrCreate();
Task.Run(db.UpdateLoadedChannels);
return true;
}
catch (Exception e)
{
_logger?.LogError(e, "Failed to load database");
return false;
}
}
private ServiceProvider InitializeServices()
{
var services = new ServiceCollection();
services.AddLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Debug);
logging.AddConsole();
});
services.AddSingleton<ITwitchService, TwitchService>();
services.AddSingleton<IWebToolsService, WebToolsService>();
services.AddSingleton<IDatabaseService, DatabaseService>();
services.AddSingleton<IDispatcherService, DispatcherService>(_ => new DispatcherService(Dispatcher.UIThread));
services.AddTransient<MainWindowViewModel>();
return services.BuildServiceProvider();
}
public override void OnFrameworkInitializationCompleted()
{
BindingPlugins.DataValidators.RemoveAt(0);
if(_serviceProvider == null)
{
throw new FieldAccessException($"\"{nameof(_serviceProvider)}\" was null");
}
var mainWindow = new MainWindow
{
DataContext = _serviceProvider.GetRequiredService<MainWindowViewModel>()
};
switch (ApplicationLifetime)
{
case IClassicDesktopStyleApplicationLifetime desktop:
desktop.MainWindow = mainWindow;
desktop.Exit += OnDesktopOnExit;
break;
case ISingleViewApplicationLifetime singleViewPlatform:
singleViewPlatform.MainView = mainWindow;
break;
}
base.OnFrameworkInitializationCompleted();
}
private void OnDesktopOnExit(object? o, ControlledApplicationLifetimeExitEventArgs controlledApplicationLifetimeExitEventArgs)
{
if (_serviceProvider == null)
{
throw new FieldAccessException($"\"{nameof(_serviceProvider)}\" was null");
}
try
{
var db = _serviceProvider.GetRequiredService<IDatabaseService>();
db.Save();
}
catch (Exception e)
{
_logger?.LogError(e, "Failed to save database");
}
}
}