feat: add observeScrollingInterval in coordinator plugin

This commit is contained in:
pengfei.zhou
2023-03-13 22:09:57 +08:00
committed by osborn
parent 7db5d68db5
commit 52e5977d20
12 changed files with 247 additions and 15 deletions

View File

@@ -119,4 +119,57 @@ - (void)setValue:(DoricViewNode *)viewNode isNavBar:(BOOL)isNavBar name:(NSStrin
}
}
- (void)observeScrollingInterval:(NSDictionary *)params withPromise:(DoricPromise *)promise {
__weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self;
NSArray <NSString *> *scrollableIds = [params optArray:@"scrollable"];
DoricViewNode *scrollNode = nil;
for (NSString *value in scrollableIds) {
if (!scrollNode) {
scrollNode = [self.doricContext targetViewNode:value];
} else {
if ([scrollNode isKindOfClass:[DoricSuperNode class]]) {
scrollNode = [((DoricSuperNode *) scrollNode) subNodeWithViewId:value];
}
}
}
if (!scrollNode) {
[promise reject:@"Cannot find scrollable view"];
return;
}
NSString *callbackId = [params optString:@"onScrolledInterval"];
NSArray *observingInterval = [params optArray:@"observingInterval"];
DoricPromise *currentPromise = [[DoricPromise alloc] initWithContext:self.doricContext
callbackId:callbackId];
BOOL leftInclusive = [[params optString:@"inclusive"] isEqualToString:@"Left"];
if ([scrollNode conformsToProtocol:@protocol(DoricScrollableProtocol)]) {
__block NSUInteger rangeIdx = 0;
[(id <DoricScrollableProtocol>) scrollNode addDidScrollBlock:^(UIScrollView *scrollView) {
CGFloat scrollY = scrollView.contentOffset.y;
__block NSUInteger currentRangeIdx = observingInterval.count;
[observingInterval enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL *stop) {
if (leftInclusive) {
if (scrollY < obj.floatValue) {
currentRangeIdx = idx;
*stop = YES;
}
} else {
if (scrollY <= obj.floatValue) {
currentRangeIdx = idx;
*stop = YES;
}
}
}];
if (rangeIdx != currentRangeIdx) {
rangeIdx = currentRangeIdx;
[currentPromise resolve:@(rangeIdx)];
}
}];
} else {
[promise reject:@"Scroller type error"];
}
}];
}
@end