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 @@ -13,6 +13,8 @@ namespace Microsoft.Maui.Controls.Handlers.Items2
public class SelectableItemsViewController2<TItemsView> : StructuredItemsViewController2<TItemsView>
where TItemsView : SelectableItemsView
{
bool _updateSelectionWhenAttached;

public SelectableItemsViewController2(TItemsView selectableItemsView, UICollectionViewLayout layout)
: base(selectableItemsView, layout)
{
Expand Down Expand Up @@ -58,10 +60,7 @@ internal void SelectItem(object selectedItem)
// Ensure the selected index is updated after the collection view's items generation is completed
if (!CollectionView.IsLoaded())
{
CollectionView.PerformBatchUpdates(null, _ =>
{
ValidateAndSelectItem(selectedItem, originalSource);
});
_updateSelectionWhenAttached = true;
}
else
{
Expand All @@ -70,6 +69,19 @@ internal void SelectItem(object selectedItem)
}
}

private protected override void AttachingToWindow()
{
base.AttachingToWindow();

if (!_updateSelectionWhenAttached)
{
return;
}

_updateSelectionWhenAttached = false;
UpdatePlatformSelection();
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
}

private void ValidateAndSelectItem(object selectedItem, object originalSource)
{
// Ensure ItemsSource hasn't been disposed
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
163 changes: 163 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue38443.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#nullable enable
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls.Shapes;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 38443, "CollectionView SelectedItem set during Shell navigation does not update the visual state", PlatformAffected.iOS | PlatformAffected.macOS)]
public class Issue38443 : TestShell
{
const string DetailRoute = "Issue38443Detail";

protected override void Init()
{
Routing.RegisterRoute(DetailRoute, typeof(Issue38443DetailPage));

var navigateButton = new Button
{
AutomationId = "NavigateButton",
Text = "Navigate to CollectionView"
};
navigateButton.Clicked += async (sender, args) => await GoToAsync(DetailRoute);

Items.Add(new ShellContent
{
Title = "Issue 38443",
Content = new ContentPage
{
Content = new Grid
{
Children = { navigateButton }
}
}
});
}
}

public class Issue38443DetailPage : ContentPage, IQueryAttributable, INotifyPropertyChanged
{
string? _selectedBoxColor;

public Issue38443DetailPage()
{
Title = "Box Colors";
BindingContext = this;

var collectionView = new CollectionView
{
AutomationId = "ColorsCollectionView",
HorizontalOptions = LayoutOptions.Center,
ItemsSource = BoxColors,
SelectionMode = SelectionMode.Single,
VerticalOptions = LayoutOptions.Center,
ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical)
{
ItemSpacing = 5
},
ItemTemplate = new DataTemplate(() =>
{
var boxView = new BoxView
{
HeightRequest = 40,
WidthRequest = 40
};
boxView.SetBinding(BoxView.ColorProperty, ".");

var border = new Border
{
Padding = 4,
Stroke = Colors.Transparent,
StrokeShape = new RoundRectangle { CornerRadius = 4 },
StrokeThickness = 3,
Content = boxView
};

VisualStateManager.SetVisualStateGroups(border,
[
new VisualStateGroup
{
Name = "CommonStates",
States =
{
new VisualState { Name = "Normal" },
new VisualState
{
Name = "Selected",
Setters =
{
new Setter { Property = Border.StrokeProperty, Value = Colors.Black }
}
}
}
}
]);

return border;
})
};
collectionView.SetBinding(SelectableItemsView.SelectedItemProperty, nameof(SelectedBoxColor));

var instructionsLabel = new Label
{
HorizontalTextAlignment = TextAlignment.Center,
AutomationId = "InstructionsLabel",
Text = "Expected: the cyan item is selected when this page opens."
};

var selectedColorLabel = new Label
{
AutomationId = "SelectedColorLabel",
HorizontalTextAlignment = TextAlignment.Center
};
selectedColorLabel.SetBinding(Label.TextProperty, new Binding(nameof(SelectedBoxColor), stringFormat: "SelectedItem: {0}"));

var content = new Grid
{
Padding = 24,
RowSpacing = 16,
RowDefinitions =
{
new RowDefinition(GridLength.Auto),
new RowDefinition(GridLength.Star),
new RowDefinition(GridLength.Auto)
}
};
content.Add(instructionsLabel);
content.Add(collectionView, row: 1);
content.Add(selectedColorLabel, row: 2);

Content = content;
}

public ObservableCollection<string> BoxColors { get; } =
[
"#113FFC", "#00A1CD", "#7c0e64", "#d2a023", "#ca1765", "#14e147"
];

public string? SelectedBoxColor
{
get => _selectedBoxColor;
set
{
if (_selectedBoxColor == value)
return;

_selectedBoxColor = value;
OnPropertyChanged();
}
}

public void ApplyQueryAttributes(IDictionary<string, object> query)
{
SelectedBoxColor = BoxColors[1];
}

public new event PropertyChangedEventHandler? PropertyChanged;

protected new void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

internal class Issue38443 : _IssuesUITest
{
public Issue38443(TestDevice device) : base(device)
{
}

public override string Issue => "CollectionView SelectedItem set during Shell navigation does not update the visual state";

[Test]
[ShardedTestCategory(UITestCategories.CollectionView, shard: 1)]
public void SelectedItemSetDuringShellNavigationUpdatesVisualState()
{
App.WaitForElement("NavigateButton");
App.Tap("NavigateButton");

App.WaitForElement("InstructionsLabel");
VerifyScreenshot();
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
}
}
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading