Removed wake up notifications

This commit is contained in:
Maximilian Giller 2024-07-25 23:36:58 +02:00
parent 60f39df682
commit 8d5f132743

View file

@ -180,14 +180,6 @@
<br /> <br />
<br /> <br />
<a href="mailto:max@giller.dev">Write me an e-mail</a> <a href="mailto:max@giller.dev">Write me an e-mail</a>
<h1>Settings</h1>
<table class="settings-table">
<tr>
<td><input type="checkbox" id="notifications" name="notifications" value="notifications" /></td>
<td><label for="notifications">Enable wake up
notifications</label></td>
</tr>
</table>
<div class="profile"> <div class="profile">
<img title="Primary profile picture" src="profile.png" /> <img title="Primary profile picture" src="profile.png" />
@ -197,13 +189,10 @@
<script> <script>
const url = "https://giller.dev/g/my-status"; const url = "https://giller.dev/g/my-status";
const notifications = [];
let userOnPage = true;
let previousStatus = undefined; let previousStatus = undefined;
let currentStatus = undefined; let currentStatus = undefined;
let updateRoutine = undefined; let updateRoutine = undefined;
const updateInterval = 60 * 1000; const updateInterval = 60 * 1000;
const notificationSettingName = "wake-up-notifications";
function setText(id, text) { function setText(id, text) {
document.getElementById(id).innerHTML = text; document.getElementById(id).innerHTML = text;
@ -251,78 +240,11 @@
}); });
} }
function dismissNotifications() {
notifications.forEach(notification => notification.close());
notifications.length = 0;
}
function hasWakenUp() { function hasWakenUp() {
return previousStatus && previousStatus.title.toLowerCase() === "sleeping" && currentStatus.title.toLowerCase() !== "sleeping"; return previousStatus && previousStatus.title.toLowerCase() === "sleeping" && currentStatus.title.toLowerCase() !== "sleeping";
} }
function enableNotifications() {
if (updateRoutine) {
return;
}
// Request permission to show notifications
Notification.requestPermission().then(function (result) {
if (result === "granted") {
// Save setting
localStorage.setItem(notificationSettingName, "true");
// Enable notifications
updateRoutine = setInterval(function () {
updateStatus();
if (hasWakenUp()) {
showNotification("Max Status", "Max has woken up!");
}
}, updateInterval);
}
});
}
function disableNotifications() {
if (!updateRoutine) {
return;
}
clearInterval(updateRoutine);
updateRoutine = undefined;
dismissNotifications();
// Save setting
localStorage.setItem(notificationSettingName, "false");
}
function showNotification(title, body) {
if (userOnPage) {
return;
}
const notification = new Notification(title, {
body: body,
icon: "https://giller.dev/favicon.ico"
});
notification.onclick = function () {
window.focus();
};
notifications.push(notification);
}
function loadNotificationSetting() {
if (localStorage.getItem(notificationSettingName) === "true") {
document.getElementById("notifications").checked = true;
enableNotifications();
} else {
document.getElementById("notifications").checked = false;
disableNotifications();
}
}
function updateSleepStreak(streakData) { function updateSleepStreak(streakData) {
if (streakData.length == 0) { if (streakData.length == 0) {
@ -356,31 +278,6 @@
} }
} }
// On window focus, dismiss all notifications
window.onfocus = function () {
dismissNotifications();
userOnPage = true;
};
// On window blur, enable notifications
window.onblur = function () {
userOnPage = false;
};
// On page unload, dismiss all notifications
window.onbeforeunload = function () {
dismissNotifications();
};
// On checkbox change, enable/disable notifications
document.getElementById("notifications").onchange = function () {
if (this.checked) {
enableNotifications();
} else {
disableNotifications();
}
};
// On scroll, hide settings notice // On scroll, hide settings notice
window.onscroll = function () { window.onscroll = function () {
const scrollPosition = window.scrollY; const scrollPosition = window.scrollY;
@ -392,7 +289,6 @@
}; };
updateStatus(); updateStatus();
loadNotificationSetting();
</script> </script>
</body> </body>