-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
146 lines (130 loc) · 4.34 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
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, Dimensions } from 'react-native';
import WebView from 'react-native-webview';
import { Camera, useCameraPermissions } from 'expo-camera';
const API_KEY = "TO_DO_REPLACE";
const POSETRACKER_API = "https://app.posetracker.com/pose_tracker/tracking";
const { width, height } = Dimensions.get('window');
export default function App() {
const [poseTrackerInfos, setCurrentPoseTrackerInfos] = useState();
const [repsCounter, setRepsCounter] = useState(0);
const [permission, requestPermission] = useCameraPermissions();
useEffect(() => {
if (!permission?.granted) {
requestPermission();
}
}, []);
const exercise = "squat";
const difficulty = "easy";
const skeleton = true;
const posetracker_url = `${POSETRACKER_API}?token=${API_KEY}&exercise=${exercise}&difficulty=${difficulty}&width=${width}&height=${height}`;
// Bridge JavaScript BETWEEN POSETRACKER & YOUR APP
const jsBridge = `
window.addEventListener('message', function(event) {
window.ReactNativeWebView.postMessage(JSON.stringify(event.data));
});
window.webViewCallback = function(data) {
window.ReactNativeWebView.postMessage(JSON.stringify(data));
};
const originalPostMessage = window.postMessage;
window.postMessage = function(data) {
window.ReactNativeWebView.postMessage(typeof data === 'string' ? data : JSON.stringify(data));
};
true; // Important for a correct injection
`;
const handleCounter = (count) => {
setRepsCounter(count);
};
const handleInfos = (infos) => {
setCurrentPoseTrackerInfos(infos);
console.log('Received infos:', infos);
};
const webViewCallback = (info) => {
if (info?.type === 'counter') {
handleCounter(info.current_count);
} else {
handleInfos(info);
}
};
const onMessage = (event) => {
try {
let parsedData;
if (typeof event.nativeEvent.data === 'string') {
parsedData = JSON.parse(event.nativeEvent.data);
} else {
parsedData = event.nativeEvent.data;
}
console.log('Parsed data:', parsedData);
webViewCallback(parsedData);
} catch (error) {
console.error('Error processing message:', error);
console.log('Problematic data:', event.nativeEvent.data);
}
};
return (
<View style={styles.container}>
<WebView
javaScriptEnabled={true}
domStorageEnabled={true}
allowsInlineMediaPlayback={true}
mediaPlaybackRequiresUserAction={false}
style={styles.webView}
source={{ uri: posetracker_url }}
originWhitelist={['*']}
injectedJavaScript={jsBridge}
onMessage={onMessage}
// Activer le debug pour voir les logs WebView
debuggingEnabled={true}
// Permettre les communications mixtes HTTP/HTTPS si nécessaire
mixedContentMode="compatibility"
// Ajouter un gestionnaire d'erreurs
onError={(syntheticEvent) => {
const { nativeEvent } = syntheticEvent;
console.warn('WebView error:', nativeEvent);
}}
// Ajouter un gestionnaire pour les erreurs de chargement
onLoadingError={(syntheticEvent) => {
const { nativeEvent } = syntheticEvent;
console.warn('WebView loading error:', nativeEvent);
}}
/>
<View style={styles.infoContainer}>
<Text>Status : {!poseTrackerInfos ? "loading AI..." : "AI Running"}</Text>
<Text>Info type : {!poseTrackerInfos ? "loading AI..." : poseTrackerInfos.type}</Text>
<Text>Counter: {repsCounter}</Text>
{poseTrackerInfos?.ready === false ? (
<>
<Text>Placement ready: false</Text>
<Text>Placement info: Move {poseTrackerInfos?.postureDirection}</Text>
</>
) : (
<>
<Text>Placement ready: true</Text>
<Text>Placement info: You can start doing squats 🏋️</Text>
</>
)}
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
webView: {
width: '100%',
height: '100%',
zIndex: 1,
},
infoContainer: {
position: 'absolute',
top: 60,
left: 0,
right: 0,
alignItems: 'center',
zIndex: 2,
backgroundColor: 'rgba(255, 255, 255, 0.8)',
padding: 10,
},
});