Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit ce190a4

Browse files
committed
ios - Changed WeakEventManager so it is a protected static object on the popup renderer. This will help create a weak reference to the popup delegate for the GC
1 parent b622f52 commit ce190a4

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Popup/iOS/PopupRenderer.ios.cs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ namespace Xamarin.CommunityToolkit.UI.Views
1414
{
1515
public class PopupRenderer : UIViewController, IVisualElementRenderer
1616
{
17-
readonly WeakEventManager eventManager = new WeakEventManager();
1817
bool isDisposed;
1918

19+
protected static WeakEventManager EventManager { get; private set; } = new WeakEventManager();
20+
2021
public IVisualElementRenderer Control { get; private set; }
2122

2223
public BasePopup Element { get; private set; }
@@ -32,15 +33,11 @@ public class PopupRenderer : UIViewController, IVisualElementRenderer
3233
public event EventHandler<PropertyChangedEventArgs> ElementPropertyChanged;
3334

3435
[Preserve(Conditional = true)]
35-
public PopupRenderer()
36-
{
37-
eventManager.AddEventHandler(OnDismiss, nameof(OnDismiss));
38-
}
36+
public PopupRenderer() =>
37+
EventManager.AddEventHandler(OnDismiss, nameof(OnDismiss));
3938

40-
public void SetElementSize(Size size)
41-
{
39+
public void SetElementSize(Size size) =>
4240
Control?.SetElementSize(size);
43-
}
4441

4542
public override void ViewDidLayoutSubviews()
4643
{
@@ -55,10 +52,8 @@ public override void ViewDidAppear(bool animated)
5552
ModalInPopover = !Element.IsLightDismissEnabled;
5653
}
5754

58-
public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
59-
{
60-
return NativeView.GetSizeRequest(widthConstraint, heightConstraint);
61-
}
55+
public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) =>
56+
NativeView.GetSizeRequest(widthConstraint, heightConstraint);
6257

6358
void IVisualElementRenderer.SetElement(VisualElement element)
6459
{
@@ -135,10 +130,8 @@ void SetViewController()
135130
ViewController = currentPageRenderer.ViewController;
136131
}
137132

138-
void SetEvents()
139-
{
133+
void SetEvents() =>
140134
Element.Dismissed += OnDismissed;
141-
}
142135

143136
void SetSize()
144137
{
@@ -187,10 +180,8 @@ void SetLayout()
187180
}
188181
}
189182

190-
void SetBackgroundColor()
191-
{
183+
void SetBackgroundColor() =>
192184
Control.NativeView.BackgroundColor = Element.Color.ToUIColor();
193-
}
194185

195186
void SetView()
196187
{
@@ -205,13 +196,11 @@ void SetPresentationController()
205196

206197
// Setting PermittedArrowDirector to 0 breaks the Popover layout. It would be nice if there is no anchor to remove the arrow.
207198
((UIPopoverPresentationController)PresentationController).PermittedArrowDirections = UIPopoverArrowDirection.Up;
208-
((UIPopoverPresentationController)PresentationController).Delegate = new PopoverDelegate(eventManager);
199+
((UIPopoverPresentationController)PresentationController).Delegate = new PopoverDelegate();
209200
}
210201

211-
void AddToCurrentPageViewController()
212-
{
202+
void AddToCurrentPageViewController() =>
213203
ViewController.PresentViewController(this, true, () => Element.OnOpened());
214-
}
215204

216205
void OnDismissed(object sender, PopupDismissedEventArgs e) =>
217206
ViewController.DismissViewControllerAsync(true);
@@ -224,6 +213,9 @@ protected override void Dispose(bool disposing)
224213
isDisposed = true;
225214
if (disposing)
226215
{
216+
EventManager.RemoveEventHandler(OnDismiss, nameof(OnDismiss));
217+
EventManager = null;
218+
227219
if (Element != null)
228220
{
229221
Element.PropertyChanged -= OnElementPropertyChanged;
@@ -263,16 +255,11 @@ void OnDismiss(object sender, EventArgs e)
263255

264256
class PopoverDelegate : UIPopoverPresentationControllerDelegate
265257
{
266-
readonly WeakEventManager eventManager;
267-
268-
public PopoverDelegate(WeakEventManager eventManager) =>
269-
this.eventManager = eventManager;
270-
271258
public override UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController forPresentationController) =>
272259
UIModalPresentationStyle.None;
273260

274261
public override void DidDismiss(UIPresentationController presentationController) =>
275-
eventManager.RaiseEvent(this, new EventArgs(), nameof(OnDismiss));
262+
EventManager.RaiseEvent(this, new EventArgs(), nameof(OnDismiss));
276263
}
277264
}
278265
}

0 commit comments

Comments
 (0)