-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
359 lines (319 loc) · 8.59 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import React, { useReducer, useEffect, useState } from 'react';
import {
View,
StyleSheet,
Text,
Dimensions,
Platform,
TouchableOpacity,
AppState,
Switch,
} from 'react-native';
import DeviceInfo from 'react-native-device-info';
import Sound from 'react-native-sound';
import KeepAwake from 'react-native-keep-awake';
// Enable playback in silence mode
Sound.setCategory('Playback');
export default (App = () => {
const [appState, setAppState] = useState('active');
const [contBack, setContBack] = useState(true);
const [thirtyMin, setThirtyMin] = useState(false);
let interval = thirtyMin ? 1800 : 30;
const reducer = (state, action) => {
// add one more sec.
let hour = state.hour,
min = state.min,
sec = state.sec;
let seconds = state.seconds,
turnRest = state.turnRest,
count = state.count;
// but if state has become 'active', then assign new.
if (appState === 'active') {
let date = new Date();
if (contBack && !state.paused) {
let newHour = date.getHours();
let newMin = date.getMinutes();
let newSec = date.getSeconds();
// get diff from latest hour, min, sec.
let sec1 = (newHour * 60 + newMin) * 60 + newSec;
let sec0 = (hour * 60 + min) * 60 + sec;
let diffSec = sec1 - sec0 + (sec1 > sec0 ? 0 : 24 * 60 * 60);
console.log(`diffSec is ${diffSec}`);
hour = newHour;
min = newMin;
sec = newSec;
// then, apply to seconds and count as well as turnRest.
var quotient = Math.floor(diffSec / interval);
let remainder = diffSec % interval;
// apply to seconds.
seconds = seconds + remainder;
if (seconds > interval) {
seconds -= interval;
quotient++;
}
// apply to count.
count = count + Math.floor(((turnRest ? 1 : 0) + quotient) / 2);
// apply to turnRest.
turnRest = quotient % 2 == 0 ? turnRest : !turnRest;
} else {
hour = date.getHours();
min = date.getMinutes();
sec = date.getSeconds();
}
} else {
// if just clock tick, advance sec.
sec++;
// adjust sec, min, and hour.
sec === 60
? ((sec = 0),
min++ ,
min === 60
? ((min = 0), hour++ , hour == 24 ? (hour = 0) : null)
: null)
: null;
}
if (state.paused == false) {
if (appState !== 'active') {
seconds++;
}
if (seconds === interval) {
seconds = 0;
turnRest = !turnRest;
// Load the sound file 'whoosh.mp3' from the app bundle
// See notes below about preloading sounds within initialization code below.
let whoosh = new Sound(
'beyond_doubt_2.mp3',
Sound.MAIN_BUNDLE,
error => {
if (error) {
console.log('failed to load the sound', error);
return;
} else {
whoosh.play();
}
// loaded successfully
console.log(
'duration in seconds: ' +
whoosh.getDuration() +
' number of channels: ' +
whoosh.getNumberOfChannels(),
);
},
);
// update battery level only when count up is called.
DeviceInfo.getBatteryLevel().then(batteryLevel => {
dispatch({ type: 'battery_level', payload: batteryLevel * 100 });
});
// if rest finishes, count up.
if (turnRest == false) {
count++;
}
}
}
if (appState !== 'has_been_active') {
setAppState('has_been_active');
}
switch (action.type) {
case 'clock_tick':
return {
...state,
seconds,
turnRest,
count,
hour,
min,
sec,
};
case 'pause_pressed':
return { ...state, paused: !state.paused };
case 'reset_pressed':
return { ...state, seconds: 0, count: 1, paused: false };
case 'battery_level':
return { ...state, batteryLevel: action.payload };
default:
console.log('default inside reducer');
return { ...state, hour, min, sec };
}
};
const date = new Date();
const [state, dispatch] = useReducer(reducer, {
seconds: 0,
count: 1,
turnRest: false,
paused: false,
hour: date.getHours(),
min: date.getMinutes(),
sec: date.getSeconds(),
batteryLevel: 100,
});
const screenWidth = Dimensions.get('window').width;
const screenHeight = Dimensions.get('window').height;
const fontSize = screenWidth < screenHeight ? screenWidth / 6 : screenHeight / 6;
const buttonFontSize = fontSize / 3;
console.log(
`contBack: ${contBack} seconds: ${state.seconds} count: ${
state.count
} turnRest: ${state.turnRest} paused: ${
state.paused
} appState: ${appState} 30min: ${thirtyMin} int: ${interval}`
);
useEffect(() => {
const handleAppState = nextAppState => setAppState(nextAppState);
AppState.addEventListener('change', handleAppState);
return () => {
AppState.removeEventListener('change', handleAppState);
};
}, []);
// effects runs right after every re-render. (including the first rendering)
// if array is attached, skip effects if elements' values are unchanged.
// so if blank, always skip - actually, runs only after first render
// (and after unmount if return is included) - states change from undefined.
useEffect(() => {
// it will execute setTimeout() and also stores the timer into the variable.
let timer = setInterval(() => {
// if seconds is now n-1, set to 0, instead of n.
dispatch({ type: 'clock_tick' });
}, 1000);
// function returned will be executed before running effects again.
return () => {
clearInterval(timer);
};
}, []);
// make the app prevent the phone from sleeping.
useEffect(() => {
KeepAwake.activate();
return () => {
KeepAwake.deactivate();
};
}, []);
const fontFamily = Platform.OS === 'ios' ? 'Menlo' : 'monospace';
// change color depending on turnRest.
const switchDidChange = () => {
setContBack(!contBack);
};
const switch30DidChange = () => {
setThirtyMin(!thirtyMin);
dispatch({ type: 'reset_pressed' });
};
return (
<View style={styles.container}>
{/* time */}
<Text
style={[styles.textWhite, { fontFamily, fontSize, color: '#C6D6F7' }]}>
{(state.hour < 10 ? '0' : null) + state.hour}:
{(state.min < 10 ? '0' : null) + state.min}:
{(state.sec < 10 ? '0' : null) + state.sec}
</Text>
{/* count and seconds */}
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
{/* <Text style={{ color: 'white', fontSize: buttonFontSize, marginLeft: 20 }}>
Set :
</Text> */}
<Text
style={[
state.turnRest ? styles.textGreen : styles.textWhite,
{
fontFamily,
fontSize,
},
]}>
{state.count}-
</Text>
<Text style={[styles.textWhite, { fontFamily, fontSize }]}>
{state.seconds < 60 ? state.seconds : Math.floor(state.seconds / 60) + ':' + (state.seconds % 60)}
</Text>
</View>
{/* play and reset button */}
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity
style={styles.button}
onPress={() => {
dispatch({ type: 'pause_pressed' });
}}
color="white"
title="Pause">
<Text style={{ color: 'white', fontSize: buttonFontSize }}>
{state.paused ? 'Play' : 'Pause'}
</Text>
</TouchableOpacity>
<Text
style={{
paddingVertical: 5,
color: 'white',
fontSize: buttonFontSize,
}}>
{state.batteryLevel.toFixed(0)}%
</Text>
<TouchableOpacity
style={styles.button}
onPress={() => {
dispatch({ type: 'reset_pressed' });
}}
color="white"
title="Pause">
<Text style={{ color: 'white', fontSize: buttonFontSize }}>Reset</Text>
</TouchableOpacity>
</View>
{/* toggle switch */}
<View
style={{
borderRadius: 20,
flexDirection: 'row',
marginVertical: 20,
paddingHorizontal: 20,
backgroundColor: 'lightgrey',
}}>
<Text
style={{
paddingVertical: 5,
color: 'black',
fontSize: buttonFontSize,
paddingRight: 10,
}}>
Continue on Bkgd.
</Text>
<Switch onValueChange={switchDidChange} value={contBack} />
</View>
{/* toggle switch */}
<View
style={{
borderRadius: 20,
flexDirection: 'row',
marginVertical: 10,
paddingHorizontal: 20,
backgroundColor: 'lightgrey',
}}>
<Text
style={{
paddingVertical: 5,
color: 'black',
fontSize: buttonFontSize,
paddingRight: 10,
}}>
30 Min Mode
</Text>
<Switch onValueChange={switch30DidChange} value={thirtyMin} />
</View>
</View>
);
});
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#000',
alignItems: 'center',
justifyContent: 'center',
},
textWhite: {
fontWeight: 'bold',
color: 'white',
},
textGreen: {
fontWeight: 'bold',
color: 'green',
},
button: {
paddingHorizontal: 30,
paddingVertical: 5,
},
});