Skip to content

Commit c04ec08

Browse files
author
thyttan
committed
messagegui: further refactoring
1 parent 840e832 commit c04ec08

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

apps/messagegui/app.js

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -250,65 +250,62 @@ function showMusicMessage(msg) {
250250
function showMessagesScroller(msg, persist, alreadyProcessed) {
251251
if (persist===undefined) {persist = true;}
252252
const MSG_IDX = msg ? MESSAGES.findIndex((m)=>m.id==msg.id) : 0;
253-
const INITIATED_FROM =
253+
const INITIATED_FROM = (
254254
alreadyProcessed===undefined ? "other function" :
255-
(MSG_IDX<=alreadyProcessed.startIdx ? "scrolling up" :
256-
(MSG_IDX >= alreadyProcessed.stopIdx-1 ? "scrolling down" : undefined));
255+
(MSG_IDX<=alreadyProcessed.idxSpan.start ? "scrolling up" :
256+
(MSG_IDX >= alreadyProcessed.idxSpan.stop-1 ? "scrolling down" :
257+
undefined))
258+
);
259+
if (!alreadyProcessed) {alreadyProcessed = {idxSpan:{}};} // So `alreadyProcessed.idxSpan.start/stop` can be looked for on first run.
257260

258261
const WU = require("widget_utils");
259262
WU.hide();
260263

261264
if (replying) { return; }
262265
if (persist) {cancelReloadTimeout();} else {resetReloadTimeout();}
263266

264-
let updatedProcessed = {};
265-
let startIdx = 0;
266-
let stopIdx = 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;
274-
stopIdx = alreadyProcessed.startIdx;
275-
Object.assign(updatedProcessed,
276-
{startIdx:startIdx, stopIdx:alreadyProcessed.stopIdx});
277-
} else if (INITIATED_FROM === "scrolling down") {
278-
startIdx = alreadyProcessed.stopIdx;
279-
stopIdx = Math.min(MSG_IDX+1, MESSAGES.length);
280-
Object.assign(updatedProcessed,
281-
{startIdx:alreadyProcessed.startIdx, stopIdx:stopIdx});
282-
}
267+
let idxSpan = (
268+
INITIATED_FROM==="other function" ?
269+
{start : Math.max(MSG_IDX-1, 0), stop : Math.min(MSG_IDX+2, MESSAGES.length)} :
270+
(INITIATED_FROM==="scrolling up" ?
271+
{start : MSG_IDX, stop : alreadyProcessed.idxSpan.start } :
272+
(INITIATED_FROM==="scrolling down" ?
273+
{ start : alreadyProcessed.idxSpan.stop, stop : Math.min(MSG_IDX+1, MESSAGES.length) } :
274+
undefined))
275+
);
283276

284277
active = "scroller";
285278
var bodyFont = fontBig;
286279
g.setFont(bodyFont);
287280
var titleLines = [];
288281
var messagesWrapped = [];
289-
for (let i=startIdx ; i<stopIdx ; i++) {
282+
for (let i=idxSpan.start ; i<idxSpan.stop ; i++) {
290283
let msgLocal = MESSAGES[i];
291284
msgLocal.new = false;
292285

293286
if (msgLocal.id=="music" || msgLocal.source=="maps" || msgLocal.id =="call") {
294-
stopIdx++
295-
if (stopIdx>=MESSAGES.length) {break;}
287+
idxSpan.stop++
288+
if (idxSpan.stop>=MESSAGES.length) {break;}
296289
continue;
297290
}
298291

299292
var lines = [];
300-
const TITLE_STRING = "".concat(msgLocal.title, msgLocal.title&&"\n",
301-
msgLocal.sender, msgLocal.sender&&"\n", msgLocal.subject,
302-
msgLocal.subject&&"\n", msgLocal.src) || "No Title";
293+
const TITLE_STRING = msgLocal.title||msgLocal.sender||msgLocal.subject||msgLocal.src||"No Title";
294+
//const TITLE_STRING = "".concat(msgLocal.title, msgLocal.title&&"\n",
295+
// msgLocal.sender, msgLocal.sender&&"\n",
296+
// msgLocal.subject, msgLocal.subject&&"\n", msgLocal.src) || "No Title";
303297
lines = g.wrapString(TITLE_STRING, g.getWidth()-10);
304298
for (let i=0; i<lines.length; i++) {
305299
titleLines.push(i + (messagesWrapped[0]?messagesWrapped[0].length:0) +
306300
(messagesWrapped[1]?messagesWrapped[1].length:0));
307301
}
308302
var titleCnt = lines.length;
309303
if (titleCnt) {lines.push("");} // add blank line after title
304+
//const PAD_LEN = (MESSAGES.length-i)/2;
305+
//const END_LINE = "-".repeat(PAD_LEN)+"<"+"=".repeat(i)+">"+"-".repeat(PAD_LEN);
306+
const END_LINE = "-".repeat(12);
310307
lines = lines.concat(g.wrapString(msgLocal.body, g.getWidth()-10),
311-
["-".repeat(i+1)]);
308+
[END_LINE]);
312309
messagesWrapped.push(lines);
313310
}
314311

@@ -325,7 +322,7 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
325322
map((x) => x + allLines.length));
326323
allLines = allLines.concat(alreadyProcessed.lines);
327324
} else if (INITIATED_FROM === "scrolling down") {
328-
initScroll = alreadyProcessed.lines.length-9; // FIXME: `-9` corresponds to the Bangle.js 2 screen height - should probably be variable by screen height, font height.
325+
initScroll = alreadyProcessed.lines.length-(g.getHeight()/g.getFontHeight());
329326
titleLines = alreadyProcessed.titleLines.concat(titleLines.
330327
map((x) => x + alreadyProcessed.lines.length));
331328
allLines = alreadyProcessed.lines.concat(allLines);
@@ -336,49 +333,55 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
336333
returnToClockIfEmpty();
337334
}
338335

339-
Object.assign(updatedProcessed,
340-
{ lines : allLines,
341-
titleLines : titleLines,
342-
messagesWrappedLength : messagesWrapped.length });
336+
alreadyProcessed = { // Update with the newly processed messages.
337+
lines : allLines,
338+
titleLines : titleLines,
339+
idxSpan: {
340+
start : Math.min(idxSpan.start, alreadyProcessed.idxSpan.start||MESSAGES.length),
341+
stop : Math.max(idxSpan.stop, alreadyProcessed.idxSpan.stop||0)}
342+
};
343343

344-
let prevScrollIdx; // Used for stopping multiple triggers of next message by the scroller.
344+
let prevScrollIdx; // Used for stopping repeated triggering of next message by the scroller.
345345

346346
E.showScroller({
347347
scroll : initScroll*g.getFontHeight(),
348348
h : g.getFontHeight(), // height of each menu item in pixels
349349
c : allLines.length, // number of menu items
350350
// a function to draw a menu item
351351
draw : function(scrollIdx, r) {
352-
"ram"
353-
g.setBgColor(titleLines.find((e)=>e==scrollIdx)!==undefined ? g.theme.bg2 : g.theme.bg).
354-
setColor(titleLines.find((e)=>e==scrollIdx)!==undefined ? g.theme.fg2 : g.theme.fg).
352+
"ram";
353+
g.setBgColor(titleLines.find(e=>e==scrollIdx)!==undefined ? g.theme.bg2 : g.theme.bg).
354+
setColor(titleLines.find(e=>e==scrollIdx)!==undefined ? g.theme.fg2 : g.theme.fg).
355355
clearRect(r);
356356
g.setFont(bodyFont).setFontAlign(0,-1).drawString(allLines[scrollIdx], r.x+r.w/2, r.y);
357357
// Load in next/previous message on demand by reinitializing showMessagesScroller while passing on the processed messages.
358358
if (scrollIdx>=allLines.length-1 && scrollIdx!=prevScrollIdx &&
359-
updatedProcessed.stopIdx<MESSAGES.length) {
359+
alreadyProcessed.idxSpan.stop<MESSAGES.length) {
360360
setTimeout(() => {
361361
E.showScroller();
362-
showMessagesScroller(MESSAGES[updatedProcessed.stopIdx], true, updatedProcessed);
362+
showMessagesScroller(MESSAGES[alreadyProcessed.idxSpan.stop],
363+
true, alreadyProcessed);
363364
}, 40);
364365
}
365-
if (scrollIdx==0 && scrollIdx!=prevScrollIdx && updatedProcessed.startIdx>0) {
366+
if (scrollIdx==0 && scrollIdx!=prevScrollIdx && alreadyProcessed.idxSpan.start>0) {
366367
setTimeout(() => {
367368
E.showScroller();
368-
showMessagesScroller(MESSAGES[updatedProcessed.startIdx-1], true, updatedProcessed);
369+
showMessagesScroller(MESSAGES[alreadyProcessed.idxSpan.start-1],
370+
true, alreadyProcessed);
369371
}, 40);
370372
}
371373
prevScrollIdx = scrollIdx;
372-
}, select : function(scrollIdx, touch) {
374+
},
375+
select : function(scrollIdx, touch) {
373376
let msgSelect;
374-
let titleLinesInitials = [titleLines[0]];
377+
let firstTitleLinePerMsg = [titleLines[0]];
375378
for (let i=1; i<titleLines.length; i++) {
376379
if (titleLines[i]-titleLines[i-1] === 1) {continue;}
377-
titleLinesInitials.push(titleLines[i]);
380+
firstTitleLinePerMsg.push(titleLines[i]);
378381
}
379382
for (let i=titleLines.length-1; i>=0 ; i--) {
380-
if (scrollIdx>=titleLinesInitials[i]) {
381-
msgSelect = MESSAGES[i + updatedProcessed.startIdx];
383+
if (scrollIdx>=firstTitleLinePerMsg[i]) {
384+
msgSelect = MESSAGES[i + alreadyProcessed.idxSpan.start];
382385
break;
383386
}
384387
}

0 commit comments

Comments
 (0)