Server termination and refactoring message dialogs.

This commit is contained in:
Burak Kaan Köse
2024-08-21 22:42:52 +02:00
parent bab3272970
commit f627226da9
31 changed files with 548 additions and 388 deletions

View File

@@ -21,6 +21,7 @@ using Windows.Foundation.Metadata;
using Windows.Storage;
using Windows.System.Profile;
using Windows.UI;
using Windows.UI.Core.Preview;
using Windows.UI.Notifications;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
@@ -38,6 +39,7 @@ using Wino.Core.UWP;
using Wino.Core.UWP.Services;
using Wino.Mail.ViewModels;
using Wino.Messaging.Client.Connection;
using Wino.Messaging.Client.Navigation;
using Wino.Messaging.Server;
using Wino.Services;
@@ -107,6 +109,78 @@ namespace Wino
WeakReferenceMessenger.Default.Register(this);
}
private async void ApplicationCloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
{
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.
var accountService = Services.GetService<IAccountService>();
var accounts = await accountService.GetAccountsAsync();
if (accounts.Count > 0)
{
// User has some accounts. Check if Wino Server runs on system startup.
var dialogService = Services.GetService<IDialogService>();
var startupBehaviorService = Services.GetService<IStartupBehaviorService>();
var preferencesService = Services.GetService<IPreferencesService>();
var currentStartupBehavior = await startupBehaviorService.GetCurrentStartupBehaviorAsync();
bool? isGoToAppPreferencesRequested = null;
if (preferencesService.ServerTerminationBehavior == ServerBackgroundMode.Terminate)
{
// 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.
isGoToAppPreferencesRequested = await _dialogService.ShowWinoCustomMessageDialogAsync("Background Synchronization",
"You are terminating Wino Mail and your app close behavior is set to 'Terminate'.\nThis will stop all background synchronizations and notifications.\nDo you want to go to App Preferences to set Wino Mail to run minimized or in the background?",
Translator.Buttons_Yes,
WinoCustomMessageDialogIcon.Warning,
Translator.Buttons_No,
"DontAskTerminateServerBehavior");
}
if (isGoToAppPreferencesRequested == null && currentStartupBehavior != StartupBehaviorResult.Enabled)
{
// Startup behavior is not enabled.
isGoToAppPreferencesRequested = await dialogService.ShowWinoCustomMessageDialogAsync("Background Synchronization",
"Application has not been set to launch on Windows startup.\nThis may impact notifications and background synchronization.\nDo you want to go to App Preferences page to enable it?",
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<bool, TerminateServerRequested>(new TerminateServerRequested());
Log.Information("Server is killed.");
}
catch (Exception ex)
{
Log.Error(ex, "Failed to kill server.");
}
}
}
deferral.Complete();
}
private async void OnResuming(object sender, object e)
{
// App Service connection was lost on suspension.
@@ -233,6 +307,7 @@ namespace Wino
LogActivation("Window is created.");
ConfigureTitleBar();
TryRegisterAppCloseChange();
}
protected override async void OnLaunched(LaunchActivatedEventArgs args)
@@ -245,6 +320,18 @@ namespace Wino
}
}
private void TryRegisterAppCloseChange()
{
try
{
var systemNavigationManagerPreview = SystemNavigationManagerPreview.GetForCurrentView();
systemNavigationManagerPreview.CloseRequested -= ApplicationCloseRequested;
systemNavigationManagerPreview.CloseRequested += ApplicationCloseRequested;
}
catch { }
}
protected override async void OnFileActivated(FileActivatedEventArgs args)
{
base.OnFileActivated(args);