make messagegui handle new messages (adds to Bangle.MESSAGES, handles removals, saves to flash) even when in app.

master
Bryan 2024-08-03 00:57:30 -06:00
parent 4976ce0df3
commit 246360efc1
2 changed files with 30 additions and 35 deletions

View File

@ -115,27 +115,23 @@ events.saveMessages = function() {
E.on("kill", events.saveMessages); E.on("kill", events.saveMessages);
const newMessage = (type, msg) => { const newMessage = (type, msg) => {
// console.log("new message"); console.log("new message");
console.log(type, msg);
if (type === "text" && active === "message") { if (type === "text" && active === "message") {
require("messages").apply(msg, Bangle.MESSAGES);
if (idle) showText(Bangle.MESSAGES); // we are idle so show new messages right away if (idle) showText(Bangle.MESSAGES); // we are idle so show new messages right away
else haveNewMessage = true; // otherwise set new message flag and wait until idle else haveNewMessage = true; // otherwise set new message flag and wait until idle
} }
// TODO deal with other types of messages // TODO deal with other types of messages
}; };
setListener("message", newMessage); setListener("breakingnews", newMessage);
// Check for new messages, wait until there is no interaction for `idleTime` ms // Check for new messages, wait until there is no interaction for `idleTime` ms
const checkForNewMessages = function(idleTime) { const checkForNewMessages = function(idleTime) {
// console.log("checkForNewMessages");
idleTime = idleTime ?? 3000; // how much time without interaction before we are considered idle idleTime = idleTime ?? 3000; // how much time without interaction before we are considered idle
// console.log("idleTime = ", idleTime);
idle = false; idle = false;
if (timeouts.idleTimer) clearTimeout(timeouts.idleTimer); if (timeouts.idleTimer) clearTimeout(timeouts.idleTimer);
timeouts.idleTimer = setTimeout(() => { timeouts.idleTimer = setTimeout(() => {
// console.log("update messages");
if (haveNewMessage) { if (haveNewMessage) {
// console.log("call showText()");
haveNewMessage = false; haveNewMessage = false;
showText(Bangle.MESSAGES); showText(Bangle.MESSAGES);
} }
@ -226,8 +222,10 @@ const showText = function(showAll) {
const removeMessage = function() { const removeMessage = function() {
const removeAndReset = () => { const removeAndReset = () => {
let id = msgBoxes[messageNum].msg.id; const id = msgBoxes[messageNum].msg.id;
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id !== id); // remove from Bangle.MESSAGES const idx = Bangle.findIndex(m => m.id === id);
Bangle.MESSAGES.splice(idx, 1); // remove from Bangle.MESSAGES
//Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id !== id); // remove from Bangle.MESSAGES
msgBoxes.splice(messageNum, 1); // remove from msgBoxes msgBoxes.splice(messageNum, 1); // remove from msgBoxes
if (!msgBoxes.length) { if (!msgBoxes.length) {
Bangle.setUI(); Bangle.setUI();
@ -390,9 +388,8 @@ const showText = function(showAll) {
const handler = function(e) { const handler = function(e) {
if (e.b === 0) { if (e.b === 0) {
firstTouch = true; firstTouch = true;
checkForNewMessages();
} }
// console.log("top of handler: first touch = ", firstTouch);
// // console.log("top of handler: e = ", e);
if (switching) return; // don't respond to touch while we are animating if (switching) return; // don't respond to touch while we are animating
if (firstTouch && e.b === 1) { if (firstTouch && e.b === 1) {
if (buzzing) { if (buzzing) {
@ -403,12 +400,8 @@ const showText = function(showAll) {
let idx = Bangle.MESSAGES.findIndex(m => m.id === msgBoxes[messageNum].msg.id); // TODO maybe we don't to do this check every time let idx = Bangle.MESSAGES.findIndex(m => m.id === msgBoxes[messageNum].msg.id); // TODO maybe we don't to do this check every time
if (idx >= 0) delete Bangle.MESSAGES[idx].new; if (idx >= 0) delete Bangle.MESSAGES[idx].new;
msgBoxes[messageNum].clearNew(); msgBoxes[messageNum].clearNew();
// console.log("do tests");
// console.log("dy, dx", e.dy, e.dx);
// console.log("e", e);
if (Math.abs(e.dy) > Math.abs(e.dx)) { if (Math.abs(e.dy) > Math.abs(e.dx)) {
firstTouch = false; firstTouch = false;
// console.log("up down");
if (e.dy > 0 && msgBoxes[messageNum].top) { if (e.dy > 0 && msgBoxes[messageNum].top) {
mode = "prev"; mode = "prev";
} else if (msgBoxes[messageNum].bottom) { // e.dy will be < 0 here } else if (msgBoxes[messageNum].bottom) { // e.dy will be < 0 here
@ -418,7 +411,6 @@ const showText = function(showAll) {
} }
} else if (Math.abs(e.dx) > Math.abs(e.dy)) { } else if (Math.abs(e.dx) > Math.abs(e.dy)) {
firstTouch = false; firstTouch = false;
// console.log("left right");
if (e.dx < 0) { if (e.dx < 0) {
mode = "left"; mode = "left";
} else { } else {
@ -437,12 +429,7 @@ const showText = function(showAll) {
}; };
const nextHandler = function(e) { const nextHandler = function(e) {
// console.log("nextHandler");
// console.log(e);
if (e.b == 0) { if (e.b == 0) {
// console.log("firstTouch = ", firstTouch);
checkForNewMessages();
// firstTouch = true;
} }
if (messageNum == msgBoxes.length - 1) return; // already on last message if (messageNum == msgBoxes.length - 1) return; // already on last message
switching = true; switching = true;
@ -453,8 +440,6 @@ const showText = function(showAll) {
const prevHandler = function(e) { const prevHandler = function(e) {
if (e.b == 0) { if (e.b == 0) {
checkForNewMessages();
// firstTouch = true;
} }
if (messageNum == 0) return; // already on first message if (messageNum == 0) return; // already on first message
switching = true; switching = true;
@ -466,7 +451,6 @@ const showText = function(showAll) {
const leftHandler = function(e) { const leftHandler = function(e) {
const msgBox = msgBoxes[messageNum]; const msgBox = msgBoxes[messageNum];
if (e.b == 0) { if (e.b == 0) {
// firstTouch = true;
if (msgBox.xOffset > -swipeThreshold) { if (msgBox.xOffset > -swipeThreshold) {
msgBox.xOffset = 0; msgBox.xOffset = 0;
msgBox.draw(); msgBox.draw();
@ -478,7 +462,6 @@ const showText = function(showAll) {
dismissOnPhone(); dismissOnPhone();
removeMessage(); removeMessage();
switching = false; switching = false;
checkForNewMessages();
}); });
} }
return; return;
@ -493,7 +476,6 @@ const showText = function(showAll) {
const rightHandler = function(e) { const rightHandler = function(e) {
const msgBox = msgBoxes[messageNum]; const msgBox = msgBoxes[messageNum];
if (e.b == 0) { if (e.b == 0) {
// firstTouch = true;
if (msgBox.xOffset < swipeThreshold) { if (msgBox.xOffset < swipeThreshold) {
msgBox.xOffset = 0; msgBox.xOffset = 0;
msgBox.draw(); msgBox.draw();
@ -504,7 +486,6 @@ const showText = function(showAll) {
g.setBgColor(g.theme.bg); g.setBgColor(g.theme.bg);
removeMessage(); removeMessage();
switching = false; switching = false;
checkForNewMessages();
}); });
} }
return; return;
@ -518,8 +499,6 @@ const showText = function(showAll) {
const scrollHandler = function(e) { const scrollHandler = function(e) {
if (e.b === 0) { if (e.b === 0) {
// firstTouch = true;
checkForNewMessages();
} }
msgBoxes[messageNum].scroll(e.dy); msgBoxes[messageNum].scroll(e.dy);
}; };

View File

@ -1,14 +1,30 @@
const loadWillReset = () => Bangle.load === load || !Bangle.uiRemove; const loadWillReset = () => Bangle.load === load || !Bangle.uiRemove;
exports.messageListener = function(type, msg) { exports.messageListener = function(type, msg) {
if (msg.handled || global.__FILE__ === "messagecenter.app.js") return; // already handled or in the app let inApp = false;
if (msg.handled) return;
if (global.__FILE__ === "messagecenter.app.js") inApp = true;
if (!Bangle.MESSAGES || !Bangle.MESSAGES.length) Bangle.MESSAGES = require("messages").getMessages(msg);
if (type === "text") { if (type === "text") {
msg.show = true; if (msg.t === "add") {
msg.new = true; msg.show = true;
if (!Bangle.MESSAGES || !Bangle.MESSAGES.length) Bangle.MESSAGES = require("messages").getMessages(msg); msg.new = true;
require("messages").apply(msg, Bangle.MESSAGES); require("messages").apply(msg, Bangle.MESSAGES);
} else if (msg.t === "remove") {
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id != msg.id);
require("messages").write(Bangle.MESSAGES); // write removal to flash
if (!inApp) return; // don't open app for removal
} else if (msg.t === "modify") {
msg.show = true;
msg.new = true;
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id != msg.id); // remove old version of message
Bangle.MESSAGES.unshift(msg); // put modified message at top of list
}
}
if (!inApp) {
if (loadWillReset()) require("messages").write(Bangle.MESSAGES); if (loadWillReset()) require("messages").write(Bangle.MESSAGES);
Bangle.notify = true;
Bangle.load("messagecenter.notify.js"); Bangle.load("messagecenter.notify.js");
} else {
Bangle.emit("breakingnews", type, msg);
} }
} }