-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPowerKitMvvmBuilder.cs
94 lines (79 loc) · 3.24 KB
/
MPowerKitMvvmBuilder.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
using MPowerKit.Navigation.Behaviors;
using MPowerKit.Navigation.Interfaces;
using MPowerKit.Navigation.Utilities;
using MPowerKit.Navigation.WindowInfrastructure;
namespace MPowerKit.Navigation;
public class MPowerKitInitializationService : IMauiInitializeService
{
/// <summary>
/// Initializes the modules.
/// </summary>
public void Initialize(IServiceProvider services)
{
var builder = services.GetRequiredService<MPowerKitMvvmBuilder>();
builder.OnInitialized(services);
}
}
public class MPowerKitMvvmBuilder
{
protected List<Action<IServiceProvider>> Initializations { get; } = [];
protected bool Initialized { get; set; }
public IServiceCollection Services { get; }
protected Func<IServiceProvider, INavigationService, ValueTask>? OnAppStartedAction;
public MPowerKitMvvmBuilder(MauiAppBuilder mauiAppBuilder)
{
ThreadUtil.Init(SynchronizationContext.Current!);
#if IOS || MACCATALYST
mauiAppBuilder.ConfigureMauiHandlers(h =>
{
h.AddHandler<NavigationPage, MPowerKit.Navigation.PlatfromSpecific.MPowerKitNavigtaionRenderer>();
});
#endif
Services = mauiAppBuilder.Services;
Services.AddSingleton(this);
Services.AddSingleton<IMauiInitializeService, MPowerKitInitializationService>();
Services.AddSingleton<IWindowCreator, MPowerKitWindowCreator>();
Services.AddSingleton<IWindowManager, MPowerKitWindowManager>();
Services.AddTransient<IMPowerKitWindow, MPowerKitWindow>();
Services.AddScoped<INavigationService, NavigationService>();
Services.AddScoped<IPageAccessor, PageAccessor>();
//Pages
Services.RegisterBehavior<Page, PageLifecycleAwareBehavior>();
Services.RegisterBehavior<TabbedPage, TabbedPageActiveTabAwareBehavior>();
Services.RegisterBehavior<FlyoutPage, FlyoutPageFlyoutPresentedAwareBehavior>();
Services.RegisterForNavigation<NavigationPage>();
Services.RegisterForNavigation<TabNavigationPage>();
Services.RegisterForNavigation<TabbedPage>();
Services.RegisterForNavigation<FlyoutPage>();
}
public virtual MPowerKitMvvmBuilder OnInitialized(Action<IServiceProvider> action)
{
Initializations.Add(action);
return this;
}
public virtual MPowerKitMvvmBuilder UsePageEventsInRegions()
{
MvvmHelpers.UsePageEventsInRegions = true;
return this;
}
public virtual void OnInitialized(IServiceProvider provider)
{
if (Initialized)
return;
Initialized = true;
Initializations.ForEach(action => action(provider));
}
public virtual void OnAppStarted(IServiceProvider provider)
{
if (OnAppStartedAction is null)
throw new ArgumentException("You must call OnAppStart on the NavigationBuilder.");
// Ensure that this is executed before we navigate.
OnInitialized(provider);
ThreadUtil.RunOnUIThread(() => OnAppStartedAction(provider, provider.GetRequiredService<INavigationService>()));
}
public virtual MPowerKitMvvmBuilder OnAppStart(Func<IServiceProvider, INavigationService, ValueTask> onAppStarted)
{
OnAppStartedAction = onAppStarted;
return this;
}
}