66 * Copyright (c) 2017 ljunb <cookiejlim@gmail.com>
77 */
88
9- import React , { PureComponent , PropTypes } from 'react' ;
9+ import React , { PureComponent , PropTypes } from 'react' ;
1010import {
1111 StyleSheet ,
1212 Text ,
1313 TouchableOpacity ,
1414 ViewPropTypes ,
1515 AppState ,
16- NetInfo
16+ NetInfo ,
1717} from 'react-native' ;
1818
1919/**
@@ -25,10 +25,26 @@ import {
2525const CountdownStatus = {
2626 None : 0 ,
2727 Counting : 1 ,
28- Over : 2
28+ Over : 2 ,
2929} ;
3030
31- class Countdown extends PureComponent {
31+ const styles = StyleSheet . create ( {
32+ container : {
33+ borderWidth : StyleSheet . hairlineWidth ,
34+ borderColor : '#ebebeb' ,
35+ borderRadius : 2 ,
36+ height : 30 ,
37+ width : 100 ,
38+ justifyContent : 'center' ,
39+ alignItems : 'center'
40+ } ,
41+ title : {
42+ fontSize : 12 ,
43+ color : '#aaa' ,
44+ } ,
45+ } ) ;
46+
47+ export default class Countdown extends PureComponent {
3248 static propTypes = {
3349 style : ViewPropTypes . style ,
3450 title : PropTypes . string ,
@@ -39,7 +55,7 @@ class Countdown extends PureComponent {
3955 countingTitleTemplate : PropTypes . string ,
4056 countingTitleStyle : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . number , PropTypes . array ] ) ,
4157 shouldStartCountdown : PropTypes . func ,
42- onNetworkFailed : PropTypes . func
58+ onNetworkFailed : PropTypes . func ,
4359 } ;
4460
4561 static defaultProps = {
@@ -56,7 +72,7 @@ class Countdown extends PureComponent {
5672 this . state = {
5773 second : props . time ,
5874 status : CountdownStatus . None ,
59- isConnected : true
75+ isConnected : true ,
6076 }
6177 }
6278
@@ -71,12 +87,17 @@ class Countdown extends PureComponent {
7187 NetInfo . isConnected . removeEventListener ( 'change' , this . handleNetworkConnectivityChange ) ;
7288 }
7389
74- handleNetworkConnectivityChange = isConnected => this . setState ( { isConnected} ) ;
90+ handleNetworkConnectivityChange = isConnected => this . setState ( { isConnected } ) ;
91+
92+ startCountdown = ( ) => {
93+ if ( this . state . status === CountdownStatus . Counting ) return ;
94+ this . handlePress ( ) ;
95+ }
7596
7697 stopCountdown = ( ) => {
7798 this . setState ( {
7899 status : CountdownStatus . Over ,
79- second : this . props . time
100+ second : this . props . time ,
80101 } , this . clearTimer ) ;
81102 } ;
82103
@@ -93,26 +114,27 @@ class Countdown extends PureComponent {
93114 turnsOnTimer = ( ) => {
94115 const now = new Date ( ) ;
95116 const diff = Math . round ( ( now - this . recodTime ) / 1000 ) ;
117+ // timer should be over
96118 if ( this . state . second - diff <= 0 ) {
97- this . setState ( { status : CountdownStatus . Over , second : this . props . time } ) ;
119+ this . setState ( { status : CountdownStatus . Over , second : this . props . time } ) ;
98120 } else {
99121 this . setState ( {
100122 status : CountdownStatus . Counting ,
101- second : this . state . second - diff
102- } , this . startTimer )
123+ second : this . state . second - diff ,
124+ } , this . startTimer ) ;
103125 }
104126 } ;
105127
106128 handlePress = ( ) => {
107129 if ( this . isNetworkFailed ( ) || ! this . canStartTimer ( ) ) return ;
108130
109- this . setState ( { status : CountdownStatus . Counting } , this . startTimer ) ;
131+ this . setState ( { status : CountdownStatus . Counting } , this . startTimer ) ;
110132 this . shouldShowWarningInfo ( ) ;
111133 } ;
112134
113135 isNetworkFailed = ( ) => {
114- const { onNetworkFailed} = this . props ;
115- const { isConnected} = this . state ;
136+ const { onNetworkFailed } = this . props ;
137+ const { isConnected } = this . state ;
116138 // network is failed
117139 if ( ! isConnected ) {
118140 onNetworkFailed && onNetworkFailed ( ) ;
@@ -121,42 +143,37 @@ class Countdown extends PureComponent {
121143 } ;
122144
123145 canStartTimer = ( ) => {
124- const { shouldHandleBeforeCountdown, shouldStartCountdown} = this . props ;
125-
126- let canStartTimer = shouldStartCountdown ( ) ;
127- if ( shouldHandleBeforeCountdown !== undefined && typeof shouldHandleBeforeCountdown === 'function' ) {
128- canStartTimer = shouldHandleBeforeCountdown ( ) ;
129- console . warn ( `[rn-countdown] Warning: "shouldHandleBeforeCountdown" is deprecated, use "shouldStartCountdown" instead.` ) ;
130- }
131- return canStartTimer ;
146+ const { shouldStartCountdown } = this . props ;
147+ return shouldStartCountdown ( ) ;
132148 } ;
133149
134150 shouldShowWarningInfo = ( ) => {
135- const { countingTitleTemplate} = this . props ;
151+ const { countingTitleTemplate } = this . props ;
136152 const isCorrectFormat = countingTitleTemplate . includes ( '{time}' ) ;
137153 if ( ! isCorrectFormat ) {
138154 console . warn ( "[rn-countdown] Warning: Failed prop format: Invalid prop `countingTitleTemplate` of format without substring `{time}`." ) ;
139155 }
140156 } ;
141157
142158 startTimer = ( ) => {
143- const { time} = this . props ;
159+ const { time } = this . props ;
144160
145161 this . timer = setInterval ( ( ) => {
146162 let nextSecond = this . state . second - 1 ;
163+ // countdown over
147164 if ( nextSecond === 0 ) {
148165 this . clearTimer ( ) ;
149- this . setState ( { status : CountdownStatus . Over , second : time } ) ;
166+ this . setState ( { status : CountdownStatus . Over , second : time } ) ;
150167 return ;
151168 }
152- this . setState ( { second : nextSecond } ) ;
169+ this . setState ( { second : nextSecond } ) ;
153170 } , 1000 ) ;
154171 } ;
155172
156173 clearTimer = ( ) => this . timer && clearInterval ( this . timer ) ;
157174
158175 render ( ) {
159- const { status, second} = this . state ;
176+ const { status, second } = this . state ;
160177 const {
161178 title, style, overTitle, titleStyle,
162179 countingTitleTemplate, countingStyle, countingTitleStyle
@@ -176,31 +193,13 @@ class Countdown extends PureComponent {
176193
177194 return (
178195 < TouchableOpacity
179- disabled = { status === CountdownStatus . Counting }
196+ disabled = { status === CountdownStatus . Counting }
180197 activeOpacity = { 0.75 }
181198 style = { containerStyle }
182199 onPress = { this . handlePress }
183200 >
184201 < Text style = { textStyle } > { promptTitle } </ Text >
185202 </ TouchableOpacity >
186- )
187- }
188- }
189-
190- const styles = StyleSheet . create ( {
191- container : {
192- borderWidth : StyleSheet . hairlineWidth ,
193- borderColor : '#ebebeb' ,
194- borderRadius : 2 ,
195- height : 30 ,
196- width : 100 ,
197- justifyContent : 'center' ,
198- alignItems : 'center'
199- } ,
200- title : {
201- fontSize : 12 ,
202- color : '#aaa' ,
203+ ) ;
203204 }
204- } ) ;
205-
206- export default Countdown ;
205+ }
0 commit comments