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"
|
|
|
|
|
2019-11-15 14:57:41 +08:00
|
|
|
@interface DoricGroupNode ()
|
|
|
|
@property(nonatomic, copy) NSArray<DoricViewNode *> *childNodes;
|
|
|
|
@property(nonatomic, copy) NSArray <NSString *> *childViewIds;
|
|
|
|
@end
|
|
|
|
|
2019-07-30 15:20:11 +08:00
|
|
|
@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-11-15 14:57:41 +08:00
|
|
|
_childNodes = @[];
|
|
|
|
_childViewIds = @[];
|
2019-11-16 13:23:11 +08:00
|
|
|
_reusable = NO;
|
2019-07-30 21:05:27 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
2019-10-12 14:48:19 +08:00
|
|
|
|
2019-11-15 14:57:41 +08:00
|
|
|
- (UIView *)build {
|
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-11-15 14:57:41 +08:00
|
|
|
if ([@"children" isEqualToString:name]) {
|
|
|
|
self.childViewIds = prop;
|
2019-07-30 21:05:27 +08:00
|
|
|
} else {
|
|
|
|
[super blendView:view forPropName:name propValue:prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:57:41 +08:00
|
|
|
- (void)blend:(NSDictionary *)props {
|
|
|
|
[super blend:props];
|
|
|
|
[self configChildNodes];
|
|
|
|
}
|
|
|
|
|
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-11-15 14:57:41 +08:00
|
|
|
- (void)configChildNodes {
|
|
|
|
NSMutableArray *childNodes = [self.childNodes mutableCopy];
|
|
|
|
for (NSUInteger idx = 0; idx < self.childViewIds.count; idx++) {
|
|
|
|
NSString *viewId = self.childViewIds[idx];
|
|
|
|
NSDictionary *model = [self subModelOf:viewId];
|
|
|
|
NSString *type = model[@"type"];
|
|
|
|
if (idx < self.childNodes.count) {
|
|
|
|
DoricViewNode *oldNode = childNodes[idx];
|
|
|
|
if ([viewId isEqualToString:oldNode.viewId]) {
|
|
|
|
///Same,skip
|
|
|
|
} else {
|
2019-11-16 13:23:11 +08:00
|
|
|
if (self.reusable) {
|
|
|
|
if ([oldNode.type isEqualToString:type]) {
|
|
|
|
///Same type,can be reused
|
|
|
|
oldNode.viewId = viewId;
|
|
|
|
[oldNode blend:model[@"props"]];
|
|
|
|
} else {
|
|
|
|
///Replace this view
|
|
|
|
[childNodes removeObjectAtIndex:idx];
|
|
|
|
[oldNode.view removeFromSuperview];
|
|
|
|
DoricViewNode *viewNode = [DoricViewNode create:self.doricContext withType:type];
|
|
|
|
if ([viewNode isKindOfClass:[DoricGroupNode class]]) {
|
|
|
|
((DoricGroupNode *) viewNode).reusable = self.reusable;
|
|
|
|
}
|
|
|
|
viewNode.viewId = viewId;
|
|
|
|
[viewNode initWithSuperNode:self];
|
|
|
|
[viewNode blend:model[@"props"]];
|
|
|
|
[childNodes insertObject:viewNode atIndex:idx];
|
|
|
|
[self.view insertSubview:viewNode.view atIndex:idx];
|
2019-11-15 14:57:41 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-16 13:23:11 +08:00
|
|
|
///Find in remain nodes
|
|
|
|
NSInteger position = -1;
|
|
|
|
for (NSUInteger start = idx + 1; start < childNodes.count; start++) {
|
|
|
|
DoricViewNode *node = childNodes[start];
|
|
|
|
if ([viewId isEqualToString:node.viewId]) {
|
|
|
|
position = start;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (position >= 0) {
|
|
|
|
///Found ,swap idx,position
|
|
|
|
DoricViewNode *reused = childNodes[(NSUInteger) position];
|
|
|
|
[childNodes removeObjectAtIndex:(NSUInteger) position];
|
|
|
|
[childNodes removeObjectAtIndex:idx];
|
|
|
|
[childNodes insertObject:reused atIndex:idx];
|
|
|
|
[childNodes insertObject:oldNode atIndex:(NSUInteger) position];
|
|
|
|
|
|
|
|
///View swap index
|
|
|
|
[reused.view removeFromSuperview];
|
|
|
|
[oldNode.view removeFromSuperview];
|
|
|
|
[self.view insertSubview:reused.view atIndex:idx];
|
|
|
|
[self.view insertSubview:oldNode.view atIndex:position];
|
|
|
|
} else {
|
|
|
|
///Not found,insert
|
|
|
|
DoricViewNode *viewNode = [DoricViewNode create:self.doricContext withType:type];
|
|
|
|
viewNode.viewId = viewId;
|
|
|
|
[viewNode initWithSuperNode:self];
|
|
|
|
[viewNode blend:model[@"props"]];
|
|
|
|
[childNodes insertObject:viewNode atIndex:idx];
|
|
|
|
[self.view insertSubview:viewNode.view atIndex:idx];
|
|
|
|
}
|
2019-11-15 14:57:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/// Insert
|
|
|
|
DoricViewNode *viewNode = [DoricViewNode create:self.doricContext withType:type];
|
2019-11-16 13:23:11 +08:00
|
|
|
if ([viewNode isKindOfClass:[DoricGroupNode class]]) {
|
|
|
|
((DoricGroupNode *) viewNode).reusable = self.reusable;
|
|
|
|
}
|
2019-11-15 14:57:41 +08:00
|
|
|
viewNode.viewId = viewId;
|
|
|
|
[viewNode initWithSuperNode:self];
|
|
|
|
[viewNode blend:model[@"props"]];
|
|
|
|
[childNodes addObject:viewNode];
|
|
|
|
[self.view addSubview:viewNode.view];
|
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-11-15 14:57:41 +08:00
|
|
|
NSUInteger count = childNodes.count;
|
|
|
|
for (NSUInteger idx = self.childViewIds.count; idx < count; idx++) {
|
|
|
|
DoricViewNode *viewNode = childNodes.lastObject;
|
|
|
|
[childNodes removeLastObject];
|
|
|
|
[viewNode.view removeFromSuperview];
|
|
|
|
}
|
|
|
|
self.childNodes = [childNodes copy];
|
2019-07-30 15:20:11 +08:00
|
|
|
}
|
2019-07-31 14:18:20 +08:00
|
|
|
|
2019-11-15 14:57:41 +08:00
|
|
|
- (void)blendSubNode:(NSDictionary *)subModel {
|
|
|
|
NSString *viewId = subModel[@"id"];
|
|
|
|
[self.childNodes enumerateObjectsUsingBlock:^(DoricViewNode *obj, NSUInteger idx, BOOL *stop) {
|
|
|
|
if ([viewId isEqualToString:obj.viewId]) {
|
|
|
|
[obj blend:subModel[@"props"]];
|
|
|
|
*stop = YES;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
2019-07-30 15:20:11 +08:00
|
|
|
@end
|