Skip to content

Commit 527d11c

Browse files
lexsfacebook-github-bot-7
authored andcommitted
Support progress in ProgressBarAndroid
Differential Revision: D2626321 fb-gh-sync-id: a358a1db8c8f3b4a41dc9a600ee691e6e60310f3
1 parent 96b76fc commit 527d11c

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

Examples/UIExplorer/ProgressBarAndroidExample.android.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ var React = require('React');
2020
var UIExplorerBlock = require('UIExplorerBlock');
2121
var UIExplorerPage = require('UIExplorerPage');
2222

23+
var TimerMixin = require('react-timer-mixin');
24+
25+
var MovingBar = React.createClass({
26+
mixins: [TimerMixin],
27+
28+
getInitialState: function() {
29+
return {
30+
progress: 0
31+
};
32+
},
33+
34+
componentDidMount: function() {
35+
this.setInterval(
36+
() => {
37+
var progress = (this.state.progress + 0.02) % 1;
38+
this.setState({progress: progress});
39+
}, 50
40+
);
41+
},
42+
43+
render: function() {
44+
return <ProgressBar progress={this.state.progress} {...this.props} />;
45+
},
46+
});
47+
2348
var ProgressBarAndroidExample = React.createClass({
2449

2550
statics: {
@@ -55,9 +80,25 @@ var ProgressBarAndroidExample = React.createClass({
5580
<ProgressBar styleAttr="LargeInverse" />
5681
</UIExplorerBlock>
5782

83+
<UIExplorerBlock title="Horizontal Indeterminate ProgressBar">
84+
<ProgressBar styleAttr="Horizontal" />
85+
</UIExplorerBlock>
86+
87+
<UIExplorerBlock title="Horizontal ProgressBar">
88+
<MovingBar styleAttr="Horizontal" indeterminate={false} />
89+
</UIExplorerBlock>
90+
5891
<UIExplorerBlock title="Large Red ProgressBar">
5992
<ProgressBar styleAttr="Large" color="red" />
6093
</UIExplorerBlock>
94+
95+
<UIExplorerBlock title="Horizontal Black Indeterminate ProgressBar">
96+
<ProgressBar styleAttr="Horizontal" color="black" />
97+
</UIExplorerBlock>
98+
99+
<UIExplorerBlock title="Horizontal Blue ProgressBar">
100+
<MovingBar styleAttr="Horizontal" indeterminate={false} color="blue" />
101+
</UIExplorerBlock>
61102
</UIExplorerPage>
62103
);
63104
},

Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.android.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ var STYLE_ATTRIBUTES = [
2626
'LargeInverse'
2727
];
2828

29+
var indeterminateType = function(props, propName, componentName) {
30+
var checker = function() {
31+
var indeterminate = props[propName];
32+
var styleAttr = props.styleAttr;
33+
if (!indeterminate && styleAttr !== 'Horizontal') {
34+
return new Error('indeterminate=false is only valid for styleAttr=Horizontal');
35+
}
36+
};
37+
38+
return ReactPropTypes.bool(props, propName, componentName) || checker();
39+
};
40+
2941
/**
3042
* React component that wraps the Android-only `ProgressBar`. This component is used to indicate
3143
* that the app is loading or there is some activity in the app.
@@ -62,6 +74,15 @@ var ProgressBarAndroid = React.createClass({
6274
* - LargeInverse
6375
*/
6476
styleAttr: ReactPropTypes.oneOf(STYLE_ATTRIBUTES),
77+
/**
78+
* If the progress bar will show indeterminate progress. Note that this
79+
* can only be false if styleAttr is Horizontal.
80+
*/
81+
indeterminate: indeterminateType,
82+
/**
83+
* The progress value (between 0 and 1).
84+
*/
85+
progress: ReactPropTypes.number,
6586
/**
6687
* Color of the progress bar.
6788
*/
@@ -75,6 +96,7 @@ var ProgressBarAndroid = React.createClass({
7596
getDefaultProps: function() {
7697
return {
7798
styleAttr: 'Large',
99+
indeterminate: true
78100
};
79101
},
80102

ReactAndroid/src/main/java/com/facebook/react/views/progressbar/ProgressBarContainerView.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
* Controls an enclosing ProgressBar. Exists so that the ProgressBar can be recreated if
1818
* the style would change.
1919
*/
20-
class ProgressBarContainerView extends FrameLayout {
20+
/* package */ class ProgressBarContainerView extends FrameLayout {
21+
private static final int MAX_PROGRESS = 1000;
22+
2123
private @Nullable Integer mColor;
24+
private boolean mIndeterminate = true;
25+
private double mProgress;
2226
private @Nullable ProgressBar mProgressBar;
2327

2428
public ProgressBarContainerView(Context context) {
@@ -28,23 +32,35 @@ public ProgressBarContainerView(Context context) {
2832
public void setStyle(@Nullable String styleName) {
2933
int style = ReactProgressBarViewManager.getStyleFromString(styleName);
3034
mProgressBar = new ProgressBar(getContext(), null, style);
35+
mProgressBar.setMax(MAX_PROGRESS);
3136
removeAllViews();
3237
addView(
3338
mProgressBar,
3439
new ViewGroup.LayoutParams(
35-
ViewGroup.LayoutParams.WRAP_CONTENT,
36-
ViewGroup.LayoutParams.WRAP_CONTENT));
40+
ViewGroup.LayoutParams.MATCH_PARENT,
41+
ViewGroup.LayoutParams.MATCH_PARENT));
3742
}
3843

3944
public void setColor(@Nullable Integer color) {
4045
this.mColor = color;
4146
}
4247

48+
public void setIndeterminate(boolean indeterminate) {
49+
mIndeterminate = indeterminate;
50+
}
51+
52+
public void setProgress(double progress) {
53+
mProgress = progress;
54+
}
55+
4356
public void apply() {
4457
if (mProgressBar == null) {
4558
throw new JSApplicationIllegalArgumentException("setStyle() not called");
4659
}
60+
61+
mProgressBar.setIndeterminate(mIndeterminate);
4762
setColor(mProgressBar);
63+
mProgressBar.setProgress((int) (mProgress * MAX_PROGRESS));
4864
}
4965

5066
private void setColor(ProgressBar progressBar) {

ReactAndroid/src/main/java/com/facebook/react/views/progressbar/ReactProgressBarViewManager.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class ReactProgressBarViewManager extends
2727
BaseViewManager<ProgressBarContainerView, ProgressBarShadowNode> {
2828

2929
/* package */ static final String PROP_STYLE = "styleAttr";
30+
/* package */ static final String PROP_INDETERMINATE = "indeterminate";
31+
/* package */ static final String PROP_PROGRESS = "progress";
3032

3133
/* package */ static final String REACT_CLASS = "AndroidProgressBar";
3234
/* package */ static final String DEFAULT_STYLE = "Large";
@@ -51,6 +53,16 @@ public void setColor(ProgressBarContainerView view, @Nullable Integer color) {
5153
view.setColor(color);
5254
}
5355

56+
@ReactProp(name = PROP_INDETERMINATE)
57+
public void setIndeterminate(ProgressBarContainerView view, boolean indeterminate) {
58+
view.setIndeterminate(indeterminate);
59+
}
60+
61+
@ReactProp(name = PROP_PROGRESS)
62+
public void setProgress(ProgressBarContainerView view, double progress) {
63+
view.setProgress(progress);
64+
}
65+
5466
@Override
5567
public ProgressBarShadowNode createShadowNodeInstance() {
5668
return new ProgressBarShadowNode();

0 commit comments

Comments
 (0)