IGListKit/Source/IGListBindingSectionController.h
Ryan Nystrom 79348164a7 Auto diffing section controller
Summary:
Started work on the plane to get this moving since #418  is up and ready to land. We'll likely need to spend some time fleshing out the API of this, and I think I'll split it up into a couple different PRs once ready for review. Putting this up now to get early feedback.

This adds an auto-diffing section controller as outlined in #38. There are several key parts:

- Subclass a new section controller `IGListAutoSectionController` (naming wip)
- Connect a data source
- Implement the data source methods that do 3 things:
  - Given a top-level object, transform it into an array of **diffable** view models
  - Given a view model, return a cell
  - Given a view model, return a size for a cell
- A new protocol for the cell `IGListBindable` so that we can control when the cell is updated w/ the view model.
  - The most important part of this is that it unlocks moving and reloading a cell, which you can't do w/ `UICollectionView`

- [ ] Unit test `reloadObjects:`
- [x] Add
Closes https://github.com/Instagram/IGListKit/pull/494

Reviewed By: amonshiz

Differential Revision: D4696966

Pulled By: rnystrom

fbshipit-source-id: f21b8341b3ed4389f2a4a106d0d316f481ba6943
2017-03-15 07:47:24 -07:00

78 lines
2.8 KiB
Objective-C

/**
* 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 <Foundation/Foundation.h>
#import <IGListKit/IGListMacros.h>
#import <IGListKit/IGListSectionType.h>
#import <IGListKit/IGListSectionController.h>
#import <IGListKit/IGListBindingSectionControllerSelectionDelegate.h>
#import <IGListKit/IGListBindingSectionControllerDataSource.h>
@protocol IGListDiffable;
@protocol IGListBindable;
@class IGListBindingSectionController;
NS_ASSUME_NONNULL_BEGIN
/**
This section controller uses a data source to transform its "top level" object into an array of diffable view models.
It then automatically binds each view model to cells via the `IGListBindable` protocol.
Models used with `IGListBindingSectionController` should take special care to always return `YES` for identical
objects. That is, any objects with matching `-diffIdentifier`s should always be equal, that way the section controller
can create new view models via the data source, create a diff, and update the specific cells that have changed.
In Objective-C, your `-isEqualToDiffableObject:` can simply be:
```
- (BOOL)isEqualToDiffableObject:(id)object {
return YES;
}
```
In Swift:
```
func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
return true
}
```
Only when `-diffIdentifier`s match is object equality compared, so you can assume the class is the same, and the
instance has already been checked.
*/
@interface IGListBindingSectionController : IGListSectionController<IGListSectionType>
/**
A data source that transforms a top-level object into view models, and returns cells and sizes for given view models.
*/
@property (nonatomic, weak, nullable) id<IGListBindingSectionControllerDataSource> dataSource;
/**
A delegate that receives selection events from cells in an `IGListBindingSectionController` instance.
*/
@property (nonatomic, weak, nullable) id<IGListBindingSectionControllerSelectionDelegate> selectionDelegate;
/**
The array of view models created from the data source. Values are changed when the top-level object changes or by
calling `-updateAnimated:completion:` manually.
*/
@property (nonatomic, strong, readonly) NSArray<id<IGListDiffable>> *viewModels;
/**
Tells the section controller to query for new view models, diff the changes, and update its cells.
@param animated A flag indicating if the transition should be animated or not.
@param completion An optional completion block executed after updates finish. Parameter is YES if updates were applied.
*/
- (void)updateAnimated:(BOOL)animated completion:(nullable void (^)(BOOL updated))completion;
@end
NS_ASSUME_NONNULL_END