Skip to content

Commit

Permalink
NR improve application lifecycle handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 22, 2023
1 parent db4e058 commit bad8aa2
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/NewsReader/NewsReader.Presentation/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public partial class App : Application
private readonly ISettingsService settingsService;
private readonly IAppInfoService appInfoService;
private readonly IAppController appController;
private bool isCreated;

public App(ISettingsService settingsService, IAppInfoService appInfoService, Lazy<IAppController> appController, ILocalizationService? localizationService = null)
{
Expand All @@ -52,15 +53,17 @@ protected override Window CreateWindow(IActivationState? activationState)
window.MinimumHeight = 400;
window.Created += (_, _) => OnCreated();
window.Deactivated += (_, _) => OnDeactivated();
#if WINDOWS
window.Destroying += (_, _) => OnDeactivated();
#endif
window.Stopped += (_, _) => OnStopped();
window.Destroying += (_, _) => OnDestroying();
window.Resumed += (_, _) => OnResumed();
return window;
}

private void OnCreated()
{
if (isCreated) return; // On Android it seems that this might be called more than once
isCreated = true;

Log.Default.Info("App started {0}, {1} on {2}", appInfoService.AppName, appInfoService.VersionString, DateTime.Now.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ssK", CultureInfo.InvariantCulture));
Log.Default.Info("Device: {0} {1} {2}; Platform: {3} {4}", DeviceInfo.Idiom, DeviceInfo.Manufacturer, DeviceInfo.Model, DeviceInfo.Platform, DeviceInfo.Version);
string? appSecret = null;
Expand All @@ -72,6 +75,23 @@ private void OnCreated()
private void OnDeactivated()
{
Log.Default.Info("App deactivated");
Save();
}

private void OnStopped()
{
Log.Default.Info("App stopped");
Save();
}

private void OnDestroying()
{
Log.Default.Info("App destroying");
Save();
}

private void Save()
{
appController.Save();
settingsService.Save();
}
Expand Down

0 comments on commit bad8aa2

Please sign in to comment.