Make layoutOptions readonly

This commit is contained in:
rcancro 2015-08-28 20:41:40 -07:00
parent 2a8b20b102
commit fca43a651f
5 changed files with 38 additions and 29 deletions

View File

@ -24,6 +24,14 @@
- (instancetype)initWithLayoutable:(id<ASLayoutable>)layoutable;
- (void)setValuesFromLayoutable:(id<ASLayoutable>)layoutable;
#pragma mark - Subclasses should implement these!
+ (NSSet *)keyPathsForValuesAffectingChangeMonitor;
- (void)setupDefaults;
- (instancetype)copyWithZone:(NSZone *)zone;
- (void)copyIntoOptions:(ASLayoutOptions *)layoutOptions;
#pragma mark - Mutability checks
@property (nonatomic, assign) BOOL isMutable;
#if DEBUG
@ -49,6 +57,5 @@
@property (nonatomic, readwrite) ASRelativeSizeRange sizeRange;
@property (nonatomic, readwrite) CGPoint layoutPosition;
- (void)setupDefaults;
@end

View File

@ -13,7 +13,6 @@
#import <AsyncDisplayKit/ASAssert.h>
#import <AsyncDisplayKit/ASTextNode.h>
#import "ASInternalHelpers.h"
#import <objc/runtime.h>
@implementation ASLayoutOptions
@ -35,6 +34,10 @@ static Class gDefaultLayoutOptionsClass = nil;
return gDefaultLayoutOptionsClass;
}
- (instancetype)init
{
return [self initWithLayoutable:nil];
}
- (instancetype)initWithLayoutable:(id<ASLayoutable>)layoutable;
{
@ -42,8 +45,10 @@ static Class gDefaultLayoutOptionsClass = nil;
if (self) {
[self setupDefaults];
[self setValuesFromLayoutable:layoutable];
_isMutable = YES;
#if DEBUG
[self addObserver:self forKeyPath:@"changeMonitor"
[self addObserver:self
forKeyPath:@"changeMonitor"
options:NSKeyValueObservingOptionNew
context:nil];
#endif
@ -51,21 +56,20 @@ static Class gDefaultLayoutOptionsClass = nil;
return self;
}
- (void)dealloc
{
#if DEBUG
[self removeObserver:self forKeyPath:@"changeMonitor"];
#endif
}
#if DEBUG
+ (NSSet *)keyPathsForValuesAffectingChangeMonitor
{
NSMutableSet *keys = [NSMutableSet set];
unsigned int count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (size_t i = 0; i < count; ++i) {
NSString *property = [NSString stringWithCString:property_getName(properties[i]) encoding:NSASCIIStringEncoding];
if ([property isEqualToString: @"observableSelf"] == NO) {
[keys addObject: property];
}
}
free(properties);
[keys addObjectsFromArray:@[@"spacingBefore", @"spacingAfter", @"flexGrow", @"flexShrink", @"flexBasis", @"alignSelf"]];
[keys addObjectsFromArray:@[@"ascender", @"descender"]];
[keys addObjectsFromArray:@[@"sizeRange", @"layoutPosition"]];
return keys;
}
@ -89,7 +93,12 @@ static Class gDefaultLayoutOptionsClass = nil;
- (id)copyWithZone:(NSZone *)zone
{
ASLayoutOptions *copy = [[[self class] alloc] init];
[self copyIntoOptions:copy];
return copy;
}
- (void)copyIntoOptions:(ASLayoutOptions *)copy
{
copy.flexBasis = self.flexBasis;
copy.spacingAfter = self.spacingAfter;
copy.spacingBefore = self.spacingBefore;
@ -101,10 +110,9 @@ static Class gDefaultLayoutOptionsClass = nil;
copy.sizeRange = self.sizeRange;
copy.layoutPosition = self.layoutPosition;
return copy;
}
#pragma mark - Defaults
- (void)setupDefaults
{

View File

@ -17,17 +17,12 @@
{\
dispatch_once(&_layoutOptionsInitializeToken, ^{\
if (_layoutOptions == nil) {\
_layoutOptions = [[[ASLayoutOptions defaultLayoutOptionsClass] alloc] init];\
_layoutOptions = [[[ASLayoutOptions defaultLayoutOptionsClass] alloc] initWithLayoutable:self];\
}\
});\
return _layoutOptions;\
}\
\
- (void)setLayoutOptions:(ASLayoutOptions *)layoutOptions\
{\
_layoutOptions = layoutOptions;\
}\
\
- (CGFloat)spacingBefore\
{\
return self.layoutOptions.spacingBefore;\

View File

@ -66,9 +66,8 @@ static NSString * const kDefaultChildrenKey = @"kDefaultChildrenKey";
id<ASLayoutable> finalLayoutable = [child finalLayoutableWithParent:self];
if (finalLayoutable) {
ASLayoutOptions *finalLayoutOptions = [layoutOptions copy];
finalLayoutOptions.isMutable = NO;
finalLayoutable.layoutOptions = finalLayoutOptions;
[layoutOptions copyIntoOptions:finalLayoutable.layoutOptions];
finalLayoutable.layoutOptions.isMutable = NO;
return finalLayoutable;
}
return child;

View File

@ -15,5 +15,5 @@
@protocol ASLayoutablePrivate <NSObject>
- (ASLayoutSpec *)finalLayoutableWithParent:(ASLayoutSpec *)parentSpec;
@property (nonatomic, strong) ASLayoutOptions *layoutOptions;
@property (nonatomic, strong, readonly) ASLayoutOptions *layoutOptions;
@end