Skip to content

Commit

Permalink
Start work on iOS grids
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed May 7, 2024
1 parent 386b660 commit e4c9b0d
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions VirtualListView/Apple/CvLayout.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,68 @@ public CvLayout(VirtualListViewHandler handler) : base()

readonly bool isiOS11;

NFloat GetLayoutFullWidth(UIEdgeInsets sectionInset)
{
if (isiOS11)
return CollectionView.SafeAreaLayoutGuide.LayoutFrame.Width - sectionInset.Left - sectionInset.Right;
else
return CollectionView.Bounds.Width - sectionInset.Left - sectionInset.Right;
}

NFloat GetLayoutFullHeight(UIEdgeInsets sectionInset)
{
if (isiOS11)
return CollectionView.SafeAreaLayoutGuide.LayoutFrame.Height - sectionInset.Top - sectionInset.Bottom;
else
return CollectionView.Bounds.Height - sectionInset.Top - sectionInset.Bottom;
}

public override UICollectionViewLayoutAttributes LayoutAttributesForItem(NSIndexPath path)
{
var layoutAttributes = base.LayoutAttributesForItem(path);

PositionInfo? info = null;

Check warning on line 40 in VirtualListView/Apple/CvLayout.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in VirtualListView/Apple/CvLayout.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in VirtualListView/Apple/CvLayout.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in VirtualListView/Apple/CvLayout.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in VirtualListView/Apple/CvLayout.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

var columns = Handler.VirtualView.Columns;

if (columns > 1)
{
info = Handler?.PositionalViewSelector?.GetInfo(path.Item.ToInt32());
}

var sectionInset = SectionInset;

if (Handler.VirtualView.Orientation == ListOrientation.Vertical)
{
var x = SectionInset.Left;
var width = GetLayoutFullWidth(sectionInset);
NFloat gridItemInset = 0f;

NFloat width;
if (columns > 1 && info is not null && info.Kind != PositionKind.Header && info.Kind != PositionKind.Footer)
{
width = width / columns;

if (isiOS11)
width = CollectionView.SafeAreaLayoutGuide.LayoutFrame.Width - SectionInset.Left - SectionInset.Right;
else
width = CollectionView.Bounds.Width - SectionInset.Left - SectionInset.Right;
// Index 0 is first item in grid's row, so 0 additional inset
// for every next item, we need to add width of previous item
gridItemInset = width * info.ItemIndex;
}

layoutAttributes.Frame = new CGRect(x, layoutAttributes.Frame.Y, width, layoutAttributes.Frame.Height);
layoutAttributes.Frame = new CGRect(sectionInset.Left + gridItemInset, layoutAttributes.Frame.Y, width, layoutAttributes.Frame.Height);
}
else
{
var y = SectionInset.Top;
var height = GetLayoutFullHeight(sectionInset);
NFloat gridItemInset = 0f;

NFloat height;
if (columns > 1 && info is not null && info.Kind != PositionKind.Header && info.Kind != PositionKind.Footer)
{
height = height / columns;

if (isiOS11)
height = CollectionView.SafeAreaLayoutGuide.LayoutFrame.Height - SectionInset.Top - SectionInset.Bottom;
else
height = CollectionView.Bounds.Height - SectionInset.Top - SectionInset.Bottom;
// Index 0 is first item in grid's column, so 0 additional inset
// for every next item, we need to add height of previous item
gridItemInset = height * info.ItemIndex;
}

layoutAttributes.Frame = new CGRect(layoutAttributes.Frame.X, y, layoutAttributes.Frame.Width, height);
layoutAttributes.Frame = new CGRect(layoutAttributes.Frame.X, sectionInset.Top + gridItemInset, layoutAttributes.Frame.Width, height);
}

return layoutAttributes;
Expand Down

0 comments on commit e4c9b0d

Please sign in to comment.