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] Fixed the RemainingItemsThresholdReachedCommand not firing issue in CollectionView #26067

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 20 additions & 3 deletions src/Controls/src/Core/Handlers/Items/iOS/ItemsViewDelegator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,30 @@ protected virtual (bool VisibleItems, int First, int Center, int Last) GetVisibl
int firstVisibleItemIndex = -1, centerItemIndex = -1, lastVisibleItemIndex = -1;
if (VisibleItems)
{
firstVisibleItemIndex = (int)First.Item;
centerItemIndex = (int)Center.Item;
lastVisibleItemIndex = (int)Last.Item;
IItemsViewSource source = ViewController.ItemsSource;

firstVisibleItemIndex = GetItemIndex(First, source);
centerItemIndex = GetItemIndex(Center, source);
lastVisibleItemIndex = GetItemIndex(Last, source);
}
return (VisibleItems, firstVisibleItemIndex, centerItemIndex, lastVisibleItemIndex);
}

static int GetItemIndex(NSIndexPath indexPath, IItemsViewSource itemSource)
{
int index = (int)indexPath.Item;

if (indexPath.Section > 0)
{
for (int i = 0; i < indexPath.Section; i++)
{
index += itemSource.ItemCountInGroup(i);
}
}

return index;
}

static NSIndexPath GetCenteredIndexPath(UICollectionView collectionView)
{
NSIndexPath centerItemIndex = null;
Expand Down
38 changes: 38 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25889.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue25889"
xmlns:local="clr-namespace:Maui.Controls.Sample.Issues">

<ContentPage.BindingContext>
<local:_25889MainViewModel/>
</ContentPage.BindingContext>

<Grid x:Name="mainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Label x:Name="label" AutomationId="mainPageLabel" Grid.Row="0"/>

<CollectionView Grid.Row="1" x:DataType="local:_25889MainViewModel" AutomationId="collectionView"
IsGrouped="True"
ItemsSource="{Binding ActivityGroups}"
RemainingItemsThreshold="2"
RemainingItemsThresholdReachedCommand="{Binding GetDataCommand}">

<CollectionView.GroupHeaderTemplate>
<DataTemplate x:DataType="local:_25889GroupedActivity">
<Label Text="{Binding Key}" FontAttributes="Bold" Padding="5" />
</DataTemplate>
</CollectionView.GroupHeaderTemplate>

<CollectionView.ItemTemplate>
<DataTemplate x:DataType="local:_25889ActivityItem">
<Label Text="{Binding Name}" Padding="10" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
</ContentPage>
84 changes: 84 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25889.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows.Input;

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 25889, "CollectionView RemainingItemsThresholdReachedCommand not Firing", PlatformAffected.iOS)]
public partial class Issue25889 : ContentPage
{
public Issue25889()
{
InitializeComponent();
label.SetBinding(Label.TextProperty, "LabelText");
}

}

public class _25889MainViewModel : BindableObject
{
public ObservableCollection<_25889GroupedActivity> ActivityGroups { get; set; }

public ICommand GetDataCommand { get; }

private string _labelText;

public string LabelText
{
get => _labelText;
set
{
if (_labelText != value)
{
_labelText = value;
OnPropertyChanged();
}
}
}

public _25889MainViewModel()
{

var activityGroups = new ObservableCollection<_25889GroupedActivity>();

var group1Items = new List<_25889ActivityItem>();
for (int i = 1; i <= 10; i++)
{
group1Items.Add(new _25889ActivityItem { Name = $"Activity {i}" });
}
activityGroups.Add(new _25889GroupedActivity("Group 1", group1Items));

var group2Items = new List<_25889ActivityItem>();
for (int i = 11; i <= 25; i++)
{
group2Items.Add(new _25889ActivityItem { Name = $"Activity {i}" });
}
activityGroups.Add(new _25889GroupedActivity("Group 2", group2Items));

ActivityGroups = activityGroups;
GetDataCommand = new Command(RemainingItemsThresholdReachedCommandFired);

LabelText = "Not fired";
}

private void RemainingItemsThresholdReachedCommandFired()
{
LabelText = "Command Fired!";
}
}

public class _25889GroupedActivity : List<_25889ActivityItem>
{
public string Key { get; }

public _25889GroupedActivity(string key, List<_25889ActivityItem> items) : base(items)
{
Key = key;
}
}

public class _25889ActivityItem
{
public string Name { get; set; }
}
}
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
{
public class Issue25889 : _IssuesUITest
{
public Issue25889(TestDevice device) : base(device)
{
}

public override string Issue => "CollectionView RemainingItemsThresholdReachedCommand not Firing";

[Test]
[Category(UITestCategories.CollectionView)]
public void RemainingItemsThresholdReachedCommandFired()
{
App.WaitForElement("collectionView");
App.ScrollDown("collectionView");
var label = App.WaitForElement("mainPageLabel");
Assert.That(label.GetText(), Is.EqualTo("Command Fired!"));
}
}
}
Loading