iOS add View Node
This commit is contained in:
@@ -20,6 +20,10 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
- (DoricAsyncResult *)callEntity:(NSString *)method, ...;
|
||||
|
||||
- (DoricAsyncResult *)callEntity:(NSString *)method withArguments:(va_list)args;
|
||||
|
||||
- (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray *)args;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -31,4 +31,10 @@ - (DoricAsyncResult *)callEntity:(NSString *)method, ... {
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (DoricAsyncResult *)callEntity:(NSString *)method withArguments:(va_list)args {
|
||||
return [self.driver invokeContextEntity:self.contextId method:method arguments:args];
|
||||
}
|
||||
- (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray *)args {
|
||||
return [self.driver invokeContextEntity:self.contextId method:method argumentsArray:args];
|
||||
}
|
||||
@end
|
||||
|
@@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
- (DoricAsyncResult *)invokeDoricMethod:(NSString *)method, ...;
|
||||
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method, ...;
|
||||
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list) args;
|
||||
|
||||
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method argumentsArray:(NSArray *)args;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -97,6 +97,27 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method argumentsArray:(NSArray *)args {
|
||||
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
|
||||
NSMutableArray *array = [[NSMutableArray alloc] init];
|
||||
[array addObject:contextId];
|
||||
[array addObject:method];
|
||||
for (id arg in args){
|
||||
[array addObject: arg];
|
||||
}
|
||||
__weak typeof(self) _self = self;
|
||||
dispatch_async(self.jsExecutor.jsQueue, ^(){
|
||||
__strong typeof(_self) self = _self;
|
||||
if (!self) return;
|
||||
@try {
|
||||
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
|
||||
[ret setupResult:jsValue];
|
||||
} @catch (NSException *exception) {
|
||||
[ret setupError:exception];
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source {
|
||||
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
|
||||
|
@@ -20,6 +20,9 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
- (Class)acquireNativePlugin:(NSString *)name;
|
||||
|
||||
- (void)registerViewNode:(Class)nodeClass withName:(NSString *)name;
|
||||
|
||||
- (Class)acquireViewNode:(NSString *)name;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -8,11 +8,15 @@
|
||||
#import "DoricRegistry.h"
|
||||
#import "DoricModalPlugin.h"
|
||||
#import "DoricShaderPlugin.h"
|
||||
#import "DoricStackNode.h"
|
||||
#import "DoricVLayoutNode.h"
|
||||
#import "DoricHLayoutNode.h"
|
||||
|
||||
@interface DoricRegistry ()
|
||||
|
||||
@property (nonatomic,strong) NSMutableDictionary *bundles;
|
||||
@property (nonatomic,strong) NSMutableDictionary *plugins;
|
||||
@property (nonatomic,strong) NSMutableDictionary *nodes;
|
||||
|
||||
@end
|
||||
|
||||
@@ -22,6 +26,7 @@ - (instancetype)init {
|
||||
if(self = [super init]){
|
||||
_bundles = [[NSMutableDictionary alloc] init];
|
||||
_plugins = [[NSMutableDictionary alloc] init];
|
||||
_nodes = [[NSMutableDictionary alloc] init];
|
||||
[self innerRegister];
|
||||
}
|
||||
return self;
|
||||
@@ -30,6 +35,10 @@ - (instancetype)init {
|
||||
- (void)innerRegister {
|
||||
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"];
|
||||
[self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"];
|
||||
|
||||
[self registerViewNode:DoricStackNode.class withName:@"Stack"];
|
||||
[self registerViewNode:DoricVLayoutNode.class withName:@"VLayout"];
|
||||
[self registerViewNode:DoricHLayoutNode.class withName:@"HLayout"];
|
||||
}
|
||||
|
||||
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name {
|
||||
@@ -48,4 +57,12 @@ - (Class)acquireNativePlugin:(NSString *)name {
|
||||
return [self.plugins objectForKey:name];
|
||||
}
|
||||
|
||||
- (void)registerViewNode:(Class)nodeClass withName:(NSString *)name {
|
||||
[self.nodes setObject:nodeClass forKey:name];
|
||||
}
|
||||
|
||||
- (Class)acquireViewNode:(NSString *)name {
|
||||
return [self.nodes objectForKey:name];
|
||||
}
|
||||
|
||||
@end
|
||||
|
@@ -10,7 +10,12 @@
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricGroupNode : DoricViewNode<UIView *>
|
||||
|
||||
@property (nonatomic,strong) NSMutableDictionary *children;
|
||||
|
||||
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig;
|
||||
|
||||
- (LayoutParams *)generateDefaultLayoutParams;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -7,17 +7,80 @@
|
||||
|
||||
#import "DoricGroupNode.h"
|
||||
|
||||
@interface DoricGroupNode ()
|
||||
@property (nonatomic,strong) NSMutableArray *indexInfo;
|
||||
@end
|
||||
|
||||
@implementation DoricGroupNode
|
||||
|
||||
- (instancetype)init {
|
||||
if(self = [super init]) {
|
||||
_children = [[NSMutableDictionary alloc] init];
|
||||
_indexInfo = [[NSMutableArray alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (UIView *)build:(NSDictionary *)props {
|
||||
return [[UIView alloc] init];
|
||||
}
|
||||
|
||||
- (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop {
|
||||
|
||||
if([name isEqualToString:@"children"]) {
|
||||
NSArray *array = prop;
|
||||
NSInteger i;
|
||||
for (i = 0; i< array.count; i++) {
|
||||
NSDictionary *val = array[i];
|
||||
if (!val) {
|
||||
continue;
|
||||
}
|
||||
NSString *type = [val objectForKey:@"type"];
|
||||
NSString *viewId = [val objectForKey:@"id"];
|
||||
DoricViewNode *node = [self.children objectForKey:viewId];
|
||||
if (node == nil) {
|
||||
node = [DoricViewNode create:self.doricContext withType:type];
|
||||
node.index = i;
|
||||
node.parent = self;
|
||||
node.viewId = viewId;
|
||||
[self.children setObject:node forKey:viewId];
|
||||
} else if (i != node.index){
|
||||
node.index = i;
|
||||
[node.view removeFromSuperview];
|
||||
}
|
||||
LayoutParams *params = node.layoutParams;
|
||||
if (params == nil) {
|
||||
params = [self generateDefaultLayoutParams];
|
||||
}
|
||||
[node blend:[val objectForKey:@"props"]];
|
||||
if ([self.indexInfo objectAtIndex:i] == nil) {
|
||||
[self.view insertSubview:node.view atIndex:i];
|
||||
[self.indexInfo setObject:node atIndexedSubscript:i];
|
||||
}
|
||||
}
|
||||
for (; i < self.view.subviews.count; i++) {
|
||||
[self.view.subviews[i] removeFromSuperview];
|
||||
DoricViewNode *node = [self.indexInfo objectAtIndex:i];
|
||||
if (node != nil) {
|
||||
[self.children removeObjectForKey: node.viewId];
|
||||
[self.indexInfo removeObjectAtIndex:i];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
[super blendView:view forPropName:name propValue:prop];
|
||||
}
|
||||
}
|
||||
|
||||
- (LayoutParams *)generateDefaultLayoutParams {
|
||||
LayoutParams *params = [[LayoutParams alloc] init];
|
||||
return params;
|
||||
}
|
||||
|
||||
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
|
||||
|
||||
LayoutParams *params = child.layoutParams;
|
||||
NSDictionary *margin = [layoutconfig objectForKey:@"margin"];
|
||||
params.margin.top = [(NSNumber *)[margin objectForKey:@"top"] floatValue];
|
||||
params.margin.left = [(NSNumber *)[margin objectForKey:@"left"] floatValue];
|
||||
params.margin.right = [(NSNumber *)[margin objectForKey:@"right"] floatValue];
|
||||
params.margin.bottom = [(NSNumber *)[margin objectForKey:@"bottom"] floatValue];
|
||||
}
|
||||
@end
|
||||
|
16
iOS/Pod/Classes/Shader/DoricRootNode.h
Normal file
16
iOS/Pod/Classes/Shader/DoricRootNode.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// DoricRootNode.h
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/30.
|
||||
//
|
||||
|
||||
#import "DoricStackNode.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricRootNode : DoricStackNode
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
12
iOS/Pod/Classes/Shader/DoricRootNode.m
Normal file
12
iOS/Pod/Classes/Shader/DoricRootNode.m
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// DoricRootNode.m
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/30.
|
||||
//
|
||||
|
||||
#import "DoricRootNode.h"
|
||||
|
||||
@implementation DoricRootNode
|
||||
|
||||
@end
|
@@ -54,14 +54,16 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
@property (nonatomic) DoricGravity gravity;
|
||||
@end
|
||||
|
||||
@interface VLayout : UIView
|
||||
@interface LinearLayout : UIView
|
||||
@property (nonatomic) DoricGravity gravity;
|
||||
@property (nonatomic) CGFloat space;
|
||||
@end
|
||||
|
||||
@interface HLayout : UIView
|
||||
@property (nonatomic) DoricGravity gravity;
|
||||
@property (nonatomic) CGFloat space;
|
||||
|
||||
@interface VLayout : LinearLayout
|
||||
@end
|
||||
|
||||
@interface HLayout : LinearLayout
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -12,6 +12,12 @@ @implementation DoricRect
|
||||
@end
|
||||
|
||||
@implementation LayoutParams
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
_margin = [[DoricRect alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
@@ -37,13 +43,13 @@ - (void)measure {
|
||||
@end
|
||||
|
||||
@implementation Stack
|
||||
@end
|
||||
|
||||
@implementation LinearLayout
|
||||
@end
|
||||
|
||||
@implementation VLayout
|
||||
|
||||
@end
|
||||
|
||||
@implementation HLayout
|
||||
|
||||
@end
|
||||
|
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
#import "DoricContextHolder.h"
|
||||
#import "DoricViewContainer.h"
|
||||
|
||||
#import "UIView+Doric.h"
|
||||
|
||||
@@ -17,6 +18,13 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
@property (nonatomic,strong) V view;
|
||||
|
||||
@property (nonatomic,weak) DoricGroupNode *parent;
|
||||
@property (nonatomic) NSInteger index;
|
||||
|
||||
@property (nonatomic,strong) NSString *viewId;
|
||||
|
||||
@property (nonatomic,strong) LayoutParams *layoutParams;
|
||||
|
||||
@property (nonatomic,strong,readonly) NSArray<NSString *> *idList;
|
||||
|
||||
- (V)build:(NSDictionary *)props;
|
||||
|
||||
@@ -24,6 +32,10 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
- (void)blendView:(V)view forPropName:(NSString *)name propValue:(id)prop;
|
||||
|
||||
- (void)callJSResponse:(NSString *)funcId,...;
|
||||
|
||||
+ (DoricViewNode *)create:(DoricContext *)context withType:(NSString *)type;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -8,6 +8,8 @@
|
||||
#import "DoricViewNode.h"
|
||||
#import "DoricUtil.h"
|
||||
#import "DoricGroupNode.h"
|
||||
#import "DoricRootNode.h"
|
||||
#import "DoricConstant.h"
|
||||
|
||||
@implementation DoricViewNode
|
||||
|
||||
@@ -40,7 +42,40 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
|
||||
if(self.parent && [prop isKindOfClass:[NSDictionary class]]){
|
||||
[self.parent blendChild:self layoutConfig:prop];
|
||||
}
|
||||
} else {
|
||||
DoricLog(@"Blend View error for View Type :%@, prop is %@", self.class, name);
|
||||
}
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)idList {
|
||||
NSMutableArray *ret = [[NSMutableArray alloc] init];
|
||||
DoricViewNode *node = self;
|
||||
do {
|
||||
[ret addObject:node.viewId];
|
||||
node = node.parent;
|
||||
} while (node && ![node isKindOfClass:[DoricRootNode class]]);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (void)callJSResponse:(NSString *)funcId,... {
|
||||
NSMutableArray *array = [[NSMutableArray alloc] init];
|
||||
[array addObject:self.idList];
|
||||
[array addObject:funcId];
|
||||
va_list args;
|
||||
va_start(args, funcId);
|
||||
id arg;
|
||||
while ((arg = va_arg(args, id)) != nil) {
|
||||
[array addObject:arg];
|
||||
}
|
||||
[self.doricContext callEntity:DORIC_ENTITY_RESPONSE withArgumentsArray:array];
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
+ (DoricViewNode *)create:(DoricContext *)context withType:(NSString *)type {
|
||||
DoricRegistry *registry = context.driver.registry;
|
||||
Class clz = [registry acquireViewNode:type];
|
||||
return [[clz alloc] initWithContext:context];
|
||||
}
|
||||
|
||||
@end
|
||||
|
Reference in New Issue
Block a user