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

Commit

Permalink
Fixed issue with a mismatch between UICollectionView items count and …
Browse files Browse the repository at this point in the history
…Observable items count
  • Loading branch information
jsuarezruiz committed Dec 3, 2019
1 parent 964373d commit de9d0ae
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions Xamarin.Forms.Platform.iOS/CollectionView/ObservableItemsSource.cs
Expand Up @@ -25,10 +25,12 @@ public ObservableItemsSource(IEnumerable itemSource, UICollectionViewController

_itemsSource = itemSource as IList ?? itemSource as IEnumerable;

Count = ItemsCount();

((INotifyCollectionChanged)itemSource).CollectionChanged += CollectionChanged;
}

public int Count => ItemsCount();
public int Count { get; private set; }

public object this[int index] => ElementAt(index);

Expand All @@ -52,7 +54,7 @@ protected virtual void Dispose(bool disposing)

public int ItemCountInGroup(nint group)
{
return ItemsCount();
return Count;
}

public object Group(NSIndexPath indexPath)
Expand All @@ -62,7 +64,7 @@ public object Group(NSIndexPath indexPath)

public NSIndexPath GetIndexForItem(object item)
{
for (int n = 0; n < ItemsCount(); n++)
for (int n = 0; n < Count; n++)
{
if (this[n] == item)
{
Expand All @@ -75,7 +77,7 @@ public NSIndexPath GetIndexForItem(object item)

public int GroupCount => 1;

public int ItemCount => ItemsCount();
public int ItemCount => Count;

public object this[NSIndexPath indexPath]
{
Expand Down Expand Up @@ -118,6 +120,7 @@ void Reload()
{
_collectionView.ReloadData();
_collectionView.CollectionViewLayout.InvalidateLayout();
Count = ItemsCount();
}

NSIndexPath[] CreateIndexesFrom(int startIndex, int count)
Expand All @@ -141,40 +144,42 @@ bool NotLoadedYet()

void Add(NotifyCollectionChangedEventArgs args)
{
var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : IndexOf(args.NewItems[0]);
var count = args.NewItems.Count;

if (NotLoadedYet())
{
_collectionView.ReloadData();
return;
}

var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : IndexOf(args.NewItems[0]);
var count = args.NewItems.Count;

if (!_grouped && _collectionView.NumberOfItemsInSection(_section) == 0)
{
// Okay, we're going from completely empty to more than 0 items; there's an iOS bug which apparently
// will just crash if we call InsertItems here, so we have to do ReloadData.
_collectionView.ReloadData();
Count += count;
return;
}

_collectionView.PerformBatchUpdates(() =>
{
var indexes = CreateIndexesFrom(startIndex, count);
_collectionView.InsertItems(indexes);
}, null);
{
var indexes = CreateIndexesFrom(startIndex, count);
_collectionView.InsertItems(indexes);
Count += count;
}, null);
}

void Remove(NotifyCollectionChangedEventArgs args)
{
var startIndex = args.OldStartingIndex;

if (NotLoadedYet())
{
_collectionView.ReloadData();
return;
}

var startIndex = args.OldStartingIndex;

if (startIndex < 0)
{
// INCC implementation isn't giving us enough information to know where the removed items were in the
Expand All @@ -189,6 +194,7 @@ void Remove(NotifyCollectionChangedEventArgs args)
_collectionView.PerformBatchUpdates(() =>
{
_collectionView.DeleteItems(CreateIndexesFrom(startIndex, count));
Count -= count;
}, null);
}

Expand Down

0 comments on commit de9d0ae

Please sign in to comment.