Skip to content

Commit

Permalink
[iOS] Fix deselect item on CollectionView (#26315)
Browse files Browse the repository at this point in the history
* Add sample for issue #26187

* [iOS] Fix deselecting an item

* [testing] Add uitest for issue #26187

* [testing] Add android screenshot

* [testing] Add missing image windows

* [ios] Add foreach inside CollectionView.PerformBatchUpdates

* Update SelectableItemsViewController.cs

* [iOS] Don try to update empty if no items

* Fix sample
  • Loading branch information
rmarinho authored Dec 6, 2024
1 parent 23e1522 commit 66982c3
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 7 deletions.
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, _ =>
{
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.

0 comments on commit 66982c3

Please sign in to comment.