-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
143 lines (120 loc) · 5.48 KB
/
Copy pathApp.xaml.cs
File metadata and controls
143 lines (120 loc) · 5.48 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Microsoft.UI.Xaml;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NineLivesAudio.Services;
using NineLivesAudio.Services.Playback;
using NineLivesAudio.ViewModels;
using NineLivesAudio.Views;
using NineLivesAudio.Data;
using NineLivesAudio.Helpers;
namespace NineLivesAudio;
public partial class App : Application
{
private static IHost? _host;
private static MiniPlayerWindow? _miniPlayerWindow;
public static IServiceProvider Services => _host!.Services;
public App()
{
this.InitializeComponent();
// Migrate legacy "AudioBookshelfApp" folders/vault to "NineLivesAudio"
LegacyMigrationHelper.MigrateIfNeeded();
// Build dependency injection container
_host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
// Logging (register first so other services can use it)
services.AddSingleton<ILoggingService, LoggingService>();
// Services - Core
services.AddSingleton<ISettingsService, SettingsService>();
services.AddSingleton<IAudioBookshelfApiService, AudioBookshelfApiService>();
services.AddSingleton<IAudioPlaybackService, AudioPlaybackService>();
services.AddSingleton<IDownloadService, DownloadService>();
services.AddSingleton<ISyncService, SyncService>();
services.AddSingleton<ILocalDatabase, LocalDatabase>();
services.AddSingleton<IAppInitializer, AppInitializer>();
// Services - Connectivity & Offline
services.AddSingleton<IConnectivityService, ConnectivityService>();
services.AddSingleton<IOfflineProgressQueue, OfflineProgressQueue>();
// Services - Navigation
services.AddSingleton<INavigationService, NavigationService>();
// Services - UI/UX Enhancements
services.AddSingleton<INotificationService, NotificationService>();
services.AddSingleton<IMetadataNormalizer, MetadataNormalizer>();
services.AddSingleton<IPlaybackSourceResolver, PlaybackSourceResolver>();
services.AddSingleton<ILibraryFilterService, LibraryFilterService>();
services.AddSingleton<ITrackManager, TrackManager>();
// ViewModels
services.AddTransient<MainViewModel>();
services.AddTransient<HomeViewModel>();
services.AddTransient<LibraryViewModel>();
services.AddTransient<PlayerViewModel>();
services.AddTransient<DownloadsViewModel>();
services.AddTransient<SettingsViewModel>();
services.AddTransient<BookDetailViewModel>();
// Main Window
services.AddSingleton<MainWindow>();
})
.Build();
// Set up global exception handlers
this.UnhandledException += App_UnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
// CRITICAL: No blocking async calls here.
// Show the window IMMEDIATELY, then let MainWindow kick off async init.
ILoggingService? logger = null;
try
{
logger = Services.GetRequiredService<ILoggingService>();
logger.Log("App.OnLaunched: activating main window (no async blocking)");
var mainWindow = Services.GetRequiredService<MainWindow>();
mainWindow.Activate();
logger.Log("App.OnLaunched: window activated, async init will run from MainWindow.Loaded");
}
catch (Exception ex)
{
logger?.LogError("FATAL: App.OnLaunched failed", ex);
System.Diagnostics.Debug.WriteLine($"FATAL: App launch failed: {ex}");
throw;
}
}
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
var logger = Services.GetService<ILoggingService>();
logger?.LogError("UNHANDLED XAML EXCEPTION", e.Exception);
logger?.FlushAsync().GetAwaiter().GetResult();
e.Handled = true;
}
private void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
{
var logger = Services.GetService<ILoggingService>();
logger?.LogError("UNHANDLED DOMAIN EXCEPTION", e.ExceptionObject as Exception);
logger?.FlushAsync().GetAwaiter().GetResult();
}
private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
var logger = Services.GetService<ILoggingService>();
logger?.LogError("UNOBSERVED TASK EXCEPTION", e.Exception);
logger?.FlushAsync().GetAwaiter().GetResult();
e.SetObserved();
}
// --- Mini Player Window Management ---
public static void OpenMiniPlayer()
{
if (_miniPlayerWindow != null)
{
_miniPlayerWindow.Activate();
return;
}
_miniPlayerWindow = new MiniPlayerWindow();
_miniPlayerWindow.Closed += (s, e) => _miniPlayerWindow = null;
_miniPlayerWindow.Activate();
}
public static void CloseMiniPlayer()
{
_miniPlayerWindow?.Close();
_miniPlayerWindow = null;
}
}