Swiftgram/AsyncDisplayKit/Details/ASRangeController.h
Nadine Salter 7dd94a6102 Merge in downstream changes.
Introduce `ASTableView`, a UITableView subclass that uses `ASCellNode`
instead of UITableViewCell.  Add working range support via
`ASRangeController`, which observes the visible range, maintains a
working range, and handles most ASDK machinery.  ASRangeController is
loosely-enough coupled that it should be easily adapted to
UICollectionView if that's desired in the future.

Notable considerations in the ASRangeController architecture:

* There's no sense rewriting UITableView -- the real win comes from
  using nodes instead of UITableViewCells (easily parallelisable
  computation, large number of cells vs. few table views, etc.).  So,
  use a UITableView with empty cells, using UITableViewCell's
  contentView as a host for arbitrary node hierarchies.

* Instead of lazy-loading cells the instant they're needed by
  UITableView, load them in advance.  Preload a substantial number of
  nodes in the direction of scroll, as well as a small buffer in the
  other direction.

* Maintain compatibility with UITableView's API, with one primary change
  -- consumer code yields configured ASCellNodes, not UITableViewCells.

* Don't use -tableView:heightForRowAtIndexPath:.  Nodes already compute
  their preferred sizes and cache results for use at layout-time, so
  ASTableView uses their calculatedSizes directly.

* Corollary:  ASTableView is only aware of nodes that have been sized.
  This means that, if a cell appears onscreen, it has layout data and
  can display a "realistic placeholder", e.g. by making its subnodes'
  background colour grey.

Other improvements:

* Remove dead references and update headers (fixes #7, #20).

* Rename `-[ASDisplayNode sizeToFit:]` to `-measure:` and fix
  `constrainedSizeForCalulatedSize` typo (fixes #15).

* Rename `-willAppear` and `-didDisappear` to `-willEnterHierarchy` and
  `-didExitHierarchy`.  Remove `-willDisappear` -- it was redundant, and
  there was no counterpart `-didAppear`.

* Rename `viewLoaded` to `nodeLoaded`.
2014-09-22 14:33:39 -07:00

118 lines
4.1 KiB
Objective-C

/* Copyright (c) 2014-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 <AsyncDisplayKit/ASCellNode.h>
typedef struct {
// working range buffers, on either side of scroll
NSInteger trailingBufferScreenfuls;
NSInteger leadingBufferScreenfuls;
} ASRangeTuningParameters;
@protocol ASRangeControllerDelegate;
/**
* Working range controller.
*
* Used internally by ASTableView and potentially by a future ASCollectionView. Observes the visible range, maintains
* a working range, and is responsible for handling AsyncDisplayKit machinery (sizing cell nodes, enqueueing and
* cancelling their asynchronous layout and display, and so on).
*/
@interface ASRangeController : NSObject
/**
* Notify the receiver that its delegate's data source has been set or changed. This is like -[UITableView reloadData]
* but drastically more expensive, as it destroys the working range and all cached nodes.
*/
- (void)rebuildData;
/**
* Notify the receiver that the visible range has been updated.
*
* @see -rangeControllerVisibleNodeIndexPaths:
*/
- (void)visibleNodeIndexPathsDidChange;
/**
* ASTableView is only aware of nodes that have already been sized.
*
* Custom ASCellNode implementations are encouraged to have "realistic placeholders", since they can only be onscreen if
* they have enough data for layout. E.g., try setting all subnodes' background colours to [UIColor lightGrayColor].
*/
- (NSInteger)numberOfSizedSections;
- (NSInteger)numberOfSizedRowsInSection:(NSInteger)section;
/**
* Add the sized node for `indexPath` as a subview of `contentView`.
*/
- (void)configureContentView:(UIView *)contentView forIndexPath:(NSIndexPath *)indexPath;
/**
* Query the sized node at `indexPath` for its calculatedSize.
*
* TODO: Currently we disallow direct access to ASCellNode outside ASRangeController since touching the node's view can
* break async display. We should expose the node anyway, possibly with an assertion guarding against external
* use of the view property, so ASCellNode can support configuration for UITableViewCell properties (selection
* style, separator style, etc.) and ASTableView can query that data.
*/
- (CGSize)calculatedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath;
/**
* Notify the receiver that its data source has been updated to append the specified nodes.
*/
- (void)appendNodesWithIndexPaths:(NSArray *)indexPaths;
/**
* Delegate and ultimate data source. Must not be nil.
*/
@property (nonatomic, weak) id<ASRangeControllerDelegate> delegate;
/**
* Tuning parameters for the working range.
*
* Defaults to a trailing buffer of one screenful and a leading buffer of two screenfuls.
*/
@property (nonatomic, assign) ASRangeTuningParameters tuningParameters;
@end
@protocol ASRangeControllerDelegate <NSObject>
/**
* @returns an array of index paths corresponding to the nodes currently visible onscreen (i.e., the visible range).
*/
- (NSArray *)rangeControllerVisibleNodeIndexPaths:(ASRangeController *)rangeController;
/**
* @returns the receiver's viewport size (i.e., the screen space occupied by the visible range).
*/
- (CGSize)rangeControllerViewportSize:(ASRangeController *)rangeController;
/**
* Forwarded to ASTableView's data source.
*/
- (NSInteger)rangeControllerSections:(ASRangeController *)rangeController;
- (NSInteger)rangeController:(ASRangeController *)rangeController rowsInSection:(NSInteger)section;
- (ASCellNode *)rangeController:(ASRangeController *)rangeController nodeForIndexPath:(NSIndexPath *)indexPath;
/**
* @returns sizing constraints for the node at `indexPath`.
*/
- (CGSize)rangeController:(ASRangeController *)rangeController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath;
/**
* Notifies the receiver that the specified nodes have been sized and are ready for display.
*/
- (void)rangeController:(ASRangeController *)rangeController didSizeNodesWithIndexPaths:(NSArray *)indexPaths;
@end