From 6b5859d21bf772250215056f3cff00d08a3b1c70 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 5 Jul 2024 12:03:17 -0500 Subject: [PATCH] Revert "If PlatformView has been removed use index (#23408)" This reverts commit a4b10440cf22bcab0e581f178b116f6b763cdc82. --- .../Handlers/Layout/LayoutHandler.Android.cs | 11 +------- .../src/Handlers/Layout/LayoutHandler.Mac.cs | 14 +++++++++++ .../Handlers/Layout/LayoutHandler.Windows.cs | 12 +-------- .../src/Handlers/Layout/LayoutHandler.iOS.cs | 9 +------ .../Handlers/Layout/LayoutHandlerTests.cs | 25 ------------------- 5 files changed, 17 insertions(+), 54 deletions(-) create mode 100644 src/Core/src/Handlers/Layout/LayoutHandler.Mac.cs diff --git a/src/Core/src/Handlers/Layout/LayoutHandler.Android.cs b/src/Core/src/Handlers/Layout/LayoutHandler.Android.cs index 79367f87526f..eb0df4eca87b 100644 --- a/src/Core/src/Handlers/Layout/LayoutHandler.Android.cs +++ b/src/Core/src/Handlers/Layout/LayoutHandler.Android.cs @@ -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) diff --git a/src/Core/src/Handlers/Layout/LayoutHandler.Mac.cs b/src/Core/src/Handlers/Layout/LayoutHandler.Mac.cs new file mode 100644 index 000000000000..0a122534e192 --- /dev/null +++ b/src/Core/src/Handlers/Layout/LayoutHandler.Mac.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Maui.Handlers +{ + public partial class LayoutHandler : ViewHandler + { + protected override NSView CreateView() + { + return new NSView(); + } + } +} diff --git a/src/Core/src/Handlers/Layout/LayoutHandler.Windows.cs b/src/Core/src/Handlers/Layout/LayoutHandler.Windows.cs index 3a166de61efd..644750214359 100644 --- a/src/Core/src/Handlers/Layout/LayoutHandler.Windows.cs +++ b/src/Core/src/Handlers/Layout/LayoutHandler.Windows.cs @@ -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() diff --git a/src/Core/src/Handlers/Layout/LayoutHandler.iOS.cs b/src/Core/src/Handlers/Layout/LayoutHandler.iOS.cs index 896fd34f57de..a482f55212e9 100644 --- a/src/Core/src/Handlers/Layout/LayoutHandler.iOS.cs +++ b/src/Core/src/Handlers/Layout/LayoutHandler.iOS.cs @@ -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() diff --git a/src/Core/tests/DeviceTests/Handlers/Layout/LayoutHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/Layout/LayoutHandlerTests.cs index b77a1184be93..024d24a28126 100644 --- a/src/Core/tests/DeviceTests/Handlers/Layout/LayoutHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/Layout/LayoutHandlerTests.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Threading.Tasks; using Microsoft.Maui.DeviceTests.Stubs; using Microsoft.Maui.Handlers; @@ -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() {