2022-10-29 12:17:40 +00:00
{
2023-03-25 12:56:53 +00:00
const s = require ( "Storage" ) ;
const SETTINGS = s . readJSON ( "fastload.json" ) || { } ;
2022-11-22 18:05:12 +00:00
2022-10-29 12:17:40 +00:00
let loadingScreen = function ( ) {
g . reset ( ) ;
let x = g . getWidth ( ) / 2 ;
let y = g . getHeight ( ) / 2 ;
g . setColor ( g . theme . bg ) ;
g . fillRect ( x - 49 , y - 19 , x + 49 , y + 19 ) ;
g . setColor ( g . theme . fg ) ;
g . drawRect ( x - 50 , y - 20 , x + 50 , y + 20 ) ;
g . setFont ( "6x8" ) ;
g . setFontAlign ( 0 , 0 ) ;
g . drawString ( "Fastloading..." , x , y ) ;
g . flip ( true ) ;
} ;
2023-03-25 12:56:53 +00:00
let cache = s . readJSON ( "fastload.cache" ) || { } ;
2022-11-22 18:05:12 +00:00
2023-07-21 17:28:32 +00:00
const SYS _SETTINGS = "setting.json" ;
let appFastloadPossible = function ( n ) {
2023-07-24 20:32:01 +00:00
if ( SETTINGS . detectSettingsChange && ( ! cache [ SYS _SETTINGS ] || s . hash ( SYS _SETTINGS ) != cache [ SYS _SETTINGS ] ) ) {
cache [ SYS _SETTINGS ] = s . hash ( SYS _SETTINGS ) ;
2023-07-21 17:28:32 +00:00
s . writeJSON ( "fastload.cache" , cache ) ;
return false ;
}
2022-11-22 18:05:12 +00:00
// no widgets, no problem
if ( ! global . WIDGETS ) return true ;
2023-07-27 10:04:24 +00:00
let hash = s . hash ( n ) ;
if ( cache [ n ] && hash == cache [ n ] . hash )
2023-03-25 12:56:53 +00:00
return cache [ n ] . fast ;
2023-07-27 10:04:24 +00:00
let app = s . read ( n ) ;
2022-11-22 18:05:12 +00:00
cache [ n ] = { } ;
cache [ n ] . fast = app . includes ( "Bangle.loadWidgets" ) ;
2023-07-27 10:04:24 +00:00
cache [ n ] . hash = hash ;
2023-03-25 12:56:53 +00:00
s . writeJSON ( "fastload.cache" , cache ) ;
2022-11-22 18:05:12 +00:00
return cache [ n ] . fast ;
2023-03-25 12:56:53 +00:00
} ;
2022-11-22 18:05:12 +00:00
global . _load = load ;
2022-11-28 19:26:46 +00:00
let slowload = function ( n ) {
2022-11-22 18:05:12 +00:00
global . _load ( n ) ;
2023-03-25 12:56:53 +00:00
} ;
2022-11-22 18:05:12 +00:00
2022-11-28 19:26:46 +00:00
let fastload = function ( n ) {
2023-07-21 17:28:32 +00:00
if ( ! n || appFastloadPossible ( n ) ) {
2022-11-22 18:05:12 +00:00
// 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 ;
2023-03-25 12:56:53 +00:00
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 ) ; } ;
}
2022-10-29 12:17:40 +00:00
Bangle . load = ( o => ( name ) => {
2022-11-27 18:19:33 +00:00
if ( Bangle . uiRemove && ! SETTINGS . hideLoading ) loadingScreen ( ) ;
2023-03-25 12:56:53 +00:00
if ( SETTINGS . useAppHistory ) {
if ( name && name != ".bootcde" && ! ( name == "quicklaunch.app.js" && SETTINGS . disregardQuicklaunch ) ) {
// store the name of the app to launch
appHistory . push ( name ) ;
} else if ( name == ".bootcde" ) { // when Bangle.showClock is called
resetHistory ( ) ;
} else if ( name == "quicklaunch.app.js" && SETTINGS . disregardQuicklaunch ) {
// do nothing with history
} else {
// go back in history
appHistory . pop ( ) ;
name = appHistory [ appHistory . length - 1 ] ;
}
}
2022-11-22 18:05:12 +00:00
if ( SETTINGS . autoloadLauncher && ! name ) {
let orig = Bangle . load ;
Bangle . load = ( n ) => {
Bangle . load = orig ;
fastload ( n ) ;
2023-03-25 12:56:53 +00:00
} ;
2022-11-22 18:05:12 +00:00
Bangle . showLauncher ( ) ;
Bangle . load = orig ;
2023-03-25 12:56:53 +00:00
} else
2022-11-22 18:05:12 +00:00
o ( name ) ;
2022-10-29 12:17:40 +00:00
} ) ( Bangle . load ) ;
2023-03-25 12:56:53 +00:00
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.
2022-10-29 12:17:40 +00:00
}