2016-01-04 15:59:10 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTRefreshControl.h"
|
|
|
|
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2016-02-07 21:39:17 +00:00
|
|
|
@implementation RCTRefreshControl {
|
|
|
|
BOOL _initialRefreshingState;
|
|
|
|
BOOL _isInitialRender;
|
2016-04-17 07:32:28 +00:00
|
|
|
BOOL _currentRefreshingState;
|
2016-02-07 21:39:17 +00:00
|
|
|
}
|
2016-01-04 15:59:10 +00:00
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
[self addTarget:self action:@selector(refreshControlValueChanged) forControlEvents:UIControlEventValueChanged];
|
2016-02-07 21:39:17 +00:00
|
|
|
_isInitialRender = true;
|
2016-04-17 07:32:28 +00:00
|
|
|
_currentRefreshingState = false;
|
2016-01-04 15:59:10 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
|
|
|
|
2016-02-07 21:39:17 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
2016-02-19 13:54:46 +00:00
|
|
|
|
2016-02-07 21:39:17 +00:00
|
|
|
// If the control is refreshing when mounted we need to call
|
|
|
|
// beginRefreshing in layoutSubview or it doesn't work.
|
2016-05-13 21:40:47 +00:00
|
|
|
if (_currentRefreshingState && _isInitialRender && _initialRefreshingState) {
|
2016-02-07 21:39:17 +00:00
|
|
|
[self beginRefreshing];
|
|
|
|
}
|
|
|
|
_isInitialRender = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)beginRefreshing
|
|
|
|
{
|
|
|
|
// When using begin refreshing we need to adjust the ScrollView content offset manually.
|
|
|
|
UIScrollView *scrollView = (UIScrollView *)self.superview;
|
|
|
|
CGPoint offset = {scrollView.contentOffset.x, scrollView.contentOffset.y - self.frame.size.height};
|
|
|
|
// Don't animate when the prop is set initialy.
|
|
|
|
if (_isInitialRender) {
|
2016-02-19 13:54:46 +00:00
|
|
|
scrollView.contentOffset = offset;
|
2016-02-07 21:39:17 +00:00
|
|
|
[super beginRefreshing];
|
|
|
|
} else {
|
|
|
|
// `beginRefreshing` must be called after the animation is done. This is why it is impossible
|
|
|
|
// to use `setContentOffset` with `animated:YES`.
|
|
|
|
[UIView animateWithDuration:0.25
|
|
|
|
delay:0
|
|
|
|
options:UIViewAnimationOptionBeginFromCurrentState
|
|
|
|
animations:^(void) {
|
|
|
|
[scrollView setContentOffset:offset];
|
|
|
|
} completion:^(__unused BOOL finished) {
|
|
|
|
[super beginRefreshing];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 12:00:02 +00:00
|
|
|
- (void)endRefreshing
|
|
|
|
{
|
|
|
|
// The contentOffset of the scrollview MUST be greater than 0 before calling
|
|
|
|
// endRefreshing otherwise the next pull to refresh will not work properly.
|
|
|
|
UIScrollView *scrollView = (UIScrollView *)self.superview;
|
|
|
|
if (scrollView.contentOffset.y < 0) {
|
2016-04-13 15:14:13 +00:00
|
|
|
CGPoint offset = {scrollView.contentOffset.x, -scrollView.contentInset.top};
|
2016-03-08 12:00:02 +00:00
|
|
|
[UIView animateWithDuration:0.25
|
|
|
|
delay:0
|
|
|
|
options:UIViewAnimationOptionBeginFromCurrentState
|
|
|
|
animations:^(void) {
|
|
|
|
[scrollView setContentOffset:offset];
|
|
|
|
} completion:^(__unused BOOL finished) {
|
|
|
|
[super endRefreshing];
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
[super endRefreshing];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 15:59:10 +00:00
|
|
|
- (NSString *)title
|
|
|
|
{
|
|
|
|
return self.attributedTitle.string;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTitle:(NSString *)title
|
|
|
|
{
|
2016-04-16 22:14:50 +00:00
|
|
|
NSRange range = NSMakeRange(0, self.attributedTitle.length);
|
|
|
|
NSDictionary *attrs = [self.attributedTitle attributesAtIndex:0 effectiveRange: &range];
|
|
|
|
self.attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrs];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTitleColor:(UIColor *)color
|
|
|
|
{
|
|
|
|
NSRange range = NSMakeRange(0, self.attributedTitle.length);
|
|
|
|
NSDictionary *attrs = [self.attributedTitle attributesAtIndex:0 effectiveRange: &range];
|
|
|
|
NSMutableDictionary *attrsMutable = [attrs mutableCopy];
|
|
|
|
[attrsMutable setObject:color forKey:NSForegroundColorAttributeName];
|
|
|
|
self.attributedTitle = [[NSAttributedString alloc] initWithString:self.attributedTitle.string attributes:attrsMutable];
|
2016-01-04 15:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRefreshing:(BOOL)refreshing
|
|
|
|
{
|
2016-04-17 07:32:28 +00:00
|
|
|
if (_currentRefreshingState != refreshing) {
|
|
|
|
_currentRefreshingState = refreshing;
|
|
|
|
|
2016-01-04 15:59:10 +00:00
|
|
|
if (refreshing) {
|
2016-02-07 21:39:17 +00:00
|
|
|
// If it is the initial render, beginRefreshing will get called
|
|
|
|
// in layoutSubviews.
|
|
|
|
if (_isInitialRender) {
|
|
|
|
_initialRefreshingState = refreshing;
|
|
|
|
} else {
|
|
|
|
[self beginRefreshing];
|
|
|
|
}
|
2016-01-04 15:59:10 +00:00
|
|
|
} else {
|
|
|
|
[self endRefreshing];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)refreshControlValueChanged
|
|
|
|
{
|
2016-04-17 07:32:28 +00:00
|
|
|
_currentRefreshingState = super.refreshing;
|
|
|
|
|
2016-01-04 15:59:10 +00:00
|
|
|
if (_onRefresh) {
|
|
|
|
_onRefresh(nil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|