Skip to content
39 changes: 8 additions & 31 deletions src/Controls/src/Core/SwipeView/SwipeItemView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
using System;
using System.ComponentModel;
using System.Windows.Input;
using Microsoft.Maui.Controls.Internals;

namespace Microsoft.Maui.Controls
{
/// <summary>
/// Represents a swipe item that displays custom content in a <see cref="SwipeView"/>.
/// </summary>
[ContentProperty(nameof(Content))]
public partial class SwipeItemView : ContentView, Controls.ISwipeItem, Maui.ISwipeItemView
public partial class SwipeItemView : ContentView, Controls.ISwipeItem, Maui.ISwipeItemView, ICommandElement
{
/// <summary>Bindable property for <see cref="Command"/>.</summary>
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(SwipeItemView), null,
propertyChanging: (bo, o, n) => ((SwipeItemView)bo).OnCommandChanging(),
propertyChanged: (bo, o, n) => ((SwipeItemView)bo).OnCommandChanged());
propertyChanging: CommandElement.OnCommandChanging,
propertyChanged: CommandElement.OnCommandChanged);

/// <summary>Bindable property for <see cref="CommandParameter"/>.</summary>
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(SwipeItemView), null,
propertyChanged: (bo, o, n) => ((SwipeItemView)bo).OnCommandParameterChanged());
propertyChanged: CommandElement.OnCommandParameterChanged);

/// <summary>
/// Gets or sets the command invoked when this swipe item is activated. This is a bindable property.
Expand Down Expand Up @@ -52,35 +53,11 @@ public void OnInvoked()
Invoked?.Invoke(this, EventArgs.Empty);
}

WeakCommandSubscription _commandSubscription;

void OnCommandChanged()
{
IsEnabled = Command?.CanExecute(CommandParameter) ?? true;

if (Command == null)
return;

_commandSubscription = new WeakCommandSubscription(this, Command, OnCommandCanExecuteChanged);
}

void OnCommandChanging()
void ICommandElement.CanExecuteChanged(object sender, EventArgs eventArgs)
{
_commandSubscription?.Dispose();
_commandSubscription = null;
IsEnabled = CommandElement.GetCanExecute(this);
}

void OnCommandParameterChanged()
{
if (Command == null)
return;

IsEnabled = Command.CanExecute(CommandParameter);
}

void OnCommandCanExecuteChanged(object sender, EventArgs eventArgs)
{
IsEnabled = Command.CanExecute(CommandParameter);
}
WeakCommandSubscription ICommandElement.CleanupTracker { get; set; }
}
}
49 changes: 49 additions & 0 deletions src/Controls/tests/Core.UnitTests/SwipeViewTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Platform;
using Xunit;
Expand Down Expand Up @@ -416,6 +418,53 @@ public void TestSwipeItemView()
Assert.NotEmpty(swipeView.LeftItems);
}

[Fact]
public void SwipeItemViewIsNotRetainedByCommand()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[major] Regression Prevention and Test Coverage — SwipeItemViewIsNotRetainedByCommand does not demonstrate the claimed behavior change, because the same retention scenario was already exercised by CommandsSubscribedToCanExecuteCollect for SwipeItemView and ICommandElement cleanup tracking in CommandTests. Concrete failure mode: the new test can pass on the PR base as well, so it cannot fail when the fix is absent and does not prove this PR’s SwipeItemView code change.

{
var command = new TestCommand();
var swipeItemView = CreateSwipeItemViewWeakReference(command);

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.False(swipeItemView.IsAlive);
GC.KeepAlive(command);
}

[Fact]
public void ClearingCommandReEnablesSwipeItemView()
{
var swipeItemView = new SwipeItemView
{
Command = new Command(() => { }, () => false)
};

Assert.False(swipeItemView.IsEnabled);

swipeItemView.Command = null;

Assert.True(swipeItemView.IsEnabled);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static WeakReference CreateSwipeItemViewWeakReference(ICommand command)
{
var swipeItemView = new SwipeItemView { Command = command };
return new WeakReference(swipeItemView);
}

sealed class TestCommand : ICommand
{
public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter) => true;

public void Execute(object parameter)
{
}
}

[Fact]
public void SwipeItemsRemainInLogicalTreeWhenContentIsSet()
{
Expand Down
Loading