diff --git a/apps/drained/ChangeLog b/apps/drained/ChangeLog index 1a3bc1757..c7fd27981 100644 --- a/apps/drained/ChangeLog +++ b/apps/drained/ChangeLog @@ -1 +1,4 @@ 0.01: New app! +0.02: Allow boot exceptions, e.g. to load DST +0.03: Permit exceptions to load in low-power mode, e.g. daylight saving time. + Also avoid polluting global scope. diff --git a/apps/drained/app.js b/apps/drained/app.js index f9ae04605..e27fcb1d1 100644 --- a/apps/drained/app.js +++ b/apps/drained/app.js @@ -35,14 +35,16 @@ var draw = function () { var dateStr = require("locale").date(date, 0).toUpperCase() + "\n" + require("locale").dow(date, 0).toUpperCase(); + var x2 = x + 6; + var y2 = y + 66; g.reset() .clearRect(Bangle.appRect) .setFont("Vector", 55) .setFontAlign(0, 0) .drawString(timeStr, x, y) .setFont("Vector", 24) - .drawString(dateStr, x, y + 56) - .drawString("".concat(E.getBattery(), "%"), x, y + 104); + .drawString(dateStr, x2, y2) + .drawString("".concat(E.getBattery(), "%"), x2, y2 + 48); if (nextDraw) clearTimeout(nextDraw); nextDraw = setTimeout(function () { @@ -75,9 +77,9 @@ var reload = function () { }; reload(); Bangle.emit("drained", E.getBattery()); -var _a = require("Storage").readJSON("".concat(app, ".setting.json"), true) || {}, _b = _a.disableBoot, disableBoot = _b === void 0 ? false : _b, _c = _a.restore, restore = _c === void 0 ? 20 : _c; +var _a = require("Storage").readJSON("".concat(app, ".setting.json"), true) || {}, _b = _a.keepStartup, keepStartup = _b === void 0 ? true : _b, _c = _a.restore, restore = _c === void 0 ? 20 : _c, _d = _a.exceptions, exceptions = _d === void 0 ? ["widdst.0"] : _d; function drainedRestore() { - if (disableBoot) { + if (!keepStartup) { try { eval(require('Storage').read('bootupdate.js')); } @@ -87,16 +89,28 @@ function drainedRestore() { } load(); } -if (disableBoot) { - var checkCharge_1 = function () { - if (E.getBattery() < restore) - return; - drainedRestore(); - }; - if (Bangle.isCharging()) - checkCharge_1(); - Bangle.on("charging", function (charging) { - if (charging) - checkCharge_1(); - }); +var checkCharge = function () { + if (E.getBattery() < restore) + return; + drainedRestore(); +}; +if (Bangle.isCharging()) + checkCharge(); +Bangle.on("charging", function (charging) { + if (charging) + checkCharge(); +}); +if (!keepStartup) { + var storage = require("Storage"); + for (var _i = 0, exceptions_1 = exceptions; _i < exceptions_1.length; _i++) { + var boot = exceptions_1[_i]; + try { + var js = storage.read("".concat(boot, ".boot.js")); + if (js) + eval(js); + } + catch (e) { + console.log("error loading boot exception \"".concat(boot, "\": ").concat(e)); + } + } } diff --git a/apps/drained/app.ts b/apps/drained/app.ts index 9fc2665ee..3adfbec02 100644 --- a/apps/drained/app.ts +++ b/apps/drained/app.ts @@ -52,6 +52,8 @@ const draw = () => { const dateStr = require("locale").date(date, 0).toUpperCase() + "\n" + require("locale").dow(date, 0).toUpperCase(); + const x2 = x + 6; + const y2 = y + 66; g.reset() .clearRect(Bangle.appRect) @@ -59,8 +61,8 @@ const draw = () => { .setFontAlign(0, 0) .drawString(timeStr, x, y) .setFont("Vector", 24) - .drawString(dateStr, x, y + 56) - .drawString(`${E.getBattery()}%`, x, y + 104); + .drawString(dateStr, x2, y2) + .drawString(`${E.getBattery()}%`, x2, y2 + 48); if(nextDraw) clearTimeout(nextDraw); nextDraw = setTimeout(() => { @@ -97,12 +99,12 @@ reload(); Bangle.emit("drained", E.getBattery()); // restore normal boot on charge -const { disableBoot = false, restore = 20 }: DrainedSettings +const { keepStartup = true, restore = 20, exceptions = ["widdst.0"] }: DrainedSettings = require("Storage").readJSON(`${app}.setting.json`, true) || {}; // re-enable normal boot code when we're above a threshold: function drainedRestore() { // "public", to allow users to call - if(disableBoot){ + if(!keepStartup){ try{ eval(require('Storage').read('bootupdate.js')); }catch(e){ @@ -112,16 +114,26 @@ function drainedRestore() { // "public", to allow users to call load(); // necessary after updating boot.0 } -if(disableBoot){ - const checkCharge = () => { - if(E.getBattery() < restore) return; - drainedRestore(); - }; +const checkCharge = () => { + if(E.getBattery() < restore) return; + drainedRestore(); +}; - if (Bangle.isCharging()) - checkCharge(); +if (Bangle.isCharging()) + checkCharge(); - Bangle.on("charging", charging => { - if(charging) checkCharge(); - }); +Bangle.on("charging", charging => { + if(charging) checkCharge(); +}); + +if(!keepStartup){ + const storage = require("Storage"); + for(const boot of exceptions){ + try{ + const js = storage.read(`${boot}.boot.js`); + if(js) eval(js); + }catch(e){ + console.log(`error loading boot exception "${boot}": ${e}`); + } + } } diff --git a/apps/drained/boot.js b/apps/drained/boot.js index 97f405123..48c1572bd 100644 --- a/apps/drained/boot.js +++ b/apps/drained/boot.js @@ -1,13 +1,13 @@ -{ - var _a = require("Storage").readJSON("drained.setting.json", true) || {}, _b = _a.battery, threshold_1 = _b === void 0 ? 5 : _b, _c = _a.interval, interval = _c === void 0 ? 10 : _c, _d = _a.disableBoot, disableBoot_1 = _d === void 0 ? false : _d; +(function () { + var _a = require("Storage").readJSON("drained.setting.json", true) || {}, _b = _a.battery, threshold = _b === void 0 ? 5 : _b, _c = _a.interval, interval = _c === void 0 ? 10 : _c, _d = _a.keepStartup, keepStartup = _d === void 0 ? true : _d; drainedInterval = setInterval(function () { if (Bangle.isCharging()) return; - if (E.getBattery() > threshold_1) + if (E.getBattery() > threshold) return; var app = "drained.app.js"; - if (disableBoot_1) + if (!keepStartup) require("Storage").write(".boot0", "if(typeof __FILE__ === \"undefined\" || __FILE__ !== \"".concat(app, "\") setTimeout(load, 100, \"").concat(app, "\");")); load(app); }, interval * 60 * 1000); -} +})(); diff --git a/apps/drained/boot.ts b/apps/drained/boot.ts index 4dc885c20..1fcb0591b 100644 --- a/apps/drained/boot.ts +++ b/apps/drained/boot.ts @@ -1,5 +1,5 @@ -{ -const { battery: threshold = 5, interval = 10, disableBoot = false }: DrainedSettings +(() => { +const { battery: threshold = 5, interval = 10, keepStartup = true }: DrainedSettings = require("Storage").readJSON(`drained.setting.json`, true) || {}; drainedInterval = setInterval(() => { @@ -10,7 +10,7 @@ drainedInterval = setInterval(() => { const app = "drained.app.js"; - if(disableBoot) + if(!keepStartup) require("Storage").write( ".boot0", `if(typeof __FILE__ === "undefined" || __FILE__ !== "${app}") setTimeout(load, 100, "${app}");` @@ -18,4 +18,4 @@ drainedInterval = setInterval(() => { load(app); }, interval * 60 * 1000); -} +})() diff --git a/apps/drained/metadata.json b/apps/drained/metadata.json index 89a069b2f..6dfdac78d 100644 --- a/apps/drained/metadata.json +++ b/apps/drained/metadata.json @@ -1,7 +1,7 @@ { "id": "drained", "name": "Drained", - "version": "0.01", + "version": "0.03", "description": "Switches to displaying a simple clock when the battery percentage is low, and disables some peripherals", "readme": "README.md", "icon": "icon.png", @@ -14,5 +14,8 @@ {"name":"drained.app.js","url":"app.js"}, {"name":"drained.settings.js","url":"settings.js"}, {"name":"drained.img","url":"app-icon.js","evaluate":true} + ], + "data": [ + {"name":"drained.setting.json"} ] } diff --git a/apps/drained/settings.js b/apps/drained/settings.js index fe4ab9c4b..ce72f215f 100644 --- a/apps/drained/settings.js +++ b/apps/drained/settings.js @@ -1,26 +1,19 @@ (function (back) { - var _a, _b, _c, _d; + var _a, _b, _c, _d, _e; var SETTINGS_FILE = "drained.setting.json"; var storage = require("Storage"); var settings = storage.readJSON(SETTINGS_FILE, true) || {}; (_a = settings.battery) !== null && _a !== void 0 ? _a : (settings.battery = 5); (_b = settings.restore) !== null && _b !== void 0 ? _b : (settings.restore = 20); (_c = settings.interval) !== null && _c !== void 0 ? _c : (settings.interval = 10); - (_d = settings.disableBoot) !== null && _d !== void 0 ? _d : (settings.disableBoot = false); + (_d = settings.keepStartup) !== null && _d !== void 0 ? _d : (settings.keepStartup = true); + (_e = settings.exceptions) !== null && _e !== void 0 ? _e : (settings.exceptions = ["widdst.0"]); var save = function () { storage.writeJSON(SETTINGS_FILE, settings); }; - E.showMenu({ + var menu = { "": { "title": "Drained" }, "< Back": back, - "Keep startup code": { - value: settings.disableBoot, - format: function () { return settings.disableBoot ? "No" : "Yes"; }, - onchange: function () { - settings.disableBoot = !settings.disableBoot; - save(); - }, - }, "Trigger at batt%": { value: settings.battery, min: 0, @@ -54,5 +47,44 @@ save(); }, }, - }); + "Keep startup code": { + value: settings.keepStartup, + onchange: function (b) { + settings.keepStartup = b; + save(); + updateAndRedraw(); + }, + }, + }; + var updateAndRedraw = function () { + setTimeout(function () { E.showMenu(menu); }, 10); + if (settings.keepStartup) { + delete menu["Startup exceptions"]; + return; + } + menu["Startup exceptions"] = function () { return E.showMenu(bootExceptions); }; + var bootExceptions = { + "": { "title": "Startup exceptions" }, + "< Back": function () { return E.showMenu(menu); }, + }; + storage.list(/\.boot\.js/) + .map(function (name) { return name.replace(".boot.js", ""); }) + .forEach(function (name) { + bootExceptions[name] = { + value: settings.exceptions.indexOf(name) >= 0, + onchange: function (b) { + if (b) { + settings.exceptions.push(name); + } + else { + var i = settings.exceptions.indexOf(name); + if (i >= 0) + settings.exceptions.splice(i, 1); + } + save(); + }, + }; + }); + }; + updateAndRedraw(); }); diff --git a/apps/drained/settings.ts b/apps/drained/settings.ts index c79e6605c..f972f51a7 100644 --- a/apps/drained/settings.ts +++ b/apps/drained/settings.ts @@ -2,7 +2,8 @@ type DrainedSettings = { battery?: number, restore?: number, interval?: number, - disableBoot?: ShortBoolean, + keepStartup?: ShortBoolean, + exceptions?: string[], }; (back => { @@ -13,23 +14,16 @@ type DrainedSettings = { settings.battery ??= 5; settings.restore ??= 20; settings.interval ??= 10; - settings.disableBoot ??= false; + settings.keepStartup ??= true; + settings.exceptions ??= ["widdst.0"]; // daylight savings const save = () => { storage.writeJSON(SETTINGS_FILE, settings) }; - E.showMenu({ + const menu: Menu = { "": { "title": "Drained" }, "< Back": back, - "Keep startup code": { - value: settings.disableBoot, - format: () => settings.disableBoot ? "No" : "Yes", - onchange: () => { - settings.disableBoot = !settings.disableBoot; - save(); - }, - }, "Trigger at batt%": { value: settings.battery, min: 0, @@ -63,5 +57,48 @@ type DrainedSettings = { save(); }, }, - }); + "Keep startup code": { + value: settings.keepStartup as boolean, + onchange: (b: boolean) => { + settings.keepStartup = b; + save(); + updateAndRedraw(); + }, + }, + }; + + const updateAndRedraw = () => { + // will change the menu, queue redraw: + setTimeout(() => { E.showMenu(menu) }, 10); + + if (settings.keepStartup) { + delete menu["Startup exceptions"]; + return; + } + menu["Startup exceptions"] = () => E.showMenu(bootExceptions); + + const bootExceptions: Menu = { + "": { "title" : "Startup exceptions" }, + "< Back": () => E.showMenu(menu), + }; + + storage.list(/\.boot\.js/) + .map(name => name.replace(".boot.js", "")) + .forEach((name: string) => { + bootExceptions[name] = { + value: settings.exceptions!.indexOf(name) >= 0, + onchange: (b: boolean) => { + if (b) { + settings.exceptions!.push(name); + } else { + const i = settings.exceptions!.indexOf(name); + if (i >= 0) settings.exceptions!.splice(i, 1); + } + save(); + }, + }; + }); + }; + + updateAndRedraw(); }) satisfies SettingsFunc diff --git a/apps/popconlaunch/ChangeLog b/apps/popconlaunch/ChangeLog index 8a2124e33..c430b4412 100644 --- a/apps/popconlaunch/ChangeLog +++ b/apps/popconlaunch/ChangeLog @@ -1,2 +1,3 @@ 0.01: New App! 0.02: Trim old entries from the popcon app cache +0.03: Avoid polluting global scope diff --git a/apps/popconlaunch/boot.js b/apps/popconlaunch/boot.js index 9124344b2..0080a66f9 100644 --- a/apps/popconlaunch/boot.js +++ b/apps/popconlaunch/boot.js @@ -1,18 +1,18 @@ -{ - var oldRead_1 = require("Storage").readJSON; - var oneMonth_1 = 1000 * 86400 * 28; - var monthAgo_1 = Date.now() - oneMonth_1; - var cache_1; - var ensureCache_1 = function () { - if (!cache_1) { - cache_1 = oldRead_1("popcon.cache.json", true); - if (!cache_1) - cache_1 = {}; +(function () { + var oldRead = require("Storage").readJSON; + var oneMonth = 1000 * 86400 * 28; + var monthAgo = Date.now() - oneMonth; + var cache; + var ensureCache = function () { + if (!cache) { + cache = oldRead("popcon.cache.json", true); + if (!cache) + cache = {}; } - return cache_1; + return cache; }; - var trimCache_1 = function (cache) { - var threeMonthsBack = Date.now() - oneMonth_1 * 3; + var trimCache = function (cache) { + var threeMonthsBack = Date.now() - oneMonth * 3; var del = []; for (var k in cache) if (cache[k].last < threeMonthsBack) @@ -22,21 +22,21 @@ delete cache[k]; } }; - var saveCache_1 = function (cache, orderChanged) { - trimCache_1(cache); + var saveCache = function (cache, orderChanged) { + trimCache(cache); require("Storage").writeJSON("popcon.cache.json", cache); if (orderChanged) { - var info = oldRead_1("popcon.info", true); + var info = oldRead("popcon.info", true); info.cacheBuster = !info.cacheBuster; require("Storage").writeJSON("popcon.info", info); } }; - var sortCache_1 = function () { - var ents = Object.values(cache_1); + var sortCache = function () { + var ents = Object.values(cache); ents.sort(function (a, b) { var n; - var am = (a.last > monthAgo_1); - var bm = (b.last > monthAgo_1); + var am = (a.last > monthAgo); + var bm = (b.last > monthAgo); n = bm - am; if (n) return n; @@ -64,31 +64,31 @@ }; require("Storage").readJSON = (function (fname, skipExceptions) { var _a; - var j = oldRead_1(fname, skipExceptions); + var j = oldRead(fname, skipExceptions); if (/\.info$/.test(fname)) { - var cache_2 = ensureCache_1(); + var cache_1 = ensureCache(); var so = void 0; - if (j.src && (so = (_a = cache_2[j.src]) === null || _a === void 0 ? void 0 : _a.sortorder) != null) + if (j.src && (so = (_a = cache_1[j.src]) === null || _a === void 0 ? void 0 : _a.sortorder) != null) j.sortorder = so; else j.sortorder = 99; } return j; }); - var oldLoad_1 = load; + var oldLoad = load; global.load = function (src) { if (src) { - var cache_3 = ensureCache_1(); - var ent = cache_3[src] || (cache_3[src] = { + var cache_2 = ensureCache(); + var ent = cache_2[src] || (cache_2[src] = { pop: 0, last: 0, sortorder: -10, }); ent.pop++; ent.last = Date.now(); - var orderChanged = sortCache_1(); - saveCache_1(cache_3, orderChanged); + var orderChanged = sortCache(); + saveCache(cache_2, orderChanged); } - return oldLoad_1(src); + return oldLoad(src); }; -} +})(); diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts index 306d4844a..4fe638f75 100644 --- a/apps/popconlaunch/boot.ts +++ b/apps/popconlaunch/boot.ts @@ -1,4 +1,4 @@ -{ +(() => { type Timestamp = number; type Cache = { [key: string]: { @@ -113,4 +113,4 @@ global.load = (src: string) => { return oldLoad(src); }; -} +})() diff --git a/apps/popconlaunch/metadata.json b/apps/popconlaunch/metadata.json index 1acab2b6c..be3bc6a92 100644 --- a/apps/popconlaunch/metadata.json +++ b/apps/popconlaunch/metadata.json @@ -2,7 +2,7 @@ "id": "popconlaunch", "name": "Popcon Launcher", "shortName": "Popcon", - "version": "0.02", + "version": "0.03", "description": "Launcher modification - your launchers will display your favourite (popular) apps first. Overrides `readJSON`, may slow down your watch", "readme": "README.md", "icon": "app.png",