-
Notifications
You must be signed in to change notification settings - Fork 0
/
RazorComponentRenderer.cs
167 lines (152 loc) · 6.75 KB
/
RazorComponentRenderer.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
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading.Tasks;
using Xamarin.Forms;
using XF = Xamarin.Forms;
using Microsoft.Extensions.DependencyInjection;
namespace LivingThing.Core.Frameworks.XamarinRazor
{
internal class RazorComponentRenderer : Renderer
{
internal Dictionary<int, ComponentAdapterController> Adapters = new Dictionary<int, ComponentAdapterController>();
public RazorComponentRenderer(IServiceProvider serviceProvider) : base(serviceProvider, serviceProvider.GetRequiredService<ILoggerFactory>())
{
ServiceProvider = serviceProvider;
}
internal IServiceProvider ServiceProvider { get; }
internal ILoggerFactory LoggerFactory { get; }
public override Dispatcher Dispatcher => new XamarinDeviceDispatcher();
public Task<TRootElement> AddComponent<TComponent, TRootElement>(Element parentElement, Action<TComponent> parameterSetter) where TRootElement : class
{
return AddComponent<TRootElement>(typeof(TComponent), parentElement, (o)=> parameterSetter?.Invoke((TComponent)o));
}
IComponent RootComponent;
public async Task<TRootElement> AddComponent<TRootElement>(Type componentType, Element parentElement, Action<object> parameterSetter) where TRootElement:class
{
ComponentAdapterController adapter = null;
//await Dispatcher.InvokeAsync(async () =>
//{
var component = InstantiateComponent(componentType);
var componentId = AssignRootComponentId(component);
parameterSetter?.Invoke(component);
adapter = new ComponentAdapterController(this, component, parentElement);
Adapters[componentId] = adapter;
await RenderRootComponentAsync(componentId).ConfigureAwait(false);
RootComponent = component;
//}).ConfigureAwait(false);
if (adapter.RootElement is BridgeComponentBase @base)
return @base.Native as TRootElement;
return adapter.RootElement as TRootElement;
}
MethodInfo invokeAsync;
MethodInfo stateHasChanged;
Action rerender;
public void ReRender()
{
invokeAsync ??= typeof(ComponentBase).GetMethod("InvokeAsync", bindingAttr: BindingFlags.NonPublic | BindingFlags.Instance, types: new Type[] { typeof(Action) }, binder: null, modifiers: null);
stateHasChanged ??= typeof(ComponentBase).GetMethod("StateHasChanged", BindingFlags.NonPublic | BindingFlags.Instance);
rerender ??= () => stateHasChanged.Invoke(RootComponent, new object[] { });
invokeAsync.Invoke(RootComponent, new object[] { rerender });
}
void PrintException(Exception exception)
{
Debug.WriteLine($"{nameof(RazorComponentRenderer)} exception type '{exception?.GetType().Name}': '{exception?.Message}' => \r\n{exception.StackTrace}");
if (exception.InnerException != null)
{
PrintException(exception.InnerException);
}
}
protected override void HandleException(Exception exception)
{
PrintException(exception);
XF.Device.InvokeOnMainThreadAsync(() =>
{
XF.Application.Current.MainPage = GetErrorPageForException(exception);
});
}
private static XF.View GetInnerExceptioView(Exception exception)
{
if (exception == null)
return new StackLayout();
return new StackLayout()
{
Children =
{
new XF.Label
{
FontSize = XF.Device.GetNamedSize(XF.NamedSize.Medium, typeof(XF.Label)),
Text = exception?.Message,
},
new XF.Label
{
FontSize = XF.Device.GetNamedSize(XF.NamedSize.Small, typeof(XF.Label)),
Text = exception?.StackTrace,
},
GetInnerExceptioView(exception?.InnerException)
}
};
}
private static XF.ContentPage GetErrorPageForException(Exception exception)
{
var errorPage = new XF.ContentPage()
{
Title = $"Unhandled exception({exception.GetType().Name})",
BackgroundColor=XF.Color.White,
Content = new XF.StackLayout
{
Padding = 10,
Children =
{
new XF.Label
{
FontAttributes = XF.FontAttributes.Bold,
FontSize = XF.Device.GetNamedSize(XF.NamedSize.Large, typeof(XF.Label)),
Text = "Unhandled exception",
},
new XF.Label
{
Text = exception?.Message,
},
new XF.ScrollView
{
Content = new StackLayout()
{
Children =
{
new XF.Label
{
FontSize = XF.Device.GetNamedSize(XF.NamedSize.Small, typeof(XF.Label)),
Text = exception?.StackTrace,
},
GetInnerExceptioView(exception?.InnerException)
}
}
},
},
},
};
return errorPage;
}
protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
{
foreach (var updatedComponent in renderBatch.UpdatedComponents.Array.Take(renderBatch.UpdatedComponents.Count))
{
var adapter = Adapters[updatedComponent.ComponentId];
adapter.Apply(updatedComponent, renderBatch);
}
foreach (var updatedComponent in renderBatch.DisposedComponentIDs.Array.Take(renderBatch.DisposedComponentIDs.Count))
{
Adapters[updatedComponent].Dispose();
Adapters.Remove(updatedComponent);
}
return Task.CompletedTask;
}
}
}