2019-07-30 15:20:11 +08:00
|
|
|
//
|
|
|
|
// DoricGroupNode.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/7/30.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricGroupNode.h"
|
|
|
|
|
2019-07-30 21:05:27 +08:00
|
|
|
@interface DoricGroupNode ()
|
|
|
|
@property (nonatomic,strong) NSMutableArray *indexInfo;
|
|
|
|
@end
|
|
|
|
|
2019-07-30 15:20:11 +08:00
|
|
|
@implementation DoricGroupNode
|
|
|
|
|
2019-07-30 21:05:27 +08:00
|
|
|
- (instancetype)init {
|
|
|
|
if(self = [super init]) {
|
|
|
|
_children = [[NSMutableDictionary alloc] init];
|
|
|
|
_indexInfo = [[NSMutableArray alloc] init];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:20:11 +08:00
|
|
|
- (UIView *)build:(NSDictionary *)props {
|
|
|
|
return [[UIView alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop {
|
2019-07-30 21:05:27 +08:00
|
|
|
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;
|
2019-07-30 15:20:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
|
2019-07-30 21:05:27 +08:00
|
|
|
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];
|
2019-07-30 15:20:11 +08:00
|
|
|
}
|
|
|
|
@end
|