Skip to content

Commit

Permalink
Use flat positional view selector on ios/mac
Browse files Browse the repository at this point in the history
Previously was trying to still use sections that mapped to the adapter's sections but then added an extra item for a header and footer in each section, and then calculated the actual section/index.

This is inconsistent with how it's done for android and windows, where it's a flat list and natively just a single section.  This changes it to be consistent everywhere so it's easier to understand and debug, as there really was little to no advantage to doing it differently here.
  • Loading branch information
Redth committed Jul 26, 2023
1 parent 3862ba8 commit 05f41a5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 123 deletions.
14 changes: 11 additions & 3 deletions VirtualListView/Apple/CvCell.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ internal class CvCell : UICollectionViewCell
[Export("initWithFrame:")]
public CvCell(CGRect frame) : base(frame)
{
this.ContentView.AddGestureRecognizer(new UITapGestureRecognizer(() => this.TapHandler?.Invoke(this)));
this.ContentView.AddGestureRecognizer(new UITapGestureRecognizer(() => InvokeTap()));
}

public Action<CvCell> TapHandler { get; set; }

public override UIKeyCommand[] KeyCommands => new[]
UIKeyCommand[] keyCommands;

public override UIKeyCommand[] KeyCommands => keyCommands ??= new[]
{
UIKeyCommand.Create(new NSString("\r"), 0, new ObjCRuntime.Selector("keyCommandSelect")),
UIKeyCommand.Create(new NSString(" "), 0, new ObjCRuntime.Selector("keyCommandSelect")),
Expand All @@ -32,7 +34,13 @@ public CvCell(CGRect frame) : base(frame)
[Export("keyCommandSelect")]
public void KeyCommandSelect()
{
this.TapHandler?.Invoke(this);
InvokeTap();
}

void InvokeTap()
{
if (PositionInfo.Kind == PositionKind.Item)
TapHandler?.Invoke(this);
}

public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittingAttributes(UICollectionViewLayoutAttributes layoutAttributes)
Expand Down
9 changes: 3 additions & 6 deletions VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ public CvDataSource(VirtualListViewHandler handler)
readonly ReusableIdManager sectionFooterIdManager = new ReusableIdManager("SectionFooter", new NSString("SectionFooter"));

public override nint NumberOfSections(UICollectionView collectionView)
=> Handler?.PositionalViewSelector?.GetNumberOfSections() ?? 0;
=> 1;

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var section = indexPath.Section;
var itemIndex = (int)indexPath.Item;

var info = Handler?.PositionalViewSelector?.GetInfo((int)indexPath.Section, (int)indexPath.Item);
var info = Handler?.PositionalViewSelector?.GetInfo(indexPath.Item.ToInt32());

var data = Handler?.PositionalViewSelector?.Adapter?.DataFor(info.Kind, info.SectionIndex, info.ItemIndex);

Expand Down Expand Up @@ -89,6 +86,6 @@ void TapCellHandler(CvCell cell)

public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return Handler?.PositionalViewSelector?.GetNumberOfItemsForSection(section.ToInt32()) ?? 0;
return Handler?.PositionalViewSelector?.TotalCount ?? 0;
}
}
4 changes: 2 additions & 2 deletions VirtualListView/Apple/CvDelegate.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override void ItemDeselected(UICollectionView collectionView, NSIndexPath

void HandleSelection(UICollectionView collectionView, NSIndexPath indexPath, bool selected)
{
UIView.AnimationsEnabled = false;
//UIView.AnimationsEnabled = false;
var selectedCell = collectionView.CellForItem(indexPath) as CvCell;

if ((selectedCell?.PositionInfo?.Kind ?? PositionKind.Header) == PositionKind.Item)
Expand Down Expand Up @@ -55,7 +55,7 @@ public override bool ShouldDeselectItem(UICollectionView collectionView, NSIndex

bool IsRealItem(NSIndexPath indexPath)
{
var info = Handler?.PositionalViewSelector?.GetInfo(indexPath.Section, (int)indexPath.Item);
var info = Handler?.PositionalViewSelector?.GetInfo(indexPath.Item.ToInt32());
return (info?.Kind ?? PositionKind.Header) == PositionKind.Item;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ static void UpdateSelected(VirtualListViewHandler handler, ItemPosition[] itemPo
{
foreach (var itemPosition in itemPositions)
{
var realIndex = handler.PositionalViewSelector.GetIndexPath(itemPosition.SectionIndex, itemPosition.ItemIndex);
var realIndex = handler.PositionalViewSelector.GetPosition(itemPosition.SectionIndex, itemPosition.ItemIndex);

var cell = handler.collectionView.CellForItem(realIndex);
var cell = handler.collectionView.CellForItem(NSIndexPath.FromItemSection(realIndex, 0));

if (cell is CvCell cvcell)
{
Expand Down
110 changes: 0 additions & 110 deletions VirtualListView/PositionalViewSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public PositionalViewSelector(IVirtualListView virtualListView)
VirtualListView = virtualListView;
}

//int? cachedTotalCount;
public int TotalCount
=> GetTotalCount();

Expand Down Expand Up @@ -46,114 +45,6 @@ int GetTotalCount()
return sum;
}

#if IOS || MACCATALYST

internal int GetNumberOfSections()
{
var sections = Math.Max(Adapter.GetNumberOfSections(), 0);
if (HasGlobalHeader)
sections++;
if (HasGlobalFooter)
sections++;

return sections;
}

internal int GetNumberOfItemsForSection(int sectionIndex)
{
var realSection = sectionIndex;

if (HasGlobalHeader)
{
if (sectionIndex == 0)
return 1;

realSection--;
}

if (HasGlobalFooter)
{
if (sectionIndex >= GetNumberOfSections() - 1)
return 1;
}

var itemsCount = Adapter?.GetNumberOfItemsInSection((int)realSection) ?? 0;

if (ViewSelector?.SectionHasHeader((int)realSection) ?? false)
itemsCount++;

if (ViewSelector?.SectionHasFooter((int)realSection) ?? false)
itemsCount++;

return itemsCount;
}

public PositionInfo GetInfo(int sectionIndex, int itemIndex)
{
var realSectionIndex = sectionIndex;

if (HasGlobalHeader)
{
if (sectionIndex == 0)
return PositionInfo.ForHeader(0);

// Global header takes up a section, real adapter is 1 less
realSectionIndex--;
}

var realNumberOfSections = Adapter?.GetNumberOfSections() ?? 0;

if (HasGlobalFooter)
{
if (realSectionIndex >= realNumberOfSections)
return PositionInfo.ForFooter(-1);
}


var realItemsInSection = Adapter?.GetNumberOfItemsInSection(realSectionIndex) ?? 0;

var realItemIndex = itemIndex;

var itemsAdded = 0;

if (ViewSelector?.SectionHasHeader(realSectionIndex) ?? false)
{
itemsAdded++;
realItemIndex--;

if (itemIndex == 0)
return PositionInfo.ForSectionHeader(-1, realSectionIndex, realItemsInSection);
}

if (ViewSelector.SectionHasFooter(realSectionIndex))
{
itemsAdded++;

if (itemIndex >= realItemsInSection + itemsAdded - 1)
return PositionInfo.ForSectionFooter(-1, realSectionIndex, realItemsInSection);
}

return PositionInfo.ForItem(-1, realSectionIndex, realItemIndex, Adapter.GetNumberOfItemsInSection(realSectionIndex), realNumberOfSections);
}

public Foundation.NSIndexPath GetIndexPath(int positionSectionIndex, int positionItemIndex)
{
var realSectionIndex = positionSectionIndex;
var realItemIndex = positionItemIndex;

// Global header takes up one section
if (HasGlobalHeader)
realSectionIndex++;

// If the section has a header, the real item index is +1
if (ViewSelector?.SectionHasHeader(positionSectionIndex) ?? false)
realItemIndex++;

return Foundation.NSIndexPath.FromItemSection(realItemIndex, realSectionIndex);
}

#else

public int GetPosition(int sectionIndex, int itemIndex)
{
// calculate position
Expand Down Expand Up @@ -246,6 +137,5 @@ public PositionInfo GetInfo(int position)
Kind = PositionKind.Footer
};
}
#endif

}

0 comments on commit 05f41a5

Please sign in to comment.