forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MauiContext.cs
125 lines (104 loc) · 3.44 KB
/
MauiContext.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
using System;
using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Animations;
namespace Microsoft.Maui
{
public class MauiContext : IMauiContext
{
readonly WrappedServiceProvider _services;
readonly IMauiContext? _parent;
#if __ANDROID__
public MauiContext(IServiceProvider services, Android.App.Application application, IMauiContext? parent = null)
: this(services, (Android.Content.Context)application, parent)
{
AddSpecific(application);
}
public MauiContext(IServiceProvider services, Android.Content.Context context, IMauiContext? parent = null)
: this(services, parent)
{
AddWeakSpecific(context);
}
#elif __IOS__
public MauiContext(IServiceProvider services, UIKit.UIApplicationDelegate application, IMauiContext? parent = null)
: this(services, parent)
{
AddSpecific(application);
}
#elif __MACOS__
public MauiContext(IServiceProvider services, AppKit.NSApplicationDelegate application, IMauiContext? parent = null)
: this(services, parent)
{
AddSpecific(application);
}
#elif WINDOWS
public MauiContext(IServiceProvider services, UI.Xaml.Application application, IMauiContext? parent = null)
: this(services, parent)
{
AddSpecific(application);
}
#endif
public MauiContext(IMauiContext parent)
: this(parent.Services, parent)
{
}
public MauiContext(IServiceProvider services, IMauiContext? parent = null)
{
_services = new WrappedServiceProvider(services ?? throw new ArgumentNullException(nameof(services)));
_parent = parent;
// the animation manager should only be fetched once per context
// TODO: maybe this should be set from outside?
AddSpecific(() => services.GetRequiredService<IAnimationManager>());
}
public IServiceProvider Services => _services;
public IMauiHandlersServiceProvider Handlers =>
Services.GetRequiredService<IMauiHandlersServiceProvider>();
#if __ANDROID__
public Android.Content.Context? Context =>
Services.GetService<Android.Content.Context>();
#endif
internal void AddSpecific<TService>(TService instance)
where TService : class
{
_services.AddSpecific(typeof(TService), () => instance);
}
internal void AddWeakSpecific<TService>(TService instance)
where TService : class
{
var weak = new WeakReference(instance);
_services.AddSpecific(typeof(TService), () => weak.Target);
}
internal void AddSpecific<TService>(Func<TService> factory)
where TService : class
{
var lazy = new Lazy<TService>(() => factory());
_services.AddSpecific(typeof(TService), () => lazy.Value);
}
internal void AddSpecific<TService, TImplementation>(Func<TImplementation> factory)
where TService : class
where TImplementation : class, TService
{
var lazy = new Lazy<TImplementation>(() => factory());
_services.AddSpecific(typeof(TService), () => lazy.Value);
}
class WrappedServiceProvider : IServiceProvider
{
readonly IServiceProvider _serviceProvider;
readonly ConcurrentDictionary<Type, Func<object?>> _scopeStatic = new();
public WrappedServiceProvider(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public object? GetService(Type serviceType)
{
if (_scopeStatic.TryGetValue(serviceType, out var getter))
return getter.Invoke();
return _serviceProvider.GetService(serviceType);
}
public void AddSpecific(Type type, Func<object?> getter)
{
_scopeStatic[type] = getter;
}
}
}
}