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

If PlatformView has been removed use index #23408

Merged
merged 4 commits into from
Jul 4, 2024
Merged
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
11 changes: 10 additions & 1 deletion src/Core/src/Handlers/Layout/LayoutHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -57,10 +57,19 @@ public void Remove(IView child)
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");

if (child?.ToPlatform() is View view)
if (child.Handler?.PlatformView is not null && child.ToPlatform() is View view)
{
PlatformView.RemoveView(view);
}
else
{
var targetIndex = VirtualView.GetLayoutHandlerIndex(child);

if(targetIndex < PlatformView.ChildCount)
{
PlatformView.RemoveViewAt(targetIndex);
}
}
}

static void Clear(LayoutViewGroup platformView)
14 changes: 0 additions & 14 deletions src/Core/src/Handlers/Layout/LayoutHandler.Mac.cs

This file was deleted.

12 changes: 11 additions & 1 deletion src/Core/src/Handlers/Layout/LayoutHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -40,10 +40,20 @@ public void Remove(IView child)
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");

if (child?.ToPlatform() is UIElement view)
if (child.Handler?.PlatformView is not null && child.ToPlatform() is UIElement view)
{
PlatformView.Children.Remove(view);
}
else
{
var targetIndex = VirtualView.GetLayoutHandlerIndex(child);

if(targetIndex < PlatformView.Children.Count)
{
var childToRemove = PlatformView.Children[targetIndex];
PlatformView.Children.Remove(childToRemove);
}
}
}

public void Clear()
9 changes: 8 additions & 1 deletion src/Core/src/Handlers/Layout/LayoutHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -61,10 +61,17 @@ public void Remove(IView child)
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");

if (child?.ToPlatform() is PlatformView childView)
if (child.Handler?.PlatformView is not null && child.ToPlatform() is PlatformView childView)
{
childView.RemoveFromSuperview();
}
else
{
var targetIndex = VirtualView.GetLayoutHandlerIndex(child);

if(targetIndex < PlatformView.Subviews.Length)
PlatformView.Subviews[targetIndex].RemoveFromSuperview();
}
}

public void Clear()
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
@@ -75,6 +76,30 @@ public async Task HandlerRemovesChildFromNativeLayout()
Assert.Equal(0, count);
}

[Fact(DisplayName = "Removing Child with DisconnectedHandler doesn't crash")]
public async Task RemovingChildWithDisconnectedHandlerDoesntCrash()
{
var layout = new LayoutStub();
var slider = new SliderStub();
var slider2 = new SliderStub();
layout.Add(slider);
layout.Add(slider2);

slider.ZIndex = 1;
slider2.ZIndex = 0;

var handler = await CreateHandlerAsync(layout);

var count = await InvokeOnMainThreadAsync(() =>
{
slider.Handler.DisconnectHandler();
handler.Invoke(nameof(ILayoutHandler.Remove), new LayoutHandlerUpdate(0, slider));
return GetNativeChildren(handler).First() == ((IPlatformViewHandler)slider2.Handler).PlatformView;
});

Assert.True(count);
}

[Fact(DisplayName = "DisconnectHandler removes child from native layout")]
public async Task DisconnectHandlerRemovesChildFromNativeLayout()
{