Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fastload] Improve code readability + new option #2863

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/fastload/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
0.02: Allow redirection of loads to the launcher
0.03: Allow hiding the fastloading info screen
0.04: (WIP) Allow use of app history when going back (`load()` or `Bangle.load()` calls without specified app).
0.05: New option 'FastLoad into Widget-less' keep widgets in RAM but hide them.
0.06: Code overhaul increase readability.
2 changes: 2 additions & 0 deletions apps/fastload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This allows fast loading of all apps with two conditions:
* If Quick Launch is installed it can be excluded from app history
* Allows to redirect all loads usually loading the clock to the launcher instead
* The "Fastloading..." screen can be switched off
* FastLoad into Widget-less uses widget_utils to hide widgets but remain in RAM.

## App history

Expand All @@ -29,3 +30,4 @@ It checks the app to be loaded for widget use and stores the result of that and

# Contributors
[thyttan](https://github.com/thyttan)
[d3nd3](https://github.com/d3nd3)
109 changes: 75 additions & 34 deletions apps/fastload/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,78 @@ let checkApp = function(n){
return cache[n].fast;
};

global._load = load;

let slowload = function(n){
global._load(n);
};

let fastload = function(n){
if (!n || checkApp(n)){
// Bangle.load can call load, to prevent recursion this must be the system load
global.load = slowload;
Bangle.load(n);
// if fastloading worked, we need to set load back to this method
global.load = fastload;
}
else
slowload(n);
};
global.load = fastload;

let appHistory, resetHistory, recordHistory;
if (SETTINGS.useAppHistory){
appHistory = s.readJSON("fastload.history.json",true)||[];
resetHistory = ()=>{appHistory=[];s.writeJSON("fastload.history.json",appHistory);};
recordHistory = ()=>{s.writeJSON("fastload.history.json",appHistory);};
}
/*
load(file)/Bang.load(file) -> transformName() -> validate() -> (orig)Bangle.load()
slowload resets memory. -> load()
Attempt to convert a slowEntry into a fastEntry.
*/
Bangle.load = (fastload => {
//global.load()
let slowEntry = (n) => {
hookload(n,false);
};
//Bangle.load()
let fastEntry = (n) => {
hookload(n,true);
};

Bangle.load = (o => (name) => {
if (Bangle.uiRemove && !SETTINGS.hideLoading) loadingScreen();
if (SETTINGS.useAppHistory){
// double function hook
let hookload = (name,fastRequest) => {
if (SETTINGS.useAppHistory) name = rememberAppName(name);
if (!Bangle.uiRemove) {
slowload(name);
return; // never gets here.
}
if (SETTINGS.autoloadLauncher) name = transformName(name);
let force = SETTINGS.hiddenWidgets; // always true
// bootloader(bootcde) internally checks if target clock calls LoadWidgets thus why true.
let isValidApp = name ? checkApp(name) : true;
if (!SETTINGS.hideLoading) loadingScreen();
if (validate(name,isValidApp,force)) {
//if (!fastRequest) print("Upgraded slow to fast!");
//else print("Intended fast!");
if (force && name === ".bootcde") name = ""; // disguise to allow clock @bootloader.js
global.load = slowload;
fastload(name); // guaranteed to fastload (uiRemove=true)
global.load = slowEntry;
if (force) {
global.__FILE__ = name; // restore from disguise
if (isValidApp) require("widget_utils").show();
else require("widget_utils").hide();
}
}
else {
slowload(name);
}
};

// Does the target app contain LoadWidgets()?
let validate = function(n,valid,force){
if ( force || !n || checkApp(n)) return true;
// invalidate named app without LoadWidgets.
return false;
};

// Intercept App Load.
let transformName = function(name) {
// empty name is converted into launcher name
if (!name){
Bangle.load = (n)=>{
name = n;
};
Bangle.showLauncher(); // calls Bangle.load()
Bangle.load = fastEntry;
}
return name;
};

let rememberAppName = function(name) {
if (name && name!=".bootcde" && !(name=="quicklaunch.app.js" && SETTINGS.disregardQuicklaunch)) {
// store the name of the app to launch
appHistory.push(name);
Expand All @@ -73,18 +116,16 @@ Bangle.load = (o => (name) => {
appHistory.pop();
name = appHistory[appHistory.length-1];
}
}
if (SETTINGS.autoloadLauncher && !name){
let orig = Bangle.load;
Bangle.load = (n)=>{
Bangle.load = orig;
fastload(n);
};
Bangle.showLauncher();
Bangle.load = orig;
} else
o(name);
//print(appHistory);
return name;
};

let slowload = global.load;
// Intercept load too.
global.load = slowEntry;
return fastEntry;
})(Bangle.load);

if (SETTINGS.useAppHistory) E.on('kill', ()=>{if (!BTN.read()) recordHistory(); else resetHistory();}); // Usually record history, but reset it if long press of HW button was used.
// Usually record history, but reset it if long press of HW button was used.
if (SETTINGS.useAppHistory) E.on('kill', ()=>{if (!BTN.read()) recordHistory(); else resetHistory();});
}
2 changes: 1 addition & 1 deletion apps/fastload/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ "id": "fastload",
"name": "Fastload Utils",
"shortName" : "Fastload Utils",
"version": "0.04",
"version": "0.06",
"icon": "icon.png",
"description": "Enable experimental fastloading for more apps",
"type":"bootloader",
Expand Down
15 changes: 11 additions & 4 deletions apps/fastload/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
setTimeout(()=>E.showMenu(buildMainMenu()), 0); // Update the menu so it can be seen if a value was automatically set to false (app history vs load launcher).
}
}
};
};

if (isQuicklaunchPresent) {
mainmenu['Exclude Quick Launch from history'] = {
Expand All @@ -50,15 +50,22 @@
setTimeout(()=>E.showMenu(buildMainMenu()), 0); // Update the menu so it can be seen if a value was automatically set to false (app history vs load launcher).
} // Don't use app history and load to launcher together.
}
};
};

mainmenu['Hide "Fastloading..."'] = {
value: !!settings.hideLoading,
onchange: v => {
writeSettings("hideLoading",v);
}
};


};

mainmenu['FastLoad into Widget-less'] = {
value: !!settings.hiddenWidgets,
onchange: v => {
writeSettings("hiddenWidgets",v);
}
};
return mainmenu;
}

Expand Down
Loading