Update interface.html - shadowclk

Add comments
Reduce timeout to 50ms
master
stweedo 2023-05-10 05:00:03 -05:00 committed by GitHub
parent cccce9318e
commit 96342a3c39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 13 deletions

View File

@ -82,7 +82,6 @@
#message-container { #message-container {
height: 40px; height: 40px;
/* adjust the height based on your desired fixed height */
margin-top: 20px; margin-top: 20px;
text-align: center; text-align: center;
} }
@ -106,6 +105,7 @@
</div> </div>
<script> <script>
const messageDiv = document.getElementById('message'); const messageDiv = document.getElementById('message');
// Create and add color buttons to the UI
let colors = ['#000', '#f00', '#0f0', '#ff0', '#00f', '#f0f', '#0ff', '#fff']; let colors = ['#000', '#f00', '#0f0', '#ff0', '#00f', '#f0f', '#0ff', '#fff'];
let colorButtonsContainer = document.getElementById('color-buttons-container'); let colorButtonsContainer = document.getElementById('color-buttons-container');
colors.forEach((color, i) => { colors.forEach((color, i) => {
@ -121,6 +121,7 @@
}); });
}); });
// Format the current time in the desired format
function formatTime(date) { function formatTime(date) {
let hours = date.getHours(); let hours = date.getHours();
let minutes = date.getMinutes(); let minutes = date.getMinutes();
@ -129,6 +130,7 @@
return `${formattedHours}:${formattedMinutes}`; return `${formattedHours}:${formattedMinutes}`;
} }
// Get the current date as a formatted string
function getCurrentDate() { function getCurrentDate() {
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let suffixes = ["st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"]; let suffixes = ["st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"];
@ -140,12 +142,14 @@
return `${month} ${day}${suffix}, ${year}`; return `${month} ${day}${suffix}, ${year}`;
} }
// Get the current day of the week as a string
function getCurrentDay() { function getCurrentDay() {
let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
let date = new Date(); let date = new Date();
return days[date.getDay()]; return days[date.getDay()];
} }
// Toggle the background color between dark and light
function toggleBackground() { function toggleBackground() {
isDarkBg = !isDarkBg; // Toggle the background state isDarkBg = !isDarkBg; // Toggle the background state
let previewBox = document.getElementById("preview-box"); let previewBox = document.getElementById("preview-box");
@ -153,6 +157,7 @@
drawText(selectedColor); // Redraw the text with updated color drawText(selectedColor); // Redraw the text with updated color
} }
// Draw the time, date, and day of the week on the canvas
function drawText(color) { function drawText(color) {
let canvas = document.getElementById("preview-canvas"); let canvas = document.getElementById("preview-canvas");
let ctx = canvas.getContext("2d"); let ctx = canvas.getContext("2d");
@ -218,6 +223,9 @@
ctx.fillText(currentDay, xPos, yPos + 20); ctx.fillText(currentDay, xPos, yPos + 20);
} }
// Converts a hexadecimal color code to a 16-bit integer value.
// If the input hex string is in short format (#rgb), it's converted to long format (#rrggbb) first.
// Returns the 16-bit integer value representing the color.
function hexToDec(hex) { function hexToDec(hex) {
if (hex.length === 4) { if (hex.length === 4) {
hex = `#${hex[1]}${hex[1]}${hex[2]}${hex[2]}${hex[3]}${hex[3]}`; hex = `#${hex[1]}${hex[1]}${hex[2]}${hex[2]}${hex[3]}${hex[3]}`;
@ -249,6 +257,7 @@
}; };
} }
// Save the provided theme to Bangle.js storage and set Shadow Clock as the default clock
function saveThemeToSettings(theme) { function saveThemeToSettings(theme) {
Puck.eval('require("Storage").readJSON("setting.json", true)', (data) => { Puck.eval('require("Storage").readJSON("setting.json", true)', (data) => {
if (data) { if (data) {
@ -256,14 +265,11 @@
if (!data.theme) { if (!data.theme) {
data.theme = {}; data.theme = {};
} }
// Save all theme values // Save all theme values
for (let key in theme) { for (let key in theme) {
data.theme[key] = theme[key]; data.theme[key] = theme[key];
} }
data.clock = "shadowclk.app.js"; // Set Shadow Clock as default data.clock = "shadowclk.app.js"; // Set Shadow Clock as default
Puck.write(`require("Storage").write("setting.json", ${JSON.stringify(data)});\n`, (result) => { Puck.write(`require("Storage").write("setting.json", ${JSON.stringify(data)});\n`, (result) => {
console.log('Theme saved:', result); console.log('Theme saved:', result);
}); });
@ -271,18 +277,20 @@
}); });
} }
// Load Shadow Clock color and theme setting // Initialize Shadow Clock color and theme settings with defaults
let selectedColor = "#0ff"; let selectedColor = "#0ff";
let isDarkBg = false; let isDarkBg = false;
// Load settings from Bangle.js storage
function loadSettings(callback) { function loadSettings(callback) {
// Set a timeout for loading the settings // Set a timeout for loading the settings
const timeout = new Promise((_, reject) => const timeout = new Promise((_, reject) =>
setTimeout(() => reject(new Error("Timeout occurred")), 500) setTimeout(() => reject(new Error("Timeout occurred")), 50)
); );
// Load settings from Bangle.js storage
const loadSettingsFromStorage = new Promise((resolve) => { const loadSettingsFromStorage = new Promise((resolve) => {
Puck.eval('require("Storage").readJSON("shadowclk.json", true)', (data) => { Puck.eval('[require("Storage").readJSON("shadowclk.json", true)]', (dataArray) => {
let data = dataArray ? dataArray[0] : null;
if (data) { if (data) {
// Apply color and theme from JSON // Apply color and theme from JSON
const { color, theme } = data; const { color, theme } = data;
@ -297,7 +305,7 @@
resolve(); resolve();
}); });
}); });
// Execute the callback after loading the settings, or use default values in case of a timeout or error
Promise.race([loadSettingsFromStorage, timeout]) Promise.race([loadSettingsFromStorage, timeout])
.then(() => callback()) .then(() => callback())
.catch((error) => { .catch((error) => {
@ -309,35 +317,35 @@
}); });
} }
// Update the time display every second
function updateTime() { function updateTime() {
setInterval(() => { setInterval(() => {
drawText(selectedColor); drawText(selectedColor);
}, 1000); }, 1000);
} }
// Display a message for a given duration and then remove it
function displayMessage(text, timeout) { function displayMessage(text, timeout) {
// Remove any existing message // Remove any existing message
while (messageDiv.firstChild) { while (messageDiv.firstChild) {
messageDiv.removeChild(messageDiv.firstChild); messageDiv.removeChild(messageDiv.firstChild);
} }
// Create a new message element // Create a new message element
const message = document.createElement('p'); const message = document.createElement('p');
message.innerHTML = text; // Use innerHTML instead of textContent message.innerHTML = text; // Use innerHTML instead of textContent
message.style.fontSize = '24px'; message.style.fontSize = '24px';
messageDiv.appendChild(message); messageDiv.appendChild(message);
// Remove the message element after the timeout // Remove the message element after the timeout
setTimeout(() => { setTimeout(() => {
messageDiv.removeChild(message); messageDiv.removeChild(message);
}, timeout); }, timeout);
} }
// Attach an event listener to the 'upload' button to save theme and color settings to Bangle.js storage
document.getElementById("upload").addEventListener("click", function () { document.getElementById("upload").addEventListener("click", function () {
// Save theme settings to Bangle.js // Save theme settings to Bangle.js
let themeColors = createThemeColors(isDarkBg); let themeColors = createThemeColors(isDarkBg);
saveThemeToSettings(themeColors); saveThemeToSettings(themeColors);
// Save Shadow Clock color setting // Save Shadow Clock color setting
let data = { let data = {
color: selectedColor, color: selectedColor,
@ -346,11 +354,11 @@
Puck.write(`require("Storage").write("shadowclk.json", ${JSON.stringify(data)});\n`, (result) => { Puck.write(`require("Storage").write("shadowclk.json", ${JSON.stringify(data)});\n`, (result) => {
console.log('Color saved:', result); console.log('Color saved:', result);
}); });
// Display the message using displayMessage function // Display the message using displayMessage function
displayMessage('Configuration sent...<br>Hold button on Bangle.js', 5000); displayMessage('Configuration sent...<br>Hold button on Bangle.js', 5000);
}); });
// Load required fonts for the application
function loadFonts() { function loadFonts() {
return Promise.all([ return Promise.all([
document.fonts.load('81px Londrina Solid'), document.fonts.load('81px Londrina Solid'),