This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ShellSectionRenderer.cs
642 lines (514 loc) · 16.4 KB
/
ShellSectionRenderer.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
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
using Foundation;
using ObjCRuntime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using UIKit;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Platform.iOS
{
public class ShellSectionRenderer : UINavigationController, IShellSectionRenderer, IAppearanceObserver, IDisconnectable
{
#region IShellContentRenderer
public bool IsInMoreTab { get; set; }
public ShellSection ShellSection
{
get { return _shellSection; }
set
{
if (_shellSection == value)
return;
_shellSection = value;
LoadPages();
OnShellSectionSet();
_shellSection.PropertyChanged += HandlePropertyChanged;
((IShellSectionController)_shellSection).NavigationRequested += OnNavigationRequested;
}
}
IShellSectionController ShellSectionController => ShellSection;
public UIViewController ViewController => this;
#endregion IShellContentRenderer
#region IAppearanceObserver
void IAppearanceObserver.OnAppearanceChanged(ShellAppearance appearance)
{
if (appearance == null)
_appearanceTracker.ResetAppearance(this);
else
_appearanceTracker.SetAppearance(this, appearance);
}
#endregion IAppearanceObserver
IShellContext _context;
readonly Dictionary<Element, IShellPageRendererTracker> _trackers =
new Dictionary<Element, IShellPageRendererTracker>();
IShellNavBarAppearanceTracker _appearanceTracker;
Dictionary<UIViewController, TaskCompletionSource<bool>> _completionTasks =
new Dictionary<UIViewController, TaskCompletionSource<bool>>();
Page _displayedPage;
bool _disposed;
bool _firstLayoutCompleted;
TaskCompletionSource<bool> _popCompletionTask;
IShellSectionRootRenderer _renderer;
ShellSection _shellSection;
bool _ignorePopCall;
public ShellSectionRenderer(IShellContext context)
{
Delegate = new NavDelegate(this);
_context = context;
_context.Shell.PropertyChanged += HandleShellPropertyChanged;
}
[Export("navigationBar:shouldPopItem:")]
[Internals.Preserve(Conditional = true)]
public bool ShouldPopItem(UINavigationBar navigationBar, UINavigationItem item)
{
// this means the pop is already done, nothing we can do
if (ViewControllers.Length < NavigationBar.Items.Length)
return true;
foreach(var tracker in _trackers)
{
if(tracker.Value.ViewController == TopViewController)
{
var behavior = Shell.GetBackButtonBehavior(tracker.Value.Page);
var command = behavior.GetPropertyIfSet<ICommand>(BackButtonBehavior.CommandProperty, null);
var commandParameter = behavior.GetPropertyIfSet<object>(BackButtonBehavior.CommandParameterProperty, null);
if (command != null)
{
if(command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
return false;
}
break;
}
}
bool allowPop = ShouldPop();
if (allowPop)
{
// Do not remove, wonky behavior on some versions of iOS if you dont dispatch
CoreFoundation.DispatchQueue.MainQueue.DispatchAsync(() =>
{
_popCompletionTask = new TaskCompletionSource<bool>();
SendPoppedOnCompletion(_popCompletionTask.Task);
PopViewController(true);
});
}
else
{
for (int i = 0; i < NavigationBar.Subviews.Length; i++)
{
var child = NavigationBar.Subviews[i];
if (child.Alpha != 1)
UIView.Animate(.2f, () => child.Alpha = 1);
}
}
return false;
}
public override void ViewWillAppear(bool animated)
{
if (_disposed)
return;
UpdateFlowDirection();
base.ViewWillAppear(animated);
}
internal void UpdateFlowDirection()
{
View.UpdateFlowDirection(_context.Shell);
NavigationBar.UpdateFlowDirection(_context.Shell);
}
public override void ViewDidLayoutSubviews()
{
if (_disposed)
return;
base.ViewDidLayoutSubviews();
_appearanceTracker.UpdateLayout(this);
if (!_firstLayoutCompleted)
{
UpdateShadowImages();
_firstLayoutCompleted = true;
}
}
public override void ViewDidLoad()
{
if (_disposed)
return;
base.ViewDidLoad();
InteractivePopGestureRecognizer.Delegate = new GestureDelegate(this, ShouldPop);
UpdateFlowDirection();
}
void IDisconnectable.Disconnect()
{
(_renderer as IDisconnectable)?.Disconnect();
if (_displayedPage != null)
_displayedPage.PropertyChanged -= OnDisplayedPagePropertyChanged;
if (_shellSection != null)
{
_shellSection.PropertyChanged -= HandlePropertyChanged;
((IShellSectionController)ShellSection).NavigationRequested -= OnNavigationRequested;
((IShellSectionController)ShellSection).RemoveDisplayedPageObserver(this);
}
if (_context.Shell != null)
{
_context.Shell.PropertyChanged -= HandleShellPropertyChanged;
((IShellController)_context.Shell).RemoveAppearanceObserver(this);
}
}
protected override void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
this.RemoveFromParentViewController();
_disposed = true;
_renderer.Dispose();
_appearanceTracker.Dispose();
(this as IDisconnectable).Disconnect();
foreach (var tracker in ShellSection.Stack)
{
if (tracker == null)
continue;
DisposePage(tracker, true);
}
}
_disposed = true;
_displayedPage = null;
_shellSection = null;
_appearanceTracker = null;
_renderer = null;
_context = null;
base.Dispose(disposing);
}
protected virtual void HandleShellPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.Is(VisualElement.FlowDirectionProperty))
UpdateFlowDirection();
}
protected virtual void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == BaseShellItem.TitleProperty.PropertyName)
UpdateTabBarItem();
else if (e.PropertyName == BaseShellItem.IconProperty.PropertyName)
UpdateTabBarItem();
}
protected virtual IShellSectionRootRenderer CreateShellSectionRootRenderer(ShellSection shellSection, IShellContext shellContext)
{
return new ShellSectionRootRenderer(shellSection, shellContext);
}
protected virtual void LoadPages()
{
_renderer = CreateShellSectionRootRenderer(ShellSection, _context);
PushViewController(_renderer.ViewController, false);
var stack = ShellSection.Stack;
for (int i = 1; i < stack.Count; i++)
{
PushPage(stack[i], false);
}
}
protected virtual void OnDisplayedPageChanged(Page page)
{
if (_displayedPage == page)
return;
if (_displayedPage != null)
{
_displayedPage.PropertyChanged -= OnDisplayedPagePropertyChanged;
}
_displayedPage = page;
if (_displayedPage != null)
{
_displayedPage.PropertyChanged += OnDisplayedPagePropertyChanged;
UpdateNavigationBarHidden();
UpdateNavigationBarHasShadow();
}
}
protected virtual void OnInsertRequested(NavigationRequestedEventArgs e)
{
var page = e.Page;
var before = e.BeforePage;
var beforeRenderer = Platform.GetRenderer(before);
var renderer = Platform.CreateRenderer(page);
Platform.SetRenderer(page, renderer);
var tracker = _context.CreatePageRendererTracker();
tracker.ViewController = renderer.ViewController;
tracker.Page = page;
_trackers[page] = tracker;
ViewControllers.Insert(ViewControllers.IndexOf(beforeRenderer.ViewController), renderer.ViewController);
}
protected virtual void OnNavigationRequested(object sender, NavigationRequestedEventArgs e)
{
switch (e.RequestType)
{
case NavigationRequestType.Push:
OnPushRequested(e);
break;
case NavigationRequestType.Pop:
OnPopRequested(e);
break;
case NavigationRequestType.PopToRoot:
OnPopToRootRequested(e);
break;
case NavigationRequestType.Insert:
OnInsertRequested(e);
break;
case NavigationRequestType.Remove:
OnRemoveRequested(e);
break;
}
}
protected virtual async void OnPopRequested(NavigationRequestedEventArgs e)
{
var page = e.Page;
var animated = e.Animated;
_popCompletionTask = new TaskCompletionSource<bool>();
e.Task = _popCompletionTask.Task;
PopViewController(animated);
await _popCompletionTask.Task;
DisposePage(page);
}
public override UIViewController[] PopToRootViewController(bool animated)
{
if (!_ignorePopCall && ViewControllers.Length > 1)
{
ProcessPopToRoot();
}
return base.PopToRootViewController(animated);
}
async void ProcessPopToRoot()
{
var task = new TaskCompletionSource<bool>();
var pages = _shellSection.Stack.ToList();
_completionTasks[_renderer.ViewController] = task;
((IShellSectionController)ShellSection).SendPoppingToRoot(task.Task);
await task.Task;
for (int i = pages.Count - 1; i >= 1; i--)
{
var page = pages[i];
DisposePage(page);
}
}
protected virtual async void OnPopToRootRequested(NavigationRequestedEventArgs e)
{
var animated = e.Animated;
var task = new TaskCompletionSource<bool>();
var pages = _shellSection.Stack.ToList();
try
{
_ignorePopCall = true;
_completionTasks[_renderer.ViewController] = task;
e.Task = task.Task;
PopToRootViewController(animated);
}
finally
{
_ignorePopCall = false;
}
await e.Task;
for (int i = pages.Count - 1; i >= 1; i--)
{
var page = pages[i];
DisposePage(page);
}
}
protected virtual void OnPushRequested(NavigationRequestedEventArgs e)
{
var page = e.Page;
var animated = e.Animated;
var taskSource = new TaskCompletionSource<bool>();
PushPage(page, animated, taskSource);
e.Task = taskSource.Task;
}
protected virtual void OnRemoveRequested(NavigationRequestedEventArgs e)
{
var page = e.Page;
var renderer = Platform.GetRenderer(page);
var viewController = renderer?.ViewController;
if (viewController == null && _trackers.ContainsKey(page))
viewController = _trackers[page].ViewController;
if (viewController != null)
{
if (viewController == TopViewController)
{
e.Animated = false;
OnPopRequested(e);
}
if(ViewControllers.Contains(viewController))
ViewControllers = ViewControllers.Remove(viewController);
DisposePage(page);
}
}
protected virtual void OnShellSectionSet()
{
_appearanceTracker = _context.CreateNavBarAppearanceTracker();
UpdateTabBarItem();
((IShellController)_context.Shell).AddAppearanceObserver(this, ShellSection);
((IShellSectionController)ShellSection).AddDisplayedPageObserver(this, OnDisplayedPageChanged);
}
protected virtual void UpdateTabBarItem()
{
Title = ShellSection.Title;
_ = _context.ApplyNativeImageAsync(ShellSection, ShellSection.IconProperty, icon =>
{
TabBarItem = new UITabBarItem(ShellSection.Title, icon, null);
TabBarItem.AccessibilityIdentifier = ShellSection.AutomationId ?? ShellSection.Title;
});
}
void DisposePage(Page page, bool calledFromDispose = false)
{
if (_trackers.TryGetValue(page, out var tracker))
{
if(!calledFromDispose && tracker.ViewController != null && ViewControllers.Contains(tracker.ViewController))
ViewControllers = ViewControllers.Remove(_trackers[page].ViewController);
tracker.Dispose();
_trackers.Remove(page);
}
var renderer = Platform.GetRenderer(page);
if (renderer != null)
{
renderer.Dispose();
page.ClearValue(Platform.RendererProperty);
}
}
Element ElementForViewController(UIViewController viewController)
{
if (_renderer.ViewController == viewController)
return ShellSection;
foreach (var child in ShellSection.Stack)
{
if (child == null)
continue;
var renderer = Platform.GetRenderer(child);
if (viewController == renderer.ViewController)
return child;
}
return null;
}
void OnDisplayedPagePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == Shell.NavBarIsVisibleProperty.PropertyName)
UpdateNavigationBarHidden();
else if (e.PropertyName == Shell.NavBarHasShadowProperty.PropertyName)
UpdateNavigationBarHasShadow();
}
void PushPage(Page page, bool animated, TaskCompletionSource<bool> completionSource = null)
{
var renderer = Platform.CreateRenderer(page);
Platform.SetRenderer(page, renderer);
var tracker = _context.CreatePageRendererTracker();
tracker.ViewController = renderer.ViewController;
tracker.Page = page;
_trackers[page] = tracker;
if (completionSource != null)
_completionTasks[renderer.ViewController] = completionSource;
PushViewController(renderer.ViewController, animated);
}
async void SendPoppedOnCompletion(Task popTask)
{
if (popTask == null)
{
throw new ArgumentNullException(nameof(popTask));
}
var poppedPage = _shellSection.Stack[_shellSection.Stack.Count - 1];
// this is used to setup appearance changes based on the incoming page
((IShellSectionController)_shellSection).SendPopping(popTask);
await popTask;
DisposePage(poppedPage);
}
bool ShouldPop()
{
var shellItem = _context.Shell.CurrentItem;
var shellSection = shellItem?.CurrentItem;
var shellContent = shellSection?.CurrentItem;
var stack = shellSection?.Stack.ToList();
stack?.RemoveAt(stack.Count - 1);
return ((IShellController)_context.Shell).ProposeNavigation(ShellNavigationSource.Pop, shellItem, shellSection, shellContent, stack, true);
}
void UpdateNavigationBarHidden()
{
SetNavigationBarHidden(!Shell.GetNavBarIsVisible(_displayedPage), true);
}
void UpdateNavigationBarHasShadow()
{
_appearanceTracker.SetHasShadow(this, Shell.GetNavBarHasShadow(_displayedPage));
}
void UpdateShadowImages()
{
NavigationBar.SetValueForKey(NSObject.FromObject(true), new NSString("hidesShadow"));
}
class GestureDelegate : UIGestureRecognizerDelegate
{
readonly UINavigationController _parent;
readonly Func<bool> _shouldPop;
public GestureDelegate(UINavigationController parent, Func<bool> shouldPop)
{
_parent = parent;
_shouldPop = shouldPop;
}
public override bool ShouldBegin(UIGestureRecognizer recognizer)
{
if (_parent.ViewControllers.Length == 1)
return false;
return _shouldPop();
}
}
class NavDelegate : UINavigationControllerDelegate
{
readonly ShellSectionRenderer _self;
public NavDelegate(ShellSectionRenderer renderer)
{
_self = renderer;
}
// This is currently working around a Mono Interpreter bug
// if you remove this code please verify that hot restart still works
// https://github.com/xamarin/Xamarin.Forms/issues/10519
[Export("navigationController:animationControllerForOperation:fromViewController:toViewController:")]
[Foundation.Preserve(Conditional = true)]
public new IUIViewControllerAnimatedTransitioning GetAnimationControllerForOperation(UINavigationController navigationController, UINavigationControllerOperation operation, UIViewController fromViewController, UIViewController toViewController)
{
return null;
}
public override void DidShowViewController(UINavigationController navigationController, [Transient] UIViewController viewController, bool animated)
{
var tasks = _self._completionTasks;
var popTask = _self._popCompletionTask;
if (tasks.TryGetValue(viewController, out var source))
{
source.TrySetResult(true);
tasks.Remove(viewController);
}
else if (popTask != null)
{
popTask.TrySetResult(true);
}
}
public override void WillShowViewController(UINavigationController navigationController, [Transient] UIViewController viewController, bool animated)
{
var element = _self.ElementForViewController(viewController);
bool navBarVisible;
if (element is ShellSection)
navBarVisible = _self._renderer.ShowNavBar;
else
navBarVisible = Shell.GetNavBarIsVisible(element);
navigationController.SetNavigationBarHidden(!navBarVisible, true);
var coordinator = viewController.GetTransitionCoordinator();
if (coordinator != null && coordinator.IsInteractive)
{
// handle swipe to dismiss gesture
if (Forms.IsiOS10OrNewer)
coordinator.NotifyWhenInteractionChanges(OnInteractionChanged);
else
coordinator.NotifyWhenInteractionEndsUsingBlock(OnInteractionChanged);
}
}
void OnInteractionChanged(IUIViewControllerTransitionCoordinatorContext context)
{
if (!context.IsCancelled)
{
_self._popCompletionTask = new TaskCompletionSource<bool>();
_self.SendPoppedOnCompletion(_self._popCompletionTask.Task);
}
}
}
}
}