make messagegui handle new messages (adds to Bangle.MESSAGES, handles removals, saves to flash) even when in app.
parent
4976ce0df3
commit
246360efc1
|
|
@ -115,27 +115,23 @@ events.saveMessages = function() {
|
|||
E.on("kill", events.saveMessages);
|
||||
|
||||
const newMessage = (type, msg) => {
|
||||
// console.log("new message");
|
||||
console.log("new message");
|
||||
console.log(type, msg);
|
||||
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
|
||||
else haveNewMessage = true; // otherwise set new message flag and wait until idle
|
||||
}
|
||||
// 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
|
||||
const checkForNewMessages = function(idleTime) {
|
||||
// console.log("checkForNewMessages");
|
||||
idleTime = idleTime ?? 3000; // how much time without interaction before we are considered idle
|
||||
// console.log("idleTime = ", idleTime);
|
||||
idle = false;
|
||||
if (timeouts.idleTimer) clearTimeout(timeouts.idleTimer);
|
||||
timeouts.idleTimer = setTimeout(() => {
|
||||
// console.log("update messages");
|
||||
if (haveNewMessage) {
|
||||
// console.log("call showText()");
|
||||
haveNewMessage = false;
|
||||
showText(Bangle.MESSAGES);
|
||||
}
|
||||
|
|
@ -226,8 +222,10 @@ const showText = function(showAll) {
|
|||
|
||||
const removeMessage = function() {
|
||||
const removeAndReset = () => {
|
||||
let id = msgBoxes[messageNum].msg.id;
|
||||
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id !== id); // remove from Bangle.MESSAGES
|
||||
const id = msgBoxes[messageNum].msg.id;
|
||||
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
|
||||
if (!msgBoxes.length) {
|
||||
Bangle.setUI();
|
||||
|
|
@ -390,9 +388,8 @@ const showText = function(showAll) {
|
|||
const handler = function(e) {
|
||||
if (e.b === 0) {
|
||||
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 (firstTouch && e.b === 1) {
|
||||
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
|
||||
if (idx >= 0) delete Bangle.MESSAGES[idx].new;
|
||||
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)) {
|
||||
firstTouch = false;
|
||||
// console.log("up down");
|
||||
if (e.dy > 0 && msgBoxes[messageNum].top) {
|
||||
mode = "prev";
|
||||
} 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)) {
|
||||
firstTouch = false;
|
||||
// console.log("left right");
|
||||
if (e.dx < 0) {
|
||||
mode = "left";
|
||||
} else {
|
||||
|
|
@ -437,12 +429,7 @@ const showText = function(showAll) {
|
|||
};
|
||||
|
||||
const nextHandler = function(e) {
|
||||
// console.log("nextHandler");
|
||||
// console.log(e);
|
||||
if (e.b == 0) {
|
||||
// console.log("firstTouch = ", firstTouch);
|
||||
checkForNewMessages();
|
||||
// firstTouch = true;
|
||||
}
|
||||
if (messageNum == msgBoxes.length - 1) return; // already on last message
|
||||
switching = true;
|
||||
|
|
@ -453,8 +440,6 @@ const showText = function(showAll) {
|
|||
|
||||
const prevHandler = function(e) {
|
||||
if (e.b == 0) {
|
||||
checkForNewMessages();
|
||||
// firstTouch = true;
|
||||
}
|
||||
if (messageNum == 0) return; // already on first message
|
||||
switching = true;
|
||||
|
|
@ -466,7 +451,6 @@ const showText = function(showAll) {
|
|||
const leftHandler = function(e) {
|
||||
const msgBox = msgBoxes[messageNum];
|
||||
if (e.b == 0) {
|
||||
// firstTouch = true;
|
||||
if (msgBox.xOffset > -swipeThreshold) {
|
||||
msgBox.xOffset = 0;
|
||||
msgBox.draw();
|
||||
|
|
@ -478,7 +462,6 @@ const showText = function(showAll) {
|
|||
dismissOnPhone();
|
||||
removeMessage();
|
||||
switching = false;
|
||||
checkForNewMessages();
|
||||
});
|
||||
}
|
||||
return;
|
||||
|
|
@ -493,7 +476,6 @@ const showText = function(showAll) {
|
|||
const rightHandler = function(e) {
|
||||
const msgBox = msgBoxes[messageNum];
|
||||
if (e.b == 0) {
|
||||
// firstTouch = true;
|
||||
if (msgBox.xOffset < swipeThreshold) {
|
||||
msgBox.xOffset = 0;
|
||||
msgBox.draw();
|
||||
|
|
@ -504,7 +486,6 @@ const showText = function(showAll) {
|
|||
g.setBgColor(g.theme.bg);
|
||||
removeMessage();
|
||||
switching = false;
|
||||
checkForNewMessages();
|
||||
});
|
||||
}
|
||||
return;
|
||||
|
|
@ -518,8 +499,6 @@ const showText = function(showAll) {
|
|||
|
||||
const scrollHandler = function(e) {
|
||||
if (e.b === 0) {
|
||||
// firstTouch = true;
|
||||
checkForNewMessages();
|
||||
}
|
||||
msgBoxes[messageNum].scroll(e.dy);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,14 +1,30 @@
|
|||
const loadWillReset = () => Bangle.load === load || !Bangle.uiRemove;
|
||||
|
||||
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") {
|
||||
msg.show = true;
|
||||
msg.new = true;
|
||||
if (!Bangle.MESSAGES || !Bangle.MESSAGES.length) Bangle.MESSAGES = require("messages").getMessages(msg);
|
||||
require("messages").apply(msg, Bangle.MESSAGES);
|
||||
if (msg.t === "add") {
|
||||
msg.show = true;
|
||||
msg.new = true;
|
||||
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);
|
||||
Bangle.notify = true;
|
||||
Bangle.load("messagecenter.notify.js");
|
||||
} else {
|
||||
Bangle.emit("breakingnews", type, msg);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue