Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[iOS] Fix CollectionView issue selection multiple items #14621

Merged
merged 3 commits into from
Oct 11, 2021
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
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test 13592" xmlns:local="using:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue13592">
<local:TestContentPage.Resources>
<ResourceDictionary>

<Style TargetType="StackLayout">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter
Property="BackgroundColor"
Value="Yellow" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>

</ResourceDictionary>
</local:TestContentPage.Resources>
<StackLayout>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="If both CollectionViews have items selected, the test has passed."/>
<StackLayout>
<StackLayout Orientation="Horizontal">
<CollectionView
x:Name="stringCollectionView"
ItemsSource="{Binding StringItems}"
SelectedItems="{Binding SelectedStringItems, Mode=TwoWay}"
BackgroundColor="White"
ItemSizingStrategy="MeasureFirstItem"
SelectionMode="Multiple">
<CollectionView.Header>
<Label
Text="String collection:"
BackgroundColor="Gray" />
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<StackLayout>
<Label Text="{Binding .}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<CollectionView
x:Name="intCollectionView"
ItemsSource="{Binding IntItems}"
SelectedItems="{Binding SelectedIntItems, Mode=TwoWay}"
BackgroundColor="White"
ItemSizingStrategy="MeasureFirstItem"
SelectionMode="Multiple">
<CollectionView.Header>
<Label
Text="Int collection:"
BackgroundColor="Gray" />
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:Int32">
<StackLayout>
<Label Text="{Binding .}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
<CollectionView
ItemsSource="{Binding Log}"
VerticalOptions="End"
HeightRequest="200"
BackgroundColor="LightGray"
ItemsUpdatingScrollMode="KeepLastItemInView">
<CollectionView.Header>
<Label Text="Log" />
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<StackLayout>
<Label
Text="{Binding .}"
FontSize="Small" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</StackLayout>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.CollectionView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 13592,
"[Bug] CollectionView initial selection is broken",
PlatformAffected.iOS)]
public partial class Issue13592 : TestContentPage
{
public Issue13592()
{
#if APP
InitializeComponent();
BindingContext = new Issue13592ViewModel();
#endif
}

protected override void Init()
{
}
}

public class Issue13592ViewModel : BindableObject
{
const int NumItems = 20;
const int NumSelected = 3;

ObservableCollection<string> _stringItems;
ObservableCollection<object> _selectedStringItems;
ObservableCollection<int> _intItems;
ObservableCollection<object> _selectedIntItems;
ObservableCollection<string> _log;

public Issue13592ViewModel()
{
Log = new ObservableCollection<string>();

IntItems = new ObservableCollection<int>();

for (var i = 0; i < NumItems; i++)
IntItems.Add(i);

StringItems = new ObservableCollection<string>();

for (var i = 0; i < NumItems; i++)
StringItems.Add(i.ToString());

AddToLog($"Loaded {NumItems} items");

SelectedIntItems = new ObservableCollection<object>();

for (var i = 0; i < NumSelected; i++)
SelectedIntItems.Add(i);

SelectedStringItems = new ObservableCollection<object>();

for (var i = 0; i < NumSelected; i++)
SelectedStringItems.Add(i.ToString());

AddToLog($"Loaded initial selection of: {string.Join(", ", this.SelectedIntItems)}");
}

public ObservableCollection<string> StringItems
{
get { return _stringItems; }
set
{
_stringItems = value;
OnPropertyChanged();
}
}

public ObservableCollection<object> SelectedStringItems
{
get { return _selectedStringItems; }
set
{
_selectedStringItems = value;
OnPropertyChanged();
}
}

public ObservableCollection<int> IntItems
{
get { return _intItems; }
set
{
_intItems = value;
OnPropertyChanged();
}
}

public ObservableCollection<object> SelectedIntItems
{
get { return _selectedIntItems; }
set
{
_selectedIntItems = value;
OnPropertyChanged();
}
}

public ObservableCollection<string> Log
{
get { return _log; }
set
{
_log = value;
OnPropertyChanged();
}
}

public void AddToLog(string line)
{
Log.Add($"{line}.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue11795.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14528.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14286.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13592.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue9079.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13588.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10101.xaml.cs" />
Expand Down Expand Up @@ -2245,6 +2246,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue14286.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13592.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue9079.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
26 changes: 26 additions & 0 deletions Xamarin.Forms.Platform.iOS/CollectionView/ItemComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace Xamarin.Forms.Platform.iOS
{
public static class ItemComparer
{
public static bool AreEquals(object valueA, object valueB)
{
bool result;
IComparable selfValueComparer;

selfValueComparer = valueA as IComparable;

if (valueA == null && valueB != null || valueA != null && valueB == null)
result = false;
else if (selfValueComparer != null && selfValueComparer.CompareTo(valueB) != 0)
result = false;
else if (!object.Equals(valueA, valueB))
result = false;
else
result = true;

return result;
}
}
}
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.iOS/CollectionView/ListSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public NSIndexPath GetIndexForItem(object item)
{
for (int n = 0; n < Count; n++)
{
if (this[n] == item)
if (ItemComparer.AreEquals(this[n], item))
{
return NSIndexPath.Create(0, n);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public NSIndexPath GetIndexForItem(object item)
{
for (int n = 0; n < Count; n++)
{
if (this[n] == item)
if(ItemComparer.AreEquals(this[n], item))
{
return NSIndexPath.Create(_section, n);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
<Compile Include="Renderers\IDisconnectable.cs" />
<Compile Include="Extensions\BrushExtensions.shared.cs" />
<Compile Include="Renderers\ShellFlyoutHeaderContainer.cs" />
<Compile Include="CollectionView\ItemComparer.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\StringResources.ar.resx" />
Expand Down