-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
203 lines (183 loc) · 6.12 KB
/
script.js
File metadata and controls
203 lines (183 loc) · 6.12 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Date and Time
function updateDateTime() {
const now = new Date();
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
};
document.getElementById("current-date-time").textContent = now.toLocaleString(
"en-US",
options
);
}
updateDateTime();
setInterval(updateDateTime, 1000);
// Step Tracker
let step = 0;
let lastAcceleration = null;
const stepThreshold = 1.5;
const stepCountDisplay = document.getElementById("step-count");
const resetStepsButton = document.getElementById("reset-steps");
const stepProgress = document.querySelector(".step-progress");
function showStepNotification() {
if (Notification.permission === "granted") {
new Notification("Step Count", {
body: `You've taken a step! Total: ${step} steps.`,
icon: "/path/to/your/step-icon.png", // Replace with your icon path
});
}
}
if ("DeviceMotionEvent" in window) {
window.addEventListener("devicemotion", (event) => {
const acceleration = event.accelerationIncludingGravity;
if (acceleration && lastAcceleration) {
const deltaX = acceleration.x - lastAcceleration.x;
const deltaY = acceleration.y - lastAcceleration.y;
const deltaZ = acceleration.z - lastAcceleration.z;
const delta = Math.sqrt(deltaX ** 2 + deltaY ** 2 + deltaZ ** 2);
if (delta > stepThreshold) {
step++;
stepCountDisplay.textContent = step;
const progressPercentage = Math.min((step / 1000) * 100, 100);
stepProgress.style.width = `${progressPercentage}%`;
if (Notification.permission !== "granted") {
Notification.requestPermission().then((permission) => {
if (permission === "granted") {
showStepNotification();
}
});
} else {
showStepNotification();
}
}
}
lastAcceleration = {
x: acceleration.x,
y: acceleration.y,
z: acceleration.z,
};
});
} else {
alert("Device Motion API is not supported on your device.");
}
resetStepsButton.addEventListener("click", () => {
step = 0;
stepCountDisplay.textContent = step;
stepProgress.style.width = "0%";
});
// Water Intake
let waterConsumed = 0;
let waterGoal = 0;
const waterGoalInput = document.getElementById("water-goal");
const waterTotalDisplay = document.getElementById("water-total");
const waterGoalDisplay = document.getElementById("water-goal-display");
const logWaterButton = document.getElementById("log-water");
const waterFill = document.querySelector(".water-fill");
const setWaterGoalButton = document.getElementById("set-water-goal");
function showWaterGoalNotification() {
if (Notification.permission === "granted") {
new Notification("Water Intake Goal Reached!", {
body: "Congratulations! You've met your water intake goal!",
icon: "/path/to/your/water-icon.png", // Replace with your icon path
});
}
}
setWaterGoalButton.addEventListener("click", () => {
waterGoal = parseFloat(waterGoalInput.value) || 0;
waterGoalDisplay.textContent = waterGoal.toFixed(1);
updateWaterProgress();
});
logWaterButton.addEventListener("click", () => {
waterConsumed += 0.25;
waterTotalDisplay.textContent = `${waterConsumed.toFixed(
1
)} L / ${waterGoal.toFixed(1)} L`;
updateWaterProgress();
if (waterGoal > 0 && waterConsumed >= waterGoal) {
if (Notification.permission !== "granted") {
Notification.requestPermission().then((permission) => {
if (permission === "granted") {
showWaterGoalNotification();
}
});
} else {
showWaterGoalNotification();
}
}
});
function updateWaterProgress() {
if (waterGoal === 0) {
waterFill.style.width = "0%";
return;
}
const percentage = Math.min((waterConsumed / waterGoal) * 100, 100);
waterFill.style.width = `${percentage}%`;
}
// Medicine Reminder
const medicineTimeInput = document.getElementById("medicine-time");
const setReminderButton = document.getElementById("set-reminder");
const medicineRemindersList = document.getElementById(
"medicine-reminders-list"
);
let reminders = JSON.parse(localStorage.getItem("reminders")) || [];
function showMedicineNotification(time) {
if (Notification.permission === "granted") {
new Notification("Medicine Reminder", {
body: `It's time to take your medicine! (${time})`,
icon: "/path/to/your/medicine-icon.png", // Replace with your icon path
});
}
}
function renderReminders() {
medicineRemindersList.innerHTML = "";
reminders.forEach((reminder, index) => {
const reminderElement = document.createElement("p");
reminderElement.innerHTML = `Reminder set for ${reminder.time} <button class="delete-reminder" data-index="${index}"><i class="fas fa-trash"></i></button>`;
medicineRemindersList.appendChild(reminderElement);
});
localStorage.setItem("reminders", JSON.stringify(reminders));
}
setReminderButton.addEventListener("click", () => {
const reminderTime = medicineTimeInput.value;
if (!reminderTime) {
alert("Please set a valid time.");
return;
}
const now = new Date();
const [hours, minutes] = reminderTime.split(":").map(Number);
const reminderDate = new Date();
reminderDate.setHours(hours, minutes, 0, 0);
if (reminderDate < now) {
alert("Reminder time is in the past. Please select a future time.");
return;
}
const reminder = { time: reminderTime, timeout: reminderDate - now };
reminders.push(reminder);
renderReminders();
setTimeout(() => {
if (Notification.permission !== "granted") {
Notification.requestPermission().then((permission) => {
if (permission === "granted") {
showMedicineNotification(reminderTime);
}
});
} else {
showMedicineNotification(reminderTime);
}
reminders = reminders.filter((r) => r !== reminder);
renderReminders();
}, reminder.timeout);
});
medicineRemindersList.addEventListener("click", (event) => {
if (event.target.classList.contains("fa-trash")) {
const index = event.target.parentNode.dataset.index;
reminders.splice(index, 1);
renderReminders();
}
});
renderReminders();