Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[WEEX-577][iOS] refactoring recycle list's data structure and bug-fix (
Browse files Browse the repository at this point in the history
…#1453)

* [WEEX-577][iOS] refactoring recycle list's data structure and fix v-if can not show in recycle list

* * [iOS] improve recycle list's query methods and fix typo mistake

* * [iOS] fix video component border radius with bottom-right mistake

* * [iOS] fix bug with recycle list can not show correct data.

* * [iOS] bugfix with default template can not show
  • Loading branch information
doumafang authored and cxfeng1 committed Aug 22, 2018
1 parent 64e7189 commit ff6e079
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ - (void)updateCellData:(NSDictionary *)data
{
WXAssertComponentThread();
[self updateBindingData:data];
[self _attachSlotEvent:data];
[self attachSlotEvent:data];
[self triggerLayout];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,41 +90,47 @@ - (void)updateBindingData:(NSDictionary *)data
}

__block NSMutableDictionary *newData = [NSMutableDictionary dictionary];
[templateComponent->_bindingProps enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, WXDataBindingBlock _Nonnull block, BOOL * _Nonnull stop) {
BOOL needUpdate;
id value = block(data, &needUpdate);
if (value) {
newData[key] = value;
}
}];
if (templateComponent->_bindingProps) {
[templateComponent->_bindingProps enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, WXDataBindingBlock _Nonnull block, BOOL * _Nonnull stop) {
BOOL needUpdate;
id value = block(data, &needUpdate);
if (value) {
newData[key] = value;
}
}];
}

if (self.attributes[@"@isComponentRoot"]) {
if (![recycleListComponent.dataManager virtualComponentDataWithIndexPath:indexPath]) {
NSString *templateId = self.attributes[@"@templateId"];
if (![recycleListComponent.dataManager virtualComponentDataWithIndexPath:indexPath templateId:templateId]) {
static NSUInteger __componentId = 0;
self->_virtualComponentId = [NSString stringWithFormat:@"%@@%lu", listRef, (unsigned long)__componentId % (2048*1024)];
self->_virtualComponentId = [NSString stringWithFormat:@"%@@%lu*%@", listRef, (unsigned long)__componentId % (2048*1024),templateId];
__componentId++;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[[WXSDKManager bridgeMgr] callComponentHook:self.weexInstance.instanceId componentId:self.attributes[@"@templateId"] type:@"lifecycle" hook:@"create" args:@[self->_virtualComponentId, newData] competion:^(JSValue *value) {
[newData addEntriesFromDictionary:[value toArray][0]];
[[WXSDKManager bridgeMgr] callComponentHook:self.weexInstance.instanceId componentId:templateId type:@"lifecycle" hook:@"create" args:@[self->_virtualComponentId, newData] competion:^(JSValue *value) {
[newData setObject:indexPath forKey:@"indexPath"];
[newData setObject:listRef forKey:@"recycleListComponentRef"];
[[recycleListComponent dataManager] updateVirtualComponentData:self->_virtualComponentId data:newData];
if ([[value toArray][0] isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *virtualComponentData = [value toArray][0];
[virtualComponentData setObject:indexPath forKey:@"indexPath"];
[[recycleListComponent dataManager] updateVirtualComponentData:self->_virtualComponentId data:virtualComponentData];
[newData addEntriesFromDictionary:virtualComponentData];
}
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

[self _refsConventFromData:newData];
NSIndexPath *indexPath = newData[@"item"][@"indexPath"];
NSUInteger position = [indexPath indexAtPosition:1];
[[WXSDKManager bridgeMgr] callComponentHook:self.weexInstance.instanceId componentId:self->_virtualComponentId type:@"lifecycle" hook:@"attach" args:@[@{@"virtualComponentId":self->_virtualComponentId,@"position":@(position),@"refs":self->_virtalElementInfo[@"refs"]?:@{}}] competion:nil];
[[WXSDKManager bridgeMgr] callComponentHook:self.weexInstance.instanceId componentId:self->_virtualComponentId type:@"lifecycle" hook:@"attach" args:@[@{@"virtualComponentId":self->_virtualComponentId,@"position":@(indexPath.row),@"refs":self->_virtualElementInfo[@"refs"]?:@{}}] competion:nil];
if ([newData count]) {
data = newData;
}
} else {
newData[@"componentDataId"] = self->_virtualComponentId;
NSDictionary * virtualComponentData = [recycleListComponent.dataManager virtualComponentDataWithIndexPath:indexPath];
NSDictionary *virtualComponentData = [recycleListComponent.dataManager virtualComponentDataWithIndexPath:indexPath templateId:templateId];
[newData addEntriesFromDictionary:virtualComponentData];
[newData addEntriesFromDictionary:data];
newData[@"virtualComponentId"] = self->_virtualComponentId;
[newData setObject:indexPath forKey:@"indexPath"];
[newData setObject:listRef forKey:@"recycleListComponentRef"];
data = newData;
}
}
Expand Down Expand Up @@ -312,7 +318,6 @@ - (void)_storeBindings:(NSDictionary *)stylesOrAttributesOrEvents type:(WXDataBi
WXAssert(NO, @"error binding type:%z", type);
break;
}

[stylesOrAttributesOrEvents enumerateKeysAndObjectsUsingBlock:^(id _Nonnull name, id _Nonnull binding, BOOL * _Nonnull stop) {
if ([binding isKindOfClass:[NSDictionary class]] && binding[WXBindingIdentify]) {
// {"attributeOrStyleName":{"@binding":"bindingExpression"}
Expand Down Expand Up @@ -351,7 +356,6 @@ - (void)_storeBindings:(NSDictionary *)stylesOrAttributesOrEvents type:(WXDataBi
}
}
}];

return type == WXDataBindingTypeEvents ? newArray : [newArray componentsJoinedByString:@""];
};
}
Expand Down Expand Up @@ -399,7 +403,8 @@ - (WXDataBindingBlock)bindingBlockWithExpression:(WXJSExpression *)expression
return data[identiferName];
} else {
WXLogError(@"identifer:%@ not found", identiferName);
return nil;
*needUpdate = YES;
return @"";
}
} else if (expression->is<WXJSMemberExpression>()) {
WXJSMemberExpression *member = (WXJSMemberExpression *)expression;
Expand All @@ -418,7 +423,9 @@ - (WXDataBindingBlock)bindingBlockWithExpression:(WXJSExpression *)expression
if (memberExpression->is<WXJSIdentifier>()) {
NSString *propertyName = [NSString stringWithCString:(((WXJSStringLiteral *)member->property)->value).c_str() encoding:[NSString defaultCStringEncoding]];
*needUpdate = objectNeedUpdate;
return object[propertyName];
if ([object isKindOfClass:[NSDictionary class]]) {
return object[propertyName];
}
} else {
id retvalue = [self bindingBlockWithExpression:member->property](object, &objectNeedUpdate);
*needUpdate = objectNeedUpdate || propertyNeedUpdate;
Expand Down Expand Up @@ -534,31 +541,37 @@ - (WXDataBindingBlock)bindingBlockWithExpression:(WXJSExpression *)expression
return block;
}

- (void)_attachSlotEvent:(NSDictionary *)data
- (void)attachSlotEvent:(NSDictionary *)data
{
[self _refsConventFromData:data];
if (_virtalElementInfo.count != 0) {
NSIndexPath *indexPath = data[@"item"][@"indexPath"];
NSUInteger position = [indexPath indexAtPosition:1];
[_virtalElementInfo addEntriesFromDictionary:@{@"position":@(position)}];
[[WXSDKManager bridgeMgr] fireEvent:self.weexInstance.instanceId ref:data[@"item"][@"recycleListComponentRef"] type:@"_attach_slot" params:_virtalElementInfo domChanges:nil handlerArguments:nil];
}
[self cellSlotEventHandle:data isAttach:YES];
}

- (void)detachSlotEvent:(NSDictionary *)data
{
[self cellSlotEventHandle:data isAttach:NO];
}

- (void)_detachSlotEvent:(NSDictionary *)data
- (void)cellSlotEventHandle:(NSDictionary *)data isAttach:(BOOL)isAttach
{
[self _refsConventFromData:data];
if (_virtalElementInfo.count != 0) {
NSIndexPath *indexPath = data[@"item"][@"indexPath"];
NSUInteger position = [indexPath indexAtPosition:1];
[_virtalElementInfo addEntriesFromDictionary:@{@"position":@(position)}];
[[WXSDKManager bridgeMgr] fireEvent:self.weexInstance.instanceId ref:data[@"item"][@"recycleListComponentRef"] type:@"_detach_slot" params:_virtalElementInfo domChanges:nil handlerArguments:nil];
if (_virtualElementInfo.count != 0) {
NSString *recycleListComponentRef = data[@"recycleListComponentRef"];
NSIndexPath *indexPath = data[@"indexPath"];
if (!recycleListComponentRef) {
if (data[@"aliasKey"]) {
id key = data[@"aliasKey"];
recycleListComponentRef = data[key][@"recycleListComponentRef"];
indexPath = data[key][@"indexPath"];
}
}
[_virtualElementInfo addEntriesFromDictionary:@{@"position":@(indexPath.row)}];
[[WXSDKManager bridgeMgr] fireEvent:self.weexInstance.instanceId ref:recycleListComponentRef type:isAttach ?@"_attach_slot": @"_detach_slot" params:_virtualElementInfo domChanges:nil handlerArguments:nil];
}
}

- (void )_refsConventFromData:(NSDictionary *)data
{
_virtalElementInfo = [NSMutableDictionary new];
_virtualElementInfo = [NSMutableDictionary new];
if (self.attributes[@"ref"]) {
NSMutableDictionary *subInfo = [NSMutableDictionary new];
[self _componentInfoOfRef:self subInfo:subInfo data:data];
Expand All @@ -580,23 +593,28 @@ - (void)_recursiveSlotComponent:(WXComponent *)component subInfo:(NSMutableDicti
[self _componentInfoOfRef:subcomponent subInfo:subInfo data:data];
}
if (subInfo.count !=0) {
[_virtalElementInfo setObject:subInfo forKey:@"refs"];
[_virtualElementInfo setObject:subInfo forKey:@"refs"];
}
}

- (void)_componentInfoOfRef:(WXComponent *)component subInfo:(NSMutableDictionary *)subInfo data:(NSDictionary *)data
{
if (component.attributes[@"ref"]) {
NSIndexPath *indexPath = data[@"item"][@"indexPath"];
NSUInteger position = [indexPath indexAtPosition:1];
NSString *virtalElementInfo = [NSString stringWithFormat:@"%@@%lu",component.ref,position];
NSDictionary *refInfo = @{@"attrs":component.attributes,@"type":component->_type,@"ref":virtalElementInfo,@"[[VirtualElement]]":@"true"};
NSIndexPath *indexPath = data[@"indexPath"];
if (!indexPath) {
if (data[@"aliasKey"]) {
id key = data[@"aliasKey"];
indexPath = data[key][@"indexPath"];
}
}
NSString *virtualElementInfo = [NSString stringWithFormat:@"%@@%lu",component.ref,indexPath.row];
NSDictionary *refInfo = @{@"attrs":component.attributes,@"type":component->_type,@"ref":virtualElementInfo,@"[[VirtualElement]]":@"true"};
if (subInfo[component.attributes[@"ref"]]) {
[subInfo[component.attributes[@"ref"]] addObject:refInfo];
}
else
{
[subInfo setValue:@[refInfo] forKey:component.attributes[@"ref"]];
[subInfo setValue:[NSMutableArray arrayWithArray:@[refInfo]] forKey:component.attributes[@"ref"]];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,21 +293,13 @@ - (void)_updateDataForCellSlotAtIndexPath:(NSIndexPath*)indexPath data:(NSDictio

- (void)updateData:(NSUInteger)index data:(id)data
{
NSMutableArray * newListData = [[_dataManager data] mutableCopy];
NSMutableArray *newListData = [[_dataManager data] mutableCopy];
if (!data && index > [newListData count]) {
return;
}
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:index inSection:0];
NSDictionary * virtualComponentData = [_dataManager virtualComponentDataWithIndexPath:indexPath];
if ([virtualComponentData[WXBindingOnceIdentify] boolValue]) {
return;
}

// TODO: bring the update logic to UpdateManager
newListData[index] = data;
[_dataManager updateData:newListData];
NSString* virtualComponentId = [_dataManager virtualComponentIdWithIndexPath:indexPath];
[_dataManager updateVirtualComponentData:virtualComponentId data:data];
NSMutableDictionary * newData = nil;
if (![data isKindOfClass:[NSDictionary class]]) {
newData = [NSMutableDictionary new];
Expand All @@ -316,6 +308,7 @@ - (void)updateData:(NSUInteger)index data:(id)data
}
newData = [data mutableCopy];
newData[@"@phase"] = @"update";
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self _updateDataForCellSlotAtIndexPath:indexPath data:[newData copy]];
}

Expand Down Expand Up @@ -375,63 +368,65 @@ - (void)moveData:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
}];
}

- (void)scrollTo:(NSString *)virtalElementInfo options:(NSDictionary *)options
- (void)scrollTo:(NSString *)virtualElementInfo options:(NSDictionary *)options
{
NSUInteger position = 0;
if ([virtalElementInfo isKindOfClass:[NSNumber class]]) {
position = [virtalElementInfo integerValue];
if ([virtualElementInfo isKindOfClass:[NSNumber class]]) {
position = [virtualElementInfo integerValue];
}
else
{
if (virtalElementInfo.length == 0) {
if (virtualElementInfo.length == 0) {
return;
}
position = [self _positionForVirtalElementInfo:virtalElementInfo];
position = [self _positionForVirtualElementInfo:virtualElementInfo];
}
NSIndexPath *toIndexPath = [NSIndexPath indexPathForItem:position inSection:0];
BOOL animated = options[@"animated"] ? [WXConvert BOOL:options[@"animated"]] : YES;
[_collectionView scrollToItemAtIndexPath:toIndexPath atScrollPosition:UICollectionViewScrollPositionTop animated:animated];
}

- (void)scrollToElement:(NSString *)virtalElementInfo options:(NSDictionary *)options
- (void)scrollToElement:(NSString *)virtualElementInfo options:(NSDictionary *)options
{
[self scrollTo:virtalElementInfo options:options];
[self scrollTo:virtualElementInfo options:options];
}

- (void)queryElement:(NSString *)virtalElementInfo cssSelector:(NSString *)cssSelector callback:(WXModuleCallback)callback
- (void)queryElement:(NSString *)virtualElementInfo cssSelector:(NSString *)cssSelector callback:(WXModuleCallback)callback
{
[self _queryElement:virtalElementInfo cssSelector:cssSelector callback:callback isAll:NO];
[self _queryElement:virtualElementInfo cssSelector:cssSelector callback:callback isAll:NO];
}

- (void)queryElementAll:(NSString *)virtalElementInfo cssSelector:(NSString *)cssSelector callback:(WXModuleCallback)callback
- (void)queryElementAll:(NSString *)virtualElementInfo cssSelector:(NSString *)cssSelector callback:(WXModuleCallback)callback
{
[self _queryElement:virtalElementInfo cssSelector:cssSelector callback:callback isAll:YES];
[self _queryElement:virtualElementInfo cssSelector:cssSelector callback:callback isAll:YES];
}

- (NSString *)_refForVirtalElementInfo:(NSString *)virtalElementInfo
- (NSString *)_refForVirtualElementInfo:(NSString *)virtualElementInfo
{
NSArray *stringArray = [virtalElementInfo componentsSeparatedByString:@"@"];
if (stringArray.count == 2) {
return stringArray[0];
if ([virtualElementInfo isKindOfClass:[NSString class]]){
NSArray *stringArray = [virtualElementInfo componentsSeparatedByString:@"@"];
if (stringArray.count == 2) {
return stringArray[0];
}
}
return nil;
}

- (NSUInteger )_positionForVirtalElementInfo:(NSString *)virtalElementInfo
- (NSUInteger )_positionForVirtualElementInfo:(NSString *)virtualElementInfo
{
NSArray *stringArray = [virtalElementInfo componentsSeparatedByString:@"@"];
NSArray *stringArray = [virtualElementInfo componentsSeparatedByString:@"@"];
if (stringArray.count == 2) {
return [stringArray[1] integerValue];
}
return 0;
}

- (void)closest:(NSString *)virtalElementInfo cssSelector:(NSString *)cssSelector callback:(WXModuleCallback)callback
- (void)closest:(NSString *)virtualElementInfo cssSelector:(NSString *)cssSelector callback:(WXModuleCallback)callback
{
if(callback)
{
WXPerformBlockOnComponentThread(^{
WXComponent *component = [self.weexInstance.componentManager componentForRef:[self _refForVirtalElementInfo:virtalElementInfo]];
WXComponent *component = [self.weexInstance.componentManager componentForRef:[self _refForVirtualElementInfo:virtualElementInfo]];
if (component) {
callback([self _closestComponentForCSSSelector:cssSelector component:component]);
}
Expand All @@ -455,12 +450,12 @@ - (NSDictionary *)_closestComponentForCSSSelector:(NSString *)cssSelector compon
}
}

- (void)_queryElement:(NSString *)virtalElementInfo cssSelector:(NSString *)cssSelector callback:(WXModuleCallback)callback isAll:(BOOL)isAll
- (void)_queryElement:(NSString *)virtualElementInfo cssSelector:(NSString *)cssSelector callback:(WXModuleCallback)callback isAll:(BOOL)isAll
{
if(callback)
{
WXPerformBlockSyncOnComponentThread(^{
WXComponent *component = [self.weexInstance.componentManager componentForRef:[self _refForVirtalElementInfo:virtalElementInfo]];
WXComponent *component = [self.weexInstance.componentManager componentForRef:[self _refForVirtualElementInfo:virtualElementInfo]];
if (component) {
NSMutableArray *infoArray = [NSMutableArray new];
[self _matchComponentForCSSSelector:cssSelector component:component infoArray:infoArray];
Expand Down Expand Up @@ -636,7 +631,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
// 2. get the template type specified by data, and if template is not found, return an empty view of any template to avoid crash.
NSString * templateType = [self templateType:indexPath];
_templateManager.collectionView = collectionView;
if (!templateType || (templateType && ![_templateManager isTemplateRegistered:templateType])) {
if (!templateType) {
WXLogError(@"Template %@ not registered for collection view.", templateType);
UICollectionViewCell *cellView = [_collectionView dequeueReusableCellWithReuseIdentifier:[_templateManager anyRegisteredTemplate] forIndexPath:indexPath];
for (UIView *view in cellView.contentView.subviews) {
Expand All @@ -646,6 +641,9 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
[cellView setAccessibilityIdentifier:nil];
return cellView;
}
if (![_templateManager isTemplateRegistered:templateType]) {
templateType = @"default";
}

// 3. dequeue a cell component by template type
UICollectionViewCell *cellView = [_collectionView dequeueReusableCellWithReuseIdentifier:templateType forIndexPath:indexPath];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

- (void)updateData:(NSArray *)data;

- (void)updateVirtualComponentData:(NSString *)componentId data:(NSDictionary *)data;

- (void)deleteVirtualComponentAtIndexPaths:(NSArray<NSIndexPath*>*)indexPaths;

- (NSArray *)data;

- (NSDictionary *)dataAtIndex:(NSInteger)index;
Expand All @@ -33,13 +37,10 @@

- (NSInteger)numberOfVirtualComponent;

- (NSDictionary*)virtualComponentDataWithId:(NSString*)componentId;
- (NSDictionary *)virtualComponentDataWithId:(NSString *)componentId;

- (void)updateVirtualComponentData:(NSString*)componentId data:(NSDictionary*)data;
- (NSDictionary *)virtualComponentDataWithIndexPath:(NSIndexPath *)indexPath templateId:(NSString *)templateId;

- (NSDictionary*)virtualComponentDataWithIndexPath:(NSIndexPath*)indexPath;
- (NSString *)virtualComponentIdWithIndexPath:(NSIndexPath *)indexPath templateId:(NSString *)templateId;

- (NSString*)virtualComponentIdWithIndexPath:(NSIndexPath*)indexPath;

- (void)deleteVirtualComponentAtIndexPaths:(NSArray<NSIndexPath*>*)indexPaths;
@end
Loading

0 comments on commit ff6e079

Please sign in to comment.