mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 06:35:51 +00:00
[ASLayoutSpec.mm] Remove use of dictionary to hold children
Converted the backing store of children to a std::vector. Subclass now have defined indexes instead of string keys to add children.
This commit is contained in:
@@ -14,7 +14,8 @@
|
|||||||
#import "ASBaseDefines.h"
|
#import "ASBaseDefines.h"
|
||||||
#import "ASLayout.h"
|
#import "ASLayout.h"
|
||||||
|
|
||||||
static NSString * const kBackgroundChildKey = @"kBackgroundChildKey";
|
static NSUInteger const kForegroundChildIndex = 0;
|
||||||
|
static NSUInteger const kBackgroundChildIndex = 1;
|
||||||
|
|
||||||
@interface ASBackgroundLayoutSpec ()
|
@interface ASBackgroundLayoutSpec ()
|
||||||
@end
|
@end
|
||||||
@@ -28,7 +29,7 @@ static NSString * const kBackgroundChildKey = @"kBackgroundChildKey";
|
|||||||
}
|
}
|
||||||
|
|
||||||
ASDisplayNodeAssertNotNil(child, @"Child cannot be nil");
|
ASDisplayNodeAssertNotNil(child, @"Child cannot be nil");
|
||||||
[self setChild:child];
|
[self setChild:child forIndex:kForegroundChildIndex];
|
||||||
self.background = background;
|
self.background = background;
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
@@ -63,12 +64,12 @@ static NSString * const kBackgroundChildKey = @"kBackgroundChildKey";
|
|||||||
|
|
||||||
- (void)setBackground:(id<ASLayoutable>)background
|
- (void)setBackground:(id<ASLayoutable>)background
|
||||||
{
|
{
|
||||||
[super setChild:background forIdentifier:kBackgroundChildKey];
|
[super setChild:background forIndex:kBackgroundChildIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id<ASLayoutable>)background
|
- (id<ASLayoutable>)background
|
||||||
{
|
{
|
||||||
return [super childForIdentifier:kBackgroundChildKey];
|
return [super childForIndex:kBackgroundChildIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -52,19 +52,19 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
*
|
*
|
||||||
* @param child A child to be added.
|
* @param child A child to be added.
|
||||||
*
|
*
|
||||||
* @param identifier An identifier associated with the child.
|
* @param index An index associated with the child.
|
||||||
*
|
*
|
||||||
* @discussion Every ASLayoutSpec must act on at least one child. The ASLayoutSpec base class takes the
|
* @discussion Every ASLayoutSpec must act on at least one child. The ASLayoutSpec base class takes the
|
||||||
* responsibility of holding on to the spec children. Some layout specs, like ASInsetLayoutSpec,
|
* responsibility of holding on to the spec children. Some layout specs, like ASInsetLayoutSpec,
|
||||||
* only require a single child.
|
* only require a single child.
|
||||||
*
|
*
|
||||||
* For layout specs that require a known number of children (ASBackgroundLayoutSpec, for example)
|
* For layout specs that require a known number of children (ASBackgroundLayoutSpec, for example)
|
||||||
* a subclass should use the setChild method to set the "primary" child. It can then use this method
|
* a subclass can use the setChild method to set the "primary" child. It should then use this method
|
||||||
* to set any other required children. Ideally a subclass would hide this from the user, and use the
|
* to set any other required children. Ideally a subclass would hide this from the user, and use the
|
||||||
* setChild:forIdentifier: internally. For example, ASBackgroundLayoutSpec exposes a backgroundChild
|
* setChild:forIndex: internally. For example, ASBackgroundLayoutSpec exposes a backgroundChild
|
||||||
* property that behind the scenes is calling setChild:forIdentifier:.
|
* property that behind the scenes is calling setChild:forIndex:.
|
||||||
*/
|
*/
|
||||||
- (void)setChild:(id<ASLayoutable>)child forIdentifier:(NSString *)identifier;
|
- (void)setChild:(id<ASLayoutable>)child forIndex:(NSUInteger)index;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds childen to this layout spec.
|
* Adds childen to this layout spec.
|
||||||
@@ -94,11 +94,11 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
- (nullable id<ASLayoutable>)child;
|
- (nullable id<ASLayoutable>)child;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the child added to this layout spec using the given identifier.
|
* Returns the child added to this layout spec using the given index.
|
||||||
*
|
*
|
||||||
* @param identifier An identifier associated withe the child.
|
* @param index An identifier associated withe the child.
|
||||||
*/
|
*/
|
||||||
- (nullable id<ASLayoutable>)childForIdentifier:(NSString *)identifier;
|
- (nullable id<ASLayoutable>)childForIndex:(NSUInteger)index;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all children added to this layout spec.
|
* Returns all children added to this layout spec.
|
||||||
|
|||||||
@@ -25,9 +25,7 @@
|
|||||||
@interface ASLayoutSpec() {
|
@interface ASLayoutSpec() {
|
||||||
ASEnvironmentState _environmentState;
|
ASEnvironmentState _environmentState;
|
||||||
ASDN::RecursiveMutex _propertyLock;
|
ASDN::RecursiveMutex _propertyLock;
|
||||||
|
std::vector<id<ASLayoutable>> _children;
|
||||||
NSArray *_children;
|
|
||||||
NSMutableDictionary *_childrenWithIdentifier;
|
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@@ -45,7 +43,6 @@
|
|||||||
}
|
}
|
||||||
_isMutable = YES;
|
_isMutable = YES;
|
||||||
_environmentState = ASEnvironmentStateMakeDefault();
|
_environmentState = ASEnvironmentStateMakeDefault();
|
||||||
_children = [NSArray array];
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,14 +99,6 @@
|
|||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSMutableDictionary *)childrenWithIdentifier
|
|
||||||
{
|
|
||||||
if (!_childrenWithIdentifier) {
|
|
||||||
_childrenWithIdentifier = [NSMutableDictionary dictionary];
|
|
||||||
}
|
|
||||||
return _childrenWithIdentifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)setParent:(id<ASLayoutable>)parent
|
- (void)setParent:(id<ASLayoutable>)parent
|
||||||
{
|
{
|
||||||
// FIXME: Locking should be evaluated here. _parent is not widely used yet, though.
|
// FIXME: Locking should be evaluated here. _parent is not widely used yet, though.
|
||||||
@@ -126,34 +115,29 @@
|
|||||||
if (child) {
|
if (child) {
|
||||||
id<ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable:child];
|
id<ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable:child];
|
||||||
if (finalLayoutable) {
|
if (finalLayoutable) {
|
||||||
_children = @[finalLayoutable];
|
_children.insert(_children.begin(), finalLayoutable);
|
||||||
[self propagateUpLayoutable:finalLayoutable];
|
[self propagateUpLayoutable:finalLayoutable];
|
||||||
}
|
}
|
||||||
} else {
|
} else if (_children.size() > 0) {
|
||||||
// remove the only child
|
_children.erase(_children.begin());
|
||||||
_children = [NSArray array];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setChild:(id<ASLayoutable>)child forIdentifier:(NSString *)identifier
|
- (void)setChild:(id<ASLayoutable>)child forIndex:(NSUInteger)index
|
||||||
{
|
{
|
||||||
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
|
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
|
||||||
if (child) {
|
if (child) {
|
||||||
id<ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable:child];
|
id<ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable:child];
|
||||||
self.childrenWithIdentifier[identifier] = finalLayoutable;
|
if (index > _children.size()) {
|
||||||
if (finalLayoutable) {
|
_children.insert(_children.end(), finalLayoutable);
|
||||||
_children = [_children arrayByAddingObject:finalLayoutable];
|
} else {
|
||||||
|
_children.insert(_children.begin() + index, finalLayoutable);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
id<ASLayoutable> oldChild = self.childrenWithIdentifier[identifier];
|
if (_children.begin() + index < _children.end()) {
|
||||||
if (oldChild) {
|
_children.erase(_children.begin() + index);
|
||||||
self.childrenWithIdentifier[identifier] = nil;
|
|
||||||
NSMutableArray *mutableChildren = [_children mutableCopy];
|
|
||||||
[mutableChildren removeObject:oldChild];
|
|
||||||
_children = [mutableChildren copy];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Should we propagate up the layoutable at it could happen that multiple children will propagated up their
|
// TODO: Should we propagate up the layoutable at it could happen that multiple children will propagated up their
|
||||||
// layout options and one child will overwrite values from another child
|
// layout options and one child will overwrite values from another child
|
||||||
// [self propagateUpLayoutable:finalLayoutable];
|
// [self propagateUpLayoutable:finalLayoutable];
|
||||||
@@ -163,32 +147,31 @@
|
|||||||
{
|
{
|
||||||
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
|
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
|
||||||
|
|
||||||
std::vector<id<ASLayoutable>> finalChildren;
|
_children.clear();
|
||||||
for (id<ASLayoutable> child in children) {
|
for (id<ASLayoutable> child in children) {
|
||||||
finalChildren.push_back([self layoutableToAddFromLayoutable:child]);
|
_children.push_back([self layoutableToAddFromLayoutable:child]);
|
||||||
}
|
|
||||||
|
|
||||||
_children = nil;
|
|
||||||
if (finalChildren.size() > 0) {
|
|
||||||
_children = [NSArray arrayWithObjects:&finalChildren[0] count:finalChildren.size()];
|
|
||||||
} else {
|
|
||||||
_children = [NSArray array];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id<ASLayoutable>)childForIdentifier:(NSString *)identifier
|
- (id<ASLayoutable>)childForIndex:(NSUInteger)index
|
||||||
{
|
{
|
||||||
return self.childrenWithIdentifier[identifier];
|
if (index < _children.size()) {
|
||||||
|
return _children[index];
|
||||||
|
}
|
||||||
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id<ASLayoutable>)child
|
- (id<ASLayoutable>)child
|
||||||
{
|
{
|
||||||
return [_children firstObject];
|
if (_children.size() > 0) {
|
||||||
|
return _children[0];
|
||||||
|
}
|
||||||
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSArray *)children
|
- (NSArray *)children
|
||||||
{
|
{
|
||||||
return _children;
|
return [NSArray arrayWithObjects:&_children[0] count:_children.size()];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - ASEnvironment
|
#pragma mark - ASEnvironment
|
||||||
|
|||||||
@@ -14,7 +14,8 @@
|
|||||||
#import "ASBaseDefines.h"
|
#import "ASBaseDefines.h"
|
||||||
#import "ASLayout.h"
|
#import "ASLayout.h"
|
||||||
|
|
||||||
static NSString * const kOverlayChildKey = @"kOverlayChildKey";
|
static NSUInteger const kUnderlayChildIndex = 0;
|
||||||
|
static NSUInteger const kOverlayChildIndex = 1;
|
||||||
|
|
||||||
@implementation ASOverlayLayoutSpec
|
@implementation ASOverlayLayoutSpec
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ static NSString * const kOverlayChildKey = @"kOverlayChildKey";
|
|||||||
}
|
}
|
||||||
ASDisplayNodeAssertNotNil(child, @"Child that will be overlayed on shouldn't be nil");
|
ASDisplayNodeAssertNotNil(child, @"Child that will be overlayed on shouldn't be nil");
|
||||||
self.overlay = overlay;
|
self.overlay = overlay;
|
||||||
[self setChild:child];
|
[self setChild:child forIndex:kUnderlayChildIndex];
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,12 +37,12 @@ static NSString * const kOverlayChildKey = @"kOverlayChildKey";
|
|||||||
|
|
||||||
- (void)setOverlay:(id<ASLayoutable>)overlay
|
- (void)setOverlay:(id<ASLayoutable>)overlay
|
||||||
{
|
{
|
||||||
[super setChild:overlay forIdentifier:kOverlayChildKey];
|
[super setChild:overlay forIndex:kOverlayChildIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id<ASLayoutable>)overlay
|
- (id<ASLayoutable>)overlay
|
||||||
{
|
{
|
||||||
return [super childForIdentifier:kOverlayChildKey];
|
return [super childForIndex:kOverlayChildIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user