feat:add reusable in groupnode for iOS

This commit is contained in:
pengfei.zhou 2019-11-16 13:23:11 +08:00
parent e4b729b2fd
commit 1d5b312922
4 changed files with 60 additions and 33 deletions

View File

@ -25,6 +25,7 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface DoricGroupNode <V:UIView *> : DoricSuperNode<V> @interface DoricGroupNode <V:UIView *> : DoricSuperNode<V>
@property(nonatomic, assign) BOOL reusable;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -34,6 +34,7 @@ - (instancetype)initWithContext:(DoricContext *)doricContext {
if (self = [super initWithContext:doricContext]) { if (self = [super initWithContext:doricContext]) {
_childNodes = @[]; _childNodes = @[];
_childViewIds = @[]; _childViewIds = @[];
_reusable = NO;
} }
return self; return self;
} }
@ -73,43 +74,65 @@ - (void)configChildNodes {
if ([viewId isEqualToString:oldNode.viewId]) { if ([viewId isEqualToString:oldNode.viewId]) {
///Same,skip ///Same,skip
} else { } else {
///Find in remain nodes if (self.reusable) {
NSInteger position = -1; if ([oldNode.type isEqualToString:type]) {
for (NSUInteger start = idx + 1; start < childNodes.count; start++) { ///Same type,can be reused
DoricViewNode *node = childNodes[start]; oldNode.viewId = viewId;
if ([viewId isEqualToString:node.viewId]) { [oldNode blend:model[@"props"]];
position = start; } else {
break; ///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];
}
} else {
///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];
} }
} }
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];
}
} }
} else { } else {
/// Insert /// Insert
DoricViewNode *viewNode = [DoricViewNode create:self.doricContext withType:type]; DoricViewNode *viewNode = [DoricViewNode create:self.doricContext withType:type];
if ([viewNode isKindOfClass:[DoricGroupNode class]]) {
((DoricGroupNode *) viewNode).reusable = self.reusable;
}
viewNode.viewId = viewId; viewNode.viewId = viewId;
[viewNode initWithSuperNode:self]; [viewNode initWithSuperNode:self];
[viewNode blend:model[@"props"]]; [viewNode blend:model[@"props"]];

View File

@ -30,12 +30,13 @@ NS_ASSUME_NONNULL_BEGIN
@interface DoricViewNode <V:UIView *> : DoricContextHolder @interface DoricViewNode <V:UIView *> : DoricContextHolder
@property(nonatomic, strong) V view; @property(nonatomic, strong) V view;
@property(nonatomic, weak) DoricSuperNode *superNode; @property(nonatomic, weak) DoricSuperNode *superNode;
@property(nonatomic) NSInteger index; @property(nonatomic) NSInteger index;
@property(nonatomic, copy) NSString *viewId; @property(nonatomic, copy) NSString *viewId;
@property(nonatomic, copy) NSString *type;
@property(nonatomic, readonly) DoricLayoutConfig *layoutConfig; @property(nonatomic, readonly) DoricLayoutConfig *layoutConfig;
@property(nonatomic, readonly) NSArray<NSString *> *idList; @property(nonatomic, readonly) NSArray<NSString *> *idList;

View File

@ -211,7 +211,9 @@ - (DoricAsyncResult *)callJSResponse:(NSString *)funcId, ... {
+ (__kindof DoricViewNode *)create:(DoricContext *)context withType:(NSString *)type { + (__kindof DoricViewNode *)create:(DoricContext *)context withType:(NSString *)type {
DoricRegistry *registry = context.driver.registry; DoricRegistry *registry = context.driver.registry;
Class clz = [registry acquireViewNode:type]; Class clz = [registry acquireViewNode:type];
return [(DoricViewNode *) [clz alloc] initWithContext:context]; DoricViewNode *viewNode = [(DoricViewNode *) [clz alloc] initWithContext:context];
viewNode.type = type;
return viewNode;
} }
- (void)requestLayout { - (void)requestLayout {