2019-12-04 13:29:26 +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.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/11/15.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricSuperNode.h"
|
|
|
|
#import "DoricExtensions.h"
|
|
|
|
|
|
|
|
@interface DoricSuperNode ()
|
|
|
|
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSMutableDictionary *> *subNodes;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricSuperNode
|
|
|
|
- (instancetype)initWithContext:(DoricContext *)doricContext {
|
|
|
|
if (self = [super initWithContext:doricContext]) {
|
|
|
|
_subNodes = [NSMutableDictionary new];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop {
|
|
|
|
if ([@"subviews" isEqualToString:name]) {
|
|
|
|
NSArray *subviews = prop;
|
|
|
|
for (NSMutableDictionary *subModel in subviews) {
|
|
|
|
[self mixinSubNode:subModel];
|
|
|
|
[self blendSubNode:subModel];
|
|
|
|
}
|
2021-06-15 16:06:57 +08:00
|
|
|
self.view.doricLayout.resolved = NO;
|
2019-12-04 13:29:26 +08:00
|
|
|
} else {
|
|
|
|
[super blendView:view forPropName:name propValue:prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mixinSubNode:(NSMutableDictionary *)dictionary {
|
|
|
|
NSString *viewId = dictionary[@"id"];
|
|
|
|
NSMutableDictionary *oldModel = self.subNodes[viewId];
|
|
|
|
if (oldModel) {
|
|
|
|
[self mixin:dictionary to:oldModel];
|
|
|
|
} else {
|
|
|
|
self.subNodes[viewId] = dictionary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mixin:(NSDictionary *)srcModel to:(NSMutableDictionary *)targetModel {
|
|
|
|
NSDictionary *srcProp = srcModel[@"props"];
|
2021-10-22 16:08:15 +08:00
|
|
|
NSMutableDictionary *targetProp = [targetModel[@"props"] mutableCopy];
|
2019-12-04 13:29:26 +08:00
|
|
|
[srcProp enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
|
|
|
|
if (![@"subviews" isEqualToString:key]) {
|
|
|
|
targetProp[key] = obj;
|
|
|
|
}
|
|
|
|
}];
|
2021-10-22 16:08:15 +08:00
|
|
|
targetModel[@"props"] = targetProp;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)recursiveMixin:(NSDictionary *)srcModel to:(NSMutableDictionary *)targetModel {
|
|
|
|
NSDictionary *srcProp = srcModel[@"props"];
|
2021-10-22 16:08:15 +08:00
|
|
|
NSMutableDictionary *targetProp = [targetModel[@"props"] mutableCopy];
|
2019-12-04 13:29:26 +08:00
|
|
|
NSMutableArray *targetOri = targetProp[@"subviews"];
|
2021-09-30 13:57:52 +08:00
|
|
|
NSArray *srcSubviews = srcProp[@"subviews"];
|
|
|
|
if (srcSubviews && srcSubviews.count > 0) {
|
|
|
|
for (NSDictionary *subview in srcSubviews) {
|
|
|
|
NSString *viewId = subview[@"id"];
|
|
|
|
__block NSMutableDictionary *viewModel = nil;
|
|
|
|
[targetOri enumerateObjectsUsingBlock:^(NSMutableDictionary *obj, NSUInteger idx, BOOL *stop) {
|
|
|
|
if ([viewId isEqualToString:obj[@"id"]]) {
|
|
|
|
viewModel = obj;
|
|
|
|
*stop = YES;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2021-09-30 13:57:52 +08:00
|
|
|
}];
|
|
|
|
if (viewModel) {
|
|
|
|
[self recursiveMixin:subview to:viewModel];
|
2022-12-09 16:33:00 +08:00
|
|
|
} else {
|
|
|
|
[targetOri addObject:subview];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2021-09-30 13:57:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
[srcProp enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
|
|
|
|
if (![@"subviews" isEqualToString:key]) {
|
2019-12-04 13:29:26 +08:00
|
|
|
targetProp[key] = obj;
|
|
|
|
}
|
|
|
|
}];
|
2021-10-22 16:08:15 +08:00
|
|
|
targetModel[@"props"] = targetProp;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendSubNode:(DoricViewNode *)subNode layoutConfig:(NSDictionary *)layoutConfig {
|
2020-04-03 16:36:43 +08:00
|
|
|
[subNode blendLayoutConfig:layoutConfig];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendSubNode:(NSDictionary *)subModel {
|
|
|
|
NSAssert(NO, @"Should override class:%@ ,method:%@.", NSStringFromClass([self class]),
|
|
|
|
NSStringFromSelector(_cmd));
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:36:43 +08:00
|
|
|
- (DoricLayout *)generateDefaultLayoutParams {
|
|
|
|
DoricLayout *params = [[DoricLayout alloc] init];
|
2019-12-04 13:29:26 +08:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (NSDictionary *)subModelOf:(NSString *)viewId {
|
|
|
|
return self.subNodes[viewId];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSubModel:(NSDictionary *)model in:(NSString *)viewId {
|
|
|
|
self.subNodes[viewId] = [model mutableCopy];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)clearSubModel {
|
|
|
|
[self.subNodes removeAllObjects];
|
|
|
|
}
|
|
|
|
|
2019-12-10 17:15:42 +08:00
|
|
|
- (void)removeSubModel:(NSString *)viewId {
|
|
|
|
[self.subNodes removeObjectForKey:viewId];
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:29:26 +08:00
|
|
|
- (DoricViewNode *)subNodeWithViewId:(NSString *)viewId {
|
|
|
|
NSAssert(NO, @"Should override class:%@ ,method:%@.", NSStringFromClass([self class]),
|
2019-12-10 17:15:42 +08:00
|
|
|
NSStringFromSelector(_cmd));
|
2019-12-04 13:29:26 +08:00
|
|
|
return nil;
|
|
|
|
}
|
2021-07-15 14:51:19 +08:00
|
|
|
|
|
|
|
- (NSArray *)getSubNodeViewIds {
|
|
|
|
NSMutableArray *discardedItems = [NSMutableArray array];
|
2021-09-29 19:02:14 +08:00
|
|
|
|
|
|
|
NSMutableArray *allKeys = [[NSMutableArray alloc] init];
|
2021-07-15 14:51:19 +08:00
|
|
|
allKeys = [NSMutableArray arrayWithArray:[self.subNodes allKeys]];
|
|
|
|
for (NSString *key in allKeys) {
|
|
|
|
if ([self subNodeWithViewId:key] == nil) {
|
|
|
|
[discardedItems addObject:key];
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 19:02:14 +08:00
|
|
|
|
2021-07-15 14:51:19 +08:00
|
|
|
[allKeys removeObjectsInArray:discardedItems];
|
2021-09-29 19:02:14 +08:00
|
|
|
|
2021-07-15 14:51:19 +08:00
|
|
|
return allKeys;
|
|
|
|
}
|
2021-11-03 13:27:00 +08:00
|
|
|
|
|
|
|
- (void)reset {
|
|
|
|
[super reset];
|
|
|
|
for (NSString *viewId in self.subNodes.allKeys) {
|
|
|
|
[[self subNodeWithViewId:viewId] reset];
|
|
|
|
}
|
2021-11-12 16:23:01 +08:00
|
|
|
}
|
2021-11-03 13:27:00 +08:00
|
|
|
|
2021-11-12 16:23:01 +08:00
|
|
|
- (void)subNodeContentChanged:(DoricViewNode *)subNode {
|
2023-03-23 19:30:44 +08:00
|
|
|
if (self.doricContext.destroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-12 16:23:01 +08:00
|
|
|
if (self.superNode
|
|
|
|
&& (self.view.doricLayout.widthSpec == DoricLayoutFit
|
2023-10-19 14:58:57 +08:00
|
|
|
|| self.view.doricLayout.heightSpec == DoricLayoutFit
|
|
|
|
|| self.view.doricLayout.weight > 0)) {
|
2021-11-12 16:23:01 +08:00
|
|
|
[self.superNode subNodeContentChanged:self];
|
|
|
|
} else {
|
2021-11-12 16:45:56 +08:00
|
|
|
if (![self.type isEqualToString:@"Root"]) {
|
|
|
|
[self.view.doricLayout apply];
|
|
|
|
}
|
2021-11-12 16:23:01 +08:00
|
|
|
[self requestLayout];
|
|
|
|
}
|
2021-11-03 13:27:00 +08:00
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
@end
|