2016-09-07 22:37:59 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
|
|
2017-03-22 19:40:51 +00:00
|
|
|
|
#import <IGListKit/IGListBatchContext.h>
|
|
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
@class IGListSectionController;
|
2017-03-22 19:40:51 +00:00
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
The collection context provides limited access to the collection-related information that
|
2017-07-18 15:29:54 +00:00
|
|
|
|
section controllers need for operations like sizing, dequeuing cells, inserting, deleting, reloading, etc.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
*/
|
Swift name annotations
Summary:
This adds `NS_SWIFT_NAME` annotations to all public API's to provide cleaner integration into Swift:
- Removes the need to prefix classes in Swift code, instead rely on Swift module name spacing
- Adds more argument labels to C function API's like `IGListDiff([], [], .equality)` => `ListDiff(oldArray: [], newArray: [], option: .equality)`
While this is a large API change it should be as easy as:
- Find and replace `(IGList)([^K])` to `List$2` in Xcode with a scope set to Swift
- Build and follow compiler's auto fix corrections for C API's or any missed renames
I have not updated the documentation to reflect this yet, I am totally willing to do so but before I sink that amount of time into it I wanted to see if the Instagram team is even open to this change!
- [x] All tests pass. Demo project builds and runs.
- [x] I added tests, an experiment, or detailed why my change isn't tested.
- [x] I added an entry to the `CHANGELOG.md` for any breaking changes, enhancements, or bug fixes.
- [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/.github/CONTRIBUTING.md)
- [ ] I have updated the documentation
Closes https://github.com/Instagram/IGListKit/pull/593
Reviewed By: jessesquires
Differential Revision: D5028039
Pulled By: rnystrom
fbshipit-source-id: b473d874a1f9574e56b2ebaabd5b73d1b57d4bab
2017-05-09 21:29:52 +00:00
|
|
|
|
NS_SWIFT_NAME(ListCollectionContext)
|
2016-09-07 22:37:59 +00:00
|
|
|
|
@protocol IGListCollectionContext <NSObject>
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-30 16:23:32 +00:00
|
|
|
|
The size of the collection view. You can use this for sizing cells.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
*/
|
|
|
|
|
|
@property (nonatomic, readonly) CGSize containerSize;
|
|
|
|
|
|
|
2017-03-30 16:23:32 +00:00
|
|
|
|
/**
|
|
|
|
|
|
The content insets of the collection view. You can use this for sizing cells.
|
|
|
|
|
|
*/
|
|
|
|
|
|
@property (nonatomic, readonly) UIEdgeInsets containerInset;
|
|
|
|
|
|
|
2017-12-18 16:33:24 +00:00
|
|
|
|
/**
|
|
|
|
|
|
The adjusted content insets of the collection view. Equivalent to containerInset under iOS 11.
|
|
|
|
|
|
*/
|
|
|
|
|
|
@property (nonatomic, readonly) UIEdgeInsets adjustedContainerInset;
|
|
|
|
|
|
|
2017-03-30 16:23:32 +00:00
|
|
|
|
/**
|
|
|
|
|
|
The size of the collection view with content insets applied.
|
|
|
|
|
|
*/
|
|
|
|
|
|
@property (nonatomic, readonly) CGSize insetContainerSize;
|
|
|
|
|
|
|
2017-03-15 16:25:03 +00:00
|
|
|
|
/**
|
|
|
|
|
|
Returns size of the collection view relative to the section controller.
|
|
|
|
|
|
|
|
|
|
|
|
@param sectionController The section controller requesting this information.
|
|
|
|
|
|
|
|
|
|
|
|
@return The size of the collection view minus the given section controller's insets.
|
|
|
|
|
|
*/
|
2017-04-19 15:17:56 +00:00
|
|
|
|
- (CGSize)containerSizeForSectionController:(IGListSectionController *)sectionController;
|
2017-03-15 16:25:03 +00:00
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Returns the index of the specified cell in the collection relative to the section controller.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param cell An existing cell in the collection.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
@param sectionController The section controller requesting this information.
|
|
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
@return The index of the cell or `NSNotFound` if it does not exist in the collection.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
*/
|
2016-11-16 23:15:55 +00:00
|
|
|
|
- (NSInteger)indexForCell:(UICollectionViewCell *)cell
|
2017-04-19 15:17:56 +00:00
|
|
|
|
sectionController:(IGListSectionController *)sectionController;
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Returns the cell in the collection at the specified index for the section controller.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param index The index of the desired cell.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
@param sectionController The section controller requesting this information.
|
|
|
|
|
|
|
|
|
|
|
|
@return The collection view cell, or `nil` if not found.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
@warning This method may return `nil` if the cell is offscreen.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
*/
|
|
|
|
|
|
- (nullable __kindof UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index
|
2017-04-19 15:17:56 +00:00
|
|
|
|
sectionController:(IGListSectionController *)sectionController;
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Returns the visible cells for the given section controller.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
|
|
|
|
|
@param sectionController The section controller requesting this information.
|
|
|
|
|
|
|
|
|
|
|
|
@return An array of visible cells, or an empty array if none are found.
|
|
|
|
|
|
*/
|
2017-04-19 15:17:56 +00:00
|
|
|
|
- (NSArray<UICollectionViewCell *> *)visibleCellsForSectionController:(IGListSectionController *)sectionController;
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2017-02-28 22:26:18 +00:00
|
|
|
|
/**
|
|
|
|
|
|
Returns the visible paths for the given section controller.
|
|
|
|
|
|
|
|
|
|
|
|
@param sectionController The section controller requesting this information.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
|
2017-02-28 22:26:18 +00:00
|
|
|
|
@return An array of visible index paths, or an empty array if none are found.
|
|
|
|
|
|
*/
|
2017-04-19 15:17:56 +00:00
|
|
|
|
- (NSArray<NSIndexPath *> *)visibleIndexPathsForSectionController:(IGListSectionController *) sectionController;
|
2017-02-28 22:26:18 +00:00
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
|
/**
|
|
|
|
|
|
Deselects a cell in the collection.
|
|
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param index The index of the item to deselect.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
@param sectionController The section controller requesting this information.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param animated Pass `YES` to animate the change, `NO` otherwise.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
*/
|
|
|
|
|
|
- (void)deselectItemAtIndex:(NSInteger)index
|
2017-04-19 15:17:56 +00:00
|
|
|
|
sectionController:(IGListSectionController *)sectionController
|
2016-09-07 22:37:59 +00:00
|
|
|
|
animated:(BOOL)animated;
|
|
|
|
|
|
|
2017-08-04 22:16:11 +00:00
|
|
|
|
/**
|
|
|
|
|
|
Selects a cell in the collection.
|
|
|
|
|
|
|
|
|
|
|
|
@param index The index of the item to select.
|
|
|
|
|
|
@param sectionController The section controller requesting this information.
|
|
|
|
|
|
@param animated Pass `YES` to animate the change, `NO` otherwise.
|
|
|
|
|
|
@param scrollPosition An option that specifies where the item should be positioned when scrolling finishes.
|
|
|
|
|
|
*/
|
|
|
|
|
|
- (void)selectItemAtIndex:(NSInteger)index
|
|
|
|
|
|
sectionController:(IGListSectionController *)sectionController
|
|
|
|
|
|
animated:(BOOL)animated
|
|
|
|
|
|
scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
|
|
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Dequeues a cell from the collection view reuse pool.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param cellClass The class of the cell you want to dequeue.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
@param sectionController The section controller requesting this information.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param index The index of the cell.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
@return A cell dequeued from the reuse pool or a newly created one.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
|
|
|
|
|
@note This method uses a string representation of the cell class as the identifier.
|
|
|
|
|
|
*/
|
|
|
|
|
|
- (__kindof UICollectionViewCell *)dequeueReusableCellOfClass:(Class)cellClass
|
2017-04-19 15:17:56 +00:00
|
|
|
|
forSectionController:(IGListSectionController *)sectionController
|
2016-09-07 22:37:59 +00:00
|
|
|
|
atIndex:(NSInteger)index;
|
|
|
|
|
|
|
2016-10-15 01:46:16 +00:00
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Dequeues a cell from the collection view reuse pool.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param nibName The name of the nib file.
|
|
|
|
|
|
@param bundle The bundle in which to search for the nib file. If `nil`, this method searches the main bundle.
|
2016-10-15 01:46:16 +00:00
|
|
|
|
@param sectionController The section controller requesting this information.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param index The index of the cell.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
@return A cell dequeued from the reuse pool or a newly created one.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2016-10-15 01:46:16 +00:00
|
|
|
|
@note This method uses a string representation of the cell class as the identifier.
|
|
|
|
|
|
*/
|
|
|
|
|
|
- (__kindof UICollectionViewCell *)dequeueReusableCellWithNibName:(NSString *)nibName
|
|
|
|
|
|
bundle:(nullable NSBundle *)bundle
|
2017-04-19 15:17:56 +00:00
|
|
|
|
forSectionController:(IGListSectionController *)sectionController
|
2016-10-15 01:46:16 +00:00
|
|
|
|
atIndex:(NSInteger)index;
|
|
|
|
|
|
|
2016-10-25 22:17:45 +00:00
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Dequeues a storyboard prototype cell from the collection view reuse pool.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param identifier The identifier of the cell prototype in storyboard.
|
2016-10-25 22:17:45 +00:00
|
|
|
|
@param sectionController The section controller requesting this information.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param index The index of the cell.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
@return A cell dequeued from the reuse pool or a newly created one.
|
2016-10-25 22:17:45 +00:00
|
|
|
|
*/
|
|
|
|
|
|
- (__kindof UICollectionViewCell *)dequeueReusableCellFromStoryboardWithIdentifier:(NSString *)identifier
|
2017-04-19 15:17:56 +00:00
|
|
|
|
forSectionController:(IGListSectionController *)sectionController
|
2016-10-25 22:17:45 +00:00
|
|
|
|
atIndex:(NSInteger)index;
|
|
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Dequeues a supplementary view from the collection view reuse pool.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param elementKind The kind of supplementary view.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
@param sectionController The section controller requesting this information.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param viewClass The class of the supplementary view.
|
|
|
|
|
|
@param index The index of the supplementary view.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
@return A supplementary view dequeued from the reuse pool or a newly created one.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
|
|
|
|
|
@note This method uses a string representation of the view class as the identifier.
|
|
|
|
|
|
*/
|
|
|
|
|
|
- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind
|
2017-04-19 15:17:56 +00:00
|
|
|
|
forSectionController:(IGListSectionController *)sectionController
|
2016-09-07 22:37:59 +00:00
|
|
|
|
class:(Class)viewClass
|
|
|
|
|
|
atIndex:(NSInteger)index;
|
|
|
|
|
|
|
2016-11-01 17:56:06 +00:00
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Dequeues a supplementary view from the collection view reuse pool.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param elementKind The kind of supplementary view.
|
|
|
|
|
|
@param identifier The identifier of the supplementary view in storyboard.
|
2016-11-01 17:56:06 +00:00
|
|
|
|
@param sectionController The section controller requesting this information.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param index The index of the supplementary view.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
@return A supplementary view dequeued from the reuse pool or a newly created one.
|
2016-11-01 17:56:06 +00:00
|
|
|
|
*/
|
|
|
|
|
|
- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewFromStoryboardOfKind:(NSString *)elementKind
|
|
|
|
|
|
withIdentifier:(NSString *)identifier
|
2017-04-19 15:17:56 +00:00
|
|
|
|
forSectionController:(IGListSectionController *)sectionController
|
2016-11-01 17:56:06 +00:00
|
|
|
|
atIndex:(NSInteger)index;
|
2016-11-05 23:27:32 +00:00
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Dequeues a supplementary view from the collection view reuse pool.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param elementKind The kind of supplementary view.
|
2016-11-05 23:27:32 +00:00
|
|
|
|
@param sectionController The section controller requesting this information.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param nibName The name of the nib file.
|
|
|
|
|
|
@param bundle The bundle in which to search for the nib file. If `nil`, this method searches the main bundle.
|
|
|
|
|
|
@param index The index of the supplementary view.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
@return A supplementary view dequeued from the reuse pool or a newly created one.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
2016-11-05 23:27:32 +00:00
|
|
|
|
@note This method uses a string representation of the view class as the identifier.
|
|
|
|
|
|
*/
|
|
|
|
|
|
- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind
|
2017-04-19 15:17:56 +00:00
|
|
|
|
forSectionController:(IGListSectionController *)sectionController
|
2016-11-05 23:27:32 +00:00
|
|
|
|
nibName:(NSString *)nibName
|
|
|
|
|
|
bundle:(nullable NSBundle *)bundle
|
|
|
|
|
|
atIndex:(NSInteger)index;
|
2016-11-01 17:56:06 +00:00
|
|
|
|
|
2017-02-21 23:27:26 +00:00
|
|
|
|
/**
|
|
|
|
|
|
Invalidate the backing `UICollectionViewLayout` for all items in the section controller.
|
|
|
|
|
|
|
|
|
|
|
|
@param sectionController The section controller that needs invalidating.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param completion An optional completion block to execute when the updates are finished.
|
2017-02-21 23:27:26 +00:00
|
|
|
|
|
|
|
|
|
|
@note This method can be wrapped in `UIView` animation APIs to control the duration or perform without animations. This
|
2017-03-31 19:59:23 +00:00
|
|
|
|
will end up calling `-[UICollectionView performBatchUpdates:completion:]` internally, so invalidated changes may not be
|
2017-02-21 23:27:26 +00:00
|
|
|
|
reflected in the cells immediately.
|
|
|
|
|
|
*/
|
2017-04-19 15:17:56 +00:00
|
|
|
|
- (void)invalidateLayoutForSectionController:(IGListSectionController *)sectionController
|
2017-02-21 23:27:26 +00:00
|
|
|
|
completion:(nullable void (^)(BOOL finished))completion;
|
|
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
|
/**
|
2016-11-15 19:32:54 +00:00
|
|
|
|
Batches and performs many cell-level updates in a single transaction.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param animated A flag indicating if the transition should be animated.
|
|
|
|
|
|
@param updates A block with a context parameter to make mutations.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
@param completion An optional completion block to execute when the updates are finished.
|
|
|
|
|
|
|
2017-03-22 19:40:51 +00:00
|
|
|
|
@note You should make state changes that impact the number of items in your section controller within the updates
|
|
|
|
|
|
block alongside changes on the context object.
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
For example, inside your section controllers, you may want to delete *and* insert into the data source that backs your
|
|
|
|
|
|
section controller. For example:
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
```
|
2017-03-22 19:40:51 +00:00
|
|
|
|
[self.collectionContext performBatchItemUpdates:^ (id<IGListBatchContext> batchContext>){
|
|
|
|
|
|
// perform data source changes inside the update block
|
|
|
|
|
|
[self.items addObject:newItem];
|
|
|
|
|
|
[self.items removeObjectAtIndex:0];
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2017-03-22 19:40:51 +00:00
|
|
|
|
NSIndexSet *inserts = [NSIndexSet indexSetWithIndex:[self.items count] - 1];
|
|
|
|
|
|
[batchContext insertInSectionController:self atIndexes:inserts];
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2017-03-22 19:40:51 +00:00
|
|
|
|
NSIndexSet *deletes = [NSIndexSet indexSetWithIndex:0];
|
|
|
|
|
|
[batchContext deleteInSectionController:self atIndexes:deletes];
|
2016-09-07 22:37:59 +00:00
|
|
|
|
} completion:nil];
|
2016-11-15 19:32:54 +00:00
|
|
|
|
```
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2016-11-15 19:32:54 +00:00
|
|
|
|
@warning You **must** perform data modifications **inside** the update block. Updates will not be performed
|
2016-09-07 22:37:59 +00:00
|
|
|
|
synchronously, so you should make sure that your data source changes only when necessary.
|
|
|
|
|
|
*/
|
2017-03-22 19:40:51 +00:00
|
|
|
|
- (void)performBatchAnimated:(BOOL)animated
|
|
|
|
|
|
updates:(void (^)(id<IGListBatchContext> batchContext))updates
|
|
|
|
|
|
completion:(nullable void (^)(BOOL finished))completion;
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
2016-12-02 00:01:06 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
Scrolls to the specified section controller in the list.
|
|
|
|
|
|
|
|
|
|
|
|
@param sectionController The section controller.
|
2017-04-18 16:30:53 +00:00
|
|
|
|
@param index The index of the item in the section controller to which to scroll.
|
|
|
|
|
|
@param scrollPosition An option that specifies where the item should be positioned when scrolling finishes.
|
|
|
|
|
|
@param animated A flag indicating if the scrolling should be animated.
|
2016-12-02 00:01:06 +00:00
|
|
|
|
*/
|
2017-04-19 15:17:56 +00:00
|
|
|
|
- (void)scrollToSectionController:(IGListSectionController *)sectionController
|
2016-12-02 00:01:06 +00:00
|
|
|
|
atIndex:(NSInteger)index
|
|
|
|
|
|
scrollPosition:(UICollectionViewScrollPosition)scrollPosition
|
|
|
|
|
|
animated:(BOOL)animated;
|
|
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|