-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
180 lines (167 loc) · 4.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
import React, { useEffect, useState } from 'react';
import { StyleSheet, Text, ToastAndroid, Vibration, View } from 'react-native';
import Sound from 'react-native-sound';
import SplashScreen from 'react-native-splash-screen';
import Buttons from './components/Buttons';
import LoopCount from './components/LoopCount';
import SelectSound from './components/SelectSound';
import TimeInterval from './components/TimeInterval';
import VibrationToggle from './components/VibrationToggle';
import { BACKGROUND_COLOR, MAIN_COLOR, WHITE_COLOR } from './constant';
import { vw } from './viewport';
const App = () => {
const [userSettings, setUserSettings] = useState({
soundFile: 'beep_2.wav',
vibrate: 'ON',
});
const [time, setTime] = useState({ min: '', sec: '' });
const [timeToShow, setTimeToShow] = useState({ min: '', sec: '' });
const [loopCount, setLoopCount] = useState(null);
const [originalLoopCount, setOriginalLoopCount] = useState(0);
const [startStatus, setStartStatus] = useState(false);
useEffect(() => {
// GET DATA FROM ASYNC STORAGE
SplashScreen.hide();
Sound.setCategory('Playback');
}, []);
useEffect(() => {
if (startStatus && loopCount - 1 >= 0) {
setTimeout(() => {
let seconds = parseInt(
timeToShow.sec?.length <= 0 ? '0' : timeToShow.sec,
10,
);
let minutes = parseInt(
timeToShow.min?.length <= 0 ? '0' : timeToShow.min,
10,
);
console.log('Showing seconds,minutes', seconds, minutes, timeToShow);
seconds -= 1;
if (seconds < 0) {
seconds = 59;
minutes -= 1;
if (minutes < 0) {
minutes = parseInt(time.min?.length <= 0 ? '0' : time.min, 10);
PlaySoundVibrate();
if (parseInt(loopCount, 10) <= 1) {
seconds = parseInt(time.sec?.length <= 0 ? '0' : time.sec, 10);
setLoopCount(originalLoopCount);
setStartStatus(false);
} else {
setLoopCount(loopCount - 1);
}
}
}
setTimeToShow({ sec: seconds, min: minutes });
}, 1000);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [timeToShow]);
useEffect(() => {
setOriginalLoopCount(loopCount);
console.log(loopCount, typeof loopCount);
const min = time.min?.length <= 0 ? '0' : time.min;
const sec = time.sec?.length <= 0 ? '0' : time.sec;
if (min <= '0' && sec <= '0' && startStatus) {
ToastAndroid.show('Time is not set', ToastAndroid.SHORT);
setStartStatus(false);
} else {
if (
(!loopCount || loopCount?.length <= 0 || loopCount === '0') &&
startStatus
) {
ToastAndroid.show('Please set number of loops', ToastAndroid.SHORT);
setStartStatus(false);
} else {
setTimeToShow({
min,
sec,
});
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [startStatus]);
const PlaySoundVibrate = () => {
console.log('Play sound');
const whoosh = new Sound(
userSettings.soundFile,
Sound.MAIN_BUNDLE,
(error) => {
if (error) {
console.log('Error:', error);
return;
}
whoosh.play();
if (userSettings.vibrate === 'ON') {
Vibration.vibrate(100);
}
},
);
};
const stopBtnPress = () => {
setStartStatus(false);
};
const resetBtnPress = () => {
setLoopCount(null);
setTime({ min: null, sec: null });
setTimeToShow({ min: null, sec: null });
setOriginalLoopCount(0);
};
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerText}>CLoop</Text>
</View>
<SelectSound
setUserSettings={setUserSettings}
userSettings={userSettings}
startStatus={startStatus}
/>
<VibrationToggle
setUserSettings={setUserSettings}
userSettings={userSettings}
startStatus={startStatus}
/>
<TimeInterval
time={time}
setTime={setTime}
startStatus={startStatus}
timeToShow={timeToShow}
/>
<LoopCount
loopCount={loopCount}
setLoopCount={setLoopCount}
startStatus={startStatus}
originalLoopCount={originalLoopCount}
/>
<Buttons
startStatus={startStatus}
setStartStatus={setStartStatus}
stopBtnPress={stopBtnPress}
resetBtnPress={resetBtnPress}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
height: '100%',
width: '100%',
backgroundColor: BACKGROUND_COLOR,
},
header: {
height: '8%',
width: '100%',
backgroundColor: WHITE_COLOR,
textAlign: 'center',
justifyContent: 'center',
},
headerText: {
color: MAIN_COLOR,
fontSize: 6 * vw,
fontFamily: 'Poppins',
fontWeight: 'bold',
paddingHorizontal: 3 * vw,
},
});
export default App;