diff --git a/apps/messages/ChangeLog b/apps/messages/ChangeLog index 7f7f8d3d3..421bff3e4 100644 --- a/apps/messages/ChangeLog +++ b/apps/messages/ChangeLog @@ -63,3 +63,4 @@ 0.47: Add new Icons (Nextbike, Mattermost, etc.) 0.48: When getting new message from the clock, only buzz once the messages app is loaded 0.49: Change messages icon (to fit within 24px) and ensure widget renders icons centrally +0.50: Add `getMessages` and `status` functions to library \ No newline at end of file diff --git a/apps/messages/app.js b/apps/messages/app.js index 0956b1573..40dff9635 100644 --- a/apps/messages/app.js +++ b/apps/messages/app.js @@ -48,7 +48,7 @@ we should start a timeout for settings.unreadTimeout to return to the clock. */ var unreadTimeout; /// List of all our messages -var MESSAGES = require("Storage").readJSON("messages.json",1)||[]; +var MESSAGES = require("messages").getMessages(); if (!Array.isArray(MESSAGES)) MESSAGES=[]; var onMessagesModified = function(msg) { // TODO: if new, show this new one diff --git a/apps/messages/lib.js b/apps/messages/lib.js index b51180d81..ed14a3247 100644 --- a/apps/messages/lib.js +++ b/apps/messages/lib.js @@ -102,6 +102,32 @@ exports.clearAll = function(event) { WIDGETS.messages.update(messages); } +/** + * @returns {array} All messages + */ +exports.getMessages = function() { + if ("undefined"!=typeof MESSAGES) return MESSAGES; // loaded/managed by app + return require("Storage").readJSON("messages.json",1)||[]; +} + +/** + * Check if there are any messages + * @returns {string} "new"/"old"/"none" + */ + exports.status = function() { + try { + let status= "none"; + for(const m of exports.getMessages()) { + if (["music", "map"].includes(m.id)) continue; + if (m.new) return "new"; + status = "old"; + } + return status; + } catch(e) { + return "none"; // don't bother e.g. the widget with errors + } +}; + exports.getMessageImage = function(msg) { /* * icons should be 24x24px or less with 1bpp colors and 'Transparency to Color' diff --git a/apps/messages/metadata.json b/apps/messages/metadata.json index 408901bfc..da2e0945a 100644 --- a/apps/messages/metadata.json +++ b/apps/messages/metadata.json @@ -1,7 +1,7 @@ { "id": "messages", "name": "Messages", - "version": "0.49", + "version": "0.50", "description": "App to display notifications from iOS and Gadgetbridge/Android", "icon": "app.png", "type": "app", diff --git a/apps/messages/widget.js b/apps/messages/widget.js index 835626f5d..9eaf15eba 100644 --- a/apps/messages/widget.js +++ b/apps/messages/widget.js @@ -1,10 +1,5 @@ (() => { -function getMessages() { - if ("undefined"!=typeof MESSAGES) return MESSAGES; - return require("Storage").readJSON("messages.json",1)||[]; -} - function filterMessages(msgs) { return msgs.filter(msg => msg.new && msg.id != "music") .map(m => m.src) // we only need this for icon/color @@ -82,6 +77,5 @@ WIDGETS["messages"]={area:"tl", width:0, draw:function(recall) { message but then the watch was never viewed. In that case we don't want to buzz but should still show that there are unread messages. */ if (global.MESSAGES===undefined) - WIDGETS["messages"].update(getMessages(), true); - + WIDGETS["messages"].update(require("messages").getMessages(), true); })();