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.
|
|
|
|
//
|
|
|
|
|
2019-10-23 19:37:06 +08:00
|
|
|
#import <Doric/DoricExtensions.h>
|
2019-07-30 15:20:11 +08:00
|
|
|
#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-10-23 20:06:21 +08:00
|
|
|
DoricLayoutConfig *params = node.layoutConfig;
|
2019-07-30 21:05:27 +08:00
|
|
|
if (params == nil) {
|
|
|
|
params = [self generateDefaultLayoutParams];
|
2019-10-23 19:37:06 +08:00
|
|
|
node.layoutConfig = 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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
- (DoricLayoutConfig *)generateDefaultLayoutParams {
|
|
|
|
DoricLayoutConfig *params = [[DoricLayoutConfig alloc] init];
|
2019-07-30 21:05:27 +08:00
|
|
|
return params;
|
2019-07-30 15:20:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-23 19:37:06 +08:00
|
|
|
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutConfig {
|
2019-10-23 20:06:21 +08:00
|
|
|
DoricLayoutConfig *params = child.layoutConfig;
|
2019-10-23 19:37:06 +08:00
|
|
|
|
|
|
|
[layoutConfig[@"widthSpec"] also:^(NSNumber *it) {
|
|
|
|
if (it) {
|
2019-10-23 20:06:21 +08:00
|
|
|
params.widthSpec = (DoricLayoutSpec) [it integerValue];
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
[layoutConfig[@"heightSpec"] also:^(NSNumber *it) {
|
|
|
|
if (it) {
|
2019-10-23 20:06:21 +08:00
|
|
|
params.heightSpec = (DoricLayoutSpec) [it integerValue];
|
2019-10-23 19:37:06 +08:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2019-10-23 20:06:21 +08:00
|
|
|
if ([params isKindOfClass:DoricMarginConfig.class]) {
|
|
|
|
DoricMarginConfig *marginParams = (DoricMarginConfig *) params;
|
2019-10-23 19:37:06 +08:00
|
|
|
NSDictionary *margin = layoutConfig[@"margin"];
|
2019-07-31 17:43:26 +08:00
|
|
|
if (margin) {
|
2019-10-23 20:06:21 +08:00
|
|
|
marginParams.margin = DoricMarginMake(
|
2019-10-23 19:37:06 +08:00
|
|
|
[(NSNumber *) margin[@"left"] floatValue],
|
|
|
|
[(NSNumber *) margin[@"top"] floatValue],
|
|
|
|
[(NSNumber *) margin[@"right"] floatValue],
|
|
|
|
[(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
|