Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,49 @@ internal void SelectItem(object selectedItem)
if (index.Section > -1 && index.Item > -1)
{
// Ensure the selected index is updated after the collection view's items generation is completed
CollectionView.PerformBatchUpdates(null, _ =>
if (!CollectionView.IsLoaded())
{
// Ensure ItemsSource hasn't been disposed
if (ItemsSource is EmptySource)
CollectionView.PerformBatchUpdates(null, _ =>
{
return;
}
ValidateAndSelectItem(selectedItem, originalSource);
});
}
else
{
ValidateAndSelectItem(selectedItem, originalSource);
}
}
}

// Exit if the ItemsSource reference no longer matches the one captured at invocation.
if (!ReferenceEquals(ItemsView.ItemsSource, originalSource))
{
return;
}
void ValidateAndSelectItem(object selectedItem, object originalSource)
{
// Ensure ItemsSource hasn't been disposed
if (ItemsSource is EmptySource)
{
return;
}

// Recalculate the index for the selectedItem now that the collection may have changed.(Adding, deleting etc..)
var updatedIndex = GetIndexForItem(selectedItem);
if (updatedIndex.Section < 0 || updatedIndex.Item < 0)
{
return;
}
// Exit if the ItemsSource reference no longer matches the one captured at invocation.
if (!ReferenceEquals(ItemsView.ItemsSource, originalSource))
{
return;
}

// Retrieve the current item at that index and verify it still equals the intended selection.
var liveItem = GetItemAtIndex(updatedIndex);
if (!Equals(liveItem, selectedItem))
{
return;
}
// Recalculate the index for the selectedItem now that the collection may have changed.(Adding, deleting etc..)
var updatedIndex = GetIndexForItem(selectedItem);
if (updatedIndex.Section < 0 || updatedIndex.Item < 0)
{
return;
}

CollectionView.SelectItem(index, true, UICollectionViewScrollPosition.None);
});
// Retrieve the current item at that index and verify it still equals the intended selection.
var liveItem = GetItemAtIndex(updatedIndex);
if (!Equals(liveItem, selectedItem))
{
return;
}

CollectionView.SelectItem(updatedIndex, true, UICollectionViewScrollPosition.None);
}

// Called by Forms to clear the native selection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,49 @@ internal void SelectItem(object selectedItem)
if (index.Section > -1 && index.Item > -1)
{
// Ensure the selected index is updated after the collection view's items generation is completed
CollectionView.PerformBatchUpdates(null, _ =>
if (!CollectionView.IsLoaded())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Won't this prevent item selection, if SelectedItem is pre-defined? please verify

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It will not prevent item pre-selection as long as CollectionView.SelectedItem is not set to null in the CollectionView.SelectionChanged event, as in the case reported by the user.

{
// Ensure ItemsSource hasn't been disposed
if (ItemsSource is Items.EmptySource)
CollectionView.PerformBatchUpdates(null, _ =>
{
return;
}
ValidateAndSelectItem(selectedItem, originalSource);
});
}
else
{
ValidateAndSelectItem(selectedItem, originalSource);
}
}
}

// Exit if the ItemsSource reference no longer matches the one captured at invocation.
if (!ReferenceEquals(ItemsView.ItemsSource, originalSource))
{
return;
}
private void ValidateAndSelectItem(object selectedItem, object originalSource)
{
// Ensure ItemsSource hasn't been disposed
if (ItemsSource is Items.EmptySource)
{
return;
}

// Recalculate the index for the selectedItem now that the collection may have changed.(Adding, deleting etc..)
var updatedIndex = GetIndexForItem(selectedItem);
if (updatedIndex.Section < 0 || updatedIndex.Item < 0)
{
return;
}
// Exit if the ItemsSource reference no longer matches the one captured at invocation.
if (!ReferenceEquals(ItemsView.ItemsSource, originalSource))
{
return;
}

// Retrieve the current item at that index and verify it still equals the intended selection.
var liveItem = GetItemAtIndex(updatedIndex);
if (!Equals(liveItem, selectedItem))
{
return;
}
// Recalculate the index for the selectedItem now that the collection may have changed.(Adding, deleting etc..)
var updatedIndex = GetIndexForItem(selectedItem);
if (updatedIndex.Section < 0 || updatedIndex.Item < 0)
{
return;
}

CollectionView.SelectItem(index, true, UICollectionViewScrollPosition.None);
});
// Retrieve the current item at that index and verify it still equals the intended selection.
var liveItem = GetItemAtIndex(updatedIndex);
if (!Equals(liveItem, selectedItem))
{
return;
}

CollectionView.SelectItem(updatedIndex, true, UICollectionViewScrollPosition.None);
}

// Called by Forms to clear the native selection
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 2 additions & 9 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26187.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public class CollectionViewSelectedItemNullPage : ContentPage
{
public ObservableCollection<string> Items { get; set; }

public string SelectedItem { get; set; }

public CollectionViewSelectedItemNullPage()
{
Items = new ObservableCollection<string>
Expand All @@ -27,7 +25,6 @@ public CollectionViewSelectedItemNullPage()
"Item 4",
"Item 5"
};
SelectedItem = Items.LastOrDefault();
var cv = new CollectionView
{
SelectionMode = SelectionMode.Single,
Expand All @@ -51,20 +48,16 @@ public CollectionViewSelectedItemNullPage()
};

cv.SetBinding(CollectionView.ItemsSourceProperty, new Binding(nameof(Items)));
// cv.SetBinding(CollectionView.SelectedItemProperty, new Binding(nameof(SelectedItem)));
Content = cv;

BindingContext = this;
// cv.SelectedItem = SelectedItem;

cv.SelectionChanged += CollectionView_SelectionChanged;
}

async void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.FirstOrDefault() is string issue)
{
await Navigation.PushAsync(new NewPage(issue));
_ = Navigation.PushAsync(new NewPage(issue));
}

// Clear Selection
Expand Down
60 changes: 60 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue30363.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 30363, "[iOS] CollectionView does not clear selection when SelectedItem is set to null", PlatformAffected.iOS)]
public class Issue30363 : ContentPage
{
public Issue30363()
{
ObservableCollection<string> Items = new ObservableCollection<string>
{
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5"
};


var collectionView = new CollectionView
{
ItemsSource = Items,
SelectionMode = SelectionMode.Single,
ItemTemplate = new DataTemplate(() =>
{
var label = new Label
{
FontSize = 24,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.Center,
AutomationId = "cvItem"
};
label.SetBinding(Label.TextProperty, ".");

return new Grid
{
Children = { label }
};
})
};

collectionView.SelectionChanged += (sender, e) =>
{
if (e.CurrentSelection.Count > 0)
{
var selectedItem = e.CurrentSelection[0] as string;
if (selectedItem != null)
{
// Deselect the item
collectionView.SelectedItem = null;
}
}
};

var grid = new Grid();
grid.Children.Add(collectionView);

Content = grid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue30363 : _IssuesUITest
{
public override string Issue => "[iOS] CollectionView does not clear selection when SelectedItem is set to null";

public Issue30363(TestDevice device)
: base(device)
{ }

[Test]
[Category(UITestCategories.CollectionView)]
public void CollectionViewSelectionShouldClear()
{
App.WaitForElement("cvItem");
App.Tap("cvItem");
VerifyScreenshot(retryTimeout: TimeSpan.FromSeconds(2));
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading