2019-11-15 19:30:07 +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 <JavaScriptCore/JavaScriptCore.h>
|
|
|
|
#import "DoricListNode.h"
|
|
|
|
#import "DoricExtensions.h"
|
|
|
|
#import "DoricListItemNode.h"
|
|
|
|
|
2019-11-16 15:10:21 +08:00
|
|
|
@interface DoricTableViewCell : UITableViewCell
|
|
|
|
@property(nonatomic, strong) DoricListItemNode *doricListItemNode;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricTableViewCell
|
|
|
|
@end
|
|
|
|
|
2019-11-15 19:30:07 +08:00
|
|
|
@interface DoricListNode () <UITableViewDataSource, UITableViewDelegate>
|
2019-11-16 15:10:21 +08:00
|
|
|
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSString *> *itemViewIds;
|
|
|
|
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSNumber *> *itemHeights;
|
2019-11-15 19:30:07 +08:00
|
|
|
@property(nonatomic, assign) NSUInteger itemCount;
|
|
|
|
@property(nonatomic, assign) NSUInteger batchCount;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricListNode
|
|
|
|
- (instancetype)initWithContext:(DoricContext *)doricContext {
|
|
|
|
if (self = [super initWithContext:doricContext]) {
|
2019-11-16 15:10:21 +08:00
|
|
|
_itemViewIds = [NSMutableDictionary new];
|
|
|
|
_itemHeights = [NSMutableDictionary new];
|
2019-11-15 19:30:07 +08:00
|
|
|
_batchCount = 15;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableView *)build {
|
|
|
|
return [[UITableView new] also:^(UITableView *it) {
|
|
|
|
it.dataSource = self;
|
|
|
|
it.delegate = self;
|
2019-11-16 15:10:21 +08:00
|
|
|
it.separatorStyle = UITableViewCellSeparatorStyleNone;
|
2019-11-15 19:30:07 +08:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendView:(UITableView *)view forPropName:(NSString *)name propValue:(id)prop {
|
|
|
|
if ([@"itemCount" isEqualToString:name]) {
|
|
|
|
self.itemCount = [prop unsignedIntegerValue];
|
|
|
|
} else if ([@"renderItem" isEqualToString:name]) {
|
2019-11-16 15:10:21 +08:00
|
|
|
[self.itemViewIds removeAllObjects];
|
2019-11-15 19:30:07 +08:00
|
|
|
[self clearSubModel];
|
|
|
|
} else if ([@"batchCount" isEqualToString:name]) {
|
|
|
|
self.batchCount = [prop unsignedIntegerValue];
|
|
|
|
} else {
|
|
|
|
[super blendView:view forPropName:name propValue:prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blend:(NSDictionary *)props {
|
|
|
|
[super blend:props];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
|
return self.itemCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
NSUInteger position = (NSUInteger) indexPath.row;
|
|
|
|
NSDictionary *model = [self itemModelAt:position];
|
|
|
|
NSDictionary *props = model[@"props"];
|
|
|
|
NSString *reuseId = props[@"identifier"];
|
|
|
|
|
2019-11-16 15:26:44 +08:00
|
|
|
DoricTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId ?: @"doriccell"];
|
2019-11-15 19:30:07 +08:00
|
|
|
if (!cell) {
|
2019-11-16 15:34:19 +08:00
|
|
|
cell = [[DoricTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId ?: @"doriccell"];
|
2019-11-16 15:10:21 +08:00
|
|
|
DoricListItemNode *listItemNode = [[DoricListItemNode alloc] initWithContext:self.doricContext];
|
|
|
|
[listItemNode initWithSuperNode:self];
|
|
|
|
cell.doricListItemNode = listItemNode;
|
|
|
|
[cell.contentView addSubview:listItemNode.view];
|
2019-11-15 19:30:07 +08:00
|
|
|
}
|
2019-11-16 15:10:21 +08:00
|
|
|
DoricListItemNode *node = cell.doricListItemNode;
|
|
|
|
node.viewId = model[@"id"];
|
2019-11-15 19:30:07 +08:00
|
|
|
[node blend:props];
|
2019-11-16 15:10:21 +08:00
|
|
|
CGSize size = [node.view sizeThatFits:CGSizeMake(cell.width, cell.height)];
|
|
|
|
[self callItem:position height:size.height];
|
2019-11-15 19:30:07 +08:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
NSUInteger position = (NSUInteger) indexPath.row;
|
2019-11-16 15:10:21 +08:00
|
|
|
NSNumber *heightNumber = self.itemHeights[@(position)];
|
|
|
|
if (heightNumber) {
|
|
|
|
return [heightNumber floatValue];
|
|
|
|
} else {
|
|
|
|
return 44.f;
|
|
|
|
}
|
2019-11-15 19:30:07 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)itemModelAt:(NSUInteger)position {
|
2019-11-16 15:10:21 +08:00
|
|
|
NSString *viewId = self.itemViewIds[@(position)];
|
2019-11-15 19:30:07 +08:00
|
|
|
if (viewId && viewId.length > 0) {
|
|
|
|
return [self subModelOf:viewId];
|
|
|
|
} else {
|
|
|
|
DoricAsyncResult *result = [self callJSResponse:@"renderBunchedItems", @(position), @(self.batchCount), nil];
|
|
|
|
JSValue *models = [result waitUntilResult];
|
|
|
|
NSArray *array = [models toArray];
|
|
|
|
[array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
|
|
|
|
NSString *thisViewId = obj[@"id"];
|
|
|
|
[self setSubModel:obj in:thisViewId];
|
|
|
|
NSUInteger pos = position + idx;
|
2019-11-16 15:10:21 +08:00
|
|
|
self.itemViewIds[@(pos)] = thisViewId;
|
2019-11-15 19:30:07 +08:00
|
|
|
}];
|
|
|
|
return array[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 15:10:21 +08:00
|
|
|
- (void)blendSubNode:(NSDictionary *)subModel {
|
|
|
|
NSString *viewId = subModel[@"id"];
|
|
|
|
[self.itemViewIds enumerateKeysAndObjectsUsingBlock:^(NSNumber *_Nonnull key, NSString *_Nonnull obj, BOOL *_Nonnull stop) {
|
|
|
|
if ([viewId isEqualToString:obj]) {
|
|
|
|
*stop = YES;
|
|
|
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[key integerValue] inSection:0];
|
2019-11-16 15:26:44 +08:00
|
|
|
[UIView performWithoutAnimation:^{
|
|
|
|
[self.view reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
|
|
|
|
}];
|
2019-11-16 15:10:21 +08:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)callItem:(NSUInteger)position height:(CGFloat)height {
|
|
|
|
NSNumber *old = self.itemHeights[@(position)];
|
|
|
|
if (old && old.floatValue == height) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.itemHeights[@(position)] = @(height);
|
|
|
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:position inSection:0];
|
2019-11-16 15:26:44 +08:00
|
|
|
[UIView performWithoutAnimation:^{
|
|
|
|
[self.view reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
|
|
|
|
}];
|
2019-11-16 15:10:21 +08:00
|
|
|
}
|
2019-11-15 19:30:07 +08:00
|
|
|
@end
|