-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (100 loc) · 3.57 KB
/
app.js
File metadata and controls
110 lines (100 loc) · 3.57 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
107
108
109
110
document.addEventListener("DOMContentLoaded", () => {
const webViewContainer = document.getElementById("webViewContainer");
const webView = document.getElementById("webView");
const toggleButton = document.getElementById("toggleWebView");
const integrationOptions = document.getElementById("integrationOptions");
const postData = {
userId: "YOUR USER ID",
key: "YOUR API KEY",
company: "YOUR COMPANY",
// Optional user data fields
lifestyle: "Sedentary", // Active, or Very Active
age: 50,
height: 150,
weight: 200,
gender: "Male",
};
function sendMessage(url) {
if (webView.contentWindow) {
webView.contentWindow.postMessage(postData, url);
} else {
setTimeout(() => {
try {
webView.contentWindow.postMessage(postData, url);
} catch {
webView.contentWindow.postMessage(postData, url);
}
}, 100);
}
}
integrationOptions.addEventListener("change", () => {
const selectedOption = integrationOptions.value;
toggleButton.textContent = `Start ${selectedOption}`;
});
toggleButton.addEventListener("click", () => {
const selectedOption = integrationOptions.value;
let url;
switch (selectedOption) {
case "Complete User Experience":
url = "https://ai.kinestex.com";
postData.planC = "Cardio"; // specify the category of the plans to be presented
break;
case "Workout Plan":
url = "https://ai.kinestex.com/plan/Full%20Cardio"; // in the URL, replace with the plan you want to present
break;
case "AI Experience":
url = "https://ai.kinestex.com/experiences/training"; // in the URL, replace with the experience you want to present
postData.exercise = "Manual Handling"; // specify the exercise to be presented
break;
case "Workout":
url = "https://ai.kinestex.com/workout/Fitness%20Lite"; // in the URL replace with the workout you want to display
break;
case "Motion Analysis":
url = "https://ai.kinestex.com/camera"; // in the URL replace with the workout you want to display
postData.includeRawData = false; // specify if you want to include raw data
break;
case "Challenge":
url = "https://ai.kinestex.com/challenge";
postData.exercise = "Squats"; // specify the exercise to be presented
postData.countdown = 50; // specify the timer time for the challenge
break;
}
const isVisible = webViewContainer.style.display !== "none";
webViewContainer.style.display = isVisible ? "none" : "block";
if (!isVisible) {
webView.src = url;
webView.onload = () => {
sendMessage(url);
};
}
});
window.addEventListener("message", (event) => {
// ensure data is received from KinesteX
if (event.origin !== "https://ai.kinestex.com") {
return;
}
try {
const message = JSON.parse(event.data);
console.log("Received data:", message);
switch (message.type) {
case "kinestex_loaded":
sendMessage(webView.src);
break;
case "exercise_completed":
console.log("Exercise completed:", message.data);
break;
case "plan_unlocked":
console.log("Workout plan unlocked:", message.data);
break;
case "exit_kinestex":
webViewContainer.style.display = "none";
break;
default:
console.log("Received data:", message);
break;
}
} catch (e) {
console.error("Could not parse JSON message from WebView:", e);
}
});
});