diff --git a/LightBulb/App.axaml.cs b/LightBulb/App.axaml.cs index 9db9a81..2b82025 100644 --- a/LightBulb/App.axaml.cs +++ b/LightBulb/App.axaml.cs @@ -25,6 +25,11 @@ public App() { var services = new ServiceCollection(); + // Framework + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + // Services services.AddSingleton(); services.AddSingleton(); @@ -32,10 +37,6 @@ public App() services.AddSingleton(); services.AddSingleton(); - // View model framework - services.AddSingleton(); - services.AddSingleton(); - // View models services.AddTransient(); services.AddTransient(); @@ -47,9 +48,6 @@ public App() services.AddTransient(); services.AddTransient(); - // View framework - services.AddSingleton(); - _services = services.BuildServiceProvider(true); _mainViewModel = _services.GetRequiredService().CreateMainViewModel(); } @@ -69,12 +67,6 @@ public override void OnFrameworkInitializationCompleted() Color.Parse("#343838"), Color.Parse("#F9A825") ); - - // Finalize pending updates (and restart) before launching the app - _services.GetRequiredService().FinalizePendingUpdates(); - - // Load settings - _services.GetRequiredService().Load(); } private void ShowMainWindow() @@ -126,7 +118,7 @@ private void DisableTemporarily1MinuteMenuItem_OnClick(object? sender, EventArgs _mainViewModel.Dashboard.DisableTemporarilyCommand.Execute(TimeSpan.FromMinutes(1)); private void ExitMenuItem_OnClick(object? sender, EventArgs args) => - ApplicationLifetime?.Shutdown(); + ApplicationLifetime?.TryShutdown(); public void Dispose() => _services.Dispose(); } diff --git a/LightBulb/LightBulb.csproj b/LightBulb/LightBulb.csproj index d706111..6ca3773 100644 --- a/LightBulb/LightBulb.csproj +++ b/LightBulb/LightBulb.csproj @@ -29,7 +29,7 @@ - + \ No newline at end of file diff --git a/LightBulb/Services/UpdateService.cs b/LightBulb/Services/UpdateService.cs index d3c7d1c..caa4367 100644 --- a/LightBulb/Services/UpdateService.cs +++ b/LightBulb/Services/UpdateService.cs @@ -57,7 +57,9 @@ public void FinalizePendingUpdates() return; _updateManager.LaunchUpdater(lastPreparedUpdate); - Application.Current?.ApplicationLifetime?.Shutdown(2); + + if (Application.Current?.ApplicationLifetime?.TryShutdown(2) != true) + Environment.Exit(2); } catch (UpdaterAlreadyLaunchedException) { diff --git a/LightBulb/Utils/Extensions/AvaloniaExtensions.cs b/LightBulb/Utils/Extensions/AvaloniaExtensions.cs index 268fd32..b417839 100644 --- a/LightBulb/Utils/Extensions/AvaloniaExtensions.cs +++ b/LightBulb/Utils/Extensions/AvaloniaExtensions.cs @@ -1,5 +1,4 @@ -using System; -using Avalonia.Controls; +using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; namespace LightBulb.Utils.Extensions; @@ -11,20 +10,19 @@ lifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime ? desktopLifetime.MainWindow : null; - public static void Shutdown(this IApplicationLifetime lifetime, int exitCode = 0) + public static bool TryShutdown(this IApplicationLifetime lifetime, int exitCode = 0) { if (lifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime) { - desktopLifetime.TryShutdown(exitCode); - return; + return desktopLifetime.TryShutdown(exitCode); } if (lifetime is IControlledApplicationLifetime controlledLifetime) { controlledLifetime.Shutdown(exitCode); - return; + return true; } - Environment.Exit(exitCode); + return false; } } diff --git a/LightBulb/ViewModels/MainViewModel.cs b/LightBulb/ViewModels/MainViewModel.cs index 0f2c5f0..d16ab96 100644 --- a/LightBulb/ViewModels/MainViewModel.cs +++ b/LightBulb/ViewModels/MainViewModel.cs @@ -104,6 +104,12 @@ Click LEARN MORE to find ways that you can help. [RelayCommand] private async Task InitializeAsync() { + // Finalize pending updates (and restart if necessary) + updateService.FinalizePendingUpdates(); + + // Load settings + settingsService.Load(); + await ShowGammaRangePromptAsync(); await ShowFirstTimeExperienceMessageAsync(); await ShowUkraineSupportMessageAsync();