Created
May 17, 2019 19:25
-
-
Save devmeireles/be5230bc865f36608a0b00f4131a39aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { | |
StyleSheet, | |
Text, | |
View, | |
StatusBar, | |
TouchableOpacity, | |
Dimensions, | |
Picker, | |
Platform | |
} from "react-native"; | |
const screen = Dimensions.get("window"); | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: "#07121B", | |
alignItems: "center", | |
justifyContent: "center" | |
}, | |
button: { | |
borderWidth: 10, | |
borderColor: "#89AAFF", | |
width: screen.width / 2, | |
height: screen.width / 2, | |
borderRadius: screen.width / 2, | |
alignItems: "center", | |
justifyContent: "center", | |
marginTop: 30 | |
}, | |
buttonStop: { | |
borderColor: "#FF851B" | |
}, | |
buttonText: { | |
fontSize: 45, | |
color: "#89AAFF" | |
}, | |
buttonTextStop: { | |
color: "#FF851B" | |
}, | |
timerText: { | |
color: "#fff", | |
fontSize: 90 | |
}, | |
picker: { | |
width: 50, | |
...Platform.select({ | |
android: { | |
color: "#fff", | |
backgroundColor: "#07121B", | |
marginLeft: 10 | |
} | |
}) | |
}, | |
pickerItem: { | |
color: "#fff", | |
fontSize: 20 | |
}, | |
pickerContainer: { | |
flexDirection: "row", | |
alignItems: "center" | |
} | |
}); | |
// 3 => 03, 10 => 10 | |
const formatNumber = number => `0${number}`.slice(-2); | |
const getRemaining = time => { | |
const minutes = Math.floor(time / 60); | |
const seconds = time - minutes * 60; | |
return { minutes: formatNumber(minutes), seconds: formatNumber(seconds) }; | |
}; | |
const createArray = length => { | |
const arr = []; | |
let i = 0; | |
while (i < length) { | |
arr.push(i.toString()); | |
i += 1; | |
} | |
return arr; | |
}; | |
const AVAILABLE_MINUTES = createArray(60); | |
const AVAILABLE_SECONDS = createArray(60); | |
export default class App extends React.Component { | |
state = { | |
remainingSeconds: 5, | |
isRunning: false, | |
selectedMinutes: "0", | |
selectedSeconds: "5" | |
}; | |
interval = null; | |
componentDidUpdate(prevProp, prevState) { | |
if (this.state.remainingSeconds === 0 && prevState.remainingSeconds !== 0) { | |
this.stop(); | |
} | |
} | |
componentWillUnmount() { | |
if (this.interval) { | |
clearInterval(this.interval); | |
} | |
} | |
start = () => { | |
this.setState(state => ({ | |
remainingSeconds: | |
parseInt(state.selectedMinutes, 10) * 60 + | |
parseInt(state.selectedSeconds, 10), | |
isRunning: true | |
})); | |
this.interval = setInterval(() => { | |
this.setState(state => ({ | |
remainingSeconds: state.remainingSeconds - 1 | |
})); | |
}, 1000); | |
}; | |
stop = () => { | |
clearInterval(this.interval); | |
this.interval = null; | |
this.setState({ | |
remainingSeconds: 5, // temporary | |
isRunning: false | |
}); | |
}; | |
renderPickers = () => ( | |
<View style={styles.pickerContainer}> | |
<Picker | |
style={styles.picker} | |
itemStyle={styles.pickerItem} | |
selectedValue={this.state.selectedMinutes} | |
onValueChange={itemValue => { | |
this.setState({ selectedMinutes: itemValue }); | |
}} | |
mode="dropdown" | |
> | |
{AVAILABLE_MINUTES.map(value => ( | |
<Picker.Item key={value} label={value} value={value} /> | |
))} | |
</Picker> | |
<Text style={styles.pickerItem}>minutes</Text> | |
<Picker | |
style={styles.picker} | |
itemStyle={styles.pickerItem} | |
selectedValue={this.state.selectedSeconds} | |
onValueChange={itemValue => { | |
this.setState({ selectedSeconds: itemValue }); | |
}} | |
mode="dropdown" | |
> | |
{AVAILABLE_SECONDS.map(value => ( | |
<Picker.Item key={value} label={value} value={value} /> | |
))} | |
</Picker> | |
<Text style={styles.pickerItem}>seconds</Text> | |
</View> | |
); | |
render() { | |
const { minutes, seconds } = getRemaining(this.state.remainingSeconds); | |
return ( | |
<View style={styles.container}> | |
<StatusBar barStyle="light-content" /> | |
{this.state.isRunning ? ( | |
<Text style={styles.timerText}>{`${minutes}:${seconds}`}</Text> | |
) : ( | |
this.renderPickers() | |
)} | |
{this.state.isRunning ? ( | |
<TouchableOpacity | |
onPress={this.stop} | |
style={[styles.button, styles.buttonStop]} | |
> | |
<Text style={[styles.buttonText, styles.buttonTextStop]}>Stop</Text> | |
</TouchableOpacity> | |
) : ( | |
<TouchableOpacity onPress={this.start} style={styles.button}> | |
<Text style={styles.buttonText}>Start</Text> | |
</TouchableOpacity> | |
)} | |
</View> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment