messages: Add `getMessages` and `status` functions to library

Apps/widgets should use the library instead of opening "messages.json"
For #2081
pull/2082/head
Richard de Boer 2022-08-09 15:48:04 +02:00
parent f3f8313442
commit 143bbf858d
No known key found for this signature in database
5 changed files with 30 additions and 9 deletions

View File

@ -63,3 +63,4 @@
0.47: Add new Icons (Nextbike, Mattermost, etc.) 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.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.49: Change messages icon (to fit within 24px) and ensure widget renders icons centrally
0.50: Add `getMessages` and `status` functions to library

View File

@ -48,7 +48,7 @@ we should start a timeout for settings.unreadTimeout to return
to the clock. */ to the clock. */
var unreadTimeout; var unreadTimeout;
/// List of all our messages /// List of all our messages
var MESSAGES = require("Storage").readJSON("messages.json",1)||[]; var MESSAGES = require("messages").getMessages();
if (!Array.isArray(MESSAGES)) MESSAGES=[]; if (!Array.isArray(MESSAGES)) MESSAGES=[];
var onMessagesModified = function(msg) { var onMessagesModified = function(msg) {
// TODO: if new, show this new one // TODO: if new, show this new one

View File

@ -102,6 +102,32 @@ exports.clearAll = function(event) {
WIDGETS.messages.update(messages); 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) { exports.getMessageImage = function(msg) {
/* /*
* icons should be 24x24px or less with 1bpp colors and 'Transparency to Color' * icons should be 24x24px or less with 1bpp colors and 'Transparency to Color'

View File

@ -1,7 +1,7 @@
{ {
"id": "messages", "id": "messages",
"name": "Messages", "name": "Messages",
"version": "0.49", "version": "0.50",
"description": "App to display notifications from iOS and Gadgetbridge/Android", "description": "App to display notifications from iOS and Gadgetbridge/Android",
"icon": "app.png", "icon": "app.png",
"type": "app", "type": "app",

View File

@ -1,10 +1,5 @@
(() => { (() => {
function getMessages() {
if ("undefined"!=typeof MESSAGES) return MESSAGES;
return require("Storage").readJSON("messages.json",1)||[];
}
function filterMessages(msgs) { function filterMessages(msgs) {
return msgs.filter(msg => msg.new && msg.id != "music") return msgs.filter(msg => msg.new && msg.id != "music")
.map(m => m.src) // we only need this for icon/color .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 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. */ want to buzz but should still show that there are unread messages. */
if (global.MESSAGES===undefined) if (global.MESSAGES===undefined)
WIDGETS["messages"].update(getMessages(), true); WIDGETS["messages"].update(require("messages").getMessages(), true);
})(); })();