Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ios] fix memory leak in Switch #18682

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Controls/tests/DeviceTests/Memory/MemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void SetupBuilder()
handlers.AddHandler<IScrollView, ScrollViewHandler>();
handlers.AddHandler<Stepper, StepperHandler>();
handlers.AddHandler<SwipeView, SwipeViewHandler>();
handlers.AddHandler<Switch, SwitchHandler>();
handlers.AddHandler<TimePicker, TimePickerHandler>();
handlers.AddHandler<WebView, WebViewHandler>();
});
Expand Down Expand Up @@ -71,6 +72,7 @@ void SetupBuilder()
[InlineData(typeof(ScrollView))]
[InlineData(typeof(Stepper))]
[InlineData(typeof(SwipeView))]
[InlineData(typeof(Switch))]
[InlineData(typeof(TimePicker))]
[InlineData(typeof(WebView))]
public async Task HandlerDoesNotLeak(Type type)
Expand Down
38 changes: 27 additions & 11 deletions src/Core/src/Handlers/Switch/SwitchHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.Maui.Handlers
{
public partial class SwitchHandler : ViewHandler<ISwitch, UISwitch>
{
readonly SwitchProxy _proxy = new();

// the UISwitch control becomes inaccessible if it grows to a width > 101
// An issue has been logged with Apple
// This ensures that the UISwitch remains the natural size that iOS expects
Expand All @@ -22,15 +24,13 @@ protected override UISwitch CreatePlatformView()
protected override void ConnectHandler(UISwitch platformView)
{
base.ConnectHandler(platformView);

platformView.ValueChanged += OnControlValueChanged;
_proxy.Connect(VirtualView, platformView);
}

protected override void DisconnectHandler(UISwitch platformView)
{
base.DisconnectHandler(platformView);

platformView.ValueChanged -= OnControlValueChanged;
_proxy.Disconnect(platformView);
}

public static void MapIsOn(ISwitchHandler handler, ISwitch view)
Expand All @@ -49,17 +49,33 @@ public static void MapThumbColor(ISwitchHandler handler, ISwitch view)
handler.PlatformView?.UpdateThumbColor(view);
}

void OnControlValueChanged(object? sender, EventArgs e)
static void UpdateIsOn(ISwitchHandler handler)
{
if (VirtualView is null || PlatformView is null || VirtualView.IsOn == PlatformView.On)
return;

VirtualView.IsOn = PlatformView.On;
handler.UpdateValue(nameof(ISwitch.TrackColor));
}

static void UpdateIsOn(ISwitchHandler handler)
class SwitchProxy
{
handler.UpdateValue(nameof(ISwitch.TrackColor));
WeakReference<ISwitch>? _virtualView;

ISwitch? VirtualView => _virtualView is not null && _virtualView.TryGetTarget(out var v) ? v : null;

public void Connect(ISwitch virtualView, UISwitch platformView)
{
_virtualView = new(virtualView);
platformView.ValueChanged += OnControlValueChanged;
}

public void Disconnect(UISwitch platformView)
{
platformView.ValueChanged -= OnControlValueChanged;
}

void OnControlValueChanged(object? sender, EventArgs e)
{
if (VirtualView is ISwitch virtualView && sender is UISwitch platformView && virtualView.IsOn != platformView.On)
virtualView.IsOn = platformView.On;
}
}
}
}
Loading