Skip to content

Commit

Permalink
Revert "Prevent iOS CollectionView size shifts from clearing the cell…
Browse files Browse the repository at this point in the history
… size cache (#18464)"

This reverts commit 6a5bbf0.
  • Loading branch information
rmarinho authored Nov 23, 2023
1 parent 01afe33 commit 365f66b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 210 deletions.
23 changes: 16 additions & 7 deletions src/Controls/src/Core/Handlers/Items/iOS/ItemsViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Foundation;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;
using ObjCRuntime;
using UIKit;

namespace Microsoft.Maui.Controls.Handlers.Items
Expand Down Expand Up @@ -177,12 +178,12 @@ public override void ViewDidLoad()
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
ConstrainItemsToBounds();
ConstrainToItemsView();
}

public override void ViewWillLayoutSubviews()
{
ConstrainItemsToBounds();
ConstrainToItemsView();
base.ViewWillLayoutSubviews();
InvalidateMeasureIfContentSizeChanged();
LayoutEmptyView();
Expand Down Expand Up @@ -242,19 +243,26 @@ void InvalidateMeasureIfContentSizeChanged()

internal Size? GetSize()
{
if (_emptyViewDisplayed)
if (_emptyViewDisplayed)
{
return _emptyUIView.Frame.Size.ToSize();
}

return CollectionView.CollectionViewLayout.CollectionViewContentSize.ToSize();
}

void ConstrainItemsToBounds()
void ConstrainToItemsView()
{
var contentBounds = CollectionView.AdjustedContentInset.InsetRect(CollectionView.Bounds);
var constrainedSize = contentBounds.Size;
ItemsViewLayout.UpdateConstraints(constrainedSize);
var itemsViewWidth = ItemsView.Width;
var itemsViewHeight = ItemsView.Height;

if (itemsViewHeight < 0 || itemsViewWidth < 0)
{
ItemsViewLayout.UpdateConstraints(CollectionView.Bounds.Size);
return;
}

ItemsViewLayout.UpdateConstraints(new CGSize(itemsViewWidth, itemsViewHeight));
}

void EnsureLayoutInitialized()
Expand Down Expand Up @@ -313,6 +321,7 @@ public virtual void UpdateFlowDirection()
Layout.InvalidateLayout();
}


public override nint NumberOfSections(UICollectionView collectionView)
{
CheckForEmptySource();
Expand Down
38 changes: 34 additions & 4 deletions src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using CoreGraphics;
using Foundation;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls.Internals;
using ObjCRuntime;
using UIKit;

namespace Microsoft.Maui.Controls.Handlers.Items
Expand All @@ -19,7 +21,9 @@ public abstract class ItemsViewLayout : UICollectionViewFlowLayout
CGSize _currentSize;
WeakReference<Func<UICollectionViewCell>> _getPrototype;

readonly Dictionary<object, CGSize> _cellSizeCache = new();
const double ConstraintSizeTolerance = 0.00001;

Dictionary<object, CGSize> _cellSizeCache = new Dictionary<object, CGSize>();

public ItemsUpdatingScrollMode ItemsUpdatingScrollMode { get; set; }

Expand Down Expand Up @@ -98,7 +102,7 @@ protected virtual void HandlePropertyChanged(PropertyChangedEventArgs propertyCh

internal virtual void UpdateConstraints(CGSize size)
{
if (size.IsCloseTo(_currentSize))
if (!RequiresConstraintUpdate(size, _currentSize))
{
return;
}
Expand Down Expand Up @@ -564,12 +568,23 @@ static void ForceScrollToLastItem(UICollectionView collectionView, ItemsLayout i

public override bool ShouldInvalidateLayoutForBoundsChange(CGRect newBounds)
{
if(newBounds.Size == _currentSize)
if (newBounds.Size == _currentSize)
{
return base.ShouldInvalidateLayoutForBoundsChange(newBounds);
}

UpdateConstraints(CollectionView.AdjustedContentInset.InsetRect(newBounds).Size);
if (OperatingSystem.IsIOSVersionAtLeast(11) || OperatingSystem.IsMacCatalystVersionAtLeast(11)
#if TVOS
|| OperatingSystem.IsTvOSVersionAtLeast(11)
#endif
)
{
UpdateConstraints(CollectionView.AdjustedContentInset.InsetRect(newBounds).Size);
}
else
{
UpdateConstraints(CollectionView.Bounds.Size);
}

return true;
}
Expand All @@ -595,5 +610,20 @@ internal void ClearCellSizeCache()
{
_cellSizeCache.Clear();
}

bool RequiresConstraintUpdate(CGSize newSize, CGSize current)
{
if (Math.Abs(newSize.Width - current.Width) > ConstraintSizeTolerance)
{
return true;
}

if (Math.Abs(newSize.Height - current.Height) > ConstraintSizeTolerance)
{
return true;
}

return false;
}
}
}
42 changes: 0 additions & 42 deletions src/Controls/src/Core/Handlers/Items/iOS/SizeExtensions.cs

This file was deleted.

22 changes: 21 additions & 1 deletion src/Controls/src/Core/Handlers/Items/iOS/TemplatedCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Foundation;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;
using ObjCRuntime;
using UIKit;

namespace Microsoft.Maui.Controls.Handlers.Items
Expand Down Expand Up @@ -65,7 +66,7 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin

var preferredSize = preferredAttributes.Frame.Size;

if (preferredSize.IsCloseTo(_size)
if (SizesAreSame(preferredSize, _size)
&& AttributesConsistentWithConstrainedDimension(preferredAttributes))
{
return preferredAttributes;
Expand All @@ -78,6 +79,8 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin

OnLayoutAttributesChanged(preferredAttributes);

//_isMeasured = true;

return preferredAttributes;
}

Expand Down Expand Up @@ -287,6 +290,23 @@ protected void OnLayoutAttributesChanged(UICollectionViewLayoutAttributes newAtt

protected abstract bool AttributesConsistentWithConstrainedDimension(UICollectionViewLayoutAttributes attributes);

bool SizesAreSame(CGSize preferredSize, Size elementSize)
{
const double tolerance = 0.000001;

if (Math.Abs(preferredSize.Height - elementSize.Height) > tolerance)
{
return false;
}

if (Math.Abs(preferredSize.Width - elementSize.Width) > tolerance)
{
return false;
}

return true;
}

void UpdateVisualStates()
{
if (PlatformHandler?.VirtualView is VisualElement element)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ void SetupBuilder()
handlers.AddHandler<VerticalStackLayout, LayoutHandler>();
handlers.AddHandler<Grid, LayoutHandler>();
handlers.AddHandler<Label, LabelHandler>();
#if IOS && !MACCATALYST
handlers.AddHandler<CacheTestCollectionView, CacheTestCollectionViewHandler>();
#endif
});
});
}
Expand Down
Loading

0 comments on commit 365f66b

Please sign in to comment.