mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 22:55:00 +00:00
Remove legacy layout validation code (#2311)
This commit is contained in:
committed by
GitHub
parent
5cd8f28ad5
commit
1abc1a833c
@@ -1,86 +0,0 @@
|
||||
//
|
||||
// ASLayoutValidation.h
|
||||
// AsyncDisplayKit
|
||||
//
|
||||
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <AsyncDisplayKit/ASBaseDefines.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class ASLayout;
|
||||
|
||||
// Enable or disable automatic layout validation
|
||||
#define LAYOUT_VALIDATION 0
|
||||
|
||||
ASDISPLAYNODE_EXTERN_C_BEGIN
|
||||
|
||||
extern void ASLayoutableValidateLayout(ASLayout *layout);
|
||||
|
||||
ASDISPLAYNODE_EXTERN_C_END
|
||||
|
||||
#pragma mark - ASLayoutableValidator
|
||||
|
||||
@protocol ASLayoutableValidator <NSObject>
|
||||
- (void)validateLayout:(ASLayout *)layout;
|
||||
@end
|
||||
|
||||
typedef void (^ASLayoutableBlockValidatorBlock)(id layout);
|
||||
|
||||
@interface ASLayoutableBlockValidator : NSObject<ASLayoutableValidator>
|
||||
@property (nonatomic, copy) ASLayoutableBlockValidatorBlock block;
|
||||
- (instancetype)initWithBlock:(ASLayoutableBlockValidatorBlock)block NS_DESIGNATED_INITIALIZER;
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
@end
|
||||
|
||||
/*
|
||||
* ASLayoutables that have sizeRange or layoutPosition set needs to be wrapped into a ASAbsoluteLayoutSpec. This
|
||||
* validator checks if sublayouts has sizeRange or layoutPosition set and is wrapped in a ASAbsoluteLayoutSpec
|
||||
*/
|
||||
@interface ASLayoutableStaticValidator : NSObject<ASLayoutableValidator>
|
||||
|
||||
@end
|
||||
|
||||
/*
|
||||
* ASLayoutables that have spacingBefore, spacingAfter, flexGrow, flexShrink, flexBasis, alignSelf, ascender or descender
|
||||
* set needs to be wrapped into a ASStackLayout. This validator checks if sublayouts has set one of this properties and
|
||||
* asserts if it's not wrapped in a ASStackLayout if so.
|
||||
*/
|
||||
@interface ASLayoutableStackValidator : NSObject<ASLayoutableValidator>
|
||||
|
||||
@end
|
||||
|
||||
/*
|
||||
* Not in use at the moment
|
||||
*/
|
||||
@interface ASLayoutablePreferredSizeValidator : NSObject<ASLayoutableValidator>
|
||||
|
||||
@end
|
||||
|
||||
|
||||
#pragma mark - ASLayoutableValidation
|
||||
|
||||
@interface ASLayoutableValidation : NSObject
|
||||
|
||||
/// Currently registered validators
|
||||
@property (copy, nonatomic, readonly) NSArray<id<ASLayoutableValidator>> *validators;
|
||||
|
||||
/// Start from given layout and validates each layout in the layout tree with registered validators
|
||||
- (void)validateLayout:(ASLayout *)layout;
|
||||
|
||||
/// Register a layout validator
|
||||
- (void)registerValidator:(id<ASLayoutableValidator>)validator;
|
||||
|
||||
/// Register a layout validator with a block. Method returns the registered ASLayoutableValidator object that can be used to store somewhere and unregister
|
||||
- (id<ASLayoutableValidator>)registerValidatorWithBlock:(ASLayoutableBlockValidatorBlock)block;
|
||||
|
||||
/// Unregister a validtor
|
||||
- (void)unregisterValidator:(id<ASLayoutableValidator>)validator;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -1,215 +0,0 @@
|
||||
//
|
||||
// ASLayoutValidation.mm
|
||||
// AsyncDisplayKit
|
||||
//
|
||||
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
//
|
||||
|
||||
#import "ASLayoutValidation.h"
|
||||
#import "ASLayout.h"
|
||||
#import "ASDisplayNode.h"
|
||||
|
||||
#import "ASAbsoluteLayoutSpec.h"
|
||||
#import "ASStackLayoutSpec.h"
|
||||
|
||||
#import <queue>
|
||||
|
||||
#pragma mark - Layout Validation
|
||||
|
||||
void ASLayoutableValidateLayout(ASLayout *layout) {
|
||||
ASLayoutableValidation *validation = [[ASLayoutableValidation alloc] init];
|
||||
[validation registerValidator:[[ASLayoutableStaticValidator alloc] init]];
|
||||
[validation registerValidator:[[ASLayoutableStackValidator alloc] init]];
|
||||
[validation validateLayout:layout];
|
||||
}
|
||||
|
||||
#pragma mark - Helpers
|
||||
|
||||
static NSString *ASLayoutValidationWrappingAssertMessage(SEL selector, id obj, Class cl) {
|
||||
return [NSString stringWithFormat:@"%@ was set on %@. It is either unecessary or the node needs to be wrapped in a %@", NSStringFromSelector(selector), obj, NSStringFromClass(cl)];
|
||||
}
|
||||
|
||||
#pragma mark - ASLayoutableBlockValidator
|
||||
|
||||
@implementation ASLayoutableBlockValidator
|
||||
|
||||
#pragma mark Lifecycle
|
||||
|
||||
- (instancetype)initWithBlock:(ASLayoutableBlockValidatorBlock)block
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_block = [block copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark <ASLayoutableValidator>
|
||||
|
||||
- (void)validateLayout:(ASLayout *)layout
|
||||
{
|
||||
if (self.block) {
|
||||
self.block(layout);
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark - ASLayoutableStaticValidator
|
||||
|
||||
@implementation ASLayoutableStaticValidator
|
||||
|
||||
- (void)validateLayout:(ASLayout *)layout
|
||||
{
|
||||
for (ASLayout *sublayout in layout.sublayouts) {
|
||||
id<ASLayoutable> layoutable = layout.layoutable;
|
||||
id<ASLayoutable> sublayoutLayoutable = sublayout.layoutable;
|
||||
|
||||
NSString *assertMessage = nil;
|
||||
Class stackContainerClass = [ASAbsoluteLayoutSpec class];
|
||||
|
||||
// Check for default layoutPosition
|
||||
if (!CGPointEqualToPoint(sublayoutLayoutable.style.layoutPosition, CGPointZero)) {
|
||||
assertMessage = ASLayoutValidationWrappingAssertMessage(@selector(layoutPosition), sublayoutLayoutable, stackContainerClass);
|
||||
}
|
||||
|
||||
// Sublayout layoutable should be wrapped in a ASAbsoluteLayoutSpec
|
||||
if (assertMessage == nil || [layoutable isKindOfClass:stackContainerClass]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ASDisplayNodeCAssert(NO, assertMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
#pragma mark - ASLayoutableStackValidator
|
||||
|
||||
@implementation ASLayoutableStackValidator
|
||||
|
||||
#pragma mark <ASLayoutableValidator>
|
||||
|
||||
- (void)validateLayout:(ASLayout *)layout
|
||||
{
|
||||
id<ASLayoutable> layoutable = layout.layoutable;
|
||||
for (ASLayout *sublayout in layout.sublayouts) {
|
||||
id<ASLayoutable> sublayoutLayoutable = sublayout.layoutable;
|
||||
|
||||
NSString *assertMessage = nil;
|
||||
Class stackContainerClass = [ASStackLayoutSpec class];
|
||||
|
||||
// Check if default values related to ASStackLayoutSpec have changed
|
||||
if (sublayoutLayoutable.style.spacingBefore != 0) {
|
||||
assertMessage = ASLayoutValidationWrappingAssertMessage(@selector(spacingBefore), sublayoutLayoutable, stackContainerClass);
|
||||
} else if (sublayoutLayoutable.style.spacingAfter != 0) {
|
||||
assertMessage = ASLayoutValidationWrappingAssertMessage(@selector(spacingAfter), sublayoutLayoutable, stackContainerClass);
|
||||
} else if (sublayoutLayoutable.style.flexGrow == YES) {
|
||||
assertMessage = ASLayoutValidationWrappingAssertMessage(@selector(flexGrow), sublayoutLayoutable, stackContainerClass);
|
||||
} else if (sublayoutLayoutable.style.flexShrink == YES) {
|
||||
assertMessage = ASLayoutValidationWrappingAssertMessage(@selector(flexShrink), sublayoutLayoutable, stackContainerClass);
|
||||
} else if (!ASDimensionEqualToDimension(sublayoutLayoutable.style.flexBasis, ASDimensionAuto) ) {
|
||||
assertMessage = ASLayoutValidationWrappingAssertMessage(@selector(flexBasis), sublayoutLayoutable, stackContainerClass);
|
||||
} else if (sublayoutLayoutable.style.alignSelf != ASStackLayoutAlignSelfAuto) {
|
||||
assertMessage = ASLayoutValidationWrappingAssertMessage(@selector(alignSelf), sublayoutLayoutable, stackContainerClass);
|
||||
}
|
||||
|
||||
// Sublayout layoutable should be wrapped in a ASStackLayoutSpec
|
||||
if (assertMessage == nil || [layoutable isKindOfClass:stackContainerClass]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ASDisplayNodeCAssert(NO, assertMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark ASLayoutablePreferredSizeValidator
|
||||
|
||||
@implementation ASLayoutablePreferredSizeValidator
|
||||
|
||||
#pragma mark <ASLayoutableValidator>
|
||||
|
||||
- (void)validateLayout:(ASLayout *)layout
|
||||
{
|
||||
// TODO: Implement validation that certain node classes need to have a preferredSize set e.g. ASVideoNode
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
#pragma mark - ASLayoutableValidation
|
||||
|
||||
@interface ASLayoutableValidation ()
|
||||
@end
|
||||
|
||||
@implementation ASLayoutableValidation {
|
||||
NSMutableArray *_validators;
|
||||
}
|
||||
|
||||
#pragma mark Lifecycle
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_validators = [NSMutableArray array];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark Validator Management
|
||||
|
||||
- (NSArray<id<ASLayoutableValidator>> *)validators
|
||||
{
|
||||
return [_validators copy];
|
||||
}
|
||||
|
||||
- (void)registerValidator:(id<ASLayoutableValidator>)validator
|
||||
{
|
||||
[_validators addObject:validator];
|
||||
}
|
||||
|
||||
- (id<ASLayoutableValidator>)registerValidatorWithBlock:(ASLayoutableBlockValidatorBlock)block
|
||||
{
|
||||
ASLayoutableBlockValidator *blockValidator = [[ASLayoutableBlockValidator alloc] initWithBlock:block];
|
||||
[_validators addObject:blockValidator];
|
||||
return blockValidator;
|
||||
}
|
||||
|
||||
- (void)unregisterValidator:(id<ASLayoutableValidator>)validator
|
||||
{
|
||||
[_validators removeObject:validator];
|
||||
}
|
||||
|
||||
#pragma mark Validation Process
|
||||
|
||||
- (void)validateLayout:(ASLayout *)layout
|
||||
{
|
||||
// Queue used to keep track of sublayouts while traversing this layout in a BFS fashion.
|
||||
std::queue<ASLayout *> queue;
|
||||
queue.push(layout);
|
||||
|
||||
while (!queue.empty()) {
|
||||
layout = queue.front();
|
||||
queue.pop();
|
||||
|
||||
// Validate layout with all registered validators
|
||||
for (id<ASLayoutableValidator> validator in self.validators) {
|
||||
[validator validateLayout:layout];
|
||||
}
|
||||
|
||||
// Push sublayouts to queue for validation
|
||||
for (id sublayout in [layout sublayouts]) {
|
||||
queue.push(sublayout);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user