From 4473acdbdeb962f1d1947ef9ca003e20cb865a10 Mon Sep 17 00:00:00 2001 From: Richard de Boer Date: Fri, 18 Aug 2023 21:14:43 +0200 Subject: [PATCH] messagelist: Fix app crashing when new message arrives new Layout(...) calls Bangle.setUI(), causing uiRemove() to be called whenever a new messages arrives, instead of only when the app exits. This fixes that by unsetting Bangle.uiRemove before calling Layout, and restoring it afterwards. --- apps/messagelist/ChangeLog | 4 +++- apps/messagelist/app.js | 25 +++++++++++++++++++------ apps/messagelist/metadata.json | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/apps/messagelist/ChangeLog b/apps/messagelist/ChangeLog index 37854d8ae..899e29cb6 100644 --- a/apps/messagelist/ChangeLog +++ b/apps/messagelist/ChangeLog @@ -1,3 +1,5 @@ 0.01: New app! 0.02: Fix music updates while app is already open -0.03: Fix invalid use of Bangle.setUI \ No newline at end of file +0.03: Fix invalid use of Bangle.setUI +0.04: Fix app crashing when new message arrives + diff --git a/apps/messagelist/app.js b/apps/messagelist/app.js index 207c70412..3c140a0c4 100644 --- a/apps/messagelist/app.js +++ b/apps/messagelist/app.js @@ -96,6 +96,19 @@ // If options={} assume we still want `remove` to be called when leaving via fast load (so we must have 'mode:custom') Bangle.setUI(options, cb); }; + /** + * Same as calling `new Layout(layout, options)`, except Bangle.uiRemove is not called + * @param {object} layout + * @param {object} options + * @returns {Layout} + */ + const makeLayout = function(layout, options) { + const remove = Bangle.uiRemove; + delete Bangle.uiRemove; // don't clear out things when setting up new Layout + const result = new Layout(layout, options); + if (remove) Bangle.uiRemove = remove; + return result; + } const remove = function(msg) { if (msg.id==="call") call = undefined; @@ -267,7 +280,7 @@ } else { target = map.body; } - let layout = new Layout({ + let layout = makeLayout({ type: "v", c: [ {type: "txt", font: fontNormal, label: target, bgCol: g.theme.bg2, col: g.theme.fg2, fillx: 1, pad: 2}, { @@ -369,7 +382,7 @@ else if (dur) info = dur; else info = {}; - layout = new Layout({ + layout = makeLayout({ type: "v", c: [ { type: "h", fillx: 1, bgCol: g.theme.bg2, col: g.theme.fg2, c: [ @@ -613,7 +626,7 @@ } l.c.push(row); } - layout = new Layout(l, {back: back}); + layout = makeLayout(l, {back: back}); layout.render(); if (B2) { @@ -697,7 +710,7 @@ ]; } - layout = new Layout({ + layout = makeLayout({ type: "v", c: [ { type: "h", fillx: 1, bgCol: g.theme.bg2, col: g.theme.fg2, c: [ @@ -751,7 +764,7 @@ const w = g.getWidth()-48, lines = g.setFont(fontNormal).wrapString(alarm.title, w), title = (lines.length>2) ? lines.slice(0, 2).join("\n")+"..." : lines.join("\n"); - layout = new Layout({ + layout = makeLayout({ type: "v", c: [ { type: "h", fillx: 1, bgCol: g.theme.bg2, col: g.theme.fg2, c: [ @@ -1114,7 +1127,7 @@ let imageCol = getImageColor(msg); if (g.setColor(imageCol).getColor()==hBg) imageCol = hCol; - layout = new Layout({ + layout = makeLayout({ type: "v", c: [ { type: "h", fillx: 1, bgCol: hBg, col: hCol, c: [ diff --git a/apps/messagelist/metadata.json b/apps/messagelist/metadata.json index 37fed5795..2f1abe11d 100644 --- a/apps/messagelist/metadata.json +++ b/apps/messagelist/metadata.json @@ -1,7 +1,7 @@ { "id": "messagelist", "name": "Message List", - "version": "0.03", + "version": "0.04", "description": "Display notifications from iOS and Gadgetbridge/Android as a list", "icon": "app.png", "type": "app",