Added NTSubDescriptor files and SKSubDescriptor interface

Summary: Added NTSubDescriptor file which implements the SKSubDescriptor interface to pass all NT specific data to the Layout Component Descriptor.

Reviewed By: jknoxville

Differential Revision: D12840392

fbshipit-source-id: bff295e13e8531456428424b7e073aefe7e1ced4
This commit is contained in:
Dimple Jethani
2018-11-01 14:42:07 -07:00
committed by Facebook Github Bot
parent c6aed22a25
commit aa979b8abd
7 changed files with 100 additions and 38 deletions

View File

@@ -7,10 +7,14 @@
*/
#import <Foundation/Foundation.h>
#import "SKSubDescriptor.h"
#import <FlipperKitLayoutPlugin/SKDescriptorMapper.h>
@interface FlipperKitLayoutComponentKitSupport : NSObject
+ (void)setUpWithDescriptorMapper:(SKDescriptorMapper *)mapper;
+ (void)setUpWithDescriptorMapper:(SKDescriptorMapper *)mapper
subDescriptors:(NSArray<SKSubDescriptor *>*)subDescriptors;
@end

View File

@@ -18,9 +18,22 @@
#import "SKComponentRootViewDescriptor.h"
#import "SKComponentLayoutDescriptor.h"
#import "SKComponentLayoutWrapper.h"
#import "SKSubDescriptor.h"
@implementation FlipperKitLayoutComponentKitSupport
+ (void)setUpWithDescriptorMapper:(SKDescriptorMapper *)mapper
subDescriptors:(NSArray<SKSubDescriptor *>*)subDescriptors{
[mapper registerDescriptor: [[SKComponentHostingViewDescriptor alloc] initWithDescriptorMapper: mapper]
forClass: [CKComponentHostingView class]];
[mapper registerDescriptor: [[SKComponentRootViewDescriptor alloc] initWithDescriptorMapper: mapper]
forClass: [CKComponentRootView class]];
SKComponentLayoutDescriptor *layoutDescriptor = [[SKComponentLayoutDescriptor alloc] initWithDescriptorMapper:mapper];
[layoutDescriptor addSubDescriptors:subDescriptors];
[mapper registerDescriptor: layoutDescriptor
forClass: [SKComponentLayoutWrapper class]];
}
+ (void)setUpWithDescriptorMapper:(SKDescriptorMapper *)mapper {
// What we really want here is "forProtocol:@protocol(CKInspectableView)" but no such luck.
[mapper registerDescriptor: [[SKComponentHostingViewDescriptor alloc] initWithDescriptorMapper: mapper]

View File

@@ -7,8 +7,12 @@
*/
#import <FlipperKitLayoutPlugin/SKNodeDescriptor.h>
#import "SKSubDescriptor.h"
@class SKComponentLayoutWrapper;
@interface SKComponentLayoutDescriptor: SKNodeDescriptor<SKComponentLayoutWrapper *>
- (void)addSubDescriptors:(NSArray<SKSubDescriptor *>*)subDescriptors;
@end

View File

@@ -23,7 +23,9 @@
#import <FlipperKitLayoutPlugin/SKHighlightOverlay.h>
#import <FlipperKitLayoutPlugin/SKObject.h>
#import "SKSubDescriptor.h"
#import "SKComponentLayoutWrapper.h"
#import "NTSubDescriptor.h"
#import "CKComponent+Sonar.h"
#import "Utils.h"
@@ -31,11 +33,16 @@
{
NSDictionary<NSNumber *, NSString *> *CKFlexboxAlignSelfEnumMap;
NSDictionary<NSNumber *, NSString *> *CKFlexboxPositionTypeEnumMap;
NSArray<SKSubDescriptor *>*_registeredSubdescriptors;
}
- (void)setUp {
[super setUp];
if (!_registeredSubdescriptors) {
_registeredSubdescriptors = [NSArray new];
}
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self initEnumMaps];
@@ -87,54 +94,30 @@
- (NSArray<SKNamed<NSDictionary<NSString *, NSObject *> *> *> *)dataForNode:(SKComponentLayoutWrapper *)node {
NSMutableArray<SKNamed<NSDictionary<NSString *, NSObject *> *> *> *data = [NSMutableArray new];
if (node.isFlexboxChild) {
[data addObject: [SKNamed newWithName:@"Layout" withValue:[self propsForFlexboxChild:node.flexboxChild]]];
}
NSMutableDictionary<NSString *, NSObject *> *extraData = [[NSMutableDictionary alloc] init];
for (SKSubDescriptor *s in _registeredSubdescriptors) {
[extraData setObject:[s getDataForNode:node] forKey:[s getName]];
}
if (extraData.count > 0) {
[data addObject: [SKNamed newWithName:@"Extra Sections" withValue:extraData]];
}
[data addObjectsFromArray:[node.component sonar_getData]];
return data;
}
#if !defined(FLIPPER_OSS)
- (NSDictionary<NSString *, NSString *> *) getNTMetaDataForChild:(CKFlexboxComponentChild)child
qualifier:(NSString *) qualifier
{
NSString *str = @"{\"stackTrace\":{\"Content\":\":nt:flexbox :nt:text :nt:flexbox\"},\"unminifiedData\":{\"Content\":\"text\"}, \"graphQLCalls\":{\"Content\":\"text\"}}";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if ([qualifier isEqualToString:@"Stack Trace"]) {
NSDictionary *trace = [json objectForKey:@"stackTrace"];
NSString *traceString = [[trace objectForKey:@"Content"] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray *listItems = [traceString componentsSeparatedByString:@":nt:"];
NSMutableArray *xhpComponents = [NSMutableArray array];;
for (NSString *s in listItems) {
if (![s isEqualToString:@""]) {
NSString *xhpString = [NSString stringWithFormat:@"%@%@%@", @"<nt:", s, @">"];
[xhpComponents addObject:xhpString];
}
}
return @{@"Content": [xhpComponents componentsJoinedByString:@" "]};
} else if ([qualifier isEqualToString:@"Unminified Payload"]) {
return [json objectForKey:@"unminifiedData"];
} else if ([qualifier isEqualToString:@"GraphQL Calls"]) {
return [json objectForKey:@"graphQLCalls"];
}
return @{};
- (void)addSubDescriptors:(nonnull NSArray<SKSubDescriptor *>*)subDescriptors{
_registeredSubdescriptors = subDescriptors;
}
#endif
- (NSDictionary<NSString *, NSObject *> *)propsForFlexboxChild:(CKFlexboxComponentChild)child {
return @{
@"spacingBefore": SKObject(@(child.spacingBefore)),
#if !defined(FLIPPER_OSS)
@"Native Templates": @{
@"Stack Trace": [self getNTMetaDataForChild:child qualifier:@"Stack Trace"],
@"Unminified Payload":[self getNTMetaDataForChild:child qualifier:@"Unminified Payload"],
@"GraphQL Calls":[self getNTMetaDataForChild:child qualifier:@"GraphQL Calls"],
},
#endif
@"spacingAfter": SKObject(@(child.spacingAfter)),
@"flexGrow": SKObject(@(child.flexGrow)),
@"flexShrink": SKObject(@(child.flexShrink)),

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
#import <UIKit/UIKit.h>
@class SKComponentLayoutWrapper;
/**
A SKSubDescriptor is an object which knows how to expose an Object of type T
to the SKLayoutDescriptor. This class is for frameworks wanting to pass data along
through the Layout Descriptor.
*/
@interface SKSubDescriptor : NSObject
/**
This is the SubDescriptor name.
*/
- (NSString *) getName;
/**
This is the data the SubDescriptor wants to pass up to the SKLayoutDescriptor.
*/
- (NSDictionary<NSString *, NSObject *> *)getDataForNode:(SKComponentLayoutWrapper *)node;
@end

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
#if FB_SONARKIT_ENABLED
#import "SKSubDescriptor.h"
#import "SKComponentLayoutWrapper.h"
@implementation SKSubDescriptor
{
}
- (NSDictionary<NSString *, NSObject *> *)getDataForNode:(SKComponentLayoutWrapper *)node {
return @{};
}
- (NSString *)getName {
return @"";
}
@end
#endif