Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
106 lines (96 loc) · 3.18 KB
/
Copy pathApp.js
File metadata and controls
106 lines (96 loc) · 3.18 KB
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
import { useEffect, useRef, useState } from 'react';
import { Animated, Dimensions, Modal, StyleSheet, View } from 'react-native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { Accelerometer } from 'expo-sensors';
import TriggerScreen from './screens/TriggerScreen';
import CalmScreen from './screens/CalmScreen';
import CoachScreen from './screens/CoachScreen';
const Stack = createStackNavigator();
const SHAKE_THRESHOLD = 1.8;
const { width, height } = Dimensions.get('window');
const BALL_SIZE = 60;
function ShakeWatcher() {
const navigation = useNavigation();
const [visible, setVisible] = useState(false);
const visibleRef = useRef(false);
const ballLeft = useRef(new Animated.Value(width / 2 - BALL_SIZE / 2)).current;
const ballTop = useRef(new Animated.Value(height / 2 - BALL_SIZE / 2)).current;
const shakeTimer = useRef(null);
useEffect(() => {
Accelerometer.setUpdateInterval(50);
const sub = Accelerometer.addListener(({ x, y, z }) => {
const total = Math.sqrt(x * x + y * y + z * z);
if (visibleRef.current) {
Animated.spring(ballLeft, {
toValue: Math.max(0, Math.min(width - BALL_SIZE, width / 2 - BALL_SIZE / 2 + x * 90)),
useNativeDriver: false,
tension: 60,
friction: 7,
}).start();
Animated.spring(ballTop, {
toValue: Math.max(0, Math.min(height - BALL_SIZE, height / 2 - BALL_SIZE / 2 - y * 90)),
useNativeDriver: false,
tension: 60,
friction: 7,
}).start();
}
if (total > SHAKE_THRESHOLD && !visibleRef.current) {
visibleRef.current = true;
setVisible(true);
if (shakeTimer.current) clearTimeout(shakeTimer.current);
shakeTimer.current = setTimeout(() => {
visibleRef.current = false;
setVisible(false);
navigation.navigate('Calm');
}, 2500);
}
});
return () => {
sub.remove();
if (shakeTimer.current) clearTimeout(shakeTimer.current);
};
}, []);
return (
<Modal transparent animationType="fade" visible={visible} statusBarTranslucent>
<View style={styles.overlay}>
<Animated.View style={[styles.ball, { left: ballLeft, top: ballTop }]} />
</View>
</Modal>
);
}
function RootNavigator() {
return (
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen name="Trigger" component={TriggerScreen} />
<Stack.Screen name="Calm" component={CalmScreen} />
<Stack.Screen name="Coach" component={CoachScreen} />
</Stack.Navigator>
);
}
export default function App() {
return (
<NavigationContainer>
<ShakeWatcher />
<RootNavigator />
</NavigationContainer>
);
}
const styles = StyleSheet.create({
overlay: {
flex: 1,
backgroundColor: 'rgba(10, 15, 40, 0.85)',
},
ball: {
position: 'absolute',
width: BALL_SIZE,
height: BALL_SIZE,
borderRadius: BALL_SIZE / 2,
backgroundColor: '#4f46e5',
shadowColor: '#818cf8',
shadowOffset: { width: 0, height: 0 },
shadowOpacity: 0.9,
shadowRadius: 20,
elevation: 20,
},
});