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
31 changes: 23 additions & 8 deletions src/Controls/src/Core/Handlers/Items/iOS/ItemsViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void CheckForEmptySource()

if (_isEmpty)
{
_measurementCells?.Clear();
ClearMeasurementCells();
ItemsViewLayout?.ClearCellSizeCache();
}

Expand Down Expand Up @@ -257,19 +257,23 @@ private protected virtual void LayoutSupplementaryViews()
void InvalidateLayoutIfItemsMeasureChanged()
{
var visibleCells = CollectionView.VisibleCells;
List<TemplatedCell> invalidatedCells = null;
List<NSIndexPath> invalidatedIndexPaths = null;
Comment thread
filipnavara marked this conversation as resolved.

var visibleCellsLength = visibleCells.Length;
for (int n = 0; n < visibleCellsLength; n++)
{
if (visibleCells[n] is TemplatedCell { MeasureInvalidated: true } cell)
{
invalidatedCells ??= [];
invalidatedCells.Add(cell);
var indexPath = CollectionView.IndexPathForCell(cell);
if (indexPath is not null && ItemsSource.IsIndexPathValid(indexPath))
{
invalidatedIndexPaths ??= [];
invalidatedIndexPaths.Add(indexPath);
}
}
}

if (invalidatedCells is not null)
if (invalidatedIndexPaths is not null)
{
// GridLayout has a special positioning override when there's only one item
// so we have to invalidate the layout entirely to trigger that special case.
Expand All @@ -280,7 +284,7 @@ void InvalidateLayoutIfItemsMeasureChanged()
else
{
var layoutInvalidationContext = new UICollectionViewFlowLayoutInvalidationContext();
layoutInvalidationContext.InvalidateItems(invalidatedCells.Select(CollectionView.IndexPathForCell).ToArray());
layoutInvalidationContext.InvalidateItems(invalidatedIndexPaths.ToArray());
CollectionView.CollectionViewLayout.InvalidateLayout(layoutInvalidationContext);
}
}
Expand Down Expand Up @@ -419,7 +423,7 @@ protected virtual IItemsViewSource CreateItemsViewSource()

public virtual void UpdateItemsSource()
{
_measurementCells?.Clear();
ClearMeasurementCells();
ItemsViewLayout?.ClearCellSizeCache();
ItemsSource?.Dispose();
ItemsSource = CreateItemsViewSource();
Expand All @@ -432,7 +436,7 @@ public virtual void UpdateItemsSource()

internal void DisposeItemsSource()
{
_measurementCells?.Clear();
ClearMeasurementCells();
ItemsViewLayout?.ClearCellSizeCache();
ItemsSource?.Dispose();
ItemsSource = new EmptySource();
Expand Down Expand Up @@ -514,6 +518,17 @@ protected virtual void UpdateTemplatedCell(TemplatedCell cell, NSIndexPath index
ItemsViewLayout.PrepareCellForLayout(cell);
}

void ClearMeasurementCells()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is generally true also for non-measurement cells and possible wider scope bug that should be fixed separately. (In our app we diligently reset ItemSource to null to solve this.)

{
foreach (var measurementCell in _measurementCells.Values)
{
measurementCell.LayoutAttributesChanged -= CellLayoutAttributesChanged;
measurementCell.Unbind();
}

_measurementCells.Clear();
}

public virtual NSIndexPath GetIndexForItem(object item)
{
return ItemsSource.GetIndexForItem(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,26 @@ void InvalidateLayoutIfItemsMeasureChanged()
{
var collectionView = CollectionView;
var visibleCells = collectionView.VisibleCells;
List<TemplatedCell2> invalidatedCells = null;
List<NSIndexPath> invalidatedIndexPaths = null;

var visibleCellsLength = visibleCells.Length;
for (int n = 0; n < visibleCellsLength; n++)
{
if (visibleCells[n] is TemplatedCell2 { MeasureInvalidated: true } cell)
{
invalidatedCells ??= [];
invalidatedCells.Add(cell);
var indexPath = collectionView.IndexPathForCell(cell);
if (indexPath is not null && Items.IndexPathHelpers.IsIndexPathValid(ItemsSource, indexPath))
{
invalidatedIndexPaths ??= [];
invalidatedIndexPaths.Add(indexPath);
}
}
}

if (invalidatedCells is not null)
if (invalidatedIndexPaths is not null)
{
var layoutInvalidationContext = new UICollectionViewLayoutInvalidationContext();
layoutInvalidationContext.InvalidateItems(invalidatedCells.Select(CollectionView.IndexPathForCell).ToArray());
layoutInvalidationContext.InvalidateItems(invalidatedIndexPaths.ToArray());
collectionView.CollectionViewLayout.InvalidateLayout(layoutInvalidationContext);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,86 @@ public void IndexPathValidTest()
Assert.False(source.IsIndexPathValid(invalidSection));
}

private async Task ClearingItemsSourceAfterCellMeasureInvalidationDoesNotCrashHelper<THandler>()
where THandler : class, IElementHandler
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<CollectionView, THandler>();
handlers.AddHandler<Label, LabelHandler>();
});
});

var labels = new List<Label>();
var items = new ObservableCollection<string>
{
"one",
"two",
"three",
"four"
};

var collectionView = new CollectionView
{
HeightRequest = 200,
WidthRequest = 300,
ItemsSource = items,
ItemTemplate = new DataTemplate(() =>
{
var label = new Label
{
LineBreakMode = LineBreakMode.WordWrap
};

label.SetBinding(Label.TextProperty, ".");
labels.Add(label);

return label;
})
};

var frame = collectionView.Frame;

await CreateHandlerAndAddToWindow<THandler>(collectionView, async handler =>
{
await WaitForUIUpdate(frame, collectionView);

Assert.NotEmpty(labels);

// Change text of all the labels to force a relayout, including those that were
// only used for measurement cell. Now we should be sure that all visible cells
// have their MeasureInvalidated == true.
foreach (var label in labels)
label.Text = label.Text + " with enough extra text to invalidate the measured cell size";
// Add another item to force an animation
items.Add("five");
// Reset the data source to force another animation and a layout pass
collectionView.ItemsSource = null;

var platformView = (UIView)handler.PlatformView;
var uiCollectionView = platformView as UICollectionView ?? platformView.Subviews.OfType<UICollectionView>().FirstOrDefault();
Assert.NotNull(uiCollectionView);
// Force synchronous flush of the ItemsSource reloading
await uiCollectionView.PerformBatchUpdatesAsync(() => { });
// Force a layout
platformView.LayoutIfNeeded();
});
}

[Fact(DisplayName = "CollectionView Does Not Crash After Resetting Source With Running Animation")]
public Task ClearingItemsSourceAfterCellMeasureInvalidationDoesNotCrash()
{
return ClearingItemsSourceAfterCellMeasureInvalidationDoesNotCrashHelper<CollectionViewHandler>();
}

[Fact(DisplayName = "CollectionViewHandler2 Does Not Crash After Resetting Source With Running Animation")]
public Task ClearingItemsSourceAfterCellMeasureInvalidationDoesNotCrash2()
{
return ClearingItemsSourceAfterCellMeasureInvalidationDoesNotCrashHelper<CollectionViewHandler2>();
}

[Fact(DisplayName = "CollectionView Does Not Leak With Default ItemsLayout")]
public async Task CollectionViewDoesNotLeakWithDefaultItemsLayout()
{
Expand Down Expand Up @@ -457,4 +537,4 @@ private static UIScrollView FindInternalScrollView(UICollectionView collectionVi
return null;
}
}
}
}
Loading