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.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// DoricScrollerNode.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/11/19.
|
|
|
|
//
|
|
|
|
#import "DoricScrollerNode.h"
|
|
|
|
#import "DoricExtensions.h"
|
|
|
|
#import "DoricRefreshableNode.h"
|
2020-03-03 13:10:35 +08:00
|
|
|
#import "DoricPromise.h"
|
2020-03-25 13:30:35 +08:00
|
|
|
#import "DoricJSDispatcher.h"
|
2019-12-04 13:29:26 +08:00
|
|
|
|
|
|
|
@implementation DoricScrollView
|
|
|
|
|
2020-04-03 18:15:43 +08:00
|
|
|
- (void)setContentView:(UIView *)contentView {
|
|
|
|
if (_contentView) {
|
|
|
|
[_contentView removeFromSuperview];
|
|
|
|
}
|
|
|
|
_contentView = contentView;
|
|
|
|
[self addSubview:contentView];
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-03 18:15:43 +08:00
|
|
|
- (CGSize)sizeThatFits:(CGSize)size {
|
|
|
|
if (self.contentView) {
|
|
|
|
[self.contentView.doricLayout apply:self.frame.size];
|
|
|
|
return self.contentView.frame.size;
|
|
|
|
}
|
|
|
|
return CGSizeZero;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layoutSubviews {
|
|
|
|
[super layoutSubviews];
|
|
|
|
self.contentSize = self.contentView.frame.size;
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
@end
|
|
|
|
|
2020-02-13 15:36:01 +08:00
|
|
|
@interface DoricScrollerNode () <UIScrollViewDelegate>
|
2019-12-04 13:29:26 +08:00
|
|
|
@property(nonatomic, strong) DoricViewNode *childNode;
|
|
|
|
@property(nonatomic, copy) NSString *childViewId;
|
2020-03-03 15:51:20 +08:00
|
|
|
@property(nonatomic, copy) NSString *onScrollFuncId;
|
|
|
|
@property(nonatomic, copy) NSString *onScrollEndFuncId;
|
2020-03-10 17:23:55 +08:00
|
|
|
@property(nonatomic, strong) NSMutableSet <DoricDidScrollBlock> *didScrollBlocks;
|
2020-03-25 13:30:35 +08:00
|
|
|
@property(nonatomic, strong) DoricJSDispatcher *jsDispatcher;
|
2019-12-04 13:29:26 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricScrollerNode
|
|
|
|
- (DoricScrollView *)build {
|
2019-12-07 10:59:20 +08:00
|
|
|
return [[DoricScrollView new] also:^(DoricScrollView *it) {
|
2020-02-13 15:36:01 +08:00
|
|
|
it.delegate = self;
|
2020-03-03 11:58:35 +08:00
|
|
|
it.showsHorizontalScrollIndicator = NO;
|
|
|
|
it.showsVerticalScrollIndicator = NO;
|
2019-12-07 10:59:20 +08:00
|
|
|
if (@available(iOS 11, *)) {
|
|
|
|
it.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
|
|
|
}
|
|
|
|
}];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)initWithSuperNode:(DoricSuperNode *)superNode {
|
|
|
|
[super initWithSuperNode:superNode];
|
|
|
|
if ([superNode isKindOfClass:[DoricRefreshableNode class]]) {
|
|
|
|
self.view.bounces = NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:15:43 +08:00
|
|
|
- (void)afterBlended:(NSDictionary *)props {
|
2019-12-04 13:29:26 +08:00
|
|
|
NSDictionary *childModel = [self subModelOf:self.childViewId];
|
|
|
|
if (!childModel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSString *viewId = childModel[@"id"];
|
|
|
|
NSString *type = childModel[@"type"];
|
|
|
|
NSDictionary *childProps = childModel[@"props"];
|
|
|
|
if (self.childNode) {
|
|
|
|
if ([self.childNode.viewId isEqualToString:viewId]) {
|
|
|
|
//skip
|
|
|
|
} else {
|
|
|
|
if (self.reusable && [type isEqualToString:self.childNode.type]) {
|
|
|
|
[self.childNode also:^(DoricViewNode *it) {
|
|
|
|
it.viewId = viewId;
|
|
|
|
[it blend:childProps];
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
self.childNode = [[DoricViewNode create:self.doricContext withType:type] also:^(DoricViewNode *it) {
|
|
|
|
it.viewId = viewId;
|
|
|
|
[it initWithSuperNode:self];
|
|
|
|
[it blend:childProps];
|
|
|
|
self.view.contentView = it.view;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.childNode = [[DoricViewNode create:self.doricContext withType:type] also:^(DoricViewNode *it) {
|
|
|
|
it.viewId = viewId;
|
|
|
|
[it initWithSuperNode:self];
|
|
|
|
[it blend:childProps];
|
|
|
|
self.view.contentView = it.view;
|
|
|
|
}];
|
|
|
|
}
|
2020-03-03 13:23:06 +08:00
|
|
|
if (props[@"contentOffset"]) {
|
|
|
|
NSDictionary *prop = props[@"contentOffset"];
|
|
|
|
self.view.contentOffset = CGPointMake([prop[@"x"] floatValue], [prop[@"y"] floatValue]);
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendView:(DoricScrollView *)view forPropName:(NSString *)name propValue:(id)prop {
|
|
|
|
if ([@"content" isEqualToString:name]) {
|
|
|
|
self.childViewId = prop;
|
2020-03-03 15:51:20 +08:00
|
|
|
} else if ([@"onScroll" isEqualToString:name]) {
|
|
|
|
self.onScrollFuncId = prop;
|
|
|
|
} else if ([@"onScrollEnd" isEqualToString:name]) {
|
|
|
|
self.onScrollEndFuncId = prop;
|
2019-12-04 13:29:26 +08:00
|
|
|
} else {
|
|
|
|
[super blendView:view forPropName:name propValue:prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)blendSubNode:(NSDictionary *)subModel {
|
|
|
|
[self.childNode blend:subModel[@"props"]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (DoricViewNode *)subNodeWithViewId:(NSString *)viewId {
|
|
|
|
if ([viewId isEqualToString:self.childViewId]) {
|
|
|
|
return self.childNode;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
2020-02-13 22:18:13 +08:00
|
|
|
|
2020-02-13 15:36:01 +08:00
|
|
|
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
|
2020-03-10 17:23:55 +08:00
|
|
|
for (DoricDidScrollBlock block in self.didScrollBlocks) {
|
|
|
|
block(scrollView);
|
2020-02-13 22:18:13 +08:00
|
|
|
}
|
2020-03-03 15:51:20 +08:00
|
|
|
if (self.onScrollFuncId) {
|
2020-03-25 13:30:35 +08:00
|
|
|
if (!self.jsDispatcher) {
|
|
|
|
self.jsDispatcher = [DoricJSDispatcher new];
|
|
|
|
}
|
|
|
|
__weak typeof(self) __self = self;
|
|
|
|
[self.jsDispatcher dispatch:^DoricAsyncResult * {
|
|
|
|
__strong typeof(__self) self = __self;
|
|
|
|
return [self callJSResponse:self.onScrollFuncId,
|
|
|
|
@{
|
|
|
|
@"x": @(self.view.contentOffset.x),
|
|
|
|
@"y": @(self.view.contentOffset.y),
|
|
|
|
},
|
|
|
|
nil];
|
|
|
|
}];
|
2020-03-03 15:51:20 +08:00
|
|
|
}
|
2020-02-13 15:36:01 +08:00
|
|
|
}
|
2020-03-03 13:10:35 +08:00
|
|
|
|
2020-03-04 13:57:04 +08:00
|
|
|
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
|
|
|
|
if (self.onScrollEndFuncId) {
|
|
|
|
[self callJSResponse:self.onScrollEndFuncId,
|
|
|
|
@{
|
|
|
|
@"x": @(self.view.contentOffset.x),
|
|
|
|
@"y": @(self.view.contentOffset.y),
|
|
|
|
},
|
|
|
|
nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
|
|
|
|
if (!decelerate) {
|
|
|
|
if (self.onScrollEndFuncId) {
|
|
|
|
[self callJSResponse:self.onScrollEndFuncId,
|
|
|
|
@{
|
|
|
|
@"x": @(self.view.contentOffset.x),
|
|
|
|
@"y": @(self.view.contentOffset.y),
|
|
|
|
},
|
|
|
|
nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 13:10:35 +08:00
|
|
|
- (void)scrollTo:(NSDictionary *)params {
|
|
|
|
BOOL animated = [params[@"animated"] boolValue];
|
|
|
|
NSDictionary *offsetDic = params[@"offset"];
|
|
|
|
CGPoint offset = CGPointMake([offsetDic[@"x"] floatValue], [offsetDic[@"y"] floatValue]);
|
|
|
|
[self.view setContentOffset:offset animated:animated];
|
|
|
|
}
|
2020-03-03 13:54:36 +08:00
|
|
|
|
|
|
|
- (void)scrollBy:(NSDictionary *)params {
|
|
|
|
BOOL animated = [params[@"animated"] boolValue];
|
|
|
|
NSDictionary *offsetDic = params[@"offset"];
|
|
|
|
CGPoint offset = CGPointMake([offsetDic[@"x"] floatValue], [offsetDic[@"y"] floatValue]);
|
|
|
|
[self.view setContentOffset:CGPointMake(
|
|
|
|
MIN(self.view.contentSize.width - self.view.width, MAX(0, offset.x + self.view.contentOffset.x)),
|
|
|
|
MIN(self.view.contentSize.height - self.view.height, MAX(0, offset.y + self.view.contentOffset.y)))
|
|
|
|
animated:animated];
|
|
|
|
}
|
2020-03-10 17:23:55 +08:00
|
|
|
|
|
|
|
- (NSMutableSet<DoricDidScrollBlock> *)didScrollBlocks {
|
|
|
|
if (!_didScrollBlocks) {
|
|
|
|
_didScrollBlocks = [NSMutableSet new];
|
|
|
|
}
|
|
|
|
return _didScrollBlocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener {
|
|
|
|
[self.didScrollBlocks addObject:didScrollListener];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeDidScrollBlock:(__nonnull DoricDidScrollBlock)didScrollListener {
|
|
|
|
[self.didScrollBlocks removeObject:didScrollListener];
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:29:26 +08:00
|
|
|
@end
|