Repository navigation
Expand file tree
/
Copy pathASDataController.h
More file actions
275 lines (224 loc) · 10.4 KB
/
Copy pathASDataController.h
File metadata and controls
275 lines (224 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// ASDataController.h
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#pragma once
#import <UIKit/UIKit.h>
#import <AsyncDisplayKit/ASBlockTypes.h>
#import <AsyncDisplayKit/ASDimension.h>
#ifdef __cplusplus
#import <vector>
#endif
NS_ASSUME_NONNULL_BEGIN
@class ASCellNode;
@class ASCollectionElement;
@class ASCollectionLayoutContext;
@class ASCollectionLayoutState;
@class ASDataController;
@class ASElementMap;
@class ASLayout;
@class _ASHierarchyChangeSet;
@protocol ASRangeManagingNode;
@protocol ASTraitEnvironment;
@protocol ASSectionContext;
typedef NSUInteger ASDataControllerAnimationOptions;
ASDK_EXTERN NSString * const ASDataControllerRowNodeKind;
ASDK_EXTERN NSString * const ASCollectionInvalidUpdateException;
/**
Data source for data controller
It will be invoked in the same thread as the api call of ASDataController.
*/
@protocol ASDataControllerSource <NSObject>
/**
Fetch the ASCellNode block for specific index path. This block should return the ASCellNode for the specified index path.
*/
- (ASCellNodeBlock)dataController:(ASDataController *)dataController nodeBlockAtIndexPath:(NSIndexPath *)indexPath shouldAsyncLayout:(BOOL *)shouldAsyncLayout;
/**
Fetch the number of rows in specific section.
*/
- (NSUInteger)dataController:(ASDataController *)dataController rowsInSection:(NSUInteger)section;
/**
Fetch the number of sections.
*/
- (NSUInteger)numberOfSectionsInDataController:(ASDataController *)dataController;
/**
Returns if the collection element size matches a given size.
@precondition The element is present in the data controller's visible map.
*/
- (BOOL)dataController:(ASDataController *)dataController presentedSizeForElement:(ASCollectionElement *)element matchesSize:(CGSize)size;
- (nullable id)dataController:(ASDataController *)dataController nodeModelForItemAtIndexPath:(NSIndexPath *)indexPath;
/**
* Called just after dispatching ASCellNode allocation and layout to the concurrent background queue.
* In some cases, for example on the first content load for a screen, it may be desirable to call
* -waitUntilAllUpdatesAreProcessed at this point.
*
* Returning YES will cause the ASDataController to wait on the background queue, and this ensures
* that any new / changed cells are in the hierarchy by the very next CATransaction / frame draw.
*/
- (BOOL)dataController:(ASDataController *)dataController shouldSynchronouslyProcessChangeSet:(_ASHierarchyChangeSet *)changeSet;
- (BOOL)dataController:(ASDataController *)dataController shouldEagerlyLayoutNode:(ASCellNode *)node;
- (BOOL)dataControllerShouldSerializeNodeCreation:(ASDataController *)dataController;
@optional
/**
The constrained size range for layout. Called only if collection layout delegate is not provided.
*/
- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath;
- (NSArray<NSString *> *)dataController:(ASDataController *)dataController supplementaryNodeKindsInSections:(NSIndexSet *)sections;
- (NSUInteger)dataController:(ASDataController *)dataController supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section;
- (ASCellNodeBlock)dataController:(ASDataController *)dataController supplementaryNodeBlockOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath shouldAsyncLayout:(BOOL *)shouldAsyncLayout;
/**
The constrained size range for layout. Called only if no data controller layout delegate is provided.
*/
- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
- (nullable id<ASSectionContext>)dataController:(ASDataController *)dataController contextForSection:(NSInteger)section;
@end
/**
Delegate for notify the data updating of data controller.
These methods will be invoked from main thread right now, but it may be moved to background thread in the future.
*/
@protocol ASDataControllerDelegate <NSObject>
/**
* Called for change set updates.
*
* @param changeSet The change set that includes all updates
*
* @param updates The block that performs relevant data updates.
*
* @discussion The updates block must always be executed or the data controller will get into a bad state.
* It should be called at the time the backing view is ready to process the updates,
* i.e inside the updates block of `-[UICollectionView performBatchUpdates:completion:] or after calling `-[UITableView beginUpdates]`.
*/
- (void)dataController:(ASDataController *)dataController updateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates;
@end
@protocol ASDataControllerLayoutDelegate <NSObject>
/**
* @abstract Returns a layout context needed for a coming layout pass with the given elements.
* The context should contain the elements and any additional information needed.
*
* @discussion This method will be called on main thread.
*/
- (ASCollectionLayoutContext *)layoutContextWithElements:(ASElementMap *)elements;
/**
* @abstract Prepares and returns a new layout for given context.
*
* @param context A context that was previously returned by `-layoutContextWithElements:`.
*
* @return The new layout calculated for the given context.
*
* @discussion This method is called ahead of time, i.e before the underlying collection/table view is aware of the provided elements.
* As a result, clients must solely rely on the given context and should not reach out to other objects for information not available in the context.
*
* This method will be called on background theads. It must be thread-safe and should not change any internal state of the conforming object.
* It must block the calling thread but can dispatch to other theads to reduce total blocking time.
*/
+ (ASCollectionLayoutState *)calculateLayoutWithContext:(ASCollectionLayoutContext *)context;
@end
/**
* Controller to layout data in background, and managed data updating.
*
* All operations are asynchronous and thread safe. You can call it from background thread (it is recommendated) and the data
* will be updated asynchronously. The dataSource must be updated to reflect the changes before these methods has been called.
* For each data updating, the corresponding methods in delegate will be called.
*/
@interface ASDataController : NSObject
- (instancetype)initWithDataSource:(id<ASDataControllerSource>)dataSource node:(nullable id<ASRangeManagingNode>)node NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
/**
* The node that owns this data controller, if any.
*
* NOTE: Soon we will drop support for using ASTableView/ASCollectionView without the node, so this will be non-null.
*/
@property (nullable, nonatomic, weak, readonly) id<ASRangeManagingNode> node;
/**
* The map that is currently displayed. The "UIKit index space."
*
* This property will only be changed on the main thread.
*/
@property (copy, readonly) ASElementMap *visibleMap;
/**
* The latest map fetched from the data source. May be more recent than @c visibleMap.
*
* This property will only be changed on the main thread.
*/
@property (copy, readonly) ASElementMap *pendingMap;
/**
Data source for fetching data info.
*/
@property (nonatomic, weak, readonly) id<ASDataControllerSource> dataSource;
/**
An object that will be included in the backtrace of any update validation exceptions that occur.
*/
@property (nonatomic, weak) id validationErrorSource;
/**
Delegate to notify when data is updated.
*/
@property (nonatomic, weak) id<ASDataControllerDelegate> delegate;
/**
* Delegate for preparing layouts. Main thead only.
*/
@property (nonatomic, weak) id<ASDataControllerLayoutDelegate> layoutDelegate;
#ifdef __cplusplus
/**
* Returns the most recently gathered item counts from the data source. If the counts
* have been invalidated, this synchronously queries the data source and saves the result.
*
* This must be called on the main thread.
*/
- (std::vector<NSInteger>)itemCountsFromDataSource;
#endif
/**
* Returns YES if reloadData has been called at least once. Before this point it is
* important to ignore/suppress some operations. For example, inserting a section
* before the initial data load should have no effect.
*
* This must be called on the main thread.
*/
@property (nonatomic, readonly) BOOL initialReloadDataHasBeenCalled;
/** @name Data Updating */
- (void)updateWithChangeSet:(_ASHierarchyChangeSet *)changeSet;
/**
* Re-measures all loaded nodes in the backing store.
*
* @discussion Used to respond to a change in size of the containing view
* (e.g. ASTableView or ASCollectionView after an orientation change).
*
* The invalidationBlock is called after flushing the ASMainSerialQueue, which ensures that any in-progress
* layout calculations have been applied. The block will not be called if data hasn't been loaded.
*/
- (void)relayoutAllNodesWithInvalidationBlock:(nullable void (^)(void))invalidationBlock;
/**
* Re-measures given nodes in the backing store.
*
* @discussion Used to respond to setNeedsLayout calls in ASCellNode
*/
- (void)relayoutNodes:(id<NSFastEnumeration>)nodes nodesSizeChanged:(NSMutableArray<ASCellNode *> *)nodesSizesChanged;
/**
* See ASCollectionNode.h for full documentation of these methods.
*/
@property (nonatomic, readonly) BOOL isProcessingUpdates;
- (void)onDidFinishProcessingUpdates:(void (^)(void))completion;
- (void)waitUntilAllUpdatesAreProcessed;
/**
* See ASCollectionNode.h for full documentation of these methods.
*/
@property (nonatomic, readonly, getter=isSynchronized) BOOL synchronized;
- (void)onDidFinishSynchronizing:(void (^)(void))completion;
/**
* Notifies the data controller object that its environment has changed. The object will request its environment delegate for new information
* and propagate the information to all visible elements, including ones that are being prepared in background.
*
* @discussion If called before the initial @c reloadData, this method will do nothing and the trait collection of the initial load will be requested from the environment delegate.
*
* @discussion This method can be called on any threads.
*/
- (void)environmentDidChange;
/**
* Reset visibleMap and pendingMap when asyncDataSource and asyncDelegate of collection view become nil.
*/
- (void)clearData;
@end
NS_ASSUME_NONNULL_END