Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit e5eed56

Browse files
committed
Merge pull request #1693 from rcancro/traitUpdate
[ASTraitCollection] Bug fixing/LayoutSpec propagation
2 parents 2bb3e00 + 13a35c5 commit e5eed56

28 files changed

Lines changed: 203 additions & 184 deletions

AsyncDisplayKit.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,8 @@
589589
E55D86331CA8A14000A0C26F /* ASLayoutable.mm in Sources */ = {isa = PBXBuildFile; fileRef = E55D86311CA8A14000A0C26F /* ASLayoutable.mm */; };
590590
E5711A2B1C840C81009619D4 /* ASIndexedNodeContext.h in Headers */ = {isa = PBXBuildFile; fileRef = E5711A2A1C840C81009619D4 /* ASIndexedNodeContext.h */; settings = {ATTRIBUTES = (Public, ); }; };
591591
E5711A2C1C840C81009619D4 /* ASIndexedNodeContext.h in Headers */ = {isa = PBXBuildFile; fileRef = E5711A2A1C840C81009619D4 /* ASIndexedNodeContext.h */; };
592-
E5711A2E1C840C96009619D4 /* ASIndexedNodeContext.m in Sources */ = {isa = PBXBuildFile; fileRef = E5711A2D1C840C96009619D4 /* ASIndexedNodeContext.m */; };
593-
E5711A301C840C96009619D4 /* ASIndexedNodeContext.m in Sources */ = {isa = PBXBuildFile; fileRef = E5711A2D1C840C96009619D4 /* ASIndexedNodeContext.m */; };
592+
E5711A2E1C840C96009619D4 /* ASIndexedNodeContext.mm in Sources */ = {isa = PBXBuildFile; fileRef = E5711A2D1C840C96009619D4 /* ASIndexedNodeContext.mm */; };
593+
E5711A301C840C96009619D4 /* ASIndexedNodeContext.mm in Sources */ = {isa = PBXBuildFile; fileRef = E5711A2D1C840C96009619D4 /* ASIndexedNodeContext.mm */; };
594594
/* End PBXBuildFile section */
595595

596596
/* Begin PBXContainerItemProxy section */
@@ -955,7 +955,7 @@
955955
E52405B41C8FEF16004DC8E7 /* ASLayoutTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASLayoutTransition.h; sourceTree = "<group>"; };
956956
E55D86311CA8A14000A0C26F /* ASLayoutable.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = ASLayoutable.mm; path = AsyncDisplayKit/Layout/ASLayoutable.mm; sourceTree = "<group>"; };
957957
E5711A2A1C840C81009619D4 /* ASIndexedNodeContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIndexedNodeContext.h; sourceTree = "<group>"; };
958-
E5711A2D1C840C96009619D4 /* ASIndexedNodeContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIndexedNodeContext.m; sourceTree = "<group>"; };
958+
E5711A2D1C840C96009619D4 /* ASIndexedNodeContext.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASIndexedNodeContext.mm; sourceTree = "<group>"; };
959959
EFA731F0396842FF8AB635EE /* libPods-AsyncDisplayKitTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-AsyncDisplayKitTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
960960
FB07EABBCF28656C6297BC2D /* Pods-AsyncDisplayKitTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AsyncDisplayKitTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AsyncDisplayKitTests/Pods-AsyncDisplayKitTests.debug.xcconfig"; sourceTree = "<group>"; };
961961
/* End PBXFileReference section */
@@ -1417,7 +1417,7 @@
14171417
AC026B671BD57D6F00BBC17E /* ASChangeSetDataController.h */,
14181418
AC026B681BD57D6F00BBC17E /* ASChangeSetDataController.m */,
14191419
E5711A2A1C840C81009619D4 /* ASIndexedNodeContext.h */,
1420-
E5711A2D1C840C96009619D4 /* ASIndexedNodeContext.m */,
1420+
E5711A2D1C840C96009619D4 /* ASIndexedNodeContext.mm */,
14211421
);
14221422
name = "Data Controller";
14231423
sourceTree = "<group>";
@@ -2114,7 +2114,7 @@
21142114
205F0E121B371BD7007741D0 /* ASScrollDirection.m in Sources */,
21152115
9C8898BB1C738B9800D6B02E /* ASTextKitFontSizeAdjuster.mm in Sources */,
21162116
D785F6631A74327E00291744 /* ASScrollNode.m in Sources */,
2117-
E5711A2E1C840C96009619D4 /* ASIndexedNodeContext.m in Sources */,
2117+
E5711A2E1C840C96009619D4 /* ASIndexedNodeContext.mm in Sources */,
21182118
058D0A2C195D050800B7D73C /* ASSentinel.m in Sources */,
21192119
9C8221971BA237B80037F19A /* ASStackBaselinePositionedLayout.mm in Sources */,
21202120
251B8EF81BBB3D690087C538 /* ASCollectionDataController.mm in Sources */,
@@ -2242,7 +2242,7 @@
22422242
69E100701CA89CB600D88C1B /* ASEnvironmentInternal.mm in Sources */,
22432243
254C6B891BF94F8A003EC431 /* ASTextKitRenderer+Positioning.mm in Sources */,
22442244
68355B341CB579B9001D4E68 /* ASImageNode+AnimatedImage.mm in Sources */,
2245-
E5711A301C840C96009619D4 /* ASIndexedNodeContext.m in Sources */,
2245+
E5711A301C840C96009619D4 /* ASIndexedNodeContext.mm in Sources */,
22462246
B35062511B010EFD0018CF92 /* ASDisplayNode+UIViewBridge.mm in Sources */,
22472247
B35061FC1B010EFD0018CF92 /* ASDisplayNode.mm in Sources */,
22482248
B35061FF1B010EFD0018CF92 /* ASDisplayNodeExtras.mm in Sources */,

AsyncDisplayKit/ASCollectionNode.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "ASCollectionViewLayoutFacilitatorProtocol.h"
1212
#import "ASDisplayNode+Subclasses.h"
1313
#import "ASEnvironmentInternal.h"
14+
#import "ASInternalHelpers.h"
1415
#import "ASRangeControllerUpdateRangeProtocol+Beta.h"
1516
#include <vector>
1617

AsyncDisplayKit/ASDisplayNode.mm

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ - (void)addSubnode:(ASDisplayNode *)subnode
12521252
// This call will apply our .hierarchyState to the new subnode.
12531253
// If we are a managed hierarchy, as in ASCellNode trees, it will also apply our .interfaceState.
12541254
[subnode __setSupernode:self];
1255-
1255+
12561256
if (self.nodeLoaded) {
12571257
// If this node has a view or layer, force the subnode to also create its view or layer and add it to the hierarchy here.
12581258
// Otherwise there is no way for the subnode's view or layer to enter the hierarchy, except recursing down all
@@ -1298,7 +1298,7 @@ - (void)_insertSubnode:(ASDisplayNode *)subnode atSubnodeIndex:(NSInteger)subnod
12981298
_subnodes = [[NSMutableArray alloc] init];
12991299
[_subnodes insertObject:subnode atIndex:subnodeIndex];
13001300
[subnode __setSupernode:self];
1301-
1301+
13021302
// Don't bother inserting the view/layer if in a rasterized subtree, because there are no layers in the hierarchy and none of this could possibly work.
13031303
if (!_flags.shouldRasterizeDescendants && [self __shouldLoadViewOrLayer]) {
13041304
if (_layer) {
@@ -1740,6 +1740,9 @@ - (void)__setSupernode:(ASDisplayNode *)newSupernode
17401740

17411741
[self exitHierarchyState:stateToEnterOrExit];
17421742
}
1743+
1744+
// now that we have a supernode, propagate its traits to self.
1745+
ASEnvironmentStatePropagateDown(self, [newSupernode environmentTraitCollection]);
17431746
}
17441747
}
17451748

@@ -1902,6 +1905,7 @@ - (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
19021905
if ((_methodOverrides & ASDisplayNodeMethodOverrideLayoutSpecThatFits) || _layoutSpecBlock != NULL) {
19031906
ASLayoutSpec *layoutSpec = [self layoutSpecThatFits:constrainedSize];
19041907
layoutSpec.parent = self; // This causes upward propogation of any non-default layoutable values.
1908+
layoutSpec.traitCollection = self.asyncTraitCollection;
19051909
layoutSpec.isMutable = NO;
19061910
ASLayout *layout = [layoutSpec measureWithSizeRange:constrainedSize];
19071911
// Make sure layoutableObject of the root layout is `self`, so that the flattened layout will be structurally correct.
@@ -2722,7 +2726,12 @@ - (BOOL)supportsTraitsCollectionPropagation
27222726

27232727
- (ASEnvironmentTraitCollection)environmentTraitCollection
27242728
{
2725-
return _environmentState.traitCollection;
2729+
return _environmentState.environmentTraitCollection;
2730+
}
2731+
2732+
- (void)setEnvironmentTraitCollection:(ASEnvironmentTraitCollection)environmentTraitCollection
2733+
{
2734+
_environmentState.environmentTraitCollection = environmentTraitCollection;
27262735
}
27272736

27282737
ASEnvironmentLayoutOptionsForwarding
@@ -2731,7 +2740,7 @@ - (ASEnvironmentTraitCollection)environmentTraitCollection
27312740
- (ASTraitCollection *)asyncTraitCollection
27322741
{
27332742
ASDN::MutexLocker l(_propertyLock);
2734-
return [ASTraitCollection traitCollectionWithASEnvironmentTraitCollection:_environmentState.traitCollection];
2743+
return [ASTraitCollection traitCollectionWithASEnvironmentTraitCollection:self.environmentTraitCollection];
27352744
}
27362745

27372746
#if TARGET_OS_TV

AsyncDisplayKit/ASTableNode.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
//
88

99
#import "ASEnvironmentInternal.h"
10-
#import "ASFlowLayoutController.h"
11-
#import "ASTableViewInternal.h"
1210
#import "ASDisplayNode+Subclasses.h"
11+
#import "ASFlowLayoutController.h"
12+
#import "ASInternalHelpers.h"
1313
#import "ASRangeControllerUpdateRangeProtocol+Beta.h"
14+
#import "ASTableViewInternal.h"
1415

1516
@interface _ASTablePendingState : NSObject
1617
@property (weak, nonatomic) id <ASTableDelegate> delegate;

AsyncDisplayKit/ASViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef ASTraitCollection * _Nonnull (^ASDisplayTraitsForTraitWindowSizeBlock)(C
3333
* ASVC keeps a strong reference to the context to make sure that it stays alive. If you change this value
3434
* it will propagate the change to the subnodes.
3535
*/
36-
@property (nonatomic, strong) id _Nullable traitColectionContext;
36+
@property (nonatomic, strong) id _Nullable traitCollectionContext;
3737

3838
/**
3939
* Set this block to customize the ASDisplayTraits returned when the VC transitions to the given traitCollection.

AsyncDisplayKit/ASViewController.mm

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "ASViewController.h"
1010
#import "ASAssert.h"
11+
#import "ASAvailability.h"
1112
#import "ASDimension.h"
1213
#import "ASDisplayNodeInternal.h"
1314
#import "ASDisplayNode+FrameworkPrivate.h"
@@ -55,12 +56,12 @@ - (instancetype)initWithNode:(ASDisplayNode *)node
5556

5657
- (void)dealloc
5758
{
58-
if (_traitColectionContext != nil) {
59+
if (_traitCollectionContext != nil) {
5960
// The setter will iterate through the VC's subnodes and replace the traitCollectionContext in their ASEnvironmentTraitCollection with nil.
6061
// Since the VC holds the only strong reference to this context and we are in the process of destroying
6162
// the VC, all the references in the subnodes will be unsafe unless we nil them out. More than likely all the subnodes will be dealloc'ed
6263
// as part of the VC being dealloc'ed, but this is just to make extra sure.
63-
self.traitColectionContext = nil;
64+
self.traitCollectionContext = nil;
6465
}
6566
}
6667

@@ -82,6 +83,13 @@ - (void)loadView
8283
_node.frame = frame;
8384
_node.autoresizingMask = autoresizingMask;
8485
self.view = view;
86+
87+
// ensure that self.node has a valid trait collection before a subclass's implementation of viewDidLoad.
88+
// Any subnodes added in viewDidLoad will then inherit the proper environment.
89+
if (AS_AT_LEAST_IOS8) {
90+
ASEnvironmentTraitCollection traitCollection = [self environmentTraitCollectionForUITraitCollection:self.traitCollection];
91+
[self progagateNewEnvironmentTraitCollection:traitCollection];
92+
}
8593
}
8694

8795
- (void)viewWillLayoutSubviews
@@ -187,52 +195,53 @@ - (ASInterfaceState)interfaceState
187195

188196
#pragma mark - ASEnvironmentTraitCollection
189197

190-
- (void)setTraitColectionContext:(id)traitColectionContext
198+
- (void)setTraitCollectionContext:(id)traitCollectionContext
191199
{
192-
if (_traitColectionContext != traitColectionContext) {
193-
// propagate first so that nodes aren't hanging around with a dealloc'ed pointer
194-
ASEnvironmentTraitCollectionUpdateDisplayContext(self.node, traitColectionContext);
200+
if (_traitCollectionContext != traitCollectionContext) {
201+
// nil out the displayContext in the subnodes so they aren't hanging around with a dealloc'ed pointer don't set
202+
// the new context yet as this will cause ASEnvironmentTraitCollectionIsEqualToASEnvironmentTraitCollection to fail
203+
ASEnvironmentTraitCollectionUpdateDisplayContext(self.node, nil);
195204

196-
_traitColectionContext = traitColectionContext;
205+
_traitCollectionContext = traitCollectionContext;
197206
}
198207
}
199208

200-
- (ASEnvironmentTraitCollection)displayTraitsForTraitCollection:(UITraitCollection *)traitCollection
209+
- (ASEnvironmentTraitCollection)environmentTraitCollectionForUITraitCollection:(UITraitCollection *)traitCollection
201210
{
202211
if (self.overrideDisplayTraitsWithTraitCollection) {
203212
ASTraitCollection *asyncTraitCollection = self.overrideDisplayTraitsWithTraitCollection(traitCollection);
204-
self.traitColectionContext = asyncTraitCollection.traitCollectionContext;
213+
self.traitCollectionContext = asyncTraitCollection.traitCollectionContext;
205214
return [asyncTraitCollection environmentTraitCollection];
206215
}
207216

208217
ASEnvironmentTraitCollection asyncTraitCollection = ASEnvironmentTraitCollectionFromUITraitCollection(traitCollection);
209-
asyncTraitCollection.displayContext = self.traitColectionContext;
218+
asyncTraitCollection.displayContext = self.traitCollectionContext;
210219
return asyncTraitCollection;
211220
}
212221

213-
- (ASEnvironmentTraitCollection)displayTraitsForWindowSize:(CGSize)windowSize
222+
- (ASEnvironmentTraitCollection)environmentTraitCollectionForWindowSize:(CGSize)windowSize
214223
{
215224
if (self.overrideDisplayTraitsWithWindowSize) {
216225
ASTraitCollection *traitCollection = self.overrideDisplayTraitsWithWindowSize(windowSize);
217-
self.traitColectionContext = traitCollection.traitCollectionContext;
226+
self.traitCollectionContext = traitCollection.traitCollectionContext;
218227
return [traitCollection environmentTraitCollection];
219228
}
220229
return self.node.environmentTraitCollection;
221230
}
222231

223-
- (void)progagateNewDisplayTraits:(ASEnvironmentTraitCollection)traitCollection
232+
- (void)progagateNewEnvironmentTraitCollection:(ASEnvironmentTraitCollection)environmentTraitCollection
224233
{
225234
ASEnvironmentState environmentState = self.node.environmentState;
226-
ASEnvironmentTraitCollection oldTraitCollection = environmentState.traitCollection;
235+
ASEnvironmentTraitCollection oldEnvironmentTraitCollection = environmentState.environmentTraitCollection;
227236

228-
if (ASEnvironmentTraitCollectionIsEqualToASEnvironmentTraitCollection(traitCollection, oldTraitCollection) == NO) {
229-
environmentState.traitCollection = traitCollection;
237+
if (ASEnvironmentTraitCollectionIsEqualToASEnvironmentTraitCollection(environmentTraitCollection, oldEnvironmentTraitCollection) == NO) {
238+
environmentState.environmentTraitCollection = environmentTraitCollection;
230239
self.node.environmentState = environmentState;
231240
[self.node setNeedsLayout];
232241

233242
NSArray<id<ASEnvironment>> *children = [self.node children];
234243
for (id<ASEnvironment> child in children) {
235-
ASEnvironmentStatePropagateDown(child, environmentState.traitCollection);
244+
ASEnvironmentStatePropagateDown(child, environmentState.environmentTraitCollection);
236245
}
237246
}
238247
}
@@ -241,24 +250,24 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
241250
{
242251
[super traitCollectionDidChange:previousTraitCollection];
243252

244-
ASEnvironmentTraitCollection traitCollection = [self displayTraitsForTraitCollection:self.traitCollection];
245-
[self progagateNewDisplayTraits:traitCollection];
253+
ASEnvironmentTraitCollection environmentTraitCollection = [self environmentTraitCollectionForUITraitCollection:self.traitCollection];
254+
[self progagateNewEnvironmentTraitCollection:environmentTraitCollection];
246255
}
247256

248257
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
249258
{
250259
[super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
251260

252-
ASEnvironmentTraitCollection traitCollection = [self displayTraitsForTraitCollection:newCollection];
253-
[self progagateNewDisplayTraits:traitCollection];
261+
ASEnvironmentTraitCollection environmentTraitCollection = [self environmentTraitCollectionForUITraitCollection:newCollection];
262+
[self progagateNewEnvironmentTraitCollection:environmentTraitCollection];
254263
}
255264

256265
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
257266
{
258267
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
259268

260-
ASEnvironmentTraitCollection traitCollection = [self displayTraitsForWindowSize:size];
261-
[self progagateNewDisplayTraits:traitCollection];
269+
ASEnvironmentTraitCollection environmentTraitCollection = [self environmentTraitCollectionForWindowSize:size];
270+
[self progagateNewEnvironmentTraitCollection:environmentTraitCollection];
262271
}
263272

264273
@end

AsyncDisplayKit/Details/ASCollectionDataController.mm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ - (void)willMoveSection:(NSInteger)section toSection:(NSInteger)newSection
158158

159159
- (void)_populateSupplementaryNodesOfKind:(NSString *)kind withMutableContexts:(NSMutableArray<ASIndexedNodeContext *> *)contexts
160160
{
161+
id<ASEnvironment> environment = [self.environmentDelegate dataControllerEnvironment];
162+
ASEnvironmentTraitCollection environmentTraitCollection = environment.environmentTraitCollection;
163+
161164
NSUInteger sectionCount = [self.collectionDataSource dataController:self numberOfSectionsForSupplementaryNodeOfKind:kind];
162165
for (NSUInteger i = 0; i < sectionCount; i++) {
163166
NSIndexPath *sectionIndexPath = [[NSIndexPath alloc] initWithIndex:i];
@@ -176,14 +179,18 @@ - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withMutableContexts:(
176179
ASSizeRange constrainedSize = [self constrainedSizeForNodeOfKind:kind atIndexPath:indexPath];
177180
ASIndexedNodeContext *context = [[ASIndexedNodeContext alloc] initWithNodeBlock:supplementaryCellBlock
178181
indexPath:indexPath
179-
constrainedSize:constrainedSize];
182+
constrainedSize:constrainedSize
183+
environmentTraitCollection:environmentTraitCollection];
180184
[contexts addObject:context];
181185
}
182186
}
183187
}
184188

185189
- (void)_populateSupplementaryNodesOfKind:(NSString *)kind withSections:(NSIndexSet *)sections mutableContexts:(NSMutableArray<ASIndexedNodeContext *> *)contexts
186190
{
191+
id<ASEnvironment> environment = [self.environmentDelegate dataControllerEnvironment];
192+
ASEnvironmentTraitCollection environmentTraitCollection = environment.environmentTraitCollection;
193+
187194
[sections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
188195
NSUInteger rowNum = [self.collectionDataSource dataController:self supplementaryNodesOfKind:kind inSection:idx];
189196
NSIndexPath *sectionIndex = [[NSIndexPath alloc] initWithIndex:idx];
@@ -201,7 +208,8 @@ - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withSections:(NSIndex
201208
ASSizeRange constrainedSize = [self constrainedSizeForNodeOfKind:kind atIndexPath:indexPath];
202209
ASIndexedNodeContext *context = [[ASIndexedNodeContext alloc] initWithNodeBlock:supplementaryCellBlock
203210
indexPath:indexPath
204-
constrainedSize:constrainedSize];
211+
constrainedSize:constrainedSize
212+
environmentTraitCollection:environmentTraitCollection];
205213
[contexts addObject:context];
206214
}
207215
}];

0 commit comments

Comments
 (0)