1- // Will calling Bangle.load reset everything? if false, we fast load
2- function loadWillReset ( ) {
3- return Bangle . load === load || ! Bangle . uiRemove ;
4- /* FIXME: Maybe we need a better way of deciding if an app will
5- be fast loaded than just hard-coding a Bangle.uiRemove check.
6- Bangle.load could return a bool (as the load doesn't happen immediately). */
7- }
8-
91/**
102 * Listener set up in boot.js, calls into here to keep boot.js short
113 */
@@ -40,27 +32,24 @@ exports.listener = function(type, msg) {
4032 ( autoOpen === 1 && Bangle . CLOCK ) || ( autoOpen === 2 && ( Bangle . isLocked ( ) || Bangle . CLOCK ) ) || autoOpen === 3 ||
4133 msg . important
4234 ) ; // should we load the messages app?
43- if ( type === "music" ) {
44- if ( Bangle . CLOCK && msg . state && msg . title && appSettings . openMusic ) loadMessages = true ;
45- else return ;
46- }
35+ if ( type === "music" )
36+ loadMessages = Bangle . CLOCK && msg . state && msg . title && appSettings . openMusic ;
37+
4738 // Write the message to Bangle.MESSAGES. We'll deal with it in messageTimeout below
4839 if ( ! Bangle . MESSAGES ) Bangle . MESSAGES = [ ] ;
4940 require ( "messages" ) . apply ( msg , Bangle . MESSAGES ) ;
5041 if ( ! Bangle . MESSAGES . length ) delete Bangle . MESSAGES ;
51- const saveToFlash = ( ) => {
52- // save messages from RAM to flash if we decide not to launch app
53- // We apply all of Bangle.MESSAGES here in one write
54- if ( ! Bangle . MESSAGES || ! Bangle . MESSAGES . length ) return ;
55- let messages = require ( "messages" ) . getMessages ( msg ) ;
56- ( Bangle . MESSAGES || [ ] ) . forEach ( m => require ( "messages" ) . apply ( m , messages ) ) ;
57- require ( "messages" ) . write ( messages ) ;
58- delete Bangle . MESSAGES ;
42+ else if ( ! Bangle . MESSAGES_KILL_HANDLER ) { // ensure we save on exit now
43+ Bangle . MESSAGES_KILL_HANDLER = function ( ) {
44+ if ( ! Bangle . MESSAGES || ! Bangle . MESSAGES . length ) return ;
45+ let messages = require ( "messages" ) . getMessages ( msg ) ;
46+ require ( "messages" ) . write ( messages ) ;
47+ delete Bangle . MESSAGES ;
48+ } ;
49+ E . on ( "kill" , Bangle . MESSAGES_KILL_HANDLER ) ;
5950 }
6051 msg . handled = true ;
61- if ( ( msg . t !== "add" || ! msg . new ) && ( type !== "music" ) ) // music always has t:"modify"
62- return saveToFlash ( ) ;
63-
52+ if ( ! msg . new ) return ; // if it's not new, we don't do anything - just save on exit
6453 const quiet = ( require ( "Storage" ) . readJSON ( "setting.json" , 1 ) || { } ) . quiet ;
6554 const unlockWatch = appSettings . unlockWatch ;
6655 // don't auto-open messages in quiet mode if quietNoAutOpn is true
@@ -71,20 +60,16 @@ exports.listener = function(type, msg) {
7160 exports . messageTimeout = setTimeout ( function ( ) {
7261 delete exports . messageTimeout ;
7362 if ( ! Bangle . MESSAGES ) return ; // message was removed during the delay
74- if ( type !== "music" ) {
75- if ( ! loadMessages ) {
76- // not opening the app, just buzz
77- saveToFlash ( ) ;
78- return require ( "messages" ) . buzz ( msg . src ) ;
79- }
63+ if ( ! loadMessages ) { // not opening the app, just buzz
64+ if ( type !== "music" ) require ( "messages" ) . buzz ( msg . src ) ; // buzz ignores quiet mode
65+ } else {
8066 if ( ! quiet && unlockWatch ) {
8167 Bangle . setLocked ( false ) ;
8268 Bangle . setLCDPower ( 1 ) ; // turn screen on
8369 }
70+ // now open the GUI
71+ exports . open ( msg ) ;
8472 }
85- // if loading the gui would reload everything, we must save our messages
86- if ( loadWillReset ( ) ) saveToFlash ( ) ;
87- exports . open ( msg ) ;
8873 } , 500 ) ;
8974} ;
9075
@@ -93,19 +78,13 @@ exports.listener = function(type, msg) {
9378 * @param {object } msg
9479 */
9580exports . open = function ( msg ) {
81+ // force a display by setting it as new and ensuring it ends up at the beginning of messages list
9682 if ( msg && msg . id ) {
97- // force a display by setting it as new and ensuring it ends up at the beginning of messages list
9883 msg . new = 1 ;
99- if ( loadWillReset ( ) ) {
100- // no fast loading: store message to load in flash - `msg` will be put in first
101- require ( "messages" ) . save ( msg , { force : 1 } ) ;
102- } else {
103- // fast load - putting it at the end of Bangle.MESSAGES ensures it goes at the start of messages list
104- if ( ! Bangle . MESSAGES ) Bangle . MESSAGES = [ ] ;
105- Bangle . MESSAGES = Bangle . MESSAGES . filter ( m => m . id != msg . id )
106- Bangle . MESSAGES . push ( msg ) ; // putting at the
107- }
84+ if ( ! Bangle . MESSAGES ) Bangle . MESSAGES = [ ] ;
85+ Bangle . MESSAGES = Bangle . MESSAGES . filter ( m => m . id != msg . id )
86+ Bangle . MESSAGES . push ( msg ) ;
10887 }
109-
88+ // load the app
11089 Bangle . load ( ( msg && msg . new && msg . id !== "music" ) ? "messagegui.new.js" : "messagegui.app.js" ) ;
111- } ;
90+ } ;
0 commit comments