-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
109 lines (94 loc) · 3.66 KB
/
App.xaml.cs
File metadata and controls
109 lines (94 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using Microsoft.Maui.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using SyncOne.Services;
#if ANDROID
using Android.Content;
using Android.App;
using SyncOne.Platforms.Android.Services;
using Application = Android.App.Application;
#endif
namespace SyncOne
{
public partial class App : Microsoft.Maui.Controls.Application, IDisposable
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<App> _logger;
private bool _disposed;
public App(IServiceProvider serviceProvider, ILogger<App> logger)
{
InitializeComponent();
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
var mainPage = _serviceProvider.GetRequiredService<MainPage>();
MainPage = new NavigationPage(mainPage)
{
BarBackgroundColor = Colors.Purple,
BarTextColor = Colors.White
};
// Fire and forget initialization
_ = InitializeAsync();
}
// ... (OnSleep, OnResume)
private async Task InitializeAsync()
{
try
{
var configService = _serviceProvider.GetRequiredService<ConfigurationService>();
await configService.InitializeAsync();
_logger.LogInformation("Application initialized successfully.");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to initialize application.");
try
{
await MainThread.InvokeOnMainThreadAsync(async () =>
{
await Current.MainPage.DisplayAlert(
"Initialization Error",
"There was a problem starting the application. Please try again.",
"OK");
});
}
catch (Exception alertEx)
{
_logger.LogError(alertEx, "Failed to display initialization error alert.");
}
}
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = e.ExceptionObject as Exception;
_logger.LogError(exception, "Unhandled exception occurred. IsTerminating: {IsTerminating}", e.IsTerminating);
}
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
_logger.LogError(e.Exception, "Unobserved Task exception occurred.");
e.SetObserved(); // Prevent the app from crashing
}
public void Dispose()
{
if (_disposed) return;
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// Unsubscribe from events
AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException -= TaskScheduler_UnobservedTaskException;
}
_disposed = true;
}
}
}
}