Skip to content

Commit

Permalink
Revert "If PlatformView has been removed use index (#23408)"
Browse files Browse the repository at this point in the history
This reverts commit a4b1044.
  • Loading branch information
PureWeen committed Jul 5, 2024
1 parent 7785fea commit 6b5859d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 54 deletions.
11 changes: 1 addition & 10 deletions src/Core/src/Handlers/Layout/LayoutHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,10 @@ 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.Handler?.PlatformView is not null && child.ToPlatform() is View view)
if (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)
Expand Down
14 changes: 14 additions & 0 deletions src/Core/src/Handlers/Layout/LayoutHandler.Mac.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Maui.Handlers
{
public partial class LayoutHandler : ViewHandler<ILayout, NSView>
{
protected override NSView CreateView()
{
return new NSView();
}
}
}
12 changes: 1 addition & 11 deletions src/Core/src/Handlers/Layout/LayoutHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,10 @@ 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.Handler?.PlatformView is not null && child.ToPlatform() is UIElement view)
if (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()
Expand Down
9 changes: 1 addition & 8 deletions src/Core/src/Handlers/Layout/LayoutHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,10 @@ 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.Handler?.PlatformView is not null && child.ToPlatform() is PlatformView childView)
if (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()
Expand Down
25 changes: 0 additions & 25 deletions src/Core/tests/DeviceTests/Handlers/Layout/LayoutHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
Expand Down Expand Up @@ -76,30 +75,6 @@ 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()
{
Expand Down

0 comments on commit 6b5859d

Please sign in to comment.