This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/iOS/Pod/Classes/Shader/DoricGroupNode.m

101 lines
3.6 KiB
Mathematica
Raw Normal View History

2019-07-30 15:20:11 +08:00
//
// DoricGroupNode.m
// Doric
//
// Created by pengfei.zhou on 2019/7/30.
//
#import "DoricGroupNode.h"
@implementation DoricGroupNode
2019-07-31 17:43:26 +08:00
- (instancetype)initWithContext:(DoricContext *)doricContext {
if(self = [super initWithContext:doricContext]) {
2019-07-30 21:05:27 +08:00
_children = [[NSMutableDictionary alloc] init];
2019-07-31 14:18:20 +08:00
_indexedChildren = [[NSMutableArray alloc] init];
2019-07-30 21:05:27 +08:00
}
return self;
}
2019-07-30 15:20:11 +08:00
- (UIView *)build:(NSDictionary *)props {
2019-07-31 18:13:22 +08:00
UIView *ret = [[UIView alloc] init];
ret.clipsToBounds = YES;
return ret;
2019-07-30 15:20:11 +08:00
}
- (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];
2019-07-31 19:22:00 +08:00
if (!val || (NSNull *)val == [NSNull null]) {
2019-07-30 21:05:27 +08:00
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){
2019-07-31 17:43:26 +08:00
[self.indexedChildren removeObjectAtIndex:i];
2019-07-30 21:05:27 +08:00
node.index = i;
[node.view removeFromSuperview];
}
LayoutParams *params = node.layoutParams;
if (params == nil) {
params = [self generateDefaultLayoutParams];
2019-07-31 17:43:26 +08:00
node.layoutParams = params;
2019-07-30 21:05:27 +08:00
}
[node blend:[val objectForKey:@"props"]];
2019-07-31 17:43:26 +08:00
if (self.indexedChildren.count <= i) {
[self.view addSubview:node.view];
[self.indexedChildren addObject:node];
}else if ([self.indexedChildren objectAtIndex:i] == nil) {
self.indexedChildren[i] = node;
2019-07-30 21:05:27 +08:00
[self.view insertSubview:node.view atIndex:i];
}
}
2019-07-31 14:18:20 +08:00
while (i < self.view.subviews.count) {
2019-07-31 17:43:26 +08:00
UIView *view = self.view.subviews[i];
if (i < self.indexedChildren.count) {
DoricViewNode *node = [self.indexedChildren objectAtIndex:i];
if (node && node.view == view) {
[self.children removeObjectForKey: node.viewId];
[self.indexedChildren removeObjectAtIndex:i];
[view removeFromSuperview];
}
2019-07-30 21:05:27 +08:00
}
2019-07-31 14:18:20 +08:00
i++;
2019-07-30 21:05:27 +08:00
}
} 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;
2019-07-31 17:43:26 +08:00
if ([params isKindOfClass:MarginLayoutParams.class]) {
MarginLayoutParams *marginParams = (MarginLayoutParams *)params;
NSDictionary *margin = [layoutconfig objectForKey:@"margin"];
if (margin) {
marginParams.margin.top = [(NSNumber *)[margin objectForKey:@"top"] floatValue];
marginParams.margin.left = [(NSNumber *)[margin objectForKey:@"left"] floatValue];
marginParams.margin.right = [(NSNumber *)[margin objectForKey:@"right"] floatValue];
marginParams.margin.bottom = [(NSNumber *)[margin objectForKey:@"bottom"] floatValue];
}
2019-07-31 14:18:20 +08:00
}
2019-07-31 17:43:26 +08:00
2019-07-30 15:20:11 +08:00
}
2019-07-31 14:18:20 +08:00
2019-07-30 15:20:11 +08:00
@end