messages: also emit "clearAll" message events

master
Richard de Boer 2022-09-24 17:08:58 +02:00
parent 29fc8f4d1a
commit 6f27a65143
No known key found for this signature in database
2 changed files with 15 additions and 13 deletions

View File

@ -64,8 +64,10 @@ it like this:
```js ```js
myMessageListener = Bangle.on("message", (type, message)=>{ myMessageListener = Bangle.on("message", (type, message)=>{
if (message.handled) return; // another app already handled this message if (message.handled) return; // another app already handled this message
// <type> is one of "text", "call", "alarm", "map", or "music" // <type> is one of "text", "call", "alarm", "map", "music", or "clearAll"
if (type === "clearAll") return; // not a message
// see `messages/lib.js` for possible <message> formats // see `messages/lib.js` for possible <message> formats
// message.t could be "add", "modify" or "remove"
E.showMessage(`${message.title}\n${message.body}`, `${message.t} ${type} message`); E.showMessage(`${message.title}\n${message.body}`, `${message.t} ${type} message`);
// You can prevent the default `message` app from loading by setting `message.handled = true`: // You can prevent the default `message` app from loading by setting `message.handled = true`:
message.handled = true; message.handled = true;

View File

@ -92,20 +92,20 @@ exports.pushMessage = function(event) {
},0); },0);
} }
/// Remove all messages /// Remove all messages
exports.clearAll = function(event) { exports.clearAll = function() {
var messages, inApp = "undefined"!=typeof MESSAGES; if ("undefined"!= typeof MESSAGES) { // we're in a messages app, clear that as well
if (inApp) {
MESSAGES = []; MESSAGES = [];
messages = MESSAGES; // we're in an app that has already loaded messages }
} else // no app - empty messages // Clear all messages
messages = []; require("Storage").writeJSON("messages.json", []);
// Save all messages
require("Storage").writeJSON("messages.json",messages);
// update app if in app
if (inApp) return onMessagesModified();
// if we have a widget, update it // if we have a widget, update it
if (global.WIDGETS && WIDGETS.messages) if (global.WIDGETS && WIDGETS.messages)
WIDGETS.messages.update(messages); WIDGETS.messages.update([]);
// let message listeners know
Bangle.emit("message", "clearAll", {}); // guarantee listeners an object as `message`
// clearAll cannot be marked as "handled"
// update app if in app
if ("function"== typeof onMessagesModified) onMessagesModified();
} }
/** /**