Skip to content

Commit

Permalink
fix(ios): abnormal onFooterPulling callback (#4106)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Nov 1, 2024
1 parent f7b17e1 commit 11a0ee5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,49 @@

#import "HippyFooterRefresh.h"

static NSString *const kContentSizeKey = @"contentSize";
static NSString *const kContentOffsetParamKey = @"contentOffset";

@implementation HippyFooterRefresh

- (void)setScrollView:(UIScrollView *)scrollView {
[super setScrollView:scrollView];
[scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
[scrollView addObserver:self forKeyPath:kContentSizeKey options:NSKeyValueObservingOptionNew context:NULL];
}

- (void)unsetFromScrollView {
[_scrollView removeObserver:self forKeyPath:@"contentSize"];
[_scrollView removeObserver:self forKeyPath:kContentSizeKey];
[super unsetFromScrollView];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
context:(void *)context {
if ([keyPath isEqualToString:@"contentSize"]) {
if ([keyPath isEqualToString:kContentSizeKey]) {
NSValue *sizeValue = change[@"new"];
CGSize size = [sizeValue CGSizeValue];
self.frame = CGRectMake(0, size.height, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
}
}

- (void)scrollViewDidScroll {
if (_scrollView && _scrollView.contentSize.height > 0) {
if (self.onFooterPulling && HippyRefreshStatusStartLoading != [self status] && HippyRefreshStatusFinishLoading != [self status]) {
CGFloat offset = _scrollView.contentOffset.y;
if (offset >= 0) {
self.onFooterPulling(@{ @"contentOffset": @(offset) });
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat contentSizeHeight = scrollView.contentSize.height;
if (self.onFooterPulling && contentSizeHeight > 0) {
if (HippyRefreshStatusStartLoading != [self status] &&
HippyRefreshStatusFinishLoading != [self status]) {
CGFloat offset = scrollView.contentOffset.y;
if (offset >= (contentSizeHeight - CGRectGetHeight(scrollView.bounds))) {
self.onFooterPulling(@{ kContentOffsetParamKey : @(offset) });
}
}
}
}

- (void)scrollViewDidEndDragging {
if (_scrollView) {
CGFloat offset = _scrollView.contentOffset.y;
if (offset > _scrollView.contentSize.height - CGRectGetHeight(_scrollView.bounds) + CGRectGetHeight(self.bounds)) {
self.status = HippyRefreshStatusStartLoading;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView {
CGFloat offset = scrollView.contentOffset.y;
if (offset >= scrollView.contentSize.height - CGRectGetHeight(scrollView.bounds) + CGRectGetHeight(self.bounds)) {
self.status = HippyRefreshStatusStartLoading;
}
}

Expand Down Expand Up @@ -90,7 +93,7 @@ - (void)setStatus:(HippyRefreshStatus)status {
} completion:^(BOOL finished) {
if (self.onFooterReleased) {
CGFloat offset = self.scrollView.contentOffset.y;
self.onFooterReleased(@{@"contentOffset": @(offset)});
self.onFooterReleased(@{ kContentOffsetParamKey : @(offset) });
}
}];
} break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#import "HippyHeaderRefresh.h"

static NSString *const kContentOffsetParamKey = @"contentOffset";

@implementation HippyHeaderRefresh

- (void)setFrame:(CGRect)frame {
Expand All @@ -34,23 +36,24 @@ - (void)setFrame:(CGRect)frame {
[super setFrame:properFrame];
}

- (void)scrollViewDidScroll {
if (_scrollView) {
if (self.onHeaderPulling && HippyRefreshStatusStartLoading != [self status] && HippyRefreshStatusFinishLoading != [self status]) {
CGFloat offset = _scrollView.contentOffset.y;
if (offset <= 0) {
self.onHeaderPulling(@{ @"contentOffset": @(-offset) });
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.onHeaderPulling &&
HippyRefreshStatusStartLoading != [self status] &&
HippyRefreshStatusFinishLoading != [self status]) {
CGFloat offset = scrollView.contentOffset.y;
if (offset <= 0) {
self.onHeaderPulling(@{ kContentOffsetParamKey : @(-offset) });
}
}
// Section cell will stick in wrong position while header is still refresh. in this scenario,the scrollview inset need to be reset.
if ([self status] == HippyRefreshStatusStartLoading || [self status] == HippyRefreshStatusFinishLoading) {
// Section cell will stick in wrong position while header is still refresh.
// in this scenario,the scrollview inset need to be reset.
if ([self status] == HippyRefreshStatusStartLoading ||
[self status] == HippyRefreshStatusFinishLoading) {
[self resetInset];
}
}

- (void)resetInset
{
- (void)resetInset {
CGFloat insetT = - self.scrollView.contentOffset.y > 0 ? - self.scrollView.contentOffset.y : 0;
insetT = insetT > self.frame.size.height ? self.frame.size.height : insetT;

Expand All @@ -69,8 +72,8 @@ - (void)refresh {
}];
}

- (void)scrollViewDidEndDragging {
if (_scrollView && -_scrollView.contentOffset.y > CGRectGetHeight(self.bounds)) {
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView {
if (scrollView && -scrollView.contentOffset.y > CGRectGetHeight(self.bounds)) {
self.status = HippyRefreshStatusStartLoading;
}
}
Expand All @@ -95,7 +98,7 @@ - (void)setStatus:(HippyRefreshStatus)status {
} completion:^(BOOL finished) {
if (self.onHeaderReleased) {
CGFloat offset = self.scrollView.contentOffset.y;
self.onHeaderReleased(@{@"contentOffset": @(offset)});
self.onHeaderReleased(@{ kContentOffsetParamKey : @(offset) });
}
}];
} break;
Expand Down
8 changes: 6 additions & 2 deletions renderer/native/ios/renderer/component/refresh/HippyRefresh.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ typedef NS_ENUM(NSUInteger, HippyRefreshStatus) {

- (void)unsetFromScrollView;

- (void)scrollViewDidEndDragging;
- (void)scrollViewDidScroll;
/// Called when UIScrollView's scrollViewDidEndDragging triggers
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView;

/// Called when UIScrollView's scrollViewDidScroll triggers
/// - Parameter scrollView: UIScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

- (void)refresh;
- (void)refreshFinish;
Expand Down
6 changes: 4 additions & 2 deletions renderer/native/ios/renderer/component/refresh/HippyRefresh.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ - (void)unsetFromScrollView {
[self removeFromSuperview];
}

- (void)scrollViewDidScroll {
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// nop
}

- (void)scrollViewDidEndDragging {
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView {
//nop
}

- (void)setStatus:(HippyRefreshStatus)status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[scrollViewListener scrollViewDidScroll:scrollView];
}
}
[_headerRefreshView scrollViewDidScroll];
[_footerRefreshView scrollViewDidScroll];
[_headerRefreshView scrollViewDidScroll:scrollView];
[_footerRefreshView scrollViewDidScroll:scrollView];
}

- (NSDictionary *)scrollEventDataWithState:(NativeRenderScrollState)state {
Expand Down Expand Up @@ -517,8 +517,8 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL
}
}

[_headerRefreshView scrollViewDidEndDragging];
[_footerRefreshView scrollViewDidEndDragging];
[_headerRefreshView scrollViewDidEndDragging:scrollView];
[_footerRefreshView scrollViewDidEndDragging:scrollView];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
Expand Down

0 comments on commit 11a0ee5

Please sign in to comment.