Skip to content

Commit

Permalink
Merge pull request #2980 from rigrig/messagelist-setui-better-fix
Browse files Browse the repository at this point in the history
Messagelist setUI proper fix
  • Loading branch information
gfwilliams committed Aug 29, 2023
2 parents 4adb594 + 4473acd commit 799428a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
4 changes: 3 additions & 1 deletion apps/messagelist/ChangeLog
Original file line number Diff line number Diff line change
@@ -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
0.03: Fix invalid use of Bangle.setUI
0.04: Fix app crashing when new message arrives

49 changes: 42 additions & 7 deletions apps/messagelist/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
RIGHT = 1, LEFT = -1, // swipe directions
UP = -1, DOWN = 1; // updown directions
const Layout = require("Layout");
const debug = function() {
if (global.DEBUG_MESSAGELIST) console.log.apply(console, ['messagelist:'].concat(arguments));
}

const settings = () => require("messagegui").settings();
const fontTiny = "6x8"; // fixed size, don't use this for important things
Expand All @@ -45,6 +48,7 @@
/// List of all our messages
let MESSAGES;
const saveMessages = function() {
debug('saveMessages()');
const noSave = ["alarm", "call", "music"]; // assume these are outdated once we close the app
noSave.forEach(id => remove({id: id}));
require("messages").write(MESSAGES
Expand All @@ -56,6 +60,7 @@
);
};
const uiRemove = function() {
debug('uiRemove()');
if (musicTimeout) clearTimeout(musicTimeout);
layout = undefined;
Bangle.removeListener("message", onMessage);
Expand Down Expand Up @@ -85,11 +90,25 @@
}

const setUI = function(options, cb) {
debug('setUI(', options, cb?'<callback>':cb)
delete Bangle.uiRemove; // don't clear out things when switching UI within the app
options = Object.assign({mode:"custom", remove: () => uiRemove()}, options);
// 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;
Expand All @@ -111,6 +130,7 @@
};

const onMessage = function(type, msg) {
debug(`onMessage(${type}`, msg);
if (msg.handled) return;
msg.handled = true;
switch(type) {
Expand All @@ -135,6 +155,7 @@
Bangle.on("message", onMessage);

const onCall = function(msg) {
debug('onCall(', msg);
if (msg.t==="remove") {
call = undefined;
return exitScreen("call");
Expand All @@ -145,6 +166,7 @@
showCall();
};
const onAlarm = function(msg) {
debug('onAlarm(', msg);
if (msg.t==="remove") {
alarm = undefined;
return exitScreen("alarm");
Expand All @@ -155,6 +177,7 @@
};
let musicTimeout;
const onMusic = function(msg) {
debug('onMusic(', msg);
const hadMusic = !!music;
if (musicTimeout) clearTimeout(musicTimeout);
musicTimeout = undefined;
Expand Down Expand Up @@ -184,6 +207,7 @@
}
};
const onMap = function(msg) {
debug('onMap(', msg);
const hadMap = !!map;
if (msg.t==="remove") {
map = undefined;
Expand All @@ -196,6 +220,7 @@
else if (active==="main" && !hadMap) showMain(); // refresh menu: add "Map" entry
};
const onText = function(msg) {
debug('onText(', msg);
require("messages").apply(msg, MESSAGES);
const mIdx = MESSAGES.findIndex(m => m.id===msg.id);
if (!MESSAGES[mIdx]) if (back==="messages") back = undefined;
Expand Down Expand Up @@ -237,6 +262,7 @@
};

const showMap = function() {
debug('showMap()');
setActive("map");
delete map.new;
let m, distance, street, target, eta;
Expand All @@ -254,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},
{
Expand Down Expand Up @@ -319,6 +345,7 @@
else Bangle.musicControl(action);
};
const showMusic = function() {
debug('showMusic()', music);
if (active!==music) setActive("music");
if (!music) music = {track: "<unknown>", artist: "<unknown>", album: "", state: "pause"};
delete music.new;
Expand Down Expand Up @@ -355,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: [
Expand Down Expand Up @@ -442,12 +469,14 @@
let layout;

const clearStuff = function() {
debug('clearStuff()');
delete Bangle.appRect;
layout = undefined;
setUI();
g.reset().clearRect(Bangle.appRect);
};
const setActive = function(screen, args) {
debug(`setActive(${screen}`, args);
clearStuff();
if (active && screen!==active) back = active;
if (screen==="messages") messageNum = args;
Expand Down Expand Up @@ -476,6 +505,7 @@
}
};
const showMain = function() {
debug('showMain()');
setActive("main");
let grid = {"": {title:/*LANG*/"Messages", align: 0, back: load}};
if (call) grid[/*LANG*/"Incoming Call"] = {icon: "Phone", cb: showCall};
Expand Down Expand Up @@ -596,7 +626,7 @@
}
l.c.push(row);
}
layout = new Layout(l, {back: back});
layout = makeLayout(l, {back: back});
layout.render();

if (B2) {
Expand Down Expand Up @@ -640,13 +670,15 @@
};

const showSettings = function() {
debug('showSettings()');
setActive("settings");
eval(require("Storage").read("messagelist.settings.js"))(() => {
setFont();
showMain();
});
};
const showCall = function() {
debug('showCall()');
setActive("call");
delete call.new;
Bangle.setLocked(false);
Expand Down Expand Up @@ -678,7 +710,7 @@
];
}

layout = new Layout({
layout = makeLayout({
type: "v", c: [
{
type: "h", fillx: 1, bgCol: g.theme.bg2, col: g.theme.fg2, c: [
Expand Down Expand Up @@ -722,6 +754,7 @@
});
};
const showAlarm = function() {
debug('showAlarm()');
// dismissing alarms doesn't seem to work, so this is simple */
setActive("alarm");
delete alarm.new;
Expand All @@ -731,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: [
Expand Down Expand Up @@ -830,6 +863,7 @@
);
};
const showMessage = function(num, bottom) {
debug(`showMessage(${num}, ${!!bottom})`);
if (num<0) num = 0;
if (!num) num = 0; // no number: show first
if (num>=MESSAGES.length) num = MESSAGES.length-1;
Expand Down Expand Up @@ -1093,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: [
Expand Down Expand Up @@ -1133,6 +1167,7 @@
* Stop auto-unload timeout and buzzing, remove listeners for this function
*/
const clearUnreadStuff = function() {
debug('clearUnreadStuff()');
require("messages").stopBuzz();
if (unreadTimeout) clearTimeout(unreadTimeout);
unreadTimeout = undefined;
Expand Down Expand Up @@ -1208,4 +1243,4 @@
// stop buzzing, auto-close timeout on input
["touch", "drag", "swipe"].forEach(l => Bangle.on(l, clearUnreadStuff));
(B2 ? [BTN1] : [BTN1, BTN2, BTN3]).forEach(b => watches.push(setWatch(clearUnreadStuff, b, false)));
}
}
2 changes: 1 addition & 1 deletion apps/messagelist/metadata.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 799428a

Please sign in to comment.