Skip to content

Commit 43034df

Browse files
committed
Attempt at improving messages/messagegui to stop un-needed writes to storage (and so allow music info to be stored too)
1 parent f44e5df commit 43034df

File tree

10 files changed

+50
-65
lines changed

10 files changed

+50
-65
lines changed

apps/android/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@
4747
version 85 when it's out)
4848
0.43: Ensure listRecs doesn't list old-style recorded tracks (Otherwise Gadgetbridge fails parsing the filename)
4949
0.44: Pass HTTP request timeout to Gadgetbridge
50+
0.45: Don't pass on music messages if nothing has changed

apps/android/lib.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ exports.gbSend = function(message) {
22
Bluetooth.println("");
33
Bluetooth.println(JSON.stringify(message));
44
}
5-
let lastMsg, // for music messages - may not be needed now...
5+
let lastMsg, // for GadgetBridge workaround - may not be needed now...
66
gpsState = {}, // keep information on GPS via Gadgetbridge
77
settings = Object.assign({rp:true,as:true,vibrate:".."},
88
require("Storage").readJSON("android.settings.json",1)||{}
@@ -38,6 +38,8 @@ exports.gbHandler = (event) => {
3838
},
3939
// {t:"musicstate", state:"play/pause",position,shuffle,repeat}
4040
"musicstate" : function() {
41+
if (event.state===exports.musicState) return; // no change - avoid spamming message handling with identical messages
42+
exports.musicState = event.state;
4143
require("messages").pushMessage({t:"modify",id:"music",title:"Music",state:event.state});
4244
},
4345
// {t:"musicinfo", artist,album,track,dur,c(track count),n(track num}

apps/android/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "android",
33
"name": "Android Integration",
44
"shortName": "Android",
5-
"version": "0.44",
5+
"version": "0.45",
66
"description": "Display notifications/music/etc sent from the Gadgetbridge app on Android. This replaces the old 'Gadgetbridge' Bangle.js widget.",
77
"icon": "app.png",
88
"tags": "tool,system,messages,notifications,gadgetbridge",

apps/messagegui/ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,6 @@
121121
0.89: Fix bugs related to empty titles and bodies
122122
0.90: New settings for when to auto show new message (never, on clocks -
123123
default, if locked, always)
124+
0.91: Ensure music messages are actually saved in the messages list so the currently playing track persists
125+
Use E.on("kill" event to ensure we always save messages before changing app
126+
Simplify message handling now messages lib automatically applies Bangle.messages

apps/messagegui/app.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,15 @@ to the clock. */
6161
var unreadTimeout;
6262
/// List of all our messages
6363
var MESSAGES = require("messages").getMessages();
64-
if (Bangle.MESSAGES) {
65-
// fast loading messages
66-
Bangle.MESSAGES.forEach(m => require("messages").apply(m, MESSAGES));
67-
delete Bangle.MESSAGES;
64+
delete Bangle.MESSAGES; // now we're storing all messages and handling them ourselves
65+
if (Bangle.MESSAGES_KILL_HANDLER) {
66+
E.removeListener("kill", Bangle.MESSAGES_KILL_HANDLER);
67+
delete Bangle.MESSAGES_KILL_HANDLER;
6868
}
69+
E.on("kill", function() { // now ensure we write messages on exit
70+
// if we write the exact same data it's ok - Bangle.js won't create a new file
71+
require("messages").write(MESSAGES, true/*force*/);
72+
});
6973

7074
var onMessagesModified = function(type,msg) {
7175
if (msg.handled) return;
@@ -86,10 +90,6 @@ var onMessagesModified = function(type,msg) {
8690
};
8791
Bangle.on("message", onMessagesModified);
8892

89-
function saveMessages() {
90-
require("messages").write(MESSAGES);
91-
}
92-
E.on("kill", saveMessages);
9393

9494
/* Listens to drag events to allow the user to swipe up/down to change message on Bangle.js 2
9595
returns dragHandler which should then be removed with Bangle.removeListener("drah", dragHandler); on exit */

apps/messagegui/lib.js

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
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
*/
9580
exports.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+
};

apps/messagegui/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "messagegui",
33
"name": "Message UI",
44
"shortName": "Messages",
5-
"version": "0.90",
5+
"version": "0.91",
66
"description": "Default app to display notifications from iOS and Gadgetbridge/Android",
77
"icon": "app.png",
88
"type": "app",

apps/messages/ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
Add Option to show widgets (Message GUI 0.86 removes them by default)
1515
0.68: New settings for when to auto show new message (never, on clocks -
1616
default, if locked, always)
17+
0.69: Remove .music field (which is not used - require("messages").getMessages().find(m=>m.id=="music") works fine)
18+
Ensure .getMessages uses Bangle.MESSAGES to return the most up to date message info

apps/messages/lib.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
exports.music = {};
21
/**
32
* Emit "message" event with appropriate type from Bangle
43
* @param {object} msg
@@ -18,9 +17,7 @@ function emit(msg) {
1817
*/
1918
exports.pushMessage = function(event) {
2019
// now modify/delete as appropriate
21-
if (event.t==="remove") {
22-
if (event.id==="music") exports.music = {};
23-
} else { // add/modify
20+
if (event.t!=="remove") { // add/modify
2421
if (event.t==="add") {
2522
if (event.new===undefined) event.new = true; // Assume it should be new
2623
} else if (event.t==="modify") {
@@ -29,10 +26,8 @@ exports.pushMessage = function(event) {
2926
}
3027

3128
// combine musicinfo and musicstate events
32-
if (event.id==="music") {
33-
if (event.state==="play") event.new = true; // new track, or playback (re)started
34-
event = Object.assign(exports.music, event);
35-
}
29+
if (event.id==="music" && event.state==="play")
30+
event.new = true; // new track, or playback (re)started
3631
}
3732
// reset state (just in case)
3833
delete event.handled;
@@ -153,7 +148,9 @@ exports.clearAll = function() {
153148
}
154149

155150
/**
156-
* Get saved messages
151+
* Get saved messages. If Bangle.MESSAGES was set by messagegui (messages
152+
* that aren't yet written) apply them to the messages we read so we get
153+
* up to date info.
157154
*
158155
* Optionally pass in a message to apply to the list, this is for event handlers:
159156
* By passing the message from the event, you can make sure the list is up-to-date,
@@ -170,6 +167,7 @@ exports.clearAll = function() {
170167
exports.getMessages = function(withMessage) {
171168
let messages = require("Storage").readJSON("messages.json", true);
172169
messages = Array.isArray(messages) ? messages : []; // make sure we always return an array
170+
(Bangle.MESSAGES || []).forEach(m => require("messages").apply(m, messages)); // apply any usaved messages
173171
if (withMessage && withMessage.id) exports.apply(withMessage, messages);
174172
return messages;
175173
};

apps/messages/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "messages",
33
"name": "Messages",
4-
"version": "0.68",
4+
"version": "0.69",
55
"description": "Library to handle, load and store message events received from Android/iOS",
66
"icon": "app.png",
77
"type": "module",

0 commit comments

Comments
 (0)