summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/VisualElementPackager.cs
blob: 0e08cfedbf7cb1b93ffc6298e2c0c76bda122aa7 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms.Internals;
using AView = Android.Views.View;

namespace Xamarin.Forms.Platform.Android
{
	public class VisualElementPackager : IDisposable
	{
		readonly EventHandler<ElementEventArgs> _childAddedHandler;
		readonly EventHandler<ElementEventArgs> _childRemovedHandler;
		readonly EventHandler _childReorderedHandler;
		List<IVisualElementRenderer> _childViews;

		bool _disposed;

		IVisualElementRenderer _renderer;

		public VisualElementPackager(IVisualElementRenderer renderer)
		{
			if (renderer == null)
				throw new ArgumentNullException("renderer");

			_childAddedHandler = OnChildAdded;
			_childRemovedHandler = OnChildRemoved;
			_childReorderedHandler = OnChildrenReordered;

			_renderer = renderer;
			_renderer.ElementChanged += (sender, args) => SetElement(args.OldElement, args.NewElement);
		}

		IElementController ElementController => _renderer.Element as IElementController;

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (_disposed)
				return;

			_disposed = true;

			if (_renderer != null)
			{
				if (_childViews != null)
				{
					_childViews.Clear();
					_childViews = null;
				}

				if (_renderer.Element != null)
				{
					_renderer.Element.ChildAdded -= _childAddedHandler;
					_renderer.Element.ChildRemoved -= _childRemovedHandler;

					_renderer.Element.ChildrenReordered -= _childReorderedHandler;
				}
				_renderer = null;
			}
		}

		public void Load()
		{
			SetElement(null, _renderer.Element);
		}

		void AddChild(VisualElement view, IVisualElementRenderer oldRenderer = null, RendererPool pool = null, bool sameChildren = false)
		{
			Performance.Start();

			if (_childViews == null)
				_childViews = new List<IVisualElementRenderer>();

			IVisualElementRenderer renderer = oldRenderer;
			if (pool != null)
				renderer = pool.GetFreeRenderer(view);
			if (renderer == null)
			{
				Performance.Start("New renderer");
				renderer = Platform.CreateRenderer(view);
				Performance.Stop("New renderer");
			}

			if (renderer == oldRenderer)
			{
				Platform.SetRenderer(renderer.Element, null);
				renderer.SetElement(view);
			}

			Performance.Start("Set renderer");
			Platform.SetRenderer(view, renderer);
			Performance.Stop("Set renderer");

			Performance.Start("Add view");
			if (!sameChildren)
			{
				_renderer.ViewGroup.AddView(renderer.ViewGroup);
				_childViews.Add(renderer);
			}
			Performance.Stop("Add view");

			Performance.Stop();
		}

		void EnsureChildOrder()
		{
			for (var i = 0; i < ElementController.LogicalChildren.Count; i++)
			{
				Element child = ElementController.LogicalChildren[i];
				var element = (VisualElement)child;
				if (element != null)
				{
					IVisualElementRenderer r = Platform.GetRenderer(element);
					_renderer.ViewGroup.BringChildToFront(r.ViewGroup);
				}
			}
		}

		void OnChildAdded(object sender, ElementEventArgs e)
		{
			var view = e.Element as VisualElement;
			if (view != null)
				AddChild(view);

			if (ElementController.LogicalChildren[ElementController.LogicalChildren.Count - 1] != view)
				EnsureChildOrder();
		}

		void OnChildRemoved(object sender, ElementEventArgs e)
		{
			Performance.Start();
			var view = e.Element as VisualElement;
			if (view != null)
				RemoveChild(view);

			Performance.Stop();
		}

		void OnChildrenReordered(object sender, EventArgs e)
		{
			EnsureChildOrder();
		}

		void RemoveChild(VisualElement view)
		{
			IVisualElementRenderer renderer = Platform.GetRenderer(view);
			_childViews.Remove(renderer);
			renderer.ViewGroup.RemoveFromParent();
			renderer.Dispose();
		}

		void SetElement(VisualElement oldElement, VisualElement newElement)
		{
			Performance.Start();

			var sameChildrenTypes = false;

			ReadOnlyCollection<Element> newChildren = null, oldChildren = null;

			RendererPool pool = null;
			if (oldElement != null)
			{
				if (newElement != null)
				{
					sameChildrenTypes = true;

					oldChildren = ((IElementController)oldElement).LogicalChildren;
					newChildren = ((IElementController)newElement).LogicalChildren;
					if (oldChildren.Count == newChildren.Count)
					{
						for (var i = 0; i < oldChildren.Count; i++)
						{
							if (oldChildren[i].GetType() != newChildren[i].GetType())
							{
								sameChildrenTypes = false;
								break;
							}
						}
					}
					else
						sameChildrenTypes = false;
				}

				oldElement.ChildAdded -= _childAddedHandler;
				oldElement.ChildRemoved -= _childRemovedHandler;

				oldElement.ChildrenReordered -= _childReorderedHandler;

				if (!sameChildrenTypes)
				{
					_childViews = new List<IVisualElementRenderer>();
					pool = new RendererPool(_renderer, oldElement);
					pool.ClearChildrenRenderers();
				}
			}

			if (newElement != null)
			{
				Performance.Start("Setup");
				newElement.ChildAdded += _childAddedHandler;
				newElement.ChildRemoved += _childRemovedHandler;

				newElement.ChildrenReordered += _childReorderedHandler;

				newChildren = newChildren ?? ((IElementController)newElement).LogicalChildren;

				for (var i = 0; i < newChildren.Count; i++)
				{
					IVisualElementRenderer oldRenderer = null;
					if (oldChildren != null && sameChildrenTypes)
						oldRenderer = _childViews[i];

					AddChild((VisualElement)newChildren[i], oldRenderer, pool, sameChildrenTypes);
				}

#if DEBUG
				//if (renderer.Element.LogicalChildren.Any() && renderer.ViewGroup.ChildCount != renderer.Element.LogicalChildren.Count)
				//	throw new InvalidOperationException ("SetElement did not create the correct number of children");
#endif
				Performance.Stop("Setup");
			}

			Performance.Stop();
		}
	}
}