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

132 lines
4.7 KiB
Mathematica
Raw Normal View History

2019-10-21 09:59:22 +08:00
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 {
2019-10-12 14:48:19 +08:00
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-10-12 14:48:19 +08:00
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-10-12 14:48:19 +08:00
if ([name isEqualToString:@"children"]) {
2019-07-30 21:05:27 +08:00
NSArray *array = prop;
NSInteger i;
2019-08-14 11:42:47 +08:00
NSMutableArray *tobeRemoved = [[NSMutableArray alloc] init];
2019-10-12 14:48:19 +08:00
for (i = 0; i < array.count; i++) {
2019-07-30 21:05:27 +08:00
NSDictionary *val = array[i];
2019-10-12 14:48:19 +08:00
if (!val || (NSNull *) val == [NSNull null]) {
2019-07-30 21:05:27 +08:00
continue;
}
2019-10-12 14:48:19 +08:00
NSString *type = val[@"type"];
NSString *viewId = val[@"id"];
DoricViewNode *node = self.children[viewId];
2019-07-30 21:05:27 +08:00
if (node == nil) {
node = [DoricViewNode create:self.doricContext withType:type];
node.index = i;
node.parent = self;
node.viewId = viewId;
2019-10-12 14:48:19 +08:00
self.children[viewId] = node;
2019-08-14 11:42:47 +08:00
} else {
if (i != node.index) {
[self.indexedChildren removeObjectAtIndex:i];
node.index = i;
[node.view removeFromSuperview];
}
[tobeRemoved removeObject:node];
}
2019-10-12 14:48:19 +08:00
DoricViewNode *old = i >= self.indexedChildren.count ? nil : self.indexedChildren[i];
2019-08-14 11:42:47 +08:00
if (old && old != node) {
[old.view removeFromSuperview];
self.indexedChildren[i] = [NSNull null];
[tobeRemoved addObject:old];
2019-07-30 21:05:27 +08:00
}
2019-10-12 14:48:19 +08:00
2019-07-30 21:05:27 +08:00
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
}
2019-10-12 14:48:19 +08:00
[node blend:val[@"props"]];
2019-07-31 17:43:26 +08:00
if (self.indexedChildren.count <= i) {
[self.view addSubview:node.view];
[self.indexedChildren addObject:node];
2019-10-12 14:48:19 +08:00
} else if (self.indexedChildren[i] == [NSNull null]) {
2019-07-31 17:43:26 +08:00
self.indexedChildren[i] = node;
2019-07-30 21:05:27 +08:00
[self.view insertSubview:node.view atIndex:i];
}
}
2019-10-12 14:48:19 +08:00
NSInteger start = i;
2019-08-14 11:42:47 +08:00
while (start < self.indexedChildren.count) {
2019-10-12 14:48:19 +08:00
DoricViewNode *node = self.indexedChildren[(NSUInteger) start];
2019-08-14 11:42:47 +08:00
if (node) {
2019-10-12 14:48:19 +08:00
[self.children removeObjectForKey:node.viewId];
2019-08-14 11:42:47 +08:00
[node.view removeFromSuperview];
[tobeRemoved removeObject:node];
2019-07-30 21:05:27 +08:00
}
2019-08-14 11:42:47 +08:00
start++;
}
if (i < self.indexedChildren.count) {
2019-10-12 14:48:19 +08:00
[self.indexedChildren removeObjectsInRange:NSMakeRange((NSUInteger) i, self.indexedChildren.count - i)];
2019-08-14 11:42:47 +08:00
}
2019-10-12 14:48:19 +08:00
2019-08-14 11:42:47 +08:00
for (DoricViewNode *node in tobeRemoved) {
[self.children removeObjectForKey:node.viewId];
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]) {
2019-10-12 14:48:19 +08:00
MarginLayoutParams *marginParams = (MarginLayoutParams *) params;
NSDictionary *margin = layoutconfig[@"margin"];
2019-07-31 17:43:26 +08:00
if (margin) {
2019-10-12 14:48:19 +08:00
marginParams.margin.top = [(NSNumber *) margin[@"top"] floatValue];
marginParams.margin.left = [(NSNumber *) margin[@"left"] floatValue];
marginParams.margin.right = [(NSNumber *) margin[@"right"] floatValue];
marginParams.margin.bottom = [(NSNumber *) margin[@"bottom"] floatValue];
2019-07-31 17:43:26 +08:00
}
2019-07-31 14:18:20 +08:00
}
2019-10-12 14:48:19 +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