-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
151 lines (137 loc) · 3.72 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
import React, { useState, useEffect, useRef } from 'react';
import {
AppState,
StyleSheet,
Text,
View,
TouchableOpacity,
Dimensions,
} from 'react-native';
import moment from 'moment';
import { getData, storeData } from './src/Utility/asyncStorage';
const screen = Dimensions.get('window');
const formatNumber = (number) => `0${number}`.slice(-2);
const getRemaining = (time) => {
const mins = Math.floor(time / 60);
const secs = time - mins * 60;
return { mins: formatNumber(mins), secs: formatNumber(secs) };
};
export default function App() {
const appState = useRef(AppState.currentState);
const [appStateVisible, setAppStateVisible] = useState(appState.current);
const [remainingSecs, setRemainingSecs] = useState(0);
const [isActive, setIsActive] = useState(false);
const { mins, secs } = getRemaining(remainingSecs);
const toggle = () => {
setIsActive(!isActive);
};
const reset = () => {
setRemainingSecs(0);
setIsActive(false);
};
// timer useEffect
useEffect(() => {
let interval = null;
if (isActive) {
interval = setInterval(() => {
setRemainingSecs((remainingSecs) => remainingSecs + 1);
}, 1000);
} else if (!isActive && remainingSecs !== 0) {
clearInterval(interval);
}
return () => clearInterval(interval);
}, [isActive, remainingSecs]);
// app state to check app is in foreground or background
useEffect(() => {
const subscription = AppState.addEventListener('change', (nextAppState) => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
console.log('App has come to the foreground!');
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
console.log('AppState', appState.current);
if (appState.current === 'active') {
readTime();
} else if (appState.current === 'background') {
saveTime();
}
});
return () => {
subscription?.remove();
};
});
//save data
const saveTime = async () => {
console.log('saving data');
await storeData(
{
timerTime: remainingSecs,
currentTime: getTime(),
},
'time'
);
};
// read data
const readTime = async () => {
const foregroundTime = getTime();
const data = await getData('time');
const updatedTime = foregroundTime - data.currentTime + data.timerTime;
setRemainingSecs(updatedTime);
setIsActive(true);
};
const getTime = () => {
const minute = moment().minute() * 60;
const seconds = moment().second();
return minute + seconds;
};
return (
<View style={styles.container}>
<Text style={styles.timerText}>{`${mins}:${secs}`}</Text>
<TouchableOpacity onPress={toggle} style={styles.button}>
<Text style={styles.buttonText}>{isActive ? 'Pause' : 'Start'}</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={reset}
style={[styles.button, styles.buttonReset]}
>
<Text style={[styles.buttonText, styles.buttonTextReset]}>Reset</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#07121B',
alignItems: 'center',
justifyContent: 'center',
},
button: {
borderWidth: 10,
borderColor: '#B9AAFF',
width: screen.width / 2,
height: screen.width / 2,
borderRadius: screen.width / 2,
alignItems: 'center',
justifyContent: 'center',
},
buttonText: {
fontSize: 45,
color: '#B9AAFF',
},
timerText: {
color: '#fff',
fontSize: 90,
marginBottom: 20,
},
buttonReset: {
marginTop: 20,
borderColor: '#FF851B',
},
buttonTextReset: {
color: '#FF851B',
},
});