IGListKit/Tests/Objects/IGTestDelegateDataSource.m

41 lines
1.3 KiB
Mathematica
Raw Permalink Normal View History

/*
* 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 "IGTestDelegateDataSource.h"
#import <IGListKit/IGListAdapter.h>
#import "IGTestDelegateController.h"
#import "IGTestObject.h"
NSObject *const kIGTestDelegateDataSourceSkipObject = @"kIGTestDelegateDataSourceSkipObject";
avoid crashing when not subclassing IGListSectionController Summary: Currently, if you use `IGListSectionController` without a subclass, `UICollectionView` will crash when requesting the cell. That's because `[IGListSectionController numberOfItems]` defaults to 1, but returns no cell. There's a few issues with that: 1. It can be tough to debug, because the crash in within `UICollectionView`. We don't know the dataSource or object type responsible. 2. The crash only happens if the user scrolls by the missing cell, which can make it hard to repro. 3. If we don't know how to render a single section, we might not want to crash the entire app. Passing a plain `IGListSectionController` kind of feels like the dataSource is ok with just rendering nothing. It's not clear at all that it will crash. Options: 1. Crash immediately when a plain `IGListSectionController` is passed to `IGListAdapter`, so we get a clear callstack and API feedback. If the dataSource doesn't want to crash, it must return `IGEmptySectionController` that has 0 `numberOfItems`. 2. Don't crash, but log a `IGFailAssert()`. If the dataSource wants to throw on a missing object-controller match, they can can, but it's not the IGListKit default. I'm leaning on #2 for a few reasons. It's really not obvious that passing a plain `IGListSectionController` would crash and no one will know that `IGEmptySectionController` exists. We could have a linter, but that only works within Meta. Reviewed By: DimaVartanian Differential Revision: D52087286 fbshipit-source-id: 8b8754d56e66c0c2b00f8df3b9671a6fc2287aea
2023-12-14 22:27:34 +00:00
NSObject *const kIGTestDelegateDataSourceNoSectionControllerSubclass = @"kIGTestDelegateDataSourceNoSectionControllerSubclass";
@implementation IGTestDelegateDataSource
- (NSArray *)objectsForListAdapter:(IGListAdapter *)listAdapter {
return self.objects;
}
- (IGListSectionController *)listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(id)object {
if ([object isEqual:kIGTestDelegateDataSourceSkipObject]) {
return nil;
avoid crashing when not subclassing IGListSectionController Summary: Currently, if you use `IGListSectionController` without a subclass, `UICollectionView` will crash when requesting the cell. That's because `[IGListSectionController numberOfItems]` defaults to 1, but returns no cell. There's a few issues with that: 1. It can be tough to debug, because the crash in within `UICollectionView`. We don't know the dataSource or object type responsible. 2. The crash only happens if the user scrolls by the missing cell, which can make it hard to repro. 3. If we don't know how to render a single section, we might not want to crash the entire app. Passing a plain `IGListSectionController` kind of feels like the dataSource is ok with just rendering nothing. It's not clear at all that it will crash. Options: 1. Crash immediately when a plain `IGListSectionController` is passed to `IGListAdapter`, so we get a clear callstack and API feedback. If the dataSource doesn't want to crash, it must return `IGEmptySectionController` that has 0 `numberOfItems`. 2. Don't crash, but log a `IGFailAssert()`. If the dataSource wants to throw on a missing object-controller match, they can can, but it's not the IGListKit default. I'm leaning on #2 for a few reasons. It's really not obvious that passing a plain `IGListSectionController` would crash and no one will know that `IGEmptySectionController` exists. We could have a linter, but that only works within Meta. Reviewed By: DimaVartanian Differential Revision: D52087286 fbshipit-source-id: 8b8754d56e66c0c2b00f8df3b9671a6fc2287aea
2023-12-14 22:27:34 +00:00
} else if ([object isEqual:kIGTestDelegateDataSourceNoSectionControllerSubclass]) {
return [IGListSectionController new];
}
IGTestDelegateController *sectionController = [[IGTestDelegateController alloc] init];
sectionController.cellConfigureBlock = self.cellConfigureBlock;
sectionController.overrideCell = self.overrideCell;
return sectionController;
}
- (nullable UIView *)emptyViewForListAdapter:(IGListAdapter *)listAdapter {
return nil;
}
@end