IGListKit/Tests/Objects/IGTestDelegateController.m
Jesse Seidman 03049f742f Add IGListAdapterDelegate Methods 3/n
Summary:
## CONTEXT
`IGListAdapterDelegate` currently has methods that are fired when an object is *first* displayed and when an object *ended* display on screen.  For many use cases this works but there are use cases in which it would be helpful to know whenever a cell will be displayed or ends display.  This is especially relevant in the `IGListAdapterDelegateAnnouncer` which will enable you to add code for tracking cells globally.  This change also balances the `IGListAdapterDelegate` with `IGListDisplayDelegate`  which has methods which include the cell as well

## PLAN
This diff will be go out in multiple parts
- delegate method addition (many file change due to our pattern of not preferring default implementation of protocol methods)
- Update to `IGListDisplayHandler` and `IGListAdapterDelegateAnnouncer` for invocation of delegate methods
- Unit test additions

## THIS DIFF
This diff updates the unit tests of `IGListAdapterDelegateAnnouncerTests`, `IGListAdapterE2ETests` and `IGListDisplayHandlerTests`.  We added new expectations and to existing unit tests because the functionality to confirm is already tested it was just only checking the existing delegate methods.  The new tests check the new methods as well

Reviewed By: maxolls

Differential Revision: D70406065

fbshipit-source-id: ee054a21f0709b14081fdff38e22ab9710222794
2025-04-24 13:07:16 -07:00

113 lines
4.4 KiB
Objective-C

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "IGTestDelegateController.h"
#import "IGTestCell.h"
#import "IGTestObject.h"
@implementation IGTestDelegateController
- (instancetype)init {
if (self = [super init]) {
_willDisplayCellIndexes = [NSCountedSet new];
_didEndDisplayCellIndexes = [NSCountedSet new];
_height = 10.0;
self.workingRangeDelegate = self;
}
return self;
}
- (NSInteger)numberOfItems {
if ([self.item.value isKindOfClass:[NSNumber class]]) {
return [self.item.value integerValue];
}
return 1;
}
- (CGSize)sizeForItemAtIndex:(NSInteger)index {
return CGSizeMake(self.collectionContext.containerSize.width, self.height);
}
- (UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index {
IGTestCell *cell = _overrideCell ?: [self.collectionContext dequeueReusableCellOfClass:IGTestCell.class
forSectionController:self atIndex:index];
[[cell label] setText:[NSString stringWithFormat:@"%@", self.item.value]];
[cell setDelegate:self];
if (self.cellConfigureBlock) {
self.cellConfigureBlock(self);
}
return cell;
}
- (void)didUpdateToObject:(id)object {
_updateCount++;
_item = object;
if (self.itemUpdateBlock) {
self.itemUpdateBlock();
}
}
- (id<IGListDisplayDelegate>)displayDelegate {
return self;
}
- (void)didSelectItemAtIndex:(NSInteger)index {}
#pragma mark - IGListDisplayDelegate
- (void)listAdapter:(IGListAdapter *)listAdapter willDisplaySectionController:(IGListSectionController *)sectionController {
self.willDisplayCount++;
}
- (void)listAdapter:(IGListAdapter *)listAdapter didEndDisplayingSectionController:(IGListSectionController *)sectionController {
self.didEndDisplayCount++;
}
- (void)listAdapter:(IGListAdapter *)listAdapter willDisplaySectionController:(IGListSectionController *)sectionController
cell:(UICollectionViewCell *)cell
atIndex:(NSInteger)index {
[self.willDisplayCellIndexes addObject:@(index)];
}
- (void)listAdapter:(IGListAdapter *)listAdapter didEndDisplayingSectionController:(IGListSectionController *)sectionController
cell:(UICollectionViewCell *)cell
atIndex:(NSInteger)index {
[self.didEndDisplayCellIndexes addObject:@(index)];
}
- (void)listAdapter:(IGListAdapter *)listAdapter didScrollSectionController:(IGListSectionController *)sectionController {}
#pragma mark - IGListWorkingRangeDelegate
- (void)listAdapter:(IGListAdapter *)listAdapter sectionControllerWillEnterWorkingRange:(IGListSectionController *)sectionController {
__unused UICollectionViewCell *cell = [self.collectionContext cellForItemAtIndex:0 sectionController:self];
__unused UIView *supplementaryView = [self.collectionContext viewForSupplementaryElementOfKind:UICollectionElementKindSectionHeader
atIndex:0
sectionController:self];
}
- (void)listAdapter:(IGListAdapter *)listAdapter sectionControllerDidExitWorkingRange:(IGListSectionController *)sectionController {}
#pragma mark - IGListTransitionDelegate
- (UICollectionViewLayoutAttributes *)listAdapter:(IGListAdapter *)listAdapter
customizedInitialLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes
sectionController:(IGListSectionController *)sectionController
atIndex:(NSInteger)index {
attributes.center = CGPointMake(attributes.center.x + _initialAttributesOffset.x, attributes.center.y + _initialAttributesOffset.y);
return attributes;
}
- (UICollectionViewLayoutAttributes *)listAdapter:(IGListAdapter *)listAdapter
customizedFinalLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes
sectionController:(IGListSectionController *)sectionController
atIndex:(NSInteger)index {
attributes.center = CGPointMake(attributes.center.x + _finalAttributesOffset.x, attributes.center.y + _finalAttributesOffset.y);
return attributes;
}
@end