summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/NavigationRenderer.cs
blob: 3fbfa096303e3c3815f615ddbffe5715e7514c82 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Linq;
using System.Threading.Tasks;
using Android.Views;
using Xamarin.Forms.Internals;
using AButton = Android.Widget.Button;
using AView = Android.Views.View;
using AndroidAnimation = Android.Animation;

namespace Xamarin.Forms.Platform.Android
{
	public class NavigationRenderer : VisualElementRenderer<NavigationPage>
	{
		static ViewPropertyAnimator s_currentAnimation;

		Page _current;
		bool _disposed;

		public NavigationRenderer()
		{
			AutoPackage = false;
		}

		public Task<bool> PopToRootAsync(Page page, bool animated = true)
		{
			return OnPopToRootAsync(page, animated);
		}

		public Task<bool> PopViewAsync(Page page, bool animated = true)
		{
			return OnPopViewAsync(page, animated);
		}

		public Task<bool> PushViewAsync(Page page, bool animated = true)
		{
			return OnPushAsync(page, animated);
		}

		IPageController PageController => Element as IPageController;

		protected override void Dispose(bool disposing)
		{
			if (disposing && !_disposed)
			{
				_disposed = true;

				if (Element != null)
				{
					foreach (Element element in PageController.InternalChildren)
					{
						var child = (VisualElement)element;
						if (child == null)
						{
							continue;
						}

						IVisualElementRenderer renderer = Platform.GetRenderer(child);
						renderer?.Dispose();
					}

					var navController = (INavigationPageController)Element;

					navController.PushRequested -= OnPushed;
					navController.PopRequested -= OnPopped;
					navController.PopToRootRequested -= OnPoppedToRoot;
					navController.InsertPageBeforeRequested -= OnInsertPageBeforeRequested;
					navController.RemovePageRequested -= OnRemovePageRequested;
				}
			}

			base.Dispose(disposing);
		}

		protected override void OnAttachedToWindow()
		{
			base.OnAttachedToWindow();
			PageController.SendAppearing();
		}

		protected override void OnDetachedFromWindow()
		{
			base.OnDetachedFromWindow();
			PageController.SendDisappearing();
		}

		protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
		{
			base.OnElementChanged(e);

			if (e.OldElement != null)
			{
				var oldNavController = (INavigationPageController)e.OldElement;

				oldNavController.PushRequested -= OnPushed;
				oldNavController.PopRequested -= OnPopped;
				oldNavController.PopToRootRequested -= OnPoppedToRoot;
				oldNavController.InsertPageBeforeRequested -= OnInsertPageBeforeRequested;
				oldNavController.RemovePageRequested -= OnRemovePageRequested;

				RemoveAllViews();
			}

			var newNavController = (INavigationPageController)e.NewElement;

			newNavController.PushRequested += OnPushed;
			newNavController.PopRequested += OnPopped;
			newNavController.PopToRootRequested += OnPoppedToRoot;
			newNavController.InsertPageBeforeRequested += OnInsertPageBeforeRequested;
			newNavController.RemovePageRequested += OnRemovePageRequested;

			// If there is already stuff on the stack we need to push it
			foreach(Page page in newNavController.StackCopy.Reverse())
			{
				PushViewAsync(page, false);
			}
		}

		protected override void OnLayout(bool changed, int l, int t, int r, int b)
		{
			base.OnLayout(changed, l, t, r, b);

			for (var i = 0; i < ChildCount; i++)
				GetChildAt(i).Layout(0, 0, r - l, b - t);
		}

		protected virtual Task<bool> OnPopToRootAsync(Page page, bool animated)
		{
			return SwitchContentAsync(page, animated, true);
		}

		protected virtual Task<bool> OnPopViewAsync(Page page, bool animated)
		{
			Page pageToShow = ((INavigationPageController)Element).StackCopy.Skip(1).FirstOrDefault();
			if (pageToShow == null)
				return Task.FromResult(false);

			return SwitchContentAsync(pageToShow, animated, true);
		}

		protected virtual Task<bool> OnPushAsync(Page view, bool animated)
		{
			return SwitchContentAsync(view, animated);
		}

		void InsertPageBefore(Page page, Page before)
		{

			int index = ((IPageController)Element).InternalChildren.IndexOf(before);
			if (index == -1)
				throw new InvalidOperationException("This should never happen, please file a bug");

			Device.StartTimer(TimeSpan.FromMilliseconds(0), () => {
				((Platform)Element.Platform).UpdateNavigationTitleBar();
				return false;
			});
		}

		void OnInsertPageBeforeRequested(object sender, NavigationRequestedEventArgs e)
		{
			InsertPageBefore(e.Page, e.BeforePage);
		}

		void OnPopped(object sender, NavigationRequestedEventArgs e)
		{
			e.Task = PopViewAsync(e.Page, e.Animated);
		}

		void OnPoppedToRoot(object sender, NavigationRequestedEventArgs e)
		{
			e.Task = PopToRootAsync(e.Page, e.Animated);
		}

		void OnPushed(object sender, NavigationRequestedEventArgs e)
		{
			e.Task = PushViewAsync(e.Page, e.Animated);
		}

		void OnRemovePageRequested(object sender, NavigationRequestedEventArgs e)
		{
			RemovePage(e.Page);
		}

		void RemovePage(Page page)
		{
			IVisualElementRenderer rendererToRemove = Platform.GetRenderer(page);
			PageContainer containerToRemove = rendererToRemove == null ? null : (PageContainer)rendererToRemove.ViewGroup.Parent;

			containerToRemove.RemoveFromParent();

			if (rendererToRemove != null)
			{
				rendererToRemove.ViewGroup.RemoveFromParent();
				rendererToRemove.Dispose();
			}

			containerToRemove?.Dispose();

			Device.StartTimer(TimeSpan.FromMilliseconds(0), () =>
			{
				((Platform)Element.Platform).UpdateNavigationTitleBar();
				return false;
			});
		}

		Task<bool> SwitchContentAsync(Page view, bool animated, bool removed = false)
		{
			Context.HideKeyboard(this);

			IVisualElementRenderer rendererToAdd = Platform.GetRenderer(view);
			bool existing = rendererToAdd != null;
			if (!existing)
				Platform.SetRenderer(view, rendererToAdd = Platform.CreateRenderer(view));

			Page pageToRemove = _current;
			IVisualElementRenderer rendererToRemove = pageToRemove == null ? null : Platform.GetRenderer(pageToRemove);
			PageContainer containerToRemove = rendererToRemove == null ? null : (PageContainer)rendererToRemove.ViewGroup.Parent;
			PageContainer containerToAdd = (PageContainer)rendererToAdd.ViewGroup.Parent ?? new PageContainer(Context, rendererToAdd);

			containerToAdd.SetWindowBackground();

			_current = view;

			((Platform)Element.Platform).NavAnimationInProgress = true;

			var tcs = new TaskCompletionSource<bool>();

			if (animated)
			{
				if (s_currentAnimation != null)
					s_currentAnimation.Cancel();

				if (removed)
				{
					// animate out
					if (containerToAdd.Parent != this)
						AddView(containerToAdd, ((IElementController)Element).LogicalChildren.IndexOf(rendererToAdd.Element));
					else
						((IPageController)rendererToAdd.Element).SendAppearing();
					containerToAdd.Visibility = ViewStates.Visible;

					if (containerToRemove != null)
					{
						Action<AndroidAnimation.Animator> done = a =>
						{
							containerToRemove.Visibility = ViewStates.Gone;
							containerToRemove.Alpha = 1;
							containerToRemove.ScaleX = 1;
							containerToRemove.ScaleY = 1;
							RemoveView(containerToRemove);

							tcs.TrySetResult(true);
							((Platform)Element.Platform).NavAnimationInProgress = false;

							VisualElement removedElement = rendererToRemove.Element;
							rendererToRemove.Dispose();
							if (removedElement != null)
								Platform.SetRenderer(removedElement, null);
						};

						// should always happen
						s_currentAnimation = containerToRemove.Animate().Alpha(0).ScaleX(0.8f).ScaleY(0.8f).SetDuration(250).SetListener(new GenericAnimatorListener { OnEnd = a =>
						{
							s_currentAnimation = null;
							done(a);
						},
							OnCancel = done });
					}
				}
				else
				{
					bool containerAlreadyAdded = containerToAdd.Parent == this;
					// animate in
					if (!containerAlreadyAdded)
						AddView(containerToAdd);
					else
						((IPageController)rendererToAdd.Element).SendAppearing();

					if (existing)
						Element.ForceLayout();

					containerToAdd.Alpha = 0;
					containerToAdd.ScaleX = containerToAdd.ScaleY = 0.8f;
					containerToAdd.Visibility = ViewStates.Visible;
					s_currentAnimation = containerToAdd.Animate().Alpha(1).ScaleX(1).ScaleY(1).SetDuration(250).SetListener(new GenericAnimatorListener { OnEnd = a =>
					{
						if (containerToRemove != null && containerToRemove.Handle != IntPtr.Zero)
						{
							containerToRemove.Visibility = ViewStates.Gone;
							((IPageController)pageToRemove)?.SendDisappearing();
						}
						s_currentAnimation = null;
						tcs.TrySetResult(true);
						((Platform)Element.Platform).NavAnimationInProgress = false;
					} });
				}
			}
			else
			{
				// just do it fast
				if (containerToRemove != null)
				{
					if (removed)
						RemoveView(containerToRemove);
					else
						containerToRemove.Visibility = ViewStates.Gone;
				}

				if (containerToAdd.Parent != this)
					AddView(containerToAdd);
				else
					((IPageController)rendererToAdd.Element).SendAppearing();

				if (containerToRemove != null && !removed)
					((IPageController)pageToRemove).SendDisappearing();

				if (existing)
					Element.ForceLayout();

				containerToAdd.Visibility = ViewStates.Visible;
				tcs.SetResult(true);
				((Platform)Element.Platform).NavAnimationInProgress = false;
			}

			return tcs.Task;
		}
	}
}