-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtimerText.js
174 lines (153 loc) · 3.99 KB
/
timerText.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// @flow
import * as React from 'react';
import {
Animated,
Easing,
StyleSheet,
Text,
View,
} from 'react-native';
// const ViewPropTypesStyle = ViewPropTypes
// ? ViewPropTypes.style
// : View.propTypes.style;
const styles = StyleSheet.create({
textBase: {
alignItems: 'center',
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
},
});
type Props = {
active: boolean,
isPausable?: boolean,
onTimeElapsed: Function,
reverseCount: boolean,
seconds: number,
startAt: number,
subTextStyle: Text.propTypes.style | Array<Text.propTypes.style>,
textStyle: Text.propTypes.style | Array<Text.propTypes.style>,
}
type Default = {
active: boolean,
isPausable: boolean,
onTimeElapsed: Function,
seconds: number,
startAT: number,
subTextStyle: Text.propTypes.style | Array<Text.propTypes.style>,
textStyle: Text.propTypes.style | Array<Text.propTypes.style>,
}
type State = {
active: boolean,
reverseCount: boolean,
timeProgress: any,
timeText: string,
timeReverse: any,
time: any,
};
const secondsToHms = (rd: number): string => {
const d = Number(rd);
// const h = Math.floor(d / 3600);
const m = Math.floor((d % 3600) / 60);
const s = Math.floor((d % 3600) % 60);
// const hDisplay = h > 0 ? h : '';
const mDisplay = m > 9 ? m : `0${m}`;
const sDisplay = s > 9 ? s : `0${s}`;
return `${mDisplay}:${sDisplay}`;
};
export function getInitialStateText(props: Props): State {
const timeProgress = new Animated.Value(0);
return {
timeProgress,
time: props.seconds,
timeReverse: 0,
timeText: (props.reverseCount) ? secondsToHms(0) : secondsToHms(props.seconds),
reverseCount: props.reverseCount,
active: props.active,
};
}
export default class TextTimeComponent extends React.Component<Default, Props, State> {
static defaultProps = {
active: true,
isPausable: false,
onTimeElapsed: () => null,
seconds: 10,
subTextStyle: null,
textStyle: null,
}
constructor(props: Props) {
super(props);
// this.isStarted = false;
this.state = getInitialStateText(props);
}
state: State = {
...getInitialStateText(this.props),
timeReverse: this.props.startAt,
}
componentDidMount = () => {
this.refreshTime();
this.setState({
timeReverse: this.props.startAt,
});
};
componentWillReceiveProps = (nextProps: Props) => {
if (nextProps.startAt > 0) {
this.state.timeReverse = nextProps.startAt;
this.setState({
timeReverse: nextProps.startAt,
});
}
};
componentWillUnmount = () => {
this.state.timeProgress.stopAnimation();
};
refreshTime = () => {
Animated.timing(this.state.timeProgress, {
toValue: 100,
duration: 1000,
easing: Easing.linear,
}).start(this.updateTime);
}
updateTime = () => {
const timeReverse = (this.props.reverseCount)
? this.state.timeReverse + 1
: 0;
const time = (this.props.reverseCount) ? this.state.time + 1 : this.state.time - 1;
const timeText = (this.props.reverseCount) ? secondsToHms(timeReverse) : secondsToHms(time);
const callback = (time <= 0) ? this.props.onTimeElapsed : this.refreshTime;
this.setState({
...getInitialStateText(this.props),
time,
timeReverse,
timeText,
}, callback);
}
renderSubText() {
const { active } = this.props;
if (this.props.isPausable) {
return (
<Text
numberOfLines={1} ellipsizeMode="head"
style={[this.props.subTextStyle, active ? { opacity: 0.8 } : { opacity: 1 }]}
>
{active ? 'Pause' : 'Resume'}
</Text>
);
}
return (<View />);
}
render() {
const { active } = this.props;
return (
<View style={styles.textBase}>
<Text
numberOfLines={1} ellipsizeMode="head"
style={[this.props.textStyle, active ? { opacity: 1 } : { opacity: 0.8 }]}
>
{this.state.timeText}
</Text>
{this.renderSubText()}
</View>
);
}
}