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

[iOS] Fix deselect item on CollectionView #26315

Merged
merged 9 commits into from
Dec 6, 2024
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 @@ -53,9 +53,15 @@ internal void ClearSelection()
{
var selectedItemIndexes = CollectionView.GetIndexPathsForSelectedItems();

foreach (var index in selectedItemIndexes)
if(selectedItemIndexes is not null && selectedItemIndexes.Any())
{
CollectionView.DeselectItem(index, true);
CollectionView.PerformBatchUpdates(null, _ =>
PureWeen marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (var index in selectedItemIndexes)
{
CollectionView.DeselectItem(index, true);
}
});
}
}

Expand Down Expand Up @@ -157,8 +163,11 @@ void SynchronizePlatformSelectionWithSelectedItems()
{
var itemAtPath = GetItemAtIndex(path);
if (!selectedItems.Contains(itemAtPath))
{
CollectionView.PerformBatchUpdates(null, _ =>
{
CollectionView.DeselectItem(path, true);
});
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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
// Ensure the selected index is updated after the collection view's items generation is completed
CollectionView.PerformBatchUpdates(null, _ =>
{
CollectionView.SelectItem(index, true, UICollectionViewScrollPosition.None);
Expand All @@ -53,10 +53,13 @@ internal void ClearSelection()
{
var selectedItemIndexes = CollectionView.GetIndexPathsForSelectedItems();

foreach (var index in selectedItemIndexes)
CollectionView.PerformBatchUpdates(null, _ =>
{
CollectionView.DeselectItem(index, true);
}
foreach (var index in selectedItemIndexes)
{
CollectionView.DeselectItem(index, true);
}
});
}

void FormsSelectItem(NSIndexPath indexPath)
Expand Down Expand Up @@ -158,7 +161,10 @@ void SynchronizePlatformSelectionWithSelectedItems()
var itemAtPath = GetItemAtIndex(path);
if (!selectedItems.Contains(itemAtPath))
{
CollectionView.DeselectItem(path, true);
CollectionView.PerformBatchUpdates(null, _ =>
{
CollectionView.DeselectItem(path, true);
});
}
else
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26187.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 26187, "[MAUI] Select items traces are preserved", PlatformAffected.iOS)]
public class Issue26187 : NavigationPage
{
public Issue26187()
{
PushAsync(new CollectionViewSelectedItemNullPage());
}
}

public class CollectionViewSelectedItemNullPage : ContentPage
{
public ObservableCollection<string> Items { get; set; }

public string SelectedItem { get; set; }

public CollectionViewSelectedItemNullPage()
{
Items = new ObservableCollection<string>
{
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5"
};
SelectedItem = Items.LastOrDefault();
var cv = new CollectionView
{
SelectionMode = SelectionMode.Single,
ItemTemplate = new DataTemplate(() =>
{
var label = new Label
{
FontSize = 24,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.Center,
AutomationId ="lblItem"
};
label.SetBinding(Label.TextProperty, ".");

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

};

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)
{
if (e.CurrentSelection.FirstOrDefault() is string issue)
{
await Navigation.PushAsync(new NewPage(issue));
}

// Clear Selection
var cv = sender as CollectionView;
if (cv is not null)
{
cv.SelectedItem = null;
}
}

class NewPage : ContentPage
{
public NewPage(string item)
{
Title = item;
Content = new Button
{
Text = $"Go Back Selected Item null from {item}",
Command = new Command(() => Navigation.PopAsync()),
AutomationId = "btnGoBack"
};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue26187 : _IssuesUITest
{
public override string Issue => "[MAUI] Select items traces are preserved";

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

[Test]
[Category(UITestCategories.CollectionView)]
public void SelectedItemVisualIsCleared()
{
App.WaitForElement("lblItem");
App.Tap("lblItem");
App.WaitForElement("btnGoBack");
App.Tap("btnGoBack");
App.WaitForElement("lblItem");
VerifyScreenshot();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading