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.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// DoricSliderNode.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/11/19.
|
|
|
|
//
|
|
|
|
#import <JavaScriptCore/JavaScriptCore.h>
|
|
|
|
#import "DoricSliderNode.h"
|
|
|
|
#import "Doric.h"
|
|
|
|
#import "DoricSlideItemNode.h"
|
|
|
|
|
|
|
|
@interface DoricSliderViewCell : UICollectionViewCell
|
|
|
|
@property(nonatomic, strong) DoricSlideItemNode *doricSlideItemNode;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricSliderViewCell
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface DoricSliderNode () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
|
|
|
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSString *> *itemViewIds;
|
|
|
|
@property(nonatomic, assign) NSUInteger itemCount;
|
2021-07-08 20:02:37 +08:00
|
|
|
@property(nonatomic, assign) NSUInteger propItemCount;
|
2019-12-04 13:29:26 +08:00
|
|
|
@property(nonatomic, assign) NSUInteger batchCount;
|
2019-12-06 15:46:51 +08:00
|
|
|
@property(nonatomic, copy) NSString *onPageSelectedFuncId;
|
2020-04-10 11:06:36 +08:00
|
|
|
@property(nonatomic) BOOL loop;
|
2021-07-08 20:02:37 +08:00
|
|
|
@property(nonatomic) BOOL propLoop;
|
2019-12-06 16:34:20 +08:00
|
|
|
@property(nonatomic, assign) NSUInteger lastPosition;
|
2019-12-07 10:59:20 +08:00
|
|
|
@property(nonatomic, copy) NSString *renderPageFuncId;
|
2021-07-08 20:02:37 +08:00
|
|
|
@property(nonatomic, copy) NSString *propRenderPageFuncId;
|
2019-12-04 13:29:26 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@interface DoricSliderView : UICollectionView
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricSliderView
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricSliderNode
|
|
|
|
- (instancetype)initWithContext:(DoricContext *)doricContext {
|
|
|
|
if (self = [super initWithContext:doricContext]) {
|
|
|
|
_itemViewIds = [NSMutableDictionary new];
|
|
|
|
_batchCount = 15;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UICollectionView *)build {
|
|
|
|
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
|
|
|
|
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
|
|
|
|
return [[[DoricSliderView alloc] initWithFrame:CGRectZero
|
|
|
|
collectionViewLayout:flowLayout]
|
|
|
|
also:^(UICollectionView *it) {
|
|
|
|
it.backgroundColor = [UIColor whiteColor];
|
|
|
|
it.pagingEnabled = YES;
|
|
|
|
it.delegate = self;
|
|
|
|
it.dataSource = self;
|
2021-04-30 14:20:45 +08:00
|
|
|
it.showsHorizontalScrollIndicator = NO;
|
|
|
|
it.showsVerticalScrollIndicator = NO;
|
2019-12-04 13:29:26 +08:00
|
|
|
[it registerClass:[DoricSliderViewCell class] forCellWithReuseIdentifier:@"doricCell"];
|
2020-03-13 16:43:40 +08:00
|
|
|
if (@available(iOS 11, *)) {
|
|
|
|
it.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendView:(UICollectionView *)view forPropName:(NSString *)name propValue:(id)prop {
|
2021-04-23 17:55:59 +08:00
|
|
|
if ([@"scrollable" isEqualToString:name]) {
|
|
|
|
self.view.scrollEnabled = [prop boolValue];
|
|
|
|
} else if ([@"itemCount" isEqualToString:name]) {
|
2021-07-08 20:02:37 +08:00
|
|
|
self.propItemCount = [prop unsignedIntegerValue];
|
2019-12-04 13:29:26 +08:00
|
|
|
} else if ([@"renderPage" isEqualToString:name]) {
|
2021-07-08 20:02:37 +08:00
|
|
|
self.propRenderPageFuncId = prop;
|
2019-12-04 13:29:26 +08:00
|
|
|
} else if ([@"batchCount" isEqualToString:name]) {
|
|
|
|
self.batchCount = [prop unsignedIntegerValue];
|
2019-12-06 15:46:51 +08:00
|
|
|
} else if ([@"onPageSlided" isEqualToString:name]) {
|
|
|
|
self.onPageSelectedFuncId = prop;
|
2020-04-10 11:06:36 +08:00
|
|
|
} else if ([@"loop" isEqualToString:name]) {
|
2021-07-08 20:02:37 +08:00
|
|
|
self.propLoop = [prop boolValue];
|
2021-04-30 14:28:38 +08:00
|
|
|
} else if ([@"bounces" isEqualToString:name]) {
|
|
|
|
self.view.bounces = [prop boolValue];
|
2019-12-04 13:29:26 +08:00
|
|
|
} else {
|
|
|
|
[super blendView:view forPropName:name propValue:prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:02:37 +08:00
|
|
|
- (void)afterBlended:(NSDictionary *)props {
|
|
|
|
bool needToScroll = (self.propLoop && !self.loop)
|
2021-07-23 10:15:18 +08:00
|
|
|
|| (![self.renderPageFuncId isEqualToString:self.propRenderPageFuncId])
|
|
|
|
|| (self.itemCount == 0 && self.propItemCount > 0);
|
|
|
|
|
2021-07-08 20:02:37 +08:00
|
|
|
// handle item count
|
|
|
|
if (self.itemCount != self.propItemCount) {
|
|
|
|
self.itemCount = self.propItemCount;
|
|
|
|
[self.view reloadData];
|
|
|
|
}
|
2021-07-23 10:15:18 +08:00
|
|
|
|
2021-07-08 20:02:37 +08:00
|
|
|
// handle render page
|
|
|
|
if ([self.renderPageFuncId isEqualToString:self.propRenderPageFuncId]) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
[self.itemViewIds removeAllObjects];
|
|
|
|
[self clearSubModel];
|
|
|
|
[self.view reloadData];
|
|
|
|
self.renderPageFuncId = self.propRenderPageFuncId;
|
|
|
|
}
|
2021-07-23 10:15:18 +08:00
|
|
|
|
2021-07-08 20:02:37 +08:00
|
|
|
// handle loop
|
|
|
|
self.loop = self.propLoop;
|
2021-07-23 10:15:18 +08:00
|
|
|
|
2021-07-08 20:02:37 +08:00
|
|
|
__weak typeof(self) _self = self;
|
|
|
|
if (needToScroll) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
__strong typeof(_self) self = _self;
|
2021-07-23 10:15:18 +08:00
|
|
|
|
2021-07-08 20:02:37 +08:00
|
|
|
[self.view reloadData];
|
|
|
|
[self.view setContentOffset:CGPointMake(1 * self.view.width, self.view.contentOffset.y) animated:false];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:29:26 +08:00
|
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
2021-07-08 09:48:24 +08:00
|
|
|
if (self.loop && self.itemCount > 0) {
|
2020-04-10 11:06:36 +08:00
|
|
|
return self.itemCount + 2;
|
|
|
|
} else {
|
|
|
|
return self.itemCount;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
return CGSizeMake(self.view.width, self.view.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
NSUInteger position = (NSUInteger) indexPath.row;
|
|
|
|
NSDictionary *model = [self itemModelAt:position];
|
|
|
|
NSDictionary *props = model[@"props"];
|
|
|
|
DoricSliderViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"doricCell" forIndexPath:indexPath];
|
|
|
|
if (!cell.doricSlideItemNode) {
|
2021-07-23 10:15:18 +08:00
|
|
|
DoricSlideItemNode *slideItemNode = (DoricSlideItemNode *) [DoricViewNode create:self.doricContext withType:@"SlideItem"];
|
2019-12-04 13:29:26 +08:00
|
|
|
[slideItemNode initWithSuperNode:self];
|
|
|
|
cell.doricSlideItemNode = slideItemNode;
|
|
|
|
[cell.contentView addSubview:slideItemNode.view];
|
2021-11-03 13:27:00 +08:00
|
|
|
} else {
|
|
|
|
[cell.doricSlideItemNode reset];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
DoricSlideItemNode *node = cell.doricSlideItemNode;
|
|
|
|
node.viewId = model[@"id"];
|
|
|
|
[node blend:props];
|
2020-04-08 16:41:36 +08:00
|
|
|
[node.view.doricLayout apply:CGSizeMake(collectionView.width, collectionView.height)];
|
2020-04-21 10:54:21 +08:00
|
|
|
[node requestLayout];
|
2019-12-04 13:29:26 +08:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)itemModelAt:(NSUInteger)position {
|
2020-04-10 11:06:36 +08:00
|
|
|
NSUInteger index;
|
|
|
|
if (self.loop) {
|
|
|
|
if (position == 0) {
|
|
|
|
index = self.itemCount - 1;
|
|
|
|
} else if (position == self.itemCount + 1) {
|
|
|
|
index = 0;
|
|
|
|
} else {
|
|
|
|
index = position - 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index = position;
|
|
|
|
}
|
2021-04-14 16:53:38 +08:00
|
|
|
|
2020-04-10 11:06:36 +08:00
|
|
|
NSString *viewId = self.itemViewIds[@(index)];
|
2019-12-04 13:29:26 +08:00
|
|
|
if (viewId && viewId.length > 0) {
|
|
|
|
return [self subModelOf:viewId];
|
|
|
|
} else {
|
2021-04-14 16:53:38 +08:00
|
|
|
DoricAsyncResult *result = [self pureCallJSResponse:@"renderBunchedItems", @(index), @(self.batchCount), nil];
|
2021-01-06 11:56:30 +08:00
|
|
|
NSArray *array = [result waitUntilResult:^(JSValue *models) {
|
|
|
|
return [models toArray];
|
|
|
|
}];
|
2019-12-04 13:29:26 +08:00
|
|
|
[array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
|
|
|
|
NSString *thisViewId = obj[@"id"];
|
|
|
|
[self setSubModel:obj in:thisViewId];
|
2020-04-10 11:06:36 +08:00
|
|
|
NSUInteger pos = index + idx;
|
2019-12-04 13:29:26 +08:00
|
|
|
self.itemViewIds[@(pos)] = thisViewId;
|
|
|
|
}];
|
|
|
|
return array[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (DoricViewNode *)subNodeWithViewId:(NSString *)viewId {
|
|
|
|
__block DoricViewNode *ret = nil;
|
|
|
|
[self.doricContext.driver ensureSyncInMainQueue:^{
|
|
|
|
for (UICollectionViewCell *collectionViewCell in self.view.visibleCells) {
|
|
|
|
if ([collectionViewCell isKindOfClass:[DoricSliderViewCell class]]) {
|
|
|
|
DoricSlideItemNode *node = ((DoricSliderViewCell *) collectionViewCell).doricSlideItemNode;
|
|
|
|
if ([viewId isEqualToString:node.viewId]) {
|
|
|
|
ret = node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendSubNode:(NSDictionary *)subModel {
|
|
|
|
NSString *viewId = subModel[@"id"];
|
|
|
|
DoricViewNode *viewNode = [self subNodeWithViewId:viewId];
|
2021-10-20 17:49:46 +08:00
|
|
|
BOOL skipReload = NO;
|
2019-12-04 13:29:26 +08:00
|
|
|
if (viewNode) {
|
2021-10-20 17:49:46 +08:00
|
|
|
CGSize originSize = viewNode.view.frame.size;
|
2019-12-04 13:29:26 +08:00
|
|
|
[viewNode blend:subModel[@"props"]];
|
2021-10-20 17:49:46 +08:00
|
|
|
[viewNode.view.doricLayout apply:CGSizeMake(self.view.width, self.view.height)];
|
|
|
|
[viewNode requestLayout];
|
|
|
|
if (CGSizeEqualToSize(originSize, viewNode.view.frame.size)) {
|
|
|
|
skipReload = YES;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
} else {
|
|
|
|
NSMutableDictionary *model = [[self subModelOf:viewId] mutableCopy];
|
|
|
|
[self recursiveMixin:subModel to:model];
|
|
|
|
[self setSubModel:model in:viewId];
|
|
|
|
}
|
2021-10-20 17:49:46 +08:00
|
|
|
if (skipReload) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
[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];
|
|
|
|
[UIView performWithoutAnimation:^{
|
|
|
|
[self.view reloadItemsAtIndexPaths:@[indexPath]];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
|
|
|
|
NSUInteger pageIndex = (NSUInteger) (scrollView.contentOffset.x / scrollView.width);
|
|
|
|
scrollView.contentOffset = CGPointMake(pageIndex * scrollView.width, scrollView.contentOffset.y);
|
2021-04-14 16:53:38 +08:00
|
|
|
|
2020-04-10 11:06:36 +08:00
|
|
|
if (self.loop) {
|
|
|
|
if (pageIndex == 0) {
|
|
|
|
[self.view setContentOffset:CGPointMake(self.itemCount * self.view.width, self.view.contentOffset.y) animated:false];
|
2021-07-05 12:05:35 +08:00
|
|
|
pageIndex = self.itemCount - 1;
|
2020-04-10 11:06:36 +08:00
|
|
|
} else if (pageIndex == self.itemCount + 1) {
|
|
|
|
[self.view setContentOffset:CGPointMake(1 * self.view.width, self.view.contentOffset.y) animated:false];
|
2021-07-05 12:05:35 +08:00
|
|
|
pageIndex = 0;
|
|
|
|
} else {
|
|
|
|
pageIndex = pageIndex - 1;
|
2020-04-10 11:06:36 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 16:53:38 +08:00
|
|
|
|
2019-12-06 15:46:51 +08:00
|
|
|
if (self.onPageSelectedFuncId && self.onPageSelectedFuncId.length > 0) {
|
2019-12-06 16:34:20 +08:00
|
|
|
if (pageIndex != self.lastPosition) {
|
|
|
|
[self callJSResponse:self.onPageSelectedFuncId, @(pageIndex), nil];
|
|
|
|
}
|
2019-12-06 15:46:51 +08:00
|
|
|
}
|
2019-12-06 17:12:15 +08:00
|
|
|
self.lastPosition = pageIndex;
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2019-12-06 15:46:51 +08:00
|
|
|
|
|
|
|
- (void)slidePage:(NSDictionary *)params withPromise:(DoricPromise *)promise {
|
|
|
|
NSUInteger pageIndex = [params[@"page"] unsignedIntegerValue];
|
|
|
|
BOOL smooth = [params[@"smooth"] boolValue];
|
2021-07-23 10:15:18 +08:00
|
|
|
|
2021-07-05 12:05:35 +08:00
|
|
|
if (self.loop) {
|
|
|
|
[self.view setContentOffset:CGPointMake((pageIndex + 1) * self.view.width, self.view.contentOffset.y) animated:smooth];
|
|
|
|
} else {
|
|
|
|
[self.view setContentOffset:CGPointMake(pageIndex * self.view.width, self.view.contentOffset.y) animated:smooth];
|
|
|
|
}
|
2021-07-23 10:15:18 +08:00
|
|
|
|
2019-12-06 16:34:20 +08:00
|
|
|
[promise resolve:nil];
|
|
|
|
self.lastPosition = pageIndex;
|
|
|
|
if (self.onPageSelectedFuncId && self.onPageSelectedFuncId.length > 0) {
|
|
|
|
[self callJSResponse:self.onPageSelectedFuncId, @(pageIndex), nil];
|
2019-12-06 15:46:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSNumber *)getSlidedPage {
|
|
|
|
NSUInteger pageIndex = (NSUInteger) (self.view.contentOffset.x / self.view.width);
|
2021-07-05 12:05:35 +08:00
|
|
|
if (self.loop) {
|
|
|
|
return @(pageIndex - 1);
|
|
|
|
} else {
|
|
|
|
return @(pageIndex);
|
|
|
|
}
|
2019-12-06 15:46:51 +08:00
|
|
|
}
|
|
|
|
|
2021-10-27 16:03:11 +08:00
|
|
|
- (void)reset {
|
|
|
|
[super reset];
|
|
|
|
self.view.scrollEnabled = YES;
|
|
|
|
self.onPageSelectedFuncId = nil;
|
|
|
|
self.propRenderPageFuncId = nil;
|
|
|
|
self.renderPageFuncId = nil;
|
|
|
|
}
|
2021-11-12 16:23:01 +08:00
|
|
|
|
|
|
|
- (void)subNodeContentChanged:(DoricViewNode *)subNode {
|
|
|
|
[subNode.view.doricLayout apply];
|
|
|
|
[super subNodeContentChanged:subNode];
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
@end
|