-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
263 lines (246 loc) · 7.93 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import React, { Component } from 'react'
import {
SafeAreaView, ScrollView, View, Image, Text,
Picker, Switch,
} from 'react-native'
import styles from './App.styles'
import Clock from './src/components/Clock/Clock'
import defaultClockSettings from './src/components/Clock/Clock.config'
import { calcHour, calcMinute, calcAMPM } from './src/utils/timeUtils'
import { calcDegrees } from './src/utils/angleUtils'
import mockLogo from './src/images/mockLogo.jpg'
class App extends Component {
constructor(props) {
super(props)
this.state = {
currentHour: 0,
currentMinute: 0,
currentSecond: 0,
isClockControlled: false,
isAfternoon: true,
}
this.clockTimer = null
}
/**
* Clock updating is managed via a 1000ms timer.
*/
componentDidMount() {
this.clockTimer = setInterval(() => {
const d = new Date()
return this.setState({
currentHour: d.getHours(),
currentMinute: d.getMinutes(),
currentSecond: d.getSeconds(),
})
}, 1000)
}
/**
* The clock timer must be explicitly garbage collected
* when the App unmounts.
*/
componentWillUnmount() {
return clearInterval(this.clockTimer)
}
/**
* If the clock is currently user controlled, `isAfternoon`
* truthy is PM, and `isAfternoon` falsy is AM.
*/
toggleAfternoon = () => this.setState(prevState => ({
isAfternoon: !prevState.isAfternoon,
}))
/**
* When the clock is controlled by the user, the auto-update
* timer should be disabled. When the clock is uncontrolled,
* the clock update timer should be re-enabled.
*/
toggleClockControl = () => this.setState((prevState) => {
if (prevState.isClockControlled === false) {
clearInterval(this.clockTimer)
return {
currentHour: 2,
currentMinute: 30,
isClockControlled: true,
}
}
this.clockTimer = setInterval(() => {
const d = new Date()
return this.setState({
currentHour: d.getHours(),
currentMinute: d.getMinutes(),
currentSecond: d.getSeconds(),
})
}, 1000)
return {
currentHour: 0,
currentMinute: 0,
currentSecond: 0,
isClockControlled: false,
}
})
/**
* Time is read-only in uncontrolled mode, and a Picker is used
* in controlled mode to allow custom display.
* Picker items are generated.
*/
renderHour = () => (
<Text testID="currentHourDisplay" style={styles.timeDisplay}>
{calcHour(this.state.currentHour).toString()}
</Text>
)
renderHourPicker = () => (
<Picker
enabled
testID="hourPicker"
mode="dropdown"
style={styles.timePicker}
selectedValue={this.state.currentHour}
onValueChange={hour => this.setState({ currentHour: +hour })}
>
{Array.from(Array(12), (_, x) => x).map(hour => (
<Picker.Item
key={`hour${hour + 1}`}
label={`${hour + 1}`}
value={hour + 1}
/>
))}
</Picker>
)
renderMinute = () => (
<Text testID="currentMinuteDisplay" style={styles.timeDisplay}>
{calcMinute(this.state.currentMinute).toString()}
</Text>
)
renderMinutePicker = () => (
<Picker
enabled
testID="minutePicker"
mode="dropdown"
style={styles.timePicker}
selectedValue={calcMinute(this.state.currentMinute).toString()}
onValueChange={minute => this.setState({ currentMinute: +minute })}
>
{Array.from(Array(60), (_, x) => x).map(min => (
<Picker.Item
key={`minute${min}`}
label={(min < 10) ? `0${min}` : `${min}`}
value={(min < 10) ? `0${min}` : `${min}`}
/>
))}
</Picker>
)
/**
* If clock control is currently handled automatically, the
* AM/PM status is determined by the current hour of the day.
* If clock control is delegated to the user, the AM/PM status
* is determined by `isAfternoon`.
*/
renderAMPM = () => {
const { currentHour, isClockControlled, isAfternoon } = this.state
if (!isClockControlled) {
return calcAMPM(currentHour)
}
return isAfternoon ? 'PM' : 'AM'
}
render() {
const {
currentHour, currentMinute, currentSecond,
isClockControlled, isAfternoon, errorMessage,
} = this.state
return (
<SafeAreaView style={styles.safeArea}>
<ScrollView contentContainerStyle={styles.appWrapper}>
<View style={styles.appContainer}>
<View style={styles.headingContainer}>
<Image
style={styles.headingLogo}
source={mockLogo}
/>
<Text testID="headingText" style={styles.headingText}>
INANGLES
</Text>
</View>
<View style={styles.subheadingContainer}>
<Text testID="subheadingTime" style={styles.subheadingText}>
TIME
</Text>
<View style={styles.subheadingUnderline} />
<View style={styles.clockControlContainer}>
<Text>Specify custom time:</Text>
<Switch
testID="toggleControl"
onValueChange={() => this.toggleClockControl()}
thumbTintColor="#278eca"
tintColor="#757575"
onTintColor="#757575"
value={isClockControlled}
/>
</View>
</View>
<View style={styles.timeContainer}>
{(isClockControlled === true)
? this.renderHourPicker()
: this.renderHour()}
<Text>:</Text>
{(isClockControlled === true)
? this.renderMinutePicker()
: this.renderMinute()}
<View style={styles.isAfternoonContainer}>
<Text>
{this.renderAMPM()}
</Text>
{isClockControlled && (
<Switch
testID="toggleAfternoon"
onValueChange={() => this.toggleAfternoon()}
thumbTintColor="#444"
tintColor="#757575"
onTintColor="#757575"
value={isAfternoon}
/>
)}
</View>
<Text testID="errorMessage" style={styles.errorMessage}>
{errorMessage}
</Text>
</View>
<Clock
{...defaultClockSettings}
hour={currentHour}
min={currentMinute}
sec={currentSecond}
isClockControlled={isClockControlled}
/>
<View style={styles.grayheadingContainer}>
<Text testID="subheadingAngle" style={styles.grayheadingText}>
ANGLE
</Text>
</View>
<View style={styles.secondaryTextContainer}>
<Text style={styles.secondaryText}>
<Text style={styles.degreeText}>
{calcDegrees(calcHour(currentHour), calcMinute(currentMinute)).degrees} Degrees
</Text>
<Text> is the angle between the Hour and Minute Hands.</Text>
</Text>
</View>
<View style={styles.grayheadingContainer}>
<Text testID="subheadingCoterminal" style={styles.grayheadingText}>
COTERMINAL ANGLE
</Text>
</View>
<View style={styles.secondaryTextContainer}>
<Text style={styles.secondaryText}>
<Text style={styles.degreeText}>
{calcDegrees(calcHour(currentHour), calcMinute(currentMinute)).coterminal} Degrees
</Text>
<Text> is the coterminal angle between the Hour and Minute Hands.</Text>
<Text>Other coterminal angles are excluded.</Text>
</Text>
</View>
</View>
</ScrollView>
</SafeAreaView>
)
}
}
export default App