|
| 1 | +{ |
| 2 | +// Things that use `eval` instead of `load` or `Bangle.load` (e.g. auto displaying new messages - `messagegui.new.js`) can affect the app history record unexpectedly. They don't themselves get added to the history (since we don't override `eval`) and if they use one of the other two mentioned load functions they may remove the last recorded entry. |
| 3 | + |
| 4 | +const s = require("Storage"); |
| 5 | +const SETTINGS = s.readJSON("fastload.json") || {}; |
| 6 | + |
| 7 | +global._load = global.load; |
| 8 | +Bangle._load = Bangle.load; |
| 9 | + |
| 10 | +let appHistory = s.readJSON("fastload.history.json",true)||[]; |
| 11 | +const resetHistory = ()=>{appHistory=[];s.writeJSON("fastload.history.json",appHistory);}; |
| 12 | +const recordHistory = ()=>{s.writeJSON("fastload.history.json",appHistory);}; |
| 13 | + |
| 14 | +const traverseHistory = (name)=>{ |
| 15 | + if (name && name!=".bootcde" && !(name=="quicklaunch.app.js" && SETTINGS.disregardQuicklaunch)) { |
| 16 | + // store the name of the app to launch |
| 17 | + appHistory.push(name); |
| 18 | + } else if (name==".bootcde") { // when Bangle.showClock is called |
| 19 | + resetHistory(); |
| 20 | + } else if (name=="quicklaunch.app.js" && SETTINGS.disregardQuicklaunch) { |
| 21 | + // do nothing with history |
| 22 | + } else { |
| 23 | + // go back in history |
| 24 | + appHistory.pop(); |
| 25 | + name = appHistory[appHistory.length-1]; |
| 26 | + } |
| 27 | + return name; |
| 28 | +} |
| 29 | + |
| 30 | +const addHistoryTraversal = (loadFn => (name) => { |
| 31 | + name = traverseHistory(name); |
| 32 | + loadFn(name); |
| 33 | +}) |
| 34 | + |
| 35 | +const addHistoryTraversalFastload = (loadFn => (name) => { |
| 36 | + global.load = global._load; // Only run through traverseHistory once, not both for Bangle.load and global.load. |
| 37 | + name = traverseHistory(name); |
| 38 | + loadFn(name); |
| 39 | + global.load = globalLoadWithHistory; |
| 40 | +}) |
| 41 | + |
| 42 | +const globalLoadWithHistory = addHistoryTraversal(global.load); |
| 43 | +global.load = globalLoadWithHistory; |
| 44 | +Bangle.load = addHistoryTraversalFastload(Bangle._load); |
| 45 | + |
| 46 | +E.on('kill', ()=>{ |
| 47 | + // Usually record history, but reset it if long press of HW button was used. May be tricky to release button quick enough to not trigger resetHistory. |
| 48 | + if (!BTN.read()) recordHistory(); else resetHistory(); |
| 49 | +}) |
| 50 | +} |
0 commit comments