summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs
blob: 61d0b322efb0616e9a3e0c29cdc23df2465b0be6 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Android.Animation;
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.Widget;
using Android.Support.V7.Graphics.Drawable;
using Android.Util;
using Android.Views;
using Xamarin.Forms.Internals;
using ActionBarDrawerToggle = Android.Support.V7.App.ActionBarDrawerToggle;
using AView = Android.Views.View;
using AToolbar = Android.Support.V7.Widget.Toolbar;
using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;
using FragmentTransaction = Android.Support.V4.App.FragmentTransaction;
using Object = Java.Lang.Object;

namespace Xamarin.Forms.Platform.Android.AppCompat
{
	public class NavigationPageRenderer : VisualElementRenderer<NavigationPage>, IManageFragments
	{
		readonly List<Fragment> _fragmentStack = new List<Fragment>();

		Drawable _backgroundDrawable;
		Page _current;

		bool _disposed;
		ActionBarDrawerToggle _drawerToggle;
		FragmentManager _fragmentManager;
		int _lastActionBarHeight = -1;
		AToolbar _toolbar;
		ToolbarTracker _toolbarTracker;
		bool _toolbarVisible;

		// The following is based on https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/FragmentManager.java#849
		const int TransitionDuration = 220;

		public NavigationPageRenderer()
		{
			AutoPackage = false;
			Id = FormsAppCompatActivity.GetUniqueId();
			Device.Info.PropertyChanged += DeviceInfoPropertyChanged;
		}

		internal int ContainerPadding { get; set; }

		Page Current
		{
			get { return _current; }
			set
			{
				if (_current == value)
					return;

				if (_current != null)
					_current.PropertyChanged -= CurrentOnPropertyChanged;

				_current = value;

				if (_current != null)
				{
					_current.PropertyChanged += CurrentOnPropertyChanged;
					ToolbarVisible = NavigationPage.GetHasNavigationBar(_current);
				}
			}
		}

		FragmentManager FragmentManager	=> _fragmentManager ?? (_fragmentManager = ((FormsAppCompatActivity)Context).SupportFragmentManager);

		IPageController PageController => Element as IPageController;

		bool ToolbarVisible
		{
			get { return _toolbarVisible; }
			set
			{
				if (_toolbarVisible == value)
					return;
				_toolbarVisible = value;
				RequestLayout();
			}
		}

		void IManageFragments.SetFragmentManager(FragmentManager childFragmentManager)
		{
			if (_fragmentManager == null)
				_fragmentManager = childFragmentManager;
		}

		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);
		}

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

				var activity = (FormsAppCompatActivity)Context;

				// API only exists on newer android YAY
				if ((int)Build.VERSION.SdkInt >= 17)
				{
					if (!activity.IsDestroyed)
					{
						FragmentManager fm = FragmentManager;
						FragmentTransaction trans = fm.BeginTransaction();
						foreach (Fragment fragment in _fragmentStack)
							trans.Remove(fragment);
						trans.CommitAllowingStateLoss();
						fm.ExecutePendingTransactions();
					}
				}

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

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

					var navController = (INavigationPageController)Element;

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

				if (_toolbarTracker != null)
				{
					_toolbarTracker.CollectionChanged -= ToolbarTrackerOnCollectionChanged;
					_toolbarTracker.Target = null;
					_toolbarTracker = null;
				}

				if (_toolbar != null)
				{
					_toolbar.NavigationClick -= BarOnNavigationClick;
					_toolbar.Dispose();
					_toolbar = null;
				}

				if (_drawerLayout != null && _drawerListener != null)
				{
					_drawerLayout.RemoveDrawerListener(_drawerListener);
				}

				_drawerToggle = null;

				Current = null;

				Device.Info.PropertyChanged -= DeviceInfoPropertyChanged;
			}

			base.Dispose(disposing);
		}

		protected override void OnAttachedToWindow()
		{
			base.OnAttachedToWindow();
			PageController.SendAppearing();
			_fragmentStack.Last().UserVisibleHint = true;
			RegisterToolbar();
			UpdateToolbar();
		}

		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();
				if (_toolbar != null)
					AddView(_toolbar);
			}

			if (e.NewElement != null)
			{
				if (_toolbarTracker == null)
				{
					SetupToolbar();
					_toolbarTracker = new ToolbarTracker();
					_toolbarTracker.CollectionChanged += ToolbarTrackerOnCollectionChanged;
				}

				var parents = new List<Page>();
				Page root = Element;
				while (!Application.IsApplicationOrNull(root.RealParent))
				{
					root = (Page)root.RealParent;
					parents.Add(root);
				}

				_toolbarTracker.Target = e.NewElement;
				_toolbarTracker.AdditionalTargets = parents;
				UpdateMenu();

				var navController = (INavigationPageController)e.NewElement;

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

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

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == NavigationPage.BarBackgroundColorProperty.PropertyName)
				UpdateToolbar();
			else if (e.PropertyName == NavigationPage.BarTextColorProperty.PropertyName)
				UpdateToolbar();
		}

		protected override void OnLayout(bool changed, int l, int t, int r, int b)
		{
			AToolbar bar = _toolbar;
			// make sure bar stays on top of everything
			bar.BringToFront();

			base.OnLayout(changed, l, t, r, b);

			int barHeight = ActionBarHeight();

			if (barHeight != _lastActionBarHeight && _lastActionBarHeight > 0)
			{
				ResetToolbar();
				bar = _toolbar;
			}
			_lastActionBarHeight = barHeight;

			bar.Measure(MeasureSpecFactory.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly), MeasureSpecFactory.MakeMeasureSpec(barHeight, MeasureSpecMode.Exactly));

			int internalHeight = b - t - barHeight;
			int containerHeight = ToolbarVisible ? internalHeight : b - t;
			containerHeight -= ContainerPadding;

			PageController.ContainerArea = new Rectangle(0, 0, Context.FromPixels(r - l), Context.FromPixels(containerHeight));
			// Potential for optimization here, the exact conditions by which you don't need to do this are complex
			// and the cost of doing when it's not needed is moderate to low since the layout will short circuit pretty fast
			Element.ForceLayout();

			for (var i = 0; i < ChildCount; i++)
			{
				AView child = GetChildAt(i);
				bool isBar = JNIEnv.IsSameObject(child.Handle, bar.Handle);

				if (ToolbarVisible)
				{
					if (isBar)
						bar.Layout(0, 0, r - l, barHeight);
					else
						child.Layout(0, barHeight + ContainerPadding, r, b);
				}
				else
				{
					if (isBar)
						bar.Layout(0, -1000, r, barHeight - 1000);
					else
						child.Layout(0, ContainerPadding, r, b);
				}
			}
		}

		protected virtual void SetupPageTransition(FragmentTransaction transaction, bool isPush)
		{
			if (isPush)
				transaction.SetTransition((int)FragmentTransit.FragmentOpen);
			else
				transaction.SetTransition((int)FragmentTransit.FragmentClose);
		}
		
		internal int GetNavBarHeight()
		{
			if (!ToolbarVisible)
				return 0;

			return ActionBarHeight();
		}

		int ActionBarHeight()
		{
			int attr = Resource.Attribute.actionBarSize;

			int actionBarHeight;
			using (var tv = new TypedValue())
			{
				actionBarHeight = 0;
				if (Context.Theme.ResolveAttribute(attr, tv, true))
					actionBarHeight = TypedValue.ComplexToDimensionPixelSize(tv.Data, Resources.DisplayMetrics);
			}

			if (actionBarHeight <= 0)
				return Device.Info.CurrentOrientation.IsPortrait() ? (int)Context.ToPixels(56) : (int)Context.ToPixels(48);

			return actionBarHeight;
		}

		void AnimateArrowIn()
		{
			var icon = _toolbar.NavigationIcon as DrawerArrowDrawable;
			if (icon == null)
				return;

			ValueAnimator valueAnim = ValueAnimator.OfFloat(0, 1);
			valueAnim.SetDuration(200);
			valueAnim.Update += (s, a) => icon.Progress = (float)a.Animation.AnimatedValue;
			valueAnim.Start();
		}

		void AnimateArrowOut()
		{
			var icon = _toolbar.NavigationIcon as DrawerArrowDrawable;
			if (icon == null)
				return;

			ValueAnimator valueAnim = ValueAnimator.OfFloat(1, 0);
			valueAnim.SetDuration(200);
			valueAnim.Update += (s, a) => icon.Progress = (float)a.Animation.AnimatedValue;
			valueAnim.Start();
		}

		void BarOnNavigationClick(object sender, AToolbar.NavigationClickEventArgs navigationClickEventArgs)
		{
			Element?.PopAsync();
		}

		void CurrentOnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == NavigationPage.HasNavigationBarProperty.PropertyName)
				ToolbarVisible = NavigationPage.GetHasNavigationBar(Current);
			else if (e.PropertyName == Page.TitleProperty.PropertyName)
				UpdateToolbar();
			else if (e.PropertyName == NavigationPage.HasBackButtonProperty.PropertyName)
				UpdateToolbar();
		}

#pragma warning disable 1998 // considered for removal
		async void DeviceInfoPropertyChanged(object sender, PropertyChangedEventArgs e)
#pragma warning restore 1998
		{
			if (nameof(Device.Info.CurrentOrientation) == e.PropertyName)
				ResetToolbar();
		}

		void FilterPageFragment(Page page)
		{
			_fragmentStack.RemoveAll(f => ((FragmentContainer)f).Page == page);
		}

		void HandleToolbarItemPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == MenuItem.IsEnabledProperty.PropertyName || e.PropertyName == MenuItem.TextProperty.PropertyName || e.PropertyName == MenuItem.IconProperty.PropertyName)
				UpdateMenu();
		}

		void InsertPageBefore(Page page, Page before)
		{
			UpdateToolbar();

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

			Fragment fragment = FragmentContainer.CreateInstance(page);
			_fragmentStack.Insert(index, fragment);
		}

		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);
		}

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

		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);
		}

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

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

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

		private DrawerMultiplexedListener _drawerListener;
		private DrawerLayout _drawerLayout;

		void RegisterToolbar()
		{
			Context context = Context;
			AToolbar bar = _toolbar;
			Element page = Element.RealParent;

			MasterDetailPage masterDetailPage = null;
			while (page != null)
			{
				if (page is MasterDetailPage)
				{
					masterDetailPage = page as MasterDetailPage;
					break;
				}
				page = page.RealParent;
			}

			if (masterDetailPage == null)
			{
				masterDetailPage = PageController.InternalChildren[0] as MasterDetailPage;
				if (masterDetailPage == null)
					return;
			}

			if (((IMasterDetailPageController)masterDetailPage).ShouldShowSplitMode)
				return;

			var renderer = Android.Platform.GetRenderer(masterDetailPage) as MasterDetailPageRenderer;
			if (renderer == null)
				return;

			_drawerLayout = (DrawerLayout)renderer;
			_drawerToggle = new ActionBarDrawerToggle((Activity)context, _drawerLayout, bar, global::Android.Resource.String.Ok, global::Android.Resource.String.Ok)
			{
				ToolbarNavigationClickListener = new ClickListener(Element)
			};

			if (_drawerListener != null)
			{
				_drawerLayout.RemoveDrawerListener(_drawerListener);
			}

			_drawerListener = new DrawerMultiplexedListener { Listeners = { _drawerToggle, renderer } };
			_drawerLayout.AddDrawerListener(_drawerListener);

			_drawerToggle.DrawerIndicatorEnabled = true;
		}

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

			// Also remove this page from the fragmentStack
			FilterPageFragment(page);

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

			Device.StartTimer(TimeSpan.FromMilliseconds(10), () =>
			{
				UpdateToolbar();
				return false;
			});
		}

		void ResetToolbar()
		{
			_toolbar.RemoveFromParent();
			_toolbar.NavigationClick -= BarOnNavigationClick;
			_toolbar = null;

			SetupToolbar();
			RegisterToolbar();
			UpdateToolbar();
			UpdateMenu();
		}

		void SetupToolbar()
		{
			Context context = Context;
			var activity = (FormsAppCompatActivity)context;

			AToolbar bar;
			if (FormsAppCompatActivity.ToolbarResource != 0)
				bar = activity.LayoutInflater.Inflate(FormsAppCompatActivity.ToolbarResource, null).JavaCast<AToolbar>();
			else
				bar = new AToolbar(context);

			bar.NavigationClick += BarOnNavigationClick;

			AddView(bar);
			_toolbar = bar;
		}

		Task<bool> SwitchContentAsync(Page view, bool animated, bool removed = false, bool popToRoot = false)
		{
			var tcs = new TaskCompletionSource<bool>();
			Fragment fragment = FragmentContainer.CreateInstance(view);
			FragmentManager fm = FragmentManager;

#if DEBUG
			// Enables logging of moveToState operations to logcat
			FragmentManager.EnableDebugLogging(true);
#endif

			List<Fragment> fragments = _fragmentStack;

			Current = view;

			((Platform)Element.Platform).NavAnimationInProgress = true;
			FragmentTransaction transaction = fm.BeginTransaction();

			if (animated)
				SetupPageTransition(transaction, !removed);

			transaction.DisallowAddToBackStack();

			if (fragments.Count == 0)
			{
				transaction.Add(Id, fragment);
				fragments.Add(fragment);
			}
			else
			{
				if (removed)
				{
					// pop only one page, or pop everything to the root
					var popPage = true;
					while (fragments.Count > 1 && popPage)
					{
						Fragment currentToRemove = fragments.Last();
						fragments.RemoveAt(fragments.Count - 1);
						transaction.Remove(currentToRemove);
						popPage = popToRoot;
					}
					
					Fragment toShow = fragments.Last();
					// Execute pending transactions so that we can be sure the fragment list is accurate.
					fm.ExecutePendingTransactions();
					if (fm.Fragments.Contains(toShow))
						transaction.Show(toShow);
					else
						transaction.Add(Id, toShow);
				}
				else
				{
					// push
					Fragment currentToHide = fragments.Last();
					transaction.Hide(currentToHide);
					transaction.Add(Id, fragment);
					fragments.Add(fragment);
				}
			}
			transaction.Commit();

			// The fragment transitions don't really SUPPORT telling you when they end
			// There are some hacks you can do, but they actually are worse than just doing this:

			if (animated)
			{
				if (!removed)
				{
					UpdateToolbar();
					if (_drawerToggle != null && ((INavigationPageController)Element).StackDepth == 2)
						AnimateArrowIn();
				}
				else if (_drawerToggle != null && ((INavigationPageController)Element).StackDepth == 2)
					AnimateArrowOut();

				Device.StartTimer(TimeSpan.FromMilliseconds(TransitionDuration), () =>
				{
					tcs.TrySetResult(true);
					fragment.UserVisibleHint = !removed;
					if (removed)
					{
						UpdateToolbar();
					}

					return false;
				});
			}
			else
			{
				Device.StartTimer(TimeSpan.FromMilliseconds(1), () =>
				{
					tcs.TrySetResult(true);
					fragment.UserVisibleHint = !removed;
					UpdateToolbar();

					return false;
				});
			}

			Context.HideKeyboard(this);
			((Platform)Element.Platform).NavAnimationInProgress = false;

			// TransitionDuration is how long the built-in animations are, and they are "reversible" in the sense that starting another one slightly before it's done is fine

			return tcs.Task;
		}

		void ToolbarTrackerOnCollectionChanged(object sender, EventArgs eventArgs)
		{
			UpdateMenu();
		}

		void UpdateMenu()
		{
			if (_disposed)
				return;

			AToolbar bar = _toolbar;
			Context context = Context;
			IMenu menu = bar.Menu;

			foreach (ToolbarItem item in _toolbarTracker.ToolbarItems)
				item.PropertyChanged -= HandleToolbarItemPropertyChanged;
			menu.Clear();

			foreach (ToolbarItem item in _toolbarTracker.ToolbarItems)
			{
				IMenuItemController controller = item;
				item.PropertyChanged += HandleToolbarItemPropertyChanged;
				if (item.Order == ToolbarItemOrder.Secondary)
				{
					IMenuItem menuItem = menu.Add(item.Text);
					menuItem.SetEnabled(controller.IsEnabled);
					menuItem.SetOnMenuItemClickListener(new GenericMenuClickListener(controller.Activate));
				}
				else
				{
					IMenuItem menuItem = menu.Add(item.Text);
					FileImageSource icon = item.Icon;
					if (!string.IsNullOrEmpty(icon))
					{
						Drawable iconBitmap = context.Resources.GetDrawable(icon);
						if (iconBitmap != null)
							menuItem.SetIcon(iconBitmap);
					}
					menuItem.SetEnabled(controller.IsEnabled);
					menuItem.SetShowAsAction(ShowAsAction.Always);
					menuItem.SetOnMenuItemClickListener(new GenericMenuClickListener(controller.Activate));
				}
			}
		}

		void UpdateToolbar()
		{
			if (_disposed)
				return;

			Context context = Context;
			var activity = (FormsAppCompatActivity)context;
			AToolbar bar = _toolbar;
			ActionBarDrawerToggle toggle = _drawerToggle;

			if (bar == null)
				return;

			bool isNavigated = ((INavigationPageController)Element).StackDepth > 1;
			bar.NavigationIcon = null;

			if (isNavigated)
			{
				if (toggle != null)
				{
					toggle.DrawerIndicatorEnabled = false;
					toggle.SyncState();
				}

				if (NavigationPage.GetHasBackButton(Element.CurrentPage))
				{
					var icon = new DrawerArrowDrawable(activity.SupportActionBar.ThemedContext);
					icon.Progress = 1;
					bar.NavigationIcon = icon;
				}
			}
			else
			{
				if (toggle != null)
				{
					toggle.DrawerIndicatorEnabled = true;
					toggle.SyncState();
				}
			}

			Color tintColor = Element.BarBackgroundColor;

			if (Forms.IsLollipopOrNewer)
			{
				if (tintColor.IsDefault)
					bar.BackgroundTintMode = null;
				else
				{
					bar.BackgroundTintMode = PorterDuff.Mode.Src;
					bar.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid());
				}
			}
			else
			{
				if (tintColor.IsDefault && _backgroundDrawable != null)
					bar.SetBackground(_backgroundDrawable);
				else if (!tintColor.IsDefault)
				{
					if (_backgroundDrawable == null)
						_backgroundDrawable = bar.Background;
					bar.SetBackgroundColor(tintColor.ToAndroid());
				}
			}

			Color textColor = Element.BarTextColor;
			if (!textColor.IsDefault)
				bar.SetTitleTextColor(textColor.ToAndroid().ToArgb());

			bar.Title = Element.CurrentPage.Title ?? "";
		}

		class ClickListener : Object, IOnClickListener
		{
			readonly NavigationPage _element;

			public ClickListener(NavigationPage element)
			{
				_element = element;
			}

			public void OnClick(AView v)
			{
				_element?.PopAsync();
			}
		}

		class DrawerMultiplexedListener : Object, DrawerLayout.IDrawerListener
		{
			public List<DrawerLayout.IDrawerListener> Listeners { get; } = new List<DrawerLayout.IDrawerListener>(2);

			public void OnDrawerClosed(AView drawerView)
			{
				foreach (DrawerLayout.IDrawerListener listener in Listeners)
					listener.OnDrawerClosed(drawerView);
			}

			public void OnDrawerOpened(AView drawerView)
			{
				foreach (DrawerLayout.IDrawerListener listener in Listeners)
					listener.OnDrawerOpened(drawerView);
			}

			public void OnDrawerSlide(AView drawerView, float slideOffset)
			{
				foreach (DrawerLayout.IDrawerListener listener in Listeners)
					listener.OnDrawerSlide(drawerView, slideOffset);
			}

			public void OnDrawerStateChanged(int newState)
			{
				foreach (DrawerLayout.IDrawerListener listener in Listeners)
					listener.OnDrawerStateChanged(newState);
			}
		}
	}
}