Skip to content

Commit

Permalink
[net9.0] Fix CV2 handler and uitests fixes (#25076)
Browse files Browse the repository at this point in the history
* [iOS] Make GridLayouts render header and footer

* [ios] Fix grouping not working because of StructuredView

* [tests] Add a small delay on changing orientation

* Fix duplicated header
  • Loading branch information
rmarinho committed Oct 16, 2024
1 parent 37f945d commit e4e03d7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected override UICollectionViewLayout SelectLayout()

if (itemsLayout is GridItemsLayout gridItemsLayout)
{
return LayoutFactory2.CreateGrid(gridItemsLayout, groupInfo);
return LayoutFactory2.CreateGrid(gridItemsLayout, groupInfo, headerFooterInfo);
}

if (itemsLayout is LinearItemsLayout listItemsLayout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ protected override void RegisterViewTypes()
RegisterSupplementaryViews(UICollectionElementKindSection.Footer);
}

private protected override void RegisterSupplementaryViews(UICollectionElementKindSection kind)
{
base.RegisterSupplementaryViews(kind);
if (IsHorizontal)
{
CollectionView.RegisterClassForSupplementaryView(typeof(HorizontalSupplementaryView2),
kind, HorizontalSupplementalView2ReuseId);
CollectionView.RegisterClassForSupplementaryView(typeof(HorizontalDefaultSupplementalView2),
kind, HorizontalDefaultSupplementalView2ReuseId);
}
else
{
CollectionView.RegisterClassForSupplementaryView(typeof(VerticalSupplementaryView2),
kind, VerticalSupplementaryView2ReuseId);
CollectionView.RegisterClassForSupplementaryView(typeof(VerticalDefaultSupplementalView2),
kind, VerticalDefaultSupplementalView2ReuseId);
}
}

string DetermineViewReuseId(NSString elementKind)
{
return DetermineViewReuseId(elementKind == UICollectionElementKindSectionKey.Header
Expand All @@ -68,12 +87,12 @@ string DetermineViewReuseId(NSString elementKind)
public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView,
NSString elementKind, NSIndexPath indexPath)
{
var struc = base.GetViewForSupplementaryElement(collectionView, elementKind, indexPath);
if(struc != null)
var suplementaryViewFromStructuredView = base.GetViewForSupplementaryElement(collectionView, elementKind, indexPath);
if (suplementaryViewFromStructuredView is not null)
{
return struc;
return suplementaryViewFromStructuredView;
}

var reuseId = DetermineViewReuseId(elementKind);

var view = collectionView.DequeueReusableSupplementaryView(elementKind, reuseId, indexPath) as UICollectionReusableView;
Expand Down Expand Up @@ -117,7 +136,7 @@ void UpdateTemplatedSupplementaryView(TemplatedCell2 cell, NSString elementKind,
// }
}



string DetermineViewReuseId(DataTemplate template)
{
Expand All @@ -127,20 +146,26 @@ string DetermineViewReuseId(DataTemplate template)
orientation = linearItemsLayout.Orientation;
else if (this.ItemsView.ItemsLayout is GridItemsLayout gridItemsLayout)
orientation = gridItemsLayout.Orientation;

if (template == null)
{
// No template, fall back the the default supplemental views
return orientation == ItemsLayoutOrientation.Horizontal
? HorizontalDefaultSupplementalView2.ReuseId
: VerticalDefaultSupplementalView2.ReuseId;
? HorizontalDefaultSupplementalView2ReuseId
: VerticalDefaultSupplementalView2ReuseId;
}

return orientation == ItemsLayoutOrientation.Horizontal
? HorizontalSupplementaryView2.ReuseId
: VerticalSupplementaryView2.ReuseId;
? HorizontalSupplementalView2ReuseId
: VerticalSupplementaryView2ReuseId;
}

static NSString HorizontalDefaultSupplementalView2ReuseId = new NSString("Microsoft.Maui.Controls.HorizontalDefaultSupplementalGroupView2");
static NSString VerticalDefaultSupplementalView2ReuseId = new NSString("Microsoft.Maui.Controls.VerticalDefaultSupplementaryGroupView2");
static NSString HorizontalSupplementalView2ReuseId = new NSString("Microsoft.Maui.Controls.HorizontalSupplementalGroupView2");
static NSString VerticalSupplementaryView2ReuseId = new NSString("Microsoft.Maui.Controls.VerticalSupplementaryGroupView2");


// internal CGSize GetReferenceSizeForHeader(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
// {
// if (!_isGrouped)
Expand Down
17 changes: 10 additions & 7 deletions src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public static UICollectionViewLayout CreateList(LinearItemsLayout linearItemsLay
? CreateVerticalList(linearItemsLayout, groupingInfo, headerFooterInfo)
: CreateHorizontalList(linearItemsLayout, groupingInfo, headerFooterInfo);

public static UICollectionViewLayout CreateGrid(GridItemsLayout gridItemsLayout, LayoutGroupingInfo groupingInfo)
public static UICollectionViewLayout CreateGrid(GridItemsLayout gridItemsLayout,
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo)
=> gridItemsLayout.Orientation == ItemsLayoutOrientation.Vertical
? CreateVerticalGrid(gridItemsLayout, groupingInfo)
: CreateHorizontalGrid(gridItemsLayout, groupingInfo);
? CreateVerticalGrid(gridItemsLayout, groupingInfo, headerFooterInfo)
: CreateHorizontalGrid(gridItemsLayout, groupingInfo, headerFooterInfo);

static NSCollectionLayoutBoundarySupplementaryItem[] CreateSupplementaryItems(LayoutGroupingInfo? groupingInfo, LayoutHeaderFooterInfo? layoutHeaderFooterInfo,
UICollectionViewScrollDirection scrollDirection, NSCollectionLayoutDimension width, NSCollectionLayoutDimension height)
Expand Down Expand Up @@ -140,7 +141,7 @@ static UICollectionViewLayout CreateListLayout(UICollectionViewScrollDirection s



static UICollectionViewLayout CreateGridLayout(UICollectionViewScrollDirection scrollDirection, LayoutGroupingInfo groupingInfo, LayoutSnapInfo snapInfo, NSCollectionLayoutDimension itemWidth, NSCollectionLayoutDimension itemHeight, NSCollectionLayoutDimension groupWidth, NSCollectionLayoutDimension groupHeight, double verticalItemSpacing, double horizontalItemSpacing, int columns)
static UICollectionViewLayout CreateGridLayout(UICollectionViewScrollDirection scrollDirection, LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo, LayoutSnapInfo snapInfo, NSCollectionLayoutDimension itemWidth, NSCollectionLayoutDimension itemHeight, NSCollectionLayoutDimension groupWidth, NSCollectionLayoutDimension groupHeight, double verticalItemSpacing, double horizontalItemSpacing, int columns)
{
var layoutConfiguration = new UICollectionViewCompositionalLayoutConfiguration();
layoutConfiguration.ScrollDirection = scrollDirection;
Expand Down Expand Up @@ -179,7 +180,7 @@ static UICollectionViewLayout CreateGridLayout(UICollectionViewScrollDirection s

section.BoundarySupplementaryItems = CreateSupplementaryItems(
groupingInfo,
null, // No header/footer in grid
headerFooterInfo,
scrollDirection,
groupWidth,
groupHeight);
Expand Down Expand Up @@ -222,9 +223,10 @@ public static UICollectionViewLayout CreateHorizontalList(LinearItemsLayout line
null);

public static UICollectionViewLayout CreateVerticalGrid(GridItemsLayout gridItemsLayout,
LayoutGroupingInfo groupingInfo)
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo)
=> CreateGridLayout(UICollectionViewScrollDirection.Vertical,
groupingInfo,
headerFooterInfo,
new LayoutSnapInfo { SnapType = gridItemsLayout.SnapPointsType, SnapAligment = gridItemsLayout.SnapPointsAlignment },
// Width is the number of columns
NSCollectionLayoutDimension.CreateFractionalWidth(1f / gridItemsLayout.Span),
Expand All @@ -240,9 +242,10 @@ public static UICollectionViewLayout CreateVerticalGrid(GridItemsLayout gridItem


public static UICollectionViewLayout CreateHorizontalGrid(GridItemsLayout gridItemsLayout,
LayoutGroupingInfo groupingInfo)
LayoutGroupingInfo groupingInfo, LayoutHeaderFooterInfo headerFooterInfo)
=> CreateGridLayout(UICollectionViewScrollDirection.Horizontal,
groupingInfo,
headerFooterInfo,
new LayoutSnapInfo { SnapType = gridItemsLayout.SnapPointsType, SnapAligment = gridItemsLayout.SnapPointsAlignment },
// Item width is estimated
NSCollectionLayoutDimension.CreateEstimated(30f),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ protected override void Dispose(bool disposing)

protected override bool IsHorizontal => (ItemsView?.ItemsLayout as ItemsLayout)?.Orientation == ItemsLayoutOrientation.Horizontal;

public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView,
NSString elementKind, NSIndexPath indexPath)
public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
// We don't have a header or footer, so we don't need to do anything
if(ItemsView.Header is null && ItemsView.Footer is null && ItemsView.HeaderTemplate is null && ItemsView.FooterTemplate is null)
{
return null;
}

var reuseId = DetermineViewReuseId(elementKind);

var view = collectionView.DequeueReusableSupplementaryView(elementKind, reuseId, indexPath) as UICollectionReusableView;
Expand All @@ -68,7 +73,7 @@ public override UICollectionReusableView GetViewForSupplementaryElement(UICollec
return view;
}

private protected void RegisterSupplementaryViews(UICollectionElementKindSection kind)
private protected virtual void RegisterSupplementaryViews(UICollectionElementKindSection kind)
{
if (IsHorizontal)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ public void CollectionViewFirstItemCorrectlySetsTheMeasure()
[Category(UITestCategories.CollectionView)]
[FailsOnMac("This test is failing, likely due to product issue")]
[FailsOnWindows("This test is failing, likely due to product issue")]
public void CollectionViewWorksWhenRotatingDevice()
public async Task CollectionViewWorksWhenRotatingDevice()
{
try
{
App.WaitForElement("FullSize");
App.Tap("FullSize");
App.SetOrientationPortrait();
await Task.Delay(100);
var itemSizePortrait = App.WaitForElement("Item1").GetRect();
App.SetOrientationLandscape();
await Task.Delay(100);
var itemSizeLandscape = App.WaitForElement("Item1").GetRect();
App.SetOrientationPortrait();
await Task.Delay(100);
var itemSizePortrait2 = App.WaitForElement("Item1").GetRect();

ClassicAssert.Greater(itemSizeLandscape.Width, itemSizePortrait.Width);
Expand Down

0 comments on commit e4e03d7

Please sign in to comment.