Skip to content

Commit 192c5cf

Browse files
author
thyttan
committed
messagegui: refactoring and fix
1 parent aae386a commit 192c5cf

File tree

1 file changed

+41
-50
lines changed

1 file changed

+41
-50
lines changed

apps/messagegui/app.js

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ var onMessagesModified = function(type,msg) {
9090
}
9191
if (msg && msg.id=="nav" && msg.t=="modify" && active!="map")
9292
return; // don't show an updated nav message if we're just in the menu
93-
showMessage(msg&&msg.id, false);
93+
//showMessage(msg&&msg.id, false);
94+
showMessagesScroller(msg, false)
9495
};
9596
Bangle.on("message", onMessagesModified);
9697

@@ -248,44 +249,36 @@ function showMusicMessage(msg) {
248249

249250
function showMessagesScroller(msg, persist, alreadyProcessed) {
250251
if (persist===undefined) {persist = true;}
252+
const MSG_IDX = msg ? MESSAGES.findIndex((m)=>m.id==msg.id) : 0;
253+
const INITIATED_FROM =
254+
alreadyProcessed===undefined ? "other function" :
255+
(MSG_IDX<=alreadyProcessed.startIdx ? "scrolling up" :
256+
(MSG_IDX >= alreadyProcessed.stopIdx-1 ? "scrolling down" : undefined));
251257

252258
const WU = require("widget_utils");
253259
WU.hide();
254260

255261
if (replying) { return; }
256-
if (persist) {
257-
cancelReloadTimeout();
258-
} else {
259-
resetReloadTimeout();
260-
}
261-
262-
let msgIdx = MESSAGES.findIndex((m) => m.id == msg.id)
262+
if (persist) {cancelReloadTimeout();} else {resetReloadTimeout();}
263263

264264
let updatedProcessed = {};
265265
let startIdx = 0;
266266
let stopIdx = 0;
267-
let initiatedFrom = alreadyProcessed===undefined ?
268-
"other function" :
269-
(msgIdx<=alreadyProcessed.startIdx ?
270-
"scrolling up" :
271-
(msgIdx >= alreadyProcessed.stopIdx-1 ?
272-
"scrolling down" :
273-
undefined
274-
)
275-
)
276-
277-
if (initiatedFrom === "other function") {
278-
startIdx = Math.max(msgIdx-1, 0);
279-
stopIdx = Math.min(msgIdx+2, MESSAGES.length);
280-
Object.assign(updatedProcessed, {startIdx:startIdx, stopIdx:stopIdx})
281-
} else if (initiatedFrom === "scrolling up") {
282-
startIdx = Math.max(msgIdx, 0);
267+
268+
if (INITIATED_FROM === "other function") {
269+
startIdx = Math.max(MSG_IDX-1, 0);
270+
stopIdx = Math.min(MSG_IDX+2, MESSAGES.length);
271+
Object.assign(updatedProcessed, {startIdx:startIdx, stopIdx:stopIdx});
272+
} else if (INITIATED_FROM === "scrolling up") {
273+
startIdx = MSG_IDX;
283274
stopIdx = alreadyProcessed.startIdx;
284-
Object.assign(updatedProcessed, {startIdx:startIdx, stopIdx:alreadyProcessed.stopIdx})
285-
} else if (initiatedFrom === "scrolling down") {
275+
Object.assign(updatedProcessed,
276+
{startIdx:startIdx, stopIdx:alreadyProcessed.stopIdx});
277+
} else if (INITIATED_FROM === "scrolling down") {
286278
startIdx = alreadyProcessed.stopIdx;
287-
stopIdx = Math.min(msgIdx+1, MESSAGES.length);
288-
Object.assign(updatedProcessed, {startIdx:alreadyProcessed.startIdx, stopIdx:stopIdx})
279+
stopIdx = Math.min(MSG_IDX+1, MESSAGES.length);
280+
Object.assign(updatedProcessed,
281+
{startIdx:alreadyProcessed.startIdx, stopIdx:stopIdx});
289282
}
290283

291284
active = "scroller";
@@ -307,32 +300,33 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
307300
if (msgLocal.title) {
308301
lines = g.wrapString(msgLocal.title, g.getWidth()-10);
309302
for (let i=0; i<lines.length; i++) {
310-
titleLines.push(i + (messagesWrapped[0]?messagesWrapped[0].length:0) + (messagesWrapped[1]?messagesWrapped[1].length:0));
303+
titleLines.push(i + (messagesWrapped[0]?messagesWrapped[0].length:0) +
304+
(messagesWrapped[1]?messagesWrapped[1].length:0));
311305
}
312306
}
313307
var titleCnt = lines.length;
314308
if (titleCnt) {lines.push("");} // add blank line after title
315-
lines = lines.concat(g.wrapString(msgLocal.body, g.getWidth()-10), ["-".repeat(i+1)]);
309+
lines = lines.concat(g.wrapString(msgLocal.body, g.getWidth()-10),
310+
["-".repeat(i+1)]);
316311
messagesWrapped.push(lines);
317312
}
313+
318314
let allLines = [];
319315
for (let i=0 ; i<messagesWrapped.length ; i++) {
320316
allLines = allLines.concat(messagesWrapped[i]);
321317
}
322318

323319
var initScroll = messagesWrapped[0].length;
324-
if (initiatedFrom === "other function"){
325-
if (msgIdx==0) initScroll = 0;
326-
} else if (initiatedFrom === "scrolling up") {
327-
titleLines = titleLines.concat(alreadyProcessed.titleLines.map((x) => x + allLines.length));
328-
} else if (initiatedFrom === "scrolling down"){
329-
initScroll = alreadyProcessed.lines.length-9;
330-
titleLines = alreadyProcessed.titleLines.concat(titleLines.map((x) => x + alreadyProcessed.lines.length));
331-
}
332-
333-
if (initiatedFrom === "scrolling up") {
320+
if (INITIATED_FROM === "other function") {
321+
if (MSG_IDX==0) {initScroll = 0;}
322+
} else if (INITIATED_FROM === "scrolling up") {
323+
titleLines = titleLines.concat(alreadyProcessed.titleLines.
324+
map((x) => x + allLines.length));
334325
allLines = allLines.concat(alreadyProcessed.lines);
335-
} else if (initiatedFrom === "scrolling down"){
326+
} else if (INITIATED_FROM === "scrolling down") {
327+
initScroll = alreadyProcessed.lines.length-9; // FIXME: `-9` corresponds to the Bangle.js 2 screen height - should probably be variable by screen height, font height.
328+
titleLines = alreadyProcessed.titleLines.concat(titleLines.
329+
map((x) => x + alreadyProcessed.lines.length));
336330
allLines = alreadyProcessed.lines.concat(allLines);
337331
}
338332

@@ -341,14 +335,10 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
341335
returnToClockIfEmpty();
342336
}
343337

344-
Object.assign(
345-
updatedProcessed,
346-
{
347-
lines : allLines,
338+
Object.assign(updatedProcessed,
339+
{ lines : allLines,
348340
titleLines : titleLines,
349-
messagesWrappedLength : messagesWrapped.length
350-
}
351-
)
341+
messagesWrappedLength : messagesWrapped.length });
352342

353343
let prevScrollIdx; // Used for stopping multiple triggers of next message by the scroller.
354344

@@ -364,7 +354,8 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
364354
clearRect(r);
365355
g.setFont(bodyFont).setFontAlign(0,-1).drawString(allLines[scrollIdx], r.x+r.w/2, r.y);
366356
// Load in next/previous message on demand by reinitializing showMessagesScroller while passing on the processed messages.
367-
if (scrollIdx>=allLines.length-1 && scrollIdx!=prevScrollIdx && updatedProcessed.stopIdx < MESSAGES.length) {
357+
if (scrollIdx>=allLines.length-1 && scrollIdx!=prevScrollIdx &&
358+
updatedProcessed.stopIdx<MESSAGES.length) {
368359
setTimeout(() => {
369360
E.showScroller();
370361
showMessagesScroller(MESSAGES[updatedProcessed.stopIdx], true, updatedProcessed);
@@ -629,7 +620,7 @@ function checkMessages(options) {
629620
//showMessage(newMessages[0].id, false);
630621
showMessagesScroller(newMessages[0], false);
631622
// buzz after showMessage, so being busy during layout doesn't affect the buzz pattern
632-
if (global.BUZZ_ON_NEW_MESSAGE) {espruino
623+
if (global.BUZZ_ON_NEW_MESSAGE) {
633624
// this is set if we entered the messages app by loading `messagegui.new.js`
634625
// ... but only buzz the first time we view a new message
635626
global.BUZZ_ON_NEW_MESSAGE = false;

0 commit comments

Comments
 (0)