From a4f9284970bd35772f061e67ee414354325ae7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Sun, 15 Sep 2024 00:30:08 +0200 Subject: [PATCH] Making sure that server is terminated even though there are no accounts. --- Wino.Mail/App.xaml.cs | 93 ++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/Wino.Mail/App.xaml.cs b/Wino.Mail/App.xaml.cs index 5e0c4156..ceb71e8e 100644 --- a/Wino.Mail/App.xaml.cs +++ b/Wino.Mail/App.xaml.cs @@ -114,67 +114,62 @@ namespace Wino var deferral = e.GetDeferral(); // Wino should notify user on app close if: - // 1. User has at least 1 registered account. - // 2. Startup behavior is not Enabled. - // 3. Server terminate behavior is set to Terminate. + // 1. Startup behavior is not Enabled. + // 2. Server terminate behavior is set to Terminate. - var accountService = Services.GetService(); + // User has some accounts. Check if Wino Server runs on system startup. - var accounts = await accountService.GetAccountsAsync(); + var dialogService = Services.GetService(); + var startupBehaviorService = Services.GetService(); + var preferencesService = Services.GetService(); - if (accounts.Count > 0) + var currentStartupBehavior = await startupBehaviorService.GetCurrentStartupBehaviorAsync(); + + bool? isGoToAppPreferencesRequested = null; + + if (preferencesService.ServerTerminationBehavior == ServerBackgroundMode.Terminate) { - // User has some accounts. Check if Wino Server runs on system startup. + // Starting the server is fine, but check if server termination behavior is set to terminate. + // This state will kill the server once the app is terminated. - var dialogService = Services.GetService(); - var startupBehaviorService = Services.GetService(); - var preferencesService = Services.GetService(); + isGoToAppPreferencesRequested = await _dialogService.ShowWinoCustomMessageDialogAsync(Translator.AppCloseBackgroundSynchronizationWarningTitle, + $"{Translator.AppCloseTerminateBehaviorWarningMessageFirstLine}\n{Translator.AppCloseTerminateBehaviorWarningMessageSecondLine}\n\n{Translator.AppCloseTerminateBehaviorWarningMessageThirdLine}", + Translator.Buttons_Yes, + WinoCustomMessageDialogIcon.Warning, + Translator.Buttons_No, + "DontAskTerminateServerBehavior"); + } - var currentStartupBehavior = await startupBehaviorService.GetCurrentStartupBehaviorAsync(); + if (isGoToAppPreferencesRequested == null && currentStartupBehavior != StartupBehaviorResult.Enabled) + { + // Startup behavior is not enabled. - bool? isGoToAppPreferencesRequested = null; + isGoToAppPreferencesRequested = await dialogService.ShowWinoCustomMessageDialogAsync(Translator.AppCloseBackgroundSynchronizationWarningTitle, + $"{Translator.AppCloseStartupLaunchDisabledWarningMessageFirstLine}\n{Translator.AppCloseStartupLaunchDisabledWarningMessageSecondLine}\n\n{Translator.AppCloseStartupLaunchDisabledWarningMessageThirdLine}", + Translator.Buttons_Yes, + WinoCustomMessageDialogIcon.Warning, + Translator.Buttons_No, + "DontAskDisabledStartup"); + } - if (preferencesService.ServerTerminationBehavior == ServerBackgroundMode.Terminate) + if (isGoToAppPreferencesRequested == true) + { + WeakReferenceMessenger.Default.Send(new NavigateAppPreferencesRequested()); + e.Handled = true; + } + else if (preferencesService.ServerTerminationBehavior == ServerBackgroundMode.Terminate) + { + try { - // Starting the server is fine, but check if server termination behavior is set to terminate. - // This state will kill the server once the app is terminated. + var isServerKilled = await _appServiceConnectionManager.GetResponseAsync(new TerminateServerRequested()); - isGoToAppPreferencesRequested = await _dialogService.ShowWinoCustomMessageDialogAsync(Translator.AppCloseBackgroundSynchronizationWarningTitle, - $"{Translator.AppCloseTerminateBehaviorWarningMessageFirstLine}\n{Translator.AppCloseTerminateBehaviorWarningMessageSecondLine}\n\n{Translator.AppCloseTerminateBehaviorWarningMessageThirdLine}", - Translator.Buttons_Yes, - WinoCustomMessageDialogIcon.Warning, - Translator.Buttons_No, - "DontAskTerminateServerBehavior"); + isServerKilled.ThrowIfFailed(); + + Log.Information("Server is killed."); } - - if (isGoToAppPreferencesRequested == null && currentStartupBehavior != StartupBehaviorResult.Enabled) + catch (Exception ex) { - // Startup behavior is not enabled. - - isGoToAppPreferencesRequested = await dialogService.ShowWinoCustomMessageDialogAsync(Translator.AppCloseBackgroundSynchronizationWarningTitle, - $"{Translator.AppCloseStartupLaunchDisabledWarningMessageFirstLine}\n{Translator.AppCloseStartupLaunchDisabledWarningMessageSecondLine}\n\n{Translator.AppCloseStartupLaunchDisabledWarningMessageThirdLine}", - Translator.Buttons_Yes, - WinoCustomMessageDialogIcon.Warning, - Translator.Buttons_No, - "DontAskDisabledStartup"); - } - - if (isGoToAppPreferencesRequested == true) - { - WeakReferenceMessenger.Default.Send(new NavigateAppPreferencesRequested()); - e.Handled = true; - } - else if (preferencesService.ServerTerminationBehavior == ServerBackgroundMode.Terminate) - { - try - { - var isServerKilled = await _appServiceConnectionManager.GetResponseAsync(new TerminateServerRequested()); - Log.Information("Server is killed."); - } - catch (Exception ex) - { - Log.Error(ex, "Failed to kill server."); - } + Log.Error(ex, "Failed to kill server."); } }