Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 10718: Add iOS support for progressViewOffset #30737

Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

'use strict';

import type {DirectEventHandler, WithDefault} from '../../Types/CodegenTypes';
import type {
DirectEventHandler,
Float,
WithDefault,
} from '../../Types/CodegenTypes';
import type {ColorValue} from '../../StyleSheet/StyleSheet';
import type {ViewProps} from '../View/ViewPropTypes';
import * as React from 'react';
Expand All @@ -34,6 +38,10 @@ type NativeProps = $ReadOnly<{|
* The title displayed under the refresh indicator.
*/
title?: WithDefault<string, null>,
/**
* Progress view top offset
*/
progressViewOffset?: WithDefault<Float, 0>,

/**
* Called when the view starts refreshing.
Expand Down
10 changes: 5 additions & 5 deletions Libraries/Components/RefreshControl/RefreshControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ type AndroidProps = $ReadOnly<{|
| typeof RefreshLayoutConsts.SIZE.DEFAULT
| typeof RefreshLayoutConsts.SIZE.LARGE
),
/**
* Progress view top offset
*/
progressViewOffset?: ?number,
|}>;

export type RefreshControlProps = $ReadOnly<{|
Expand All @@ -89,6 +85,11 @@ export type RefreshControlProps = $ReadOnly<{|
* Whether the view should be indicating an active refresh.
*/
refreshing: boolean,

/**
* Progress view top offset
*/
progressViewOffset?: ?number,
|}>;

/**
Expand Down Expand Up @@ -181,7 +182,6 @@ class RefreshControl extends React.Component<RefreshControlProps> {
colors,
progressBackgroundColor,
size,
progressViewOffset,
...props
} = this.props;
return (
Expand Down
1 change: 0 additions & 1 deletion Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ type OptionalProps = {|
persistentScrollbar?: ?boolean,
/**
* Set this when offset is needed for the loading indicator to show correctly.
* @platform android
*/
progressViewOffset?: number,
/**
Expand Down
21 changes: 21 additions & 0 deletions React/Views/RefreshControl/RCTRefreshControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ @implementation RCTRefreshControl {
BOOL _refreshingProgrammatically;
NSString *_title;
UIColor *_titleColor;
float _progressViewOffset;
}

- (instancetype)init
Expand All @@ -40,6 +41,7 @@ - (instancetype)init
- (void)layoutSubviews
{
[super layoutSubviews];
[self applyProgressViewOffset];

// If the control is refreshing when mounted we need to call
// beginRefreshing in layoutSubview or it doesn't work.
Expand Down Expand Up @@ -108,6 +110,19 @@ - (void)endRefreshingProgrammatically
}
}

- (void)applyProgressViewOffset
{
// progressViewOffset must be converted from the ScrollView parent's coordinate space to
// the coordinate space of the RefreshControl. This ensures that the control respects any
// offset in the view hierarchy, and that progressViewOffset is not inadvertently applied
// multiple times.
UIView *scrollView = self.superview;
UIView *target = scrollView == nil ? nil : scrollView.superview;
davidbiedenbach marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a tip about obj-c :) Btw you can just call scrollView.superview. In case scrollView is nil, nil is returned from the entire expression. No need to handle nil case explicitly.

I'll change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh. You're right, I completely forgot that dot syntax is just sending a message to a property getter here, isn't it? I'll try to remember that for next time. Thanks for the tip (and the correction)!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sending a message to nil does nothing in Obj-c. It is a feature, not a bug :)

I'll merge this on Monday.

Thank you.

CGPoint rawOffset = CGPointMake(0, _progressViewOffset);
CGPoint converted = [self convertPoint:rawOffset fromView:target];
self.frame = CGRectOffset(self.frame, 0, converted.y);
}

- (NSString *)title
{
return _title;
Expand Down Expand Up @@ -160,6 +175,12 @@ - (void)setCurrentRefreshingState:(BOOL)refreshing
_currentRefreshingStateTimestamp = _currentRefreshingStateClock++;
}

- (void)setProgressViewOffset:(float)offset
{
_progressViewOffset = offset;
[self applyProgressViewOffset];
}

- (void)refreshControlValueChanged
{
[self setCurrentRefreshingState:super.refreshing];
Expand Down
1 change: 1 addition & 0 deletions React/Views/RefreshControl/RCTRefreshControlManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(title, NSString)
RCT_EXPORT_VIEW_PROPERTY(titleColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(progressViewOffset, float)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this into CGFloat. It better expresses the use of the property.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks for the feedback!


RCT_EXPORT_METHOD(setNativeRefreshing : (nonnull NSNumber *)viewTag toRefreshing : (BOOL)refreshing)
{
Expand Down