-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMauiProgram.cs
202 lines (170 loc) · 6.59 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using CommunityToolkit.Maui;
using CommunityToolkit.Maui.ApplicationModel;
using Ifpa.BackgroundJobs;
using Ifpa.Caching;
using Ifpa.Controls;
using Ifpa.Interfaces;
using Ifpa.Models;
using Ifpa.Platforms.Renderers;
using Ifpa.Platforms.Services;
using Ifpa.Services;
using Ifpa.ViewModels;
using Ifpa.Views;
using MauiIcons.Fluent;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls.Compatibility.Hosting;
using PinballApi;
using PinballApi.Interfaces;
using Plugin.Maui.CalendarStore;
using Plugin.Maui.NativeCalendar;
using Polly;
using Serilog;
using Shiny;
using Shiny.Infrastructure;
using SkiaSharp.Views.Maui.Controls.Hosting;
using Syncfusion.Maui.Core.Hosting;
using The49.Maui.BottomSheet;
namespace Ifpa;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
//pull in appsettings.json
builder.Configuration.AddJsonPlatformBundle();
builder
.UseMauiApp<App>()
//TODO: Maui Compatibility is required for iOS App Links; remove when the below bug is resolved
//https://github.com/dotnet/maui/issues/12295
.UseMauiCompatibility()
.UseMauiCommunityToolkit()
.UseMauiMaps()
.UseFluentMauiIcons()
.UseShiny()
.UseSkiaSharp()
.UseBottomSheet()
.UseNativeCalendar()
.ConfigureSyncfusionCore()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureMauiHandlers((handlers) =>
{
#if IOS
handlers.AddHandler(typeof(InsetTableView), typeof(InsetTableViewRenderer));
#endif
})
.ConfigureLogging()
.ConfigureEssentials(essentials =>
{
//TODO: it's unclear whether icons must be in the Resources/Images folder or in the Platforms/{platform} folder
essentials
.AddAppAction("calendar", Strings.AppShell_Calendar, "IFPA Tournament Calendar", "calendar")
.AddAppAction("my-stats", Strings.AppShell_MyStats, "Your IFPA player data", "mystats")
.AddAppAction("rankings/player-search", Strings.PlayerSearchPage_Title, "Search for other players in the IFPA database", "search")
.OnAppAction(App.HandleAppActions);
essentials.UseVersionTracking();
})
// Show custom Tabbar Badges for iOS and Android
.ConfigureMauiHandlers(h =>
{
h.AddHandler<Shell, TabbarBadgeRenderer>();
})
/*
.ConfigureLifecycleEvents(events =>
{
#if IOS
events.AddiOS(ios => ios
.OpenUrl((app,url,opion) => LogEvent(app, url, opion)));
static bool LogEvent(UIKit.UIApplication application, Foundation.NSUrl url, Foundation.NSDictionary options)
{
Microsoft.Maui.Controls.Application.Current.SendOnAppLinkRequestReceived(url);
return true;
}
#endif
})
*/
.RegisterShinyServices()
.RegisterIfpaModels()
.RegisterIfpaServices();
return builder.Build();
}
static MauiAppBuilder RegisterIfpaModels(this MauiAppBuilder builder)
{
var s = builder.Services;
var c = builder.Configuration;
var appSettings = c.GetRequiredSection("AppSettings").Get<AppSettings>();
//Add all viewmodels
s.AddAllFromNamespace<BaseViewModel>();
//Add all pages
s.AddAllFromNamespace<RankingsPage>();
//Adding RankingsViewModel as a singleton because it's injected into both RankingsPage
//and RankingsFilterPage
s.AddSingleton<RankingsViewModel>();
s.AddSingleton(appSettings);
return builder;
}
static MauiAppBuilder RegisterIfpaServices(this MauiAppBuilder builder)
{
var s = builder.Services;
var c = builder.Configuration;
var appSettings = c.GetRequiredSection("AppSettings").Get<AppSettings>();
s.AddSingleton<BlogPostService>();
s.AddSingleton<NotificationService>();
s.AddSingleton<IToolbarBadgeService, ToolbarBadgeService>();
s.AddSingleton(x => new PinballRankingApiV2(appSettings.IfpaApiKey));
s.AddSingleton<PinballRankingApi>(x => new PinballRankingApi(appSettings.IfpaApiKey));
// Set up caching for IPinballRankingApi
builder.Services.AddSingleton<IPinballRankingApi>(provider =>
{
var api = provider.GetRequiredService<PinballRankingApi>();
var logger = provider.GetRequiredService<ILogger<CachingProxy<IPinballRankingApi>>>();
return CachingProxyFactory.Create<IPinballRankingApi>(api, logger);
/*
var dbPath = Path.Combine(FileSystem.AppDataDirectory, "app_cache.db");
var sqliteCacheProvider = new SQLiteCacheProvider(dbPath);
var cachingPolicy = CachingPolicyFactory.CreatePolicy(sqliteCacheProvider, logger);
return CachingProxyFactory.Create<IPinballRankingApi>(api, cachingPolicy, logger);
*/
});
s.AddSingleton(Geocoding.Default);
s.AddSingleton(Badge.Default);
s.AddSingleton(CalendarStore.Default);
s.AddSingleton(Map.Default);
return builder;
}
static MauiAppBuilder RegisterShinyServices(this MauiAppBuilder builder)
{
var s = builder.Services;
s.AddJobs();
s.AddShinyCoreServices();
s.AddJob(typeof(NotificationJob), requiredNetwork: Shiny.Jobs.InternetAccess.Any);
// shiny.notifications
s.AddNotifications(typeof(NotificationDelegate));
return builder;
}
static MauiAppBuilder ConfigureLogging(this MauiAppBuilder builder)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
#if ANDROID
.WriteTo.AndroidLog()
.Enrich.WithProperty(Serilog.Core.Constants.SourceContextPropertyName, "IFPA")
#endif
#if IOS
.WriteTo.NSLog()
.Enrich.WithProperty(Serilog.Core.Constants.SourceContextPropertyName, "IFPA")
#endif
#if DEBUG
.WriteTo.Debug()
#endif
.CreateLogger();
builder.Logging.AddSerilog(dispose: true);
Log.Logger.Debug("Logger attached to services");
return builder;
}
}