-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathASCellNode.mm
489 lines (406 loc) · 13.8 KB
/
ASCellNode.mm
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//
// ASCellNode.mm
// 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
//
#import <AsyncDisplayKit/ASCellNode+Internal.h>
#import <AsyncDisplayKit/ASEqualityHelpers.h>
#import <AsyncDisplayKit/ASInternalHelpers.h>
#import <AsyncDisplayKit/ASDisplayNode+FrameworkPrivate.h>
#import <AsyncDisplayKit/ASCollectionView+Undeprecated.h>
#import <AsyncDisplayKit/ASCollectionElement.h>
#import <AsyncDisplayKit/ASTableView+Undeprecated.h>
#import <AsyncDisplayKit/_ASDisplayView.h>
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
#import <AsyncDisplayKit/ASTextNode.h>
#import <AsyncDisplayKit/ASCollectionNode.h>
#import <AsyncDisplayKit/ASDKViewController.h>
#import <AsyncDisplayKit/ASInsetLayoutSpec.h>
#import <AsyncDisplayKit/ASDisplayNodeInternal.h>
#pragma mark -
#pragma mark ASCellNode
@interface ASCellNode ()
{
ASDisplayNodeViewControllerBlock _viewControllerBlock;
ASDisplayNodeDidLoadBlock _viewControllerDidLoadBlock;
ASDisplayNode *_viewControllerNode;
UIViewController *_viewController;
UICollectionViewLayoutAttributes *_layoutAttributes;
BOOL _suspendInteractionDelegate;
BOOL _selected;
BOOL _highlighted;
BOOL _neverShowPlaceholders;
}
@end
@implementation ASCellNode
@synthesize interactionDelegate = _interactionDelegate;
- (instancetype)init
{
if (!(self = [super init]))
return nil;
// Use UITableViewCell defaults
_selectionStyle = UITableViewCellSelectionStyleDefault;
_focusStyle = UITableViewCellFocusStyleDefault;
self.clipsToBounds = YES;
return self;
}
- (instancetype)initWithViewControllerBlock:(ASDisplayNodeViewControllerBlock)viewControllerBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{
if (!(self = [super init]))
return nil;
ASDisplayNodeAssertNotNil(viewControllerBlock, @"should initialize with a valid block that returns a UIViewController");
_viewControllerBlock = viewControllerBlock;
_viewControllerDidLoadBlock = didLoadBlock;
return self;
}
- (void)didLoad
{
[super didLoad];
if (_viewControllerBlock != nil) {
_viewController = _viewControllerBlock();
_viewControllerBlock = nil;
if ([_viewController isKindOfClass:[ASDKViewController class]]) {
ASDKViewController *asViewController = (ASDKViewController *)_viewController;
_viewControllerNode = asViewController.node;
[_viewController loadViewIfNeeded];
} else {
// Careful to avoid retain cycle
UIViewController *viewController = _viewController;
_viewControllerNode = [[ASDisplayNode alloc] initWithViewBlock:^{
return viewController.view;
}];
}
[self addSubnode:_viewControllerNode];
// Since we just loaded our node, and added _viewControllerNode as a subnode,
// _viewControllerNode must have just loaded its view, so now is an appropriate
// time to execute our didLoadBlock, if we were given one.
if (_viewControllerDidLoadBlock != nil) {
_viewControllerDidLoadBlock(self);
_viewControllerDidLoadBlock = nil;
}
}
}
- (void)layout
{
[super layout];
_viewControllerNode.frame = self.bounds;
}
- (void)_rootNodeDidInvalidateSize
{
if (_interactionDelegate != nil) {
[_interactionDelegate nodeDidInvalidateSize:self];
} else {
[super _rootNodeDidInvalidateSize];
}
}
- (void)_layoutTransitionMeasurementDidFinish
{
if (_interactionDelegate != nil) {
[_interactionDelegate nodeDidInvalidateSize:self];
} else {
[super _layoutTransitionMeasurementDidFinish];
}
}
- (BOOL)isSelected
{
return ASLockedSelf(_selected);
}
- (void)setSelected:(BOOL)selected
{
if (ASLockedSelfCompareAssign(_selected, selected)) {
if (!_suspendInteractionDelegate) {
ASPerformBlockOnMainThread(^{
[self->_interactionDelegate nodeSelectedStateDidChange:self];
});
}
}
}
- (BOOL)isHighlighted
{
return ASLockedSelf(_highlighted);
}
- (void)setHighlighted:(BOOL)highlighted
{
if (ASLockedSelfCompareAssign(_highlighted, highlighted)) {
if (!_suspendInteractionDelegate) {
ASPerformBlockOnMainThread(^{
[self->_interactionDelegate nodeHighlightedStateDidChange:self];
});
}
}
}
- (void)__setSelectedFromUIKit:(BOOL)selected
{
// Note: Race condition could mean redundant sets. Risk is low.
if (ASLockedSelf(_selected != selected)) {
_suspendInteractionDelegate = YES;
self.selected = selected;
_suspendInteractionDelegate = NO;
}
}
- (void)__setHighlightedFromUIKit:(BOOL)highlighted
{
// Note: Race condition could mean redundant sets. Risk is low.
if (ASLockedSelf(_highlighted != highlighted)) {
_suspendInteractionDelegate = YES;
self.highlighted = highlighted;
_suspendInteractionDelegate = NO;
}
}
- (BOOL)canUpdateToNodeModel:(id)nodeModel
{
return [self.nodeModel class] == [nodeModel class];
}
- (NSIndexPath *)indexPath
{
return [self.owningNode indexPathForNode:self];
}
- (UIViewController *)viewController
{
ASDisplayNodeAssertMainThread();
// Force the view to load so that we will create the
// view controller if we haven't already.
if (self.isNodeLoaded == NO) {
[self view];
}
return _viewController;
}
- (id<ASRangeManagingNode>)owningNode
{
return self.collectionElement.owningNode;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-missing-super-calls"
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
ASDisplayNodeAssertMainThread();
ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
[(_ASDisplayView *)self.view __forwardTouchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
ASDisplayNodeAssertMainThread();
ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
[(_ASDisplayView *)self.view __forwardTouchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
ASDisplayNodeAssertMainThread();
ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
[(_ASDisplayView *)self.view __forwardTouchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
ASDisplayNodeAssertMainThread();
ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
[(_ASDisplayView *)self.view __forwardTouchesCancelled:touches withEvent:event];
}
#pragma clang diagnostic pop
- (UICollectionViewLayoutAttributes *)layoutAttributes
{
return ASLockedSelf(_layoutAttributes);
}
- (void)setLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
ASDisplayNodeAssertMainThread();
if (ASLockedSelfCompareAssignObjects(_layoutAttributes, layoutAttributes)) {
if (layoutAttributes != nil) {
[self applyLayoutAttributes:layoutAttributes];
}
}
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
// To be overriden by subclasses
}
- (void)cellNodeVisibilityEvent:(ASCellNodeVisibilityEvent)event inScrollView:(UIScrollView *)scrollView withCellFrame:(CGRect)cellFrame
{
// To be overriden by subclasses
}
- (void)didEnterVisibleState
{
[super didEnterVisibleState];
if (self.neverShowPlaceholders) {
[self recursivelyEnsureDisplaySynchronously:YES];
}
[self handleVisibilityChange:YES];
}
- (void)didExitVisibleState
{
[super didExitVisibleState];
[self handleVisibilityChange:NO];
}
+ (BOOL)requestsVisibilityNotifications
{
static NSCache<Class, NSNumber *> *cache;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cache = [[NSCache alloc] init];
});
NSNumber *result = [cache objectForKey:self];
if (result == nil) {
BOOL overrides = ASSubclassOverridesSelector([ASCellNode class], self, @selector(cellNodeVisibilityEvent:inScrollView:withCellFrame:));
result = overrides ? (NSNumber *)kCFBooleanTrue : (NSNumber *)kCFBooleanFalse;
[cache setObject:result forKey:self];
}
return (result == (NSNumber *)kCFBooleanTrue);
}
- (void)handleVisibilityChange:(BOOL)isVisible
{
if ([self.class requestsVisibilityNotifications] == NO) {
return; // The work below is expensive, and only valuable for subclasses watching visibility events.
}
// NOTE: This assertion is failing in some apps and will be enabled soon.
// ASDisplayNodeAssert(self.isNodeLoaded, @"Node should be loaded in order for it to become visible or invisible. If not in this situation, we shouldn't trigger creating the view.");
UIView *view = self.view;
CGRect cellFrame = CGRectZero;
// Ensure our _scrollView is still valid before converting. It's also possible that we have already been removed from the _scrollView,
// in which case it is not valid to perform a convertRect (this actually crashes on iOS 8).
UIScrollView *scrollView = (_scrollView != nil && view.superview != nil && [view isDescendantOfView:_scrollView]) ? _scrollView : nil;
if (scrollView) {
cellFrame = [view convertRect:view.bounds toView:scrollView];
}
// If we did not convert, we'll pass along CGRectZero and a nil scrollView. The EventInvisible call is thus equivalent to
// didExitVisibileState, but is more convenient for the developer than implementing multiple methods.
[self cellNodeVisibilityEvent:isVisible ? ASCellNodeVisibilityEventVisible
: ASCellNodeVisibilityEventInvisible
inScrollView:scrollView
withCellFrame:cellFrame];
}
- (NSMutableArray<NSDictionary *> *)propertiesForDebugDescription
{
NSMutableArray *result = [super propertiesForDebugDescription];
UIScrollView *scrollView = self.scrollView;
id<ASRangeManagingNode> owningNode = self.owningNode;
if ([owningNode isKindOfClass:[ASCollectionNode class]]) {
NSIndexPath *ip = [(ASCollectionNode *)owningNode indexPathForNode:self];
if (ip != nil) {
[result addObject:@{ @"indexPath" : ip }];
}
[result addObject:@{ @"collectionNode" : owningNode }];
} else if ([owningNode isKindOfClass:[ASTableNode class]]) {
NSIndexPath *ip = [(ASTableNode *)owningNode indexPathForNode:self];
if (ip != nil) {
[result addObject:@{ @"indexPath" : ip }];
}
[result addObject:@{ @"tableNode" : owningNode }];
} else if ([scrollView isKindOfClass:[ASCollectionView class]]) {
NSIndexPath *ip = [(ASCollectionView *)scrollView indexPathForNode:self];
if (ip != nil) {
[result addObject:@{ @"indexPath" : ip }];
}
[result addObject:@{ @"collectionView" : ASObjectDescriptionMakeTiny(scrollView) }];
} else if ([scrollView isKindOfClass:[ASTableView class]]) {
NSIndexPath *ip = [(ASTableView *)scrollView indexPathForNode:self];
if (ip != nil) {
[result addObject:@{ @"indexPath" : ip }];
}
[result addObject:@{ @"tableView" : ASObjectDescriptionMakeTiny(scrollView) }];
}
return result;
}
- (NSString *)supplementaryElementKind
{
return self.collectionElement.supplementaryElementKind;
}
- (BOOL)supportsLayerBacking
{
return NO;
}
- (BOOL)shouldUseUIKitCell
{
return NO;
}
@end
#pragma mark -
#pragma mark ASWrapperCellNode
// TODO: Consider if other calls, such as willDisplayCell, should be bridged to this class.
@implementation ASWrapperCellNode : ASCellNode
- (BOOL)shouldUseUIKitCell
{
return YES;
}
@end
#pragma mark -
#pragma mark ASTextCellNode
@implementation ASTextCellNode {
NSDictionary<NSAttributedStringKey, id> *_textAttributes;
UIEdgeInsets _textInsets;
NSString *_text;
}
static const CGFloat kASTextCellNodeDefaultFontSize = 18.0f;
static const CGFloat kASTextCellNodeDefaultHorizontalPadding = 15.0f;
static const CGFloat kASTextCellNodeDefaultVerticalPadding = 11.0f;
- (instancetype)init
{
return [self initWithAttributes:[ASTextCellNode defaultTextAttributes] insets:[ASTextCellNode defaultTextInsets]];
}
- (instancetype)initWithAttributes:(NSDictionary *)textAttributes insets:(UIEdgeInsets)textInsets
{
self = [super init];
if (self) {
_textInsets = textInsets;
_textAttributes = [textAttributes copy];
_textNode = [[ASTextNode alloc] init];
self.automaticallyManagesSubnodes = YES;
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:self.textInsets child:self.textNode];
}
+ (NSDictionary *)defaultTextAttributes
{
return @{NSFontAttributeName : [UIFont systemFontOfSize:kASTextCellNodeDefaultFontSize]};
}
+ (UIEdgeInsets)defaultTextInsets
{
return UIEdgeInsetsMake(kASTextCellNodeDefaultVerticalPadding, kASTextCellNodeDefaultHorizontalPadding, kASTextCellNodeDefaultVerticalPadding, kASTextCellNodeDefaultHorizontalPadding);
}
- (NSDictionary *)textAttributes
{
return ASLockedSelf(_textAttributes);
}
- (void)setTextAttributes:(NSDictionary *)textAttributes
{
ASDisplayNodeAssertNotNil(textAttributes, @"Invalid text attributes");
ASLockScopeSelf();
if (ASCompareAssignCopy(_textAttributes, textAttributes)) {
[self locked_updateAttributedText];
}
}
- (UIEdgeInsets)textInsets
{
return ASLockedSelf(_textInsets);
}
- (void)setTextInsets:(UIEdgeInsets)textInsets
{
if (ASLockedSelfCompareAssignCustom(_textInsets, textInsets, UIEdgeInsetsEqualToEdgeInsets)) {
[self setNeedsLayout];
}
}
- (NSString *)text
{
return ASLockedSelf(_text);
}
- (void)setText:(NSString *)text
{
ASLockScopeSelf();
if (ASCompareAssignCopy(_text, text)) {
[self locked_updateAttributedText];
}
}
- (void)locked_updateAttributedText
{
if (_text == nil) {
_textNode.attributedText = nil;
return;
}
_textNode.attributedText = [[NSAttributedString alloc] initWithString:_text attributes:_textAttributes];
[self setNeedsLayout];
}
@end