-
Notifications
You must be signed in to change notification settings - Fork 0
/
MauiProgram.cs
82 lines (72 loc) · 3.3 KB
/
MauiProgram.cs
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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Reflection;
using System.Text.RegularExpressions;
using Camera.MAUI;
using CommunityToolkit.Maui;
namespace DVisit
{
public static partial class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
var a = Assembly.GetExecutingAssembly();
using var stream = a.GetManifestResourceStream("DVisit.appsettings.json");
if (stream != null)
{
var config = new ConfigurationBuilder()
.AddJsonStream(stream)
.Build();
builder.Configuration.AddConfiguration(config);
}
builder
.UseMauiApp<App>()
.UseMauiCameraView()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("FontAwesome6FreeBrands.otf", "FontAwesomeBrands");
fonts.AddFont("FontAwesome6FreeRegular.otf", "FontAwesomeRegular");
fonts.AddFont("FontAwesome6FreeSolid.otf", "FontAwesomeSolid");
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
fonts.AddFont("Roboto-Regular.ttf", "RobotoRegular");
});
#if DEBUG
builder.Logging.AddDebug();
#endif
builder.Services
.AddSingleton<ShellViewModel>()
.AddSingleton<MainViewModel>()
.AddSingleton<CaptureCardViewModel>()
.AddSingleton<SettingsViewModel>()
.AddSingleton<MainPage>()
.AddTransient<VisitorDataService>()
.AddTransient(factory =>
{
var config = factory.GetService<IConfiguration>() ?? throw new Exception("No se pudo cargar la configuración de la aplicación.");
var rest = new RestUtilityService(config);
rest.Exception += Api_Exception;
return rest;
})
.AddSingleton<LoadingPage>()
.AddSingleton<LoginPage>()
.AddSingleton<CaptureCardPage>()
.AddSingleton<RegisterPage>()
.AddSingleton<SettingsPage>();
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("es-CL");
return builder.Build();
}
static async private void Api_Exception(object? sender, ThreadExceptionEventArgs e)
{
string message = e.Exception.Message;
Regex regex = ExtractTitle();
var match = regex.Match(message);
if (match != null && Application.Current != null && Application.Current.MainPage != null)
await Application.Current.MainPage.DisplayAlert(match.Success ? match.Value : "Error", match.Success ? message.Replace("[" + match.Value + "]", string.Empty).Trim() : message, "Aceptar");
}
[GeneratedRegex("(?<=^\\[).+?(?=\\])")]
private static partial Regex ExtractTitle();
}
}