This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
App.js
110 lines (96 loc) · 3.13 KB
/
App.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
import React from 'react';
import { Button, StyleSheet, Text, View } from 'react-native';
import {Countdown, TimeInput, TimerToggleButton} from './components'
import {Timer, vibrate} from './utils'
const DEFAULT_WORK_MINS = 25
const DEFAULT_BREAK_MINS = 5
const minToSec = mins => mins * 60
const nextTimer = {work: 'break', break: 'work'}
export default class App extends React.Component {
state = {
// in seconds
workTime: minToSec(DEFAULT_WORK_MINS),
breakTime: minToSec(DEFAULT_BREAK_MINS),
// in ms
timeRemaining: minToSec(DEFAULT_WORK_MINS) * 1000,
isRunning: false,
activeTimer: 'work',
}
componentDidMount() {
this.timer = new Timer(this.state.timeRemaining, this.updateTimeRemaining, this.handleTimerEnd)
this.setState({isRunning: this.timer.isRunning})
}
componentWillUnmount() {
if (this.timer) this.timer.stop()
}
updateTime = target => (time, shouldStartTimer) => {
if (this.state.activeTimer === target) {
if (this.timer) this.timer.stop()
const timeRemaining = +time * 1000
this.timer = new Timer(timeRemaining, this.updateTimeRemaining, this.handleTimerEnd)
if (!shouldStartTimer) this.timer.stop()
this.setState({[`${target}Time`]: time, timeRemaining, isRunning: this.timer.isRunning})
} else {
this.setState({[`${target}Time`]: time, isRunning: this.timer.isRunning})
}
}
// hack: if an event is passed (ie is button press), stop timer
resetTimer = shouldStopTimer => {
const {activeTimer} = this.state
this.updateTime(activeTimer)(this.state[`${activeTimer}Time`], !shouldStopTimer)
}
updateTimeRemaining = timeRemaining => {
this.setState({timeRemaining})
}
toggleTimer = () => {
if (!this.timer) return
if (this.timer.isRunning) this.timer.stop()
else this.timer.start()
this.setState({isRunning: this.timer.isRunning})
}
handleTimerEnd = () => {
vibrate()
this.setState(prevState => ({activeTimer: nextTimer[prevState.activeTimer]}), this.resetTimer)
}
render() {
return (
<View style={styles.container}>
<Text style={[styles.title, styles.center]}>{this.state.activeTimer.toUpperCase()} TIMER</Text>
<Countdown style={styles.center} timeRemaining={this.state.timeRemaining} onToggleTimer={this.toggleTimer} />
<View style={[styles.buttonGroup, styles.center]}>
<TimerToggleButton onToggle={this.toggleTimer} isRunning={this.state.isRunning} />
<Button title="Reset" onPress={this.resetTimer} />
</View>
<TimeInput
title="Work Time:"
onChange={this.updateTime('work')}
value={this.state.workTime}
/>
<TimeInput
title="Break Time:"
onChange={this.updateTime('break')}
value={this.state.breakTime}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 150,
backgroundColor: '#fff',
alignItems: 'stretch',
},
center: {
alignSelf: 'center',
},
buttonGroup: {
flexDirection: 'row',
},
title: {
fontWeight: 'bold',
fontSize: 48,
textTransform: 'capitalize',
},
});