Skip to content

Commit e3523b3

Browse files
Nolan O'Brienfacebook-github-bot
Nolan O'Brien
authored andcommitted
Add indexesOfVisibleObjects
Summary: It's inefficient to get the unordered array of visibleObjects to then try to find the order of those objects after. Instead, introduce a new `indexesOfVisibleObjects` API to `IGListAdapter` which will inherently preserve order. Will use this to help find the previous and next "item" to scroll to with Main Feed. Differential Revision: D70835725 fbshipit-source-id: a5f66681e0184299d92f220be194b5554639a7da
1 parent 9c0ef52 commit e3523b3

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

Source/IGListKit/IGListAdapter.h

+7
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ NS_SWIFT_NAME(ListAdapter)
239239
*/
240240
- (NSArray *)visibleObjects;
241241

242+
/**
243+
Less performant that `visibleObjects` but gives you an ordering to the list of visible objects in `self.objects`.
244+
245+
@return An index set for objects in `self.objects`. Result's `.count` will be `0` if no visible objects.
246+
*/
247+
- (NSIndexSet *)indexesOfVisibleObjects;
248+
242249
/**
243250
An unordered array of the currently visible cells for a given object.
244251

Source/IGListKit/IGListAdapter.m

+27-3
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ - (NSArray *)objects {
583583
return [[self.displayHandler visibleListSections] allObjects];
584584
}
585585

586-
- (NSArray *)visibleObjects {
586+
- (NSSet *)_visibleObjectsSet FB_OBJC_DIRECT {
587587
IGAssertMainThread();
588588

589589
NSArray<UICollectionViewCell *> *visibleCells = [self.collectionView visibleCells];
@@ -602,7 +602,32 @@ - (NSArray *)visibleObjects {
602602
}
603603
}
604604
}
605-
return [visibleObjects allObjects];
605+
return visibleObjects;
606+
}
607+
608+
- (NSArray *)visibleObjects {
609+
return [[self _visibleObjectsSet] allObjects];
610+
}
611+
612+
- (NSIndexSet *)indexesOfVisibleObjects {
613+
/*
614+
This is a naive implementation, going through all objects and checking if they are visible.
615+
It is not optimized for performance, but it is correct.
616+
617+
In the future, this could potentially be optimized by getting the index paths of visible cells,
618+
and converting those index paths into a range of object indexes within `self.objects`.
619+
*/
620+
621+
NSSet *visibleObjects = [self _visibleObjectsSet];
622+
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
623+
NSUInteger idx = 0;
624+
for (id object in self.objects) {
625+
if ([visibleObjects containsObject:object]) {
626+
[indexSet addIndex:idx];
627+
}
628+
idx++;
629+
}
630+
return [indexSet copy];
606631
}
607632

608633
- (NSArray<UICollectionViewCell *> *)visibleCellsForObject:(id)object {
@@ -624,7 +649,6 @@ - (NSArray *)visibleObjects {
624649
return [visibleCells filteredArrayUsingPredicate:controllerPredicate];
625650
}
626651

627-
628652
#pragma mark - Layout
629653

630654
- (CGSize)sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

0 commit comments

Comments
 (0)