Skip to content

Commit be74887

Browse files
Refactor
1 parent 2cca62d commit be74887

File tree

1 file changed

+55
-97
lines changed

1 file changed

+55
-97
lines changed

sample/Pages/ShellRenderer.macios.cs

Lines changed: 55 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@ protected override IShellPageRendererTracker CreatePageRendererTracker()
2525
return Tracker = new CustomShellPageRendererTracker(this);
2626
}
2727

28-
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
29-
{
30-
return new CustomShellSectionRenderer(this);
31-
}
28+
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection) => new CustomShellSectionRenderer(this);
3229
}
3330

34-
3531
public class CustomShellSectionRootHeader : ShellSectionRootHeader
3632
{
3733
volatile bool _isRotating;
@@ -58,19 +54,12 @@ public override void ViewWillTransitionToSize(CGSize toSize, IUIViewControllerTr
5854
}
5955
}
6056

61-
public class CustomShellSectionRootRenderer : ShellSectionRootRenderer
57+
public class CustomShellSectionRootRenderer(ShellSection shellSection, IShellContext shellContext, CustomShellSectionRenderer customShellSectionRenderer) : ShellSectionRootRenderer(shellSection, shellContext)
6258
{
63-
readonly CustomShellSectionRenderer _customShellSectionRenderer;
6459

6560
UIEdgeInsets _additionalSafeArea = UIEdgeInsets.Zero;
6661

67-
public CustomShellSectionRootRenderer(ShellSection shellSection, IShellContext shellContext, CustomShellSectionRenderer customShellSectionRenderer) : base(shellSection, shellContext)
68-
{
69-
ShellSection = shellSection;
70-
_customShellSectionRenderer = customShellSectionRenderer;
71-
}
72-
73-
public ShellSection ShellSection { get; }
62+
public ShellSection ShellSection { get; } = shellSection;
7463

7564
public CustomShellSectionRootHeader? CustomShellSectionRootHeader { get; set; }
7665

@@ -91,13 +80,11 @@ public override void ViewDidLayoutSubviews()
9180
public override void ViewDidLoad()
9281
{
9382
base.ViewDidLoad();
94-
_customShellSectionRenderer.SnagTracker();
83+
customShellSectionRenderer.SnagTracker();
9584
}
9685

9786
protected override IShellSectionRootHeader CreateShellSectionRootHeader(IShellContext shellContext)
98-
{
99-
return CustomShellSectionRootHeader = new CustomShellSectionRootHeader(shellContext);
100-
}
87+
=> CustomShellSectionRootHeader = new CustomShellSectionRootHeader(shellContext);
10188

10289
protected override void LayoutRenderers()
10390
{
@@ -107,13 +94,12 @@ protected override void LayoutRenderers()
10794

10895
void UpdateAdditionalSafeAreaInsets()
10996
{
110-
if (OperatingSystem.IsIOSVersionAtLeast(11) && ChildViewControllers is not null)
97+
if (OperatingSystem.IsIOSVersionAtLeast(11))
11198
{
11299
ShellSectionController.GetItems();
113100

114-
for (int i = 0; i < ChildViewControllers.Length; i++)
101+
foreach (var shellContent in ChildViewControllers)
115102
{
116-
var shellContent = ChildViewControllers[i];
117103
UpdateAdditionalSafeAreaInsets(shellContent);
118104
}
119105
}
@@ -124,34 +110,28 @@ void UpdateAdditionalSafeAreaInsets(UIViewController viewController)
124110
if (viewController is not PageViewController)
125111
return;
126112

127-
if (ShellSectionController.GetItems().Count > 1)
128-
{
129-
_additionalSafeArea = new UIEdgeInsets(35, 0, 0, 0);
130-
}
131-
else
132-
{
133-
_additionalSafeArea = UIEdgeInsets.Zero;
134-
}
113+
_additionalSafeArea = ShellSectionController.GetItems().Count > 1
114+
? new UIEdgeInsets(35, 0, 0, 0)
115+
: UIEdgeInsets.Zero;
135116

136-
if (OperatingSystem.IsIOSVersionAtLeast(11) && viewController is not null)
117+
if (OperatingSystem.IsIOSVersionAtLeast(11)
118+
&& !viewController.AdditionalSafeAreaInsets.Equals(_additionalSafeArea))
137119
{
138-
if (!viewController.AdditionalSafeAreaInsets.Equals(_additionalSafeArea))
139-
viewController.AdditionalSafeAreaInsets = _additionalSafeArea;
120+
viewController.AdditionalSafeAreaInsets = _additionalSafeArea;
140121
}
141122
}
142123
}
143124

144125
public class CustomShellSectionRenderer : ShellSectionRenderer
145126
{
146-
readonly UINavigationControllerDelegate _navDelegate;
147127
readonly Dictionary<Element, IShellPageRendererTracker> _trackers = new();
148128

149129
CustomShellSectionRootRenderer? _customShellSectionRootRenderer;
150130

151131
public CustomShellSectionRenderer(IShellContext context) : base(context)
152132
{
153-
_navDelegate = (UINavigationControllerDelegate)Delegate;
154-
Delegate = new NavDelegate(_navDelegate, this);
133+
var navigationDelegate = (UINavigationControllerDelegate)Delegate;
134+
Delegate = new NavigationDelegate(navigationDelegate, this);
155135
Context = context;
156136
UpdateLargeTitle();
157137
}
@@ -176,9 +156,7 @@ protected override void HandleShellPropertyChanged(object sender, PropertyChange
176156
}
177157

178158
protected override IShellSectionRootRenderer CreateShellSectionRootRenderer(ShellSection shellSection, IShellContext shellContext)
179-
{
180-
return _customShellSectionRootRenderer = new CustomShellSectionRootRenderer(shellSection, shellContext, this);
181-
}
159+
=> _customShellSectionRootRenderer = new CustomShellSectionRootRenderer(shellSection, shellContext, this);
182160

183161
protected override void OnNavigationRequested(object sender, NavigationRequestedEventArgs e)
184162
{
@@ -227,44 +205,33 @@ void UpdateLargeTitle()
227205
}, 0);
228206
}
229207

230-
class NavDelegate : UINavigationControllerDelegate
208+
class NavigationDelegate(UINavigationControllerDelegate navigationDelegate, CustomShellSectionRenderer customShellSectionRenderer) : UINavigationControllerDelegate
231209
{
232-
readonly UINavigationControllerDelegate _navDelegate;
233-
readonly CustomShellSectionRenderer _self;
234-
235-
public NavDelegate(UINavigationControllerDelegate navDelegate, CustomShellSectionRenderer customShellSectionRenderer)
236-
{
237-
_navDelegate = navDelegate;
238-
_self = customShellSectionRenderer;
239-
}
240210

241211
// This is currently working around a Mono Interpreter bug
242212
// if you remove this code please verify that hot restart still works
243213
// https://github.com/xamarin/Xamarin.Forms/issues/10519
244214
[Export("navigationController:animationControllerForOperation:fromViewController:toViewController:")]
245215
[Foundation.Preserve(Conditional = true)]
246-
public new static IUIViewControllerAnimatedTransitioning? GetAnimationControllerForOperation(UINavigationController navigationController, UINavigationControllerOperation operation, UIViewController fromViewController, UIViewController toViewController)
247-
{
248-
return null;
249-
}
216+
public static new IUIViewControllerAnimatedTransitioning? GetAnimationControllerForOperation(UINavigationController navigationController, UINavigationControllerOperation operation, UIViewController fromViewController, UIViewController toViewController) => null;
250217

251218
public override void DidShowViewController(UINavigationController navigationController, [Transient] UIViewController viewController, bool animated)
252219
{
253-
_navDelegate.DidShowViewController(navigationController, viewController, animated);
220+
navigationDelegate.DidShowViewController(navigationController, viewController, animated);
254221
}
255222

256223
public override void WillShowViewController(UINavigationController navigationController, [Transient] UIViewController viewController, bool animated)
257224
{
258-
_navDelegate.WillShowViewController(navigationController, viewController, animated);
225+
navigationDelegate.WillShowViewController(navigationController, viewController, animated);
259226

260227
// Because the back button title needs to be set on the previous VC
261228
// We want to set the BackButtonItem as early as possible so there is no flickering
262-
var currentPage = _self.Context?.Shell?.CurrentPage;
263-
var trackers = _self._trackers;
264-
if (currentPage?.Handler is IPlatformViewHandler pvh &&
265-
pvh.ViewController == viewController &&
266-
trackers.TryGetValue(currentPage, out var tracker) &&
267-
tracker is CustomShellPageRendererTracker shellRendererTracker)
229+
var currentPage = customShellSectionRenderer.Context?.Shell?.CurrentPage;
230+
var trackers = customShellSectionRenderer._trackers;
231+
if (currentPage?.Handler is IPlatformViewHandler platformViewHandler
232+
&& platformViewHandler.ViewController?.Equals(viewController) is true
233+
&& trackers.TryGetValue(currentPage, out var tracker)
234+
&& tracker is CustomShellPageRendererTracker shellRendererTracker)
268235
{
269236
shellRendererTracker.UpdateToolbarItemsInternal(false);
270237
}
@@ -344,37 +311,30 @@ protected override void UpdateTitleView()
344311
}
345312
}
346313

347-
bool IsToolbarReady()
348-
{
349-
return ToolbarCurrentPage == Page;
350-
}
314+
bool IsToolbarReady() => ToolbarCurrentPage == Page;
351315

352316
void UpdateBackButtonTitle()
353317
{
354318
var behavior = Shell.GetBackButtonBehavior(Page);
355319
var text = behavior.GetPropertyIfSet<string?>(BackButtonBehavior.TextOverrideProperty, null);
356320

357-
var navController = ViewController?.NavigationController;
358-
359-
if (navController is not null)
321+
if (ViewController?.NavigationController is UINavigationController navigationController)
360322
{
361323
var viewControllers = ViewController?.NavigationController?.ViewControllers ?? throw new InvalidOperationException($"{nameof(ViewController.NavigationController.ViewControllers)} cannot be null");
362324
var count = viewControllers.Length;
363325

364-
if (count > 1 && viewControllers[count - 1] == ViewController)
326+
if (count > 1 && viewControllers[count - 1].Equals(ViewController))
365327
{
366-
var previousNavItem = viewControllers[count - 2].NavigationItem;
367-
if (previousNavItem is not null)
328+
var previousNavigationItem = viewControllers[count - 2].NavigationItem;
329+
330+
if (!string.IsNullOrWhiteSpace(text))
331+
{
332+
var barButtonItem = previousNavigationItem.BackBarButtonItem ??= new UIBarButtonItem();
333+
barButtonItem.Title = text;
334+
}
335+
else if (previousNavigationItem.BackBarButtonItem is not null)
368336
{
369-
if (!string.IsNullOrWhiteSpace(text))
370-
{
371-
var barButtonItem = previousNavItem.BackBarButtonItem ??= new UIBarButtonItem();
372-
barButtonItem.Title = text;
373-
}
374-
else if (previousNavItem.BackBarButtonItem is not null)
375-
{
376-
previousNavItem.BackBarButtonItem = null;
377-
}
337+
previousNavigationItem.BackBarButtonItem = null;
378338
}
379339
}
380340
}
@@ -449,7 +409,6 @@ void UpdateFrame(UIView newSuperView)
449409

450410
public class UIContainerView : UIView
451411
{
452-
readonly View _view;
453412

454413
IPlatformViewHandler? _renderer;
455414
UIView? _platformView;
@@ -458,21 +417,25 @@ public class UIContainerView : UIView
458417

459418
public UIContainerView(View view)
460419
{
461-
_view = view;
420+
View = view;
462421

463422
UpdatePlatformView();
464423
ClipsToBounds = true;
465424
MeasuredHeight = double.NaN;
466425
Margin = new Thickness(0);
467426
}
468427

469-
public virtual Thickness Margin { get; }
470-
471428
internal event EventHandler? HeaderSizeChanged;
472429

473-
internal View View => _view;
430+
public virtual Thickness Margin { get; }
431+
432+
internal View View { get; }
474433

475434
internal bool MatchHeight { get; set; }
435+
436+
internal double? Height { get; set; }
437+
438+
internal double? Width { get; set; }
476439

477440
internal double MeasuredHeight
478441
{
@@ -487,13 +450,9 @@ internal double MeasuredHeight
487450
private set => _measuredHeight = value;
488451
}
489452

490-
internal double? Height { get; set; }
491-
492-
internal double? Width { get; set; }
493-
494453
public override CGSize SizeThatFits(CGSize size)
495454
{
496-
var measuredSize = (_view as IView).Measure(size.Width, size.Height);
455+
var measuredSize = ((IView)View).Measure(size.Width, size.Height);
497456

498457
if (Height is not null && MatchHeight)
499458
{
@@ -528,9 +487,9 @@ public override void LayoutSubviews()
528487

529488

530489
if (MatchHeight)
531-
((IView)_view).Measure(width, height);
490+
((IView)View).Measure(width, height);
532491

533-
((IView)_view).Arrange(platformFrame);
492+
((IView)View).Arrange(platformFrame);
534493
}
535494

536495
internal static void Disconnect()
@@ -539,20 +498,17 @@ internal static void Disconnect()
539498

540499
internal void UpdatePlatformView()
541500
{
542-
if (GetMauiContext(_view) is IMauiContext mauiContext)
501+
if (GetMauiContext(View) is IMauiContext mauiContext)
543502
{
544-
_renderer = _view.ToHandler(mauiContext);
503+
_renderer = View.ToHandler(mauiContext);
545504
_platformView = _renderer.ContainerView ?? _renderer.PlatformView;
546505

547506
if (_platformView is not null && _platformView.Superview != this)
548507
AddSubview(_platformView);
549508
}
550509
}
551510

552-
private protected void OnHeaderSizeChanged()
553-
{
554-
HeaderSizeChanged?.Invoke(this, EventArgs.Empty);
555-
}
511+
private protected void OnHeaderSizeChanged() => HeaderSizeChanged?.Invoke(this, EventArgs.Empty);
556512

557513
protected override void Dispose(bool disposing)
558514
{
@@ -605,7 +561,9 @@ static IEnumerable<Element> GetParentsPath(Element? self)
605561
if (current is not null)
606562
{
607563
current = current.RealParent;
608-
yield return current;
564+
565+
if (current is not null)
566+
yield return current;
609567
}
610568
}
611569
}

0 commit comments

Comments
 (0)