swp2clk - Always have a swipe handler and decide what to do on every swipe

pull/2264/head
Martin Boonk 2022-11-14 20:16:03 +01:00
parent 62659c0042
commit 8b6bff8bf6
1 changed files with 31 additions and 117 deletions

View File

@ -1,133 +1,47 @@
/**
* How does this work?
*
* Every *boot.js file is executed everytime any app is loaded, including this one.
* We just need to figure out which app is currently loaded, in case we are in the white list / black list mode,
* to figure out if the swipe handler should be attached or not.
* It does not seem to be the case that this can be done easily, but we can work around it.
* It seems that every app is loaded via the global "load" function, which takes a fileName as it's first parameter to load any app
* or the default clock when the fileName is undefined.
* To be able to use this for us, we wrap the global "load" function, and determine before loading the next app,
* whether the swipe handler should be added or not, since we now know which app will be loaded.
* Note: We cannot add the swipe handler inside the wrapped "load" function, because once the "load" function is complete
* everything is cleaned up. That's why we merely save a flag, whether the swipe handler should be attached or not,
* which is evaluated once this file is executed again, which will be right after the load function completes
* (since every *boot.js file is executed everytime any app is loaded).
*/
{
const DEBUG = false;
const FILE = "swp2clk.data.json";
(function () {
var DEBUG = false;
var FILE = "swp2clk.data.json";
var swipeHandler = (dir) => {
log("swipe");
log(dir);
if (dir === 1) {
if (!Bangle.showClock) load();
else Bangle.showClock();
}
};
var main = () => {
var settings = readSettings();
if (settings.addSwipeHandler) {
Bangle.on("swipe", swipeHandler);
}
var global_load = global.load;
global.load = (fileName) => {
log("loading filename!");
log(fileName);
var settings = readSettings();
if (fileName && fileName != ".bootcde") {
// "Off"
if (settings.mode === 0) {
settings.addSwipeHandler = false;
}
// "White List"
if (settings.mode === 1) {
if (settings.whiteList.indexOf(fileName) >= 0) {
settings.addSwipeHandler = true;
} else {
settings.addSwipeHandler = false;
}
}
// "Black List"
if (settings.mode === 2) {
if (settings.blackList.indexOf(fileName) >= 0) {
settings.addSwipeHandler = false;
} else {
settings.addSwipeHandler = true;
}
}
// "Always"
if (settings.mode === 3) {
settings.addSwipeHandler = true;
}
} else {
// Clock will load
settings.addSwipeHandler = false;
}
writeSettings(settings);
global_load(fileName);
};
};
// lib functions
var log = (message) => {
let log = (message) => {
if (DEBUG) {
console.log(JSON.stringify(message));
}
};
var readSettings = () => {
let readSettings = () => {
log("reading settings");
var settings = require("Storage").readJSON(FILE, 1) || {
let settings = require("Storage").readJSON(FILE, 1) || {
mode: 0,
whiteList: [],
blackList: [],
addSwipeHandler: false,
blackList: []
};
log(settings);
return settings;
};
var writeSettings = (settings) => {
log("writing settings");
log(settings);
require("Storage").writeJSON(FILE, settings);
let settings = readSettings();
//inhibit is needed to filter swipes that had another handler load anything earlier than this handler was called
let inhibit = false;
Bangle.load = ( o => (f) => {
o(f);
log("inhibit caused by change to:" + f);
inhibit = true;
})(Bangle.load);
let swipeHandler = (dir) => {
log("swipe:" + dir + " on app: " + __FILE__);
if (!inhibit && dir === 1 && !Bangle.CLOCK && __FILE__ != ".bootcde") {
log("on a not clock app " + __FILE__);
if ((settings.mode === 1 && settings.whiteList.includes(__FILE__)) || // "White List"
(settings.mode === 2 && !settings.blackList.includes(__FILE__)) || // "Black List"
settings.mode === 3) { // "Always"
log("load clock");
Bangle.showClock();
}
}
inhibit = false;
};
// start main function
if (!Bangle.load){
log("no Bangle.load available");
main();
} else {
log("Bangle.load available");
Bangle.load = (o => (fileName) => {
log("load " + fileName);
var settings = readSettings();
log("remove swipe handler");
Bangle.removeListener("swipe", swipeHandler);
if (fileName && fileName != ".bootcde") {
log("loading a not clock app " + fileName);
if ((settings.mode === 1 && settings.whiteList.includes(fileName)) || // "White List"
(settings.mode === 2 && !settings.blackList.includes(fileName)) || // "Black List"
settings.mode === 3) { // "Always"
log("register swipe handler");
// The handler must be added in the next idle time to prevent it executing directly iff Bangle.load was called in another swipe handler
setTimeout(()=>{Bangle.on("swipe", swipeHandler);},0);
}
}
o(fileName);
})(Bangle.load);
}
})();
Bangle.on("swipe", swipeHandler);
}