2024-03-13 21:32:13 +00:00
|
|
|
(function dm() {
|
2024-03-16 09:17:11 +00:00
|
|
|
function selectRightMode(lt, dt, at) {
|
|
|
|
if (at < lt && at < dt) {
|
2024-03-13 21:32:13 +00:00
|
|
|
return "lightT";
|
2024-03-16 09:17:11 +00:00
|
|
|
} else if (at > lt && at < dt) {
|
2024-03-13 21:32:13 +00:00
|
|
|
return "darkT";
|
2024-03-16 09:17:11 +00:00
|
|
|
} else if (at > lt && at > dt) {
|
|
|
|
return "lightN";
|
|
|
|
}
|
2024-03-13 21:32:13 +00:00
|
|
|
}
|
2024-03-16 11:28:43 +00:00
|
|
|
|
2024-03-13 21:32:13 +00:00
|
|
|
function setDarkTheme() {
|
|
|
|
if (!g.theme.dark) {
|
|
|
|
upd({
|
2024-03-16 09:17:11 +00:00
|
|
|
fg: cl("#fff"), bg: cl("#000"),
|
|
|
|
fg2: cl("#fff"), bg2: cl("#004"),
|
|
|
|
fgH: cl("#fff"), bgH: cl("#00f"),
|
|
|
|
dark: true
|
|
|
|
});
|
2024-03-13 21:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLightTheme() {
|
|
|
|
if (g.theme.dark) {
|
|
|
|
upd({
|
2024-03-16 09:17:11 +00:00
|
|
|
fg: cl("#000"), bg: cl("#fff"),
|
|
|
|
fg2: cl("#000"), bg2: cl("#cff"),
|
|
|
|
fgH: cl("#000"), bgH: cl("#0ff"),
|
|
|
|
dark: false
|
2024-03-13 21:32:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function fixTime(h, m) {
|
2024-03-16 09:17:11 +00:00
|
|
|
if (h.toString().length < 2) {
|
2024-03-13 21:32:13 +00:00
|
|
|
h = "0" + h.toString();
|
|
|
|
}
|
2024-03-16 09:17:11 +00:00
|
|
|
if (m.toString().length < 2) {
|
2024-03-13 21:32:13 +00:00
|
|
|
m = "0" + m.toString();
|
|
|
|
}
|
2024-03-16 09:17:11 +00:00
|
|
|
return h.toString() + ":" + m.toString();
|
2024-03-13 21:32:13 +00:00
|
|
|
}
|
|
|
|
function calculateSunTimes() {
|
2024-03-16 09:17:11 +00:00
|
|
|
var location = require("Storage").readJSON("mylocation.json", 1) || {};
|
|
|
|
location.lat = location.lat || 51.5072;
|
|
|
|
location.lon = location.lon || 0.1276; // London
|
2024-03-13 21:32:13 +00:00
|
|
|
date = new Date(Date.now());
|
|
|
|
var times = SunCalc.getTimes(date, location.lat, location.lon);
|
2024-03-16 09:17:11 +00:00
|
|
|
sunrise = fixTime(times.sunrise.getHours(), times.sunrise.getMinutes());
|
|
|
|
sunset = fixTime(times.sunset.getHours(), times.sunset.getMinutes());
|
2024-03-13 21:32:13 +00:00
|
|
|
/* do we want to re-calculate this every day? Or we just assume
|
|
|
|
that 'show' will get called once a day? */
|
|
|
|
}
|
|
|
|
function cl(x) { return g.setColor(x).getColor(); }
|
|
|
|
function upd(th) {
|
|
|
|
g.theme = th;
|
2024-03-16 11:28:43 +00:00
|
|
|
let settings = storage.readJSON('setting.json', 1)
|
2024-03-13 21:32:13 +00:00
|
|
|
settings.theme = th;
|
|
|
|
storage.write('setting.json', settings);
|
|
|
|
delete g.reset;
|
|
|
|
g._reset = g.reset;
|
2024-03-16 09:17:11 +00:00
|
|
|
g.reset = function (n) { return g._reset().setColor(th.fg).setBgColor(th.bg); };
|
|
|
|
g.clear = function (n) { if (n) g.reset(); return g.clearRect(0, 0, g.getWidth(), g.getHeight()); };
|
2024-03-13 21:32:13 +00:00
|
|
|
g.clear(1);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (Bangle.dmTimeout) clearTimeout(Bangle.dmTimeout); // so the app can eval() this file to apply changes right away
|
|
|
|
delete Bangle.dmTimeout;
|
2024-03-16 09:17:11 +00:00
|
|
|
} catch (e) {
|
|
|
|
print("Bangle.dmTimeout does not exist");
|
|
|
|
}
|
2024-03-13 21:32:13 +00:00
|
|
|
const SETTINGS_FILE = "themeSwitch.json";
|
|
|
|
const storage = require("Storage");
|
|
|
|
var sunrise, sunset, date;
|
|
|
|
var SunCalc = require("suncalc"); // from modules folder
|
2024-03-16 09:17:11 +00:00
|
|
|
let bSettings = storage.readJSON(SETTINGS_FILE, true) || {};
|
2024-03-13 21:32:13 +00:00
|
|
|
const now = new Date();
|
2024-03-16 11:28:43 +00:00
|
|
|
let hr = now.getHours() + (now.getMinutes() / 60) + (now.getSeconds() / 3600); // current (decimal) hour
|
|
|
|
let dmH = parseFloat(bSettings.darkModeAt.split(":")[0]);
|
|
|
|
let dmM = parseFloat(bSettings.darkModeAt.split(":")[1]);
|
|
|
|
let lmH = parseFloat(bSettings.lightModeAt.split(":")[0]);
|
|
|
|
let lmM = parseFloat(bSettings.lightModeAt.split(":")[1]);
|
2024-03-13 21:32:13 +00:00
|
|
|
print("reading switch timeslots.....");
|
2024-03-16 09:17:11 +00:00
|
|
|
let dmDec = parseFloat(dmH) + parseFloat(dmM) / parseFloat(60);
|
|
|
|
let lmDec = parseFloat(lmH) + parseFloat(lmM) / parseFloat(60);
|
2024-03-16 11:28:43 +00:00
|
|
|
let targetMode = selectRightMode(parseFloat(lmDec), parseFloat(dmDec), parseFloat(hr));
|
|
|
|
let nextH, nextM;
|
2024-03-16 09:17:11 +00:00
|
|
|
if (targetMode === "lightT" || targetMode === "lightN") {
|
2024-03-13 21:32:13 +00:00
|
|
|
nextH = lmH;
|
|
|
|
nextM = lmM;
|
|
|
|
} else {
|
|
|
|
nextH = dmH;
|
|
|
|
nextM = dmM;
|
|
|
|
}
|
2024-03-16 12:33:10 +00:00
|
|
|
let nextDecH = parseFloat(nextH) + parseFloat(nextM) / parseFloat(60);
|
2024-03-16 09:17:11 +00:00
|
|
|
let t = 3600000 * (nextDecH - hr); // timeout in milliseconds
|
|
|
|
if (t < 0) { t += 86400000; } // scheduled for tomorrow: add a day
|
2024-03-13 21:32:13 +00:00
|
|
|
/* update theme mode at the correct time. */
|
2024-03-16 09:17:11 +00:00
|
|
|
Bangle.dmTimeout = setTimeout(() => {
|
|
|
|
if (bSettings.darkMode !== 0) {
|
|
|
|
if (targetMode === "lightT" || targetMode === "lightN") {
|
2024-03-13 21:32:13 +00:00
|
|
|
setLightTheme();
|
2024-03-16 09:17:11 +00:00
|
|
|
} else {
|
2024-03-13 21:32:13 +00:00
|
|
|
setDarkTheme();
|
2024-03-16 09:17:11 +00:00
|
|
|
}
|
2024-03-13 21:32:13 +00:00
|
|
|
Bangle.loadWidgets();
|
|
|
|
Bangle.drawWidgets();
|
2024-03-16 09:17:11 +00:00
|
|
|
setTimeout(load, 20);
|
|
|
|
if (bSettings.darkModeBySun !== 0) {
|
2024-03-13 21:32:13 +00:00
|
|
|
calculateSunTimes();
|
|
|
|
bSettings.lightModeAt = sunrise;
|
|
|
|
bSettings.darkModeAt = sunset;
|
2024-03-16 09:17:11 +00:00
|
|
|
storage.writeJSON(SETTINGS_FILE, bSettings);
|
2024-03-13 21:32:13 +00:00
|
|
|
}
|
|
|
|
dm(); // schedule next update
|
|
|
|
}
|
|
|
|
}, t);
|
|
|
|
})();
|