@@ -67,39 +67,51 @@ if (Bangle.MESSAGES) {
6767 delete Bangle . MESSAGES ;
6868}
6969
70- // Ensure any music message is always kept at the front of the list
71- function moveMusicToFront ( ) {
72- for ( var i = 0 ; i < MESSAGES . length ; i ++ ) {
73- if ( MESSAGES [ i ] && MESSAGES [ i ] . id == "music" ) {
74- if ( i !== 0 ) MESSAGES . unshift ( MESSAGES . splice ( i , 1 ) [ 0 ] ) ;
75- return ;
76- }
70+ // Update or add music message to MESSAGES array at front
71+ function updateMusicMessage ( musicMsg ) {
72+ var existingMusicIdx = MESSAGES . findIndex ( m => m . id === "music" ) ;
73+ if ( existingMusicIdx >= 0 ) {
74+ MESSAGES [ existingMusicIdx ] = musicMsg ;
75+ if ( existingMusicIdx !== 0 ) MESSAGES . unshift ( MESSAGES . splice ( existingMusicIdx , 1 ) [ 0 ] ) ;
76+ } else {
77+ MESSAGES . unshift ( musicMsg ) ;
7778 }
7879}
79- moveMusicToFront ( ) ;
80+
81+ // Load music message from storage if it exists
82+ var musicMsg = require ( "messages" ) . getMusic ( ) ;
83+ if ( musicMsg && ( musicMsg . track || musicMsg . artist ) ) {
84+ updateMusicMessage ( musicMsg ) ;
85+ }
8086
8187var onMessagesModified = function ( type , msg ) {
8288 if ( msg . handled ) return ;
8389 msg . handled = true ;
84- require ( "messages" ) . apply ( msg , MESSAGES ) ;
85- // Keep music message at the front so it remains the first item
86- moveMusicToFront ( ) ;
87- // TODO: if new, show this new one
88- if ( msg && msg . id !== "music" && msg . id !== "nav" && msg . new &&
89- ! ( ( require ( 'Storage' ) . readJSON ( 'setting.json' , 1 ) || { } ) . quiet ) ) {
90- require ( "messages" ) . buzz ( msg . src ) ;
91- }
92- if ( msg && msg . id == "music" ) {
93- // Track when music actually played so we can expire old music messages
94- if ( msg . state && msg . state == "play" ) {
95- // Update the stored music message (if present) with a last-played timestamp
96- var mm = MESSAGES . find ( m => m && m . id == "music" ) ;
97- if ( mm ) mm . _lastPlayed = Date . now ( ) ;
90+
91+ if ( msg . id === "music" ) {
92+ // For music messages, get the complete state from messages module
93+ updateMusicMessage ( require ( "messages" ) . getMusic ( ) ) ;
94+ var musicMsg = MESSAGES [ 0 ] ; // music is now at front
95+
96+ if ( musicMsg . state && musicMsg . state == "play" ) {
9897 openMusic = true ;
99- } else if ( msg . state && msg . state != "play" ) {
98+ } else if ( musicMsg . state && musicMsg . state != "play" ) {
10099 openMusic = false ; // no longer playing music to go back to
101100 }
102101 if ( ( active != undefined ) && ( active != "list" ) && ( active != "music" ) ) return ; // don't open music over other screens (but do if we're in the main menu)
102+ } else {
103+ require ( "messages" ) . apply ( msg , MESSAGES ) ;
104+ // Move music back to front since apply() may have pushed it down
105+ var musicIdx = MESSAGES . findIndex ( m => m . id === "music" ) ;
106+ if ( musicIdx > 0 ) {
107+ MESSAGES . unshift ( MESSAGES . splice ( musicIdx , 1 ) [ 0 ] ) ;
108+ }
109+ }
110+
111+ // TODO: if new, show this new one
112+ if ( msg && msg . id !== "music" && msg . id !== "nav" && msg . new &&
113+ ! ( ( require ( 'Storage' ) . readJSON ( 'setting.json' , 1 ) || { } ) . quiet ) ) {
114+ require ( "messages" ) . buzz ( msg . src ) ;
103115 }
104116 if ( msg && msg . id == "nav" && msg . t == "modify" && active != "map" )
105117 return ; // don't show an updated nav message if we're just in the menu
@@ -539,18 +551,8 @@ function showMessage(msgid, persist) {
539551*/
540552function checkMessages ( options ) {
541553 options = options || { } ;
542- // Remove/ignore stale music messages if they haven't played recently.
543- var musicTimeout = ( settings && settings . musicTimeoutMinutes ) ? settings . musicTimeoutMinutes : 5 ;
544- if ( isFinite ( musicTimeout ) && musicTimeout > 0 ) {
545- var now = Date . now ( ) ;
546- MESSAGES = MESSAGES . filter ( function ( m ) {
547- if ( ! m || m . id != "music" ) return true ;
548- if ( m . state == "play" || m . state == "show" ) return true ;
549- if ( m . _lastPlayed && ( now - m . _lastPlayed ) <= musicTimeout * 60000 ) return true ;
550- // otherwise drop the stale music message
551- return false ;
552- } ) ;
553- }
554+ // Remove/ignore stale music messages if they haven't played recently
555+ checkMusicExpired ( ) ;
554556 // If there's been some user interaction, it's time to stop repeated buzzing
555557 if ( ! options . dontStopBuzz )
556558 require ( "messages" ) . stopBuzz ( ) ;
@@ -650,6 +652,20 @@ function returnToClockIfEmpty() {
650652 checkMessages ( { clockIfNoMsg :1 , clockIfAllRead :0 , ignoreUnread :1 , openMusic} ) ;
651653}
652654
655+ function checkMusicExpired ( ) {
656+ var musicTimeout = ( settings && settings . musicTimeoutMinutes ) ? settings . musicTimeoutMinutes : 5 ;
657+ if ( ! isFinite ( musicTimeout ) || musicTimeout <= 0 ) return ;
658+
659+ var now = Date . now ( ) ;
660+ MESSAGES = MESSAGES . filter ( function ( m ) {
661+ if ( ! m || m . id != "music" ) return true ;
662+ if ( m . state == "play" || m . state == "show" ) return true ;
663+ if ( m . _lastPlayed && ( now - m . _lastPlayed ) <= musicTimeout * 60000 ) return true ;
664+ // otherwise drop the stale music message
665+ return false ;
666+ } ) ;
667+ }
668+
653669function cancelReloadTimeout ( ) {
654670 if ( ! unreadTimeout ) return ;
655671 clearTimeout ( unreadTimeout ) ;
0 commit comments