1515using YoutubeDownloader . ViewModels ;
1616using YoutubeDownloader . ViewModels . Components ;
1717using YoutubeDownloader . ViewModels . Dialogs ;
18- using YoutubeDownloader . Views ;
1918
2019namespace YoutubeDownloader ;
2120
2221public class App : Application , IDisposable
2322{
24- private readonly DisposableCollector _eventRoot = new ( ) ;
25-
2623 private readonly ServiceProvider _services ;
27- private readonly SettingsService _settingsService ;
28- private readonly MainViewModel _mainViewModel ;
24+ private readonly DisposableCollector _eventRoot = new ( ) ;
2925
3026 private bool _isDisposed ;
3127
@@ -57,42 +53,30 @@ public App()
5753 services . AddTransient < SettingsViewModel > ( ) ;
5854
5955 _services = services . BuildServiceProvider ( true ) ;
60- _settingsService = _services . GetRequiredService < SettingsService > ( ) ;
61- _mainViewModel = _services . GetRequiredService < ViewModelManager > ( ) . CreateMainViewModel ( ) ;
6256
6357 // Re-initialize the theme when the user changes it
6458 _eventRoot . Add (
65- _settingsService . WatchProperty (
66- o => o . Theme ,
67- ( ) =>
68- {
69- RequestedThemeVariant = _settingsService . Theme switch
59+ _services
60+ . GetRequiredService < SettingsService > ( )
61+ . WatchProperty (
62+ o => o . Theme ,
63+ ( ) =>
7064 {
71- ThemeVariant . Light => Avalonia . Styling . ThemeVariant . Light ,
72- ThemeVariant . Dark => Avalonia . Styling . ThemeVariant . Dark ,
73- _ => Avalonia . Styling . ThemeVariant . Default ,
74- } ;
75-
76- InitializeTheme ( ) ;
77- }
78- )
65+ RequestedThemeVariant = _services
66+ . GetRequiredService < SettingsService > ( )
67+ . Theme switch
68+ {
69+ ThemeVariant . Light => Avalonia . Styling . ThemeVariant . Light ,
70+ ThemeVariant . Dark => Avalonia . Styling . ThemeVariant . Dark ,
71+ _ => Avalonia . Styling . ThemeVariant . Default ,
72+ } ;
73+
74+ InitializeTheme ( ) ;
75+ }
76+ )
7977 ) ;
8078 }
8179
82- public override void Initialize ( )
83- {
84- base . Initialize ( ) ;
85-
86- AvaloniaXamlLoader . Load ( this ) ;
87- }
88-
89- public override void RegisterServices ( )
90- {
91- base . RegisterServices ( ) ;
92-
93- AvaloniaWebViewBuilder . Initialize ( config => config . IsInPrivateModeEnabled = true ) ;
94- }
95-
9680 private void InitializeTheme ( )
9781 {
9882 var actualTheme = RequestedThemeVariant ? . Key switch
@@ -108,25 +92,35 @@ private void InitializeTheme()
10892 : Theme . Create ( Theme . Dark , Color . Parse ( "#E8E8E8" ) , Color . Parse ( "#F9A825" ) ) ;
10993 }
11094
95+ public override void Initialize ( )
96+ {
97+ base . Initialize ( ) ;
98+
99+ AvaloniaXamlLoader . Load ( this ) ;
100+ }
101+
102+ public override void RegisterServices ( )
103+ {
104+ base . RegisterServices ( ) ;
105+
106+ AvaloniaWebViewBuilder . Initialize ( config => config . IsInPrivateModeEnabled = true ) ;
107+ }
108+
111109 public override void OnFrameworkInitializationCompleted ( )
112110 {
113111 if ( ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
114112 {
115- desktop . MainWindow = new MainView { DataContext = _mainViewModel } ;
116-
117- void OnExit ( object ? sender , ControlledApplicationLifetimeExitEventArgs args )
118- {
119- if ( sender is IControlledApplicationLifetime lifetime )
120- lifetime . Exit -= OnExit ;
121-
122- Dispose ( ) ;
123- }
113+ desktop . MainWindow = _services
114+ . GetRequiredService < ViewManager > ( )
115+ . TryBindWindow (
116+ _services . GetRequiredService < ViewModelManager > ( ) . CreateMainViewModel ( )
117+ ) ;
124118
125119 // Although `App.Dispose()` is invoked from `Program.Main(...)`, on some platforms
126120 // it may be called too late in the shutdown lifecycle. Attach an exit
127121 // handler to ensure timely disposal as a safeguard.
128122 // https://github.com/Tyrrrz/YoutubeDownloader/issues/795
129- desktop . Exit += OnExit ;
123+ desktop . Exit += ( _ , _ ) => Dispose ( ) ;
130124 }
131125
132126 base . OnFrameworkInitializationCompleted ( ) ;
@@ -135,7 +129,7 @@ void OnExit(object? sender, ControlledApplicationLifetimeExitEventArgs args)
135129 InitializeTheme ( ) ;
136130
137131 // Load settings
138- _settingsService . Load ( ) ;
132+ _services . GetRequiredService < SettingsService > ( ) . Load ( ) ;
139133 }
140134
141135 private void Application_OnActualThemeVariantChanged ( object ? sender , EventArgs args ) =>
0 commit comments