/
app.js
1209 lines (1158 loc) · 43.4 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* MESSAGES is a list of:
{id:int,
src,
title,
subject,
body,
sender,
tel:string,
new:true // not read yet
}
*/
/* For example for maps:
// a message
{"t":"add","id":1575479849,"src":"Hangouts","title":"A Name","body":"message contents"}
// maps
{"t":"add","id":1,"src":"Maps","title":"0 yd - High St","body":"Campton - 11:48 ETA","img":"GhqBAAAMAAAHgAAD8AAB/gAA/8AAf/gAP/8AH//gD/98B//Pg/4B8f8Afv+PP//n3/f5//j+f/wfn/4D5/8Aef+AD//AAf/gAD/wAAf4AAD8AAAeAAADAAA="}
// call
{"t":"add","id":"call","src":"Phone","name":"Bob","number":"12421312",positive:true,negative:true}
*/
{
const B2 = process.env.HWVERSION>1, // Bangle.js 2?
RIGHT = 1, LEFT = -1, // swipe directions
UP = -1, DOWN = 1; // updown directions
const Layout = require("Layout");
const settings = () => require("messagegui").settings();
const fontTiny = "6x8"; // fixed size, don't use this for important things
let fontNormal;
// setFont() is also called after we close the settings screen
const setFont = function() {
const fontSize = settings().fontSize;
if (fontSize===0) // small
fontNormal = g.getFonts().includes("6x15") ? "6x15" : "6x8:2";
else if (fontSize===2) // large
fontNormal = g.getFonts().includes("6x15") ? "6x15:2" : "6x8:4";
else // medium
fontNormal = g.getFonts().includes("12x20") ? "12x20" : "6x8:3";
};
setFont();
let active, back; // active screen, last active screen
/// List of all our messages
let MESSAGES;
const saveMessages = function() {
const noSave = ["alarm", "call", "music"]; // assume these are outdated once we close the app
noSave.forEach(id => remove({id: id}));
require("messages").write(MESSAGES
.filter(m => m.id && !noSave.includes(m.id))
.map(m => {
delete m.show;
return m;
})
);
};
const uiRemove = function() {
if (musicTimeout) clearTimeout(musicTimeout);
layout = undefined;
Bangle.removeListener("message", onMessage);
saveMessages();
clearUnreadStuff();
delete Bangle.appRect;
};
const quitApp = () => load(); // TODO: revert to Bangle.showClock after fixing memory leaks
try {
MESSAGES = require("messages").getMessages();
// Apply fast loaded messages
(Bangle.MESSAGES || []).forEach(m => require("messages").apply(m, MESSAGES));
delete Bangle.MESSAGES;
// Write them back to storage when we're done
E.on("kill", saveMessages);
} catch(e) {
g.reset().clear();
E.showPrompt(/*LANG*/"Message file corrupt, erase all messages?", {title:/*LANG*/"Delete All Messages"}).then(isYes => {
// We are troubleshooting, so do a clean "load" in both cases (instead of Bangle.load)
if (isYes) { // OK: erase message file and reload this app
require("messages").clearAll();
load("messagelist.app.js");
} else {
load(); // well, this app won't work... let's go back to the clock
}
});
}
const setUI = function(options, cb) {
delete Bangle.uiRemove; // don't clear out things when switching UI within the app
options = Object.assign({remove: () => uiRemove()}, options);
Bangle.setUI(options, cb);
};
const remove = function(msg) {
if (msg.id==="call") call = undefined;
else if (msg.id==="map") map = undefined;
else if (msg.id==="alarm") alarm = undefined;
else if (msg.id==="music") music = undefined;
else MESSAGES = MESSAGES.filter(m => m.id!==msg.id);
};
const buzz = function(msg) {
return require("messages").buzz(msg.src);
};
const show = function(msg) {
delete msg.show; // don't show this again
if (msg.id==="call") showCall(msg);
else if (msg.id==="map") showMap(msg);
else if (msg.id==="alarm") showAlarm(msg);
else if (msg.id==="music") showMusic(msg);
else showMessage(msg);
};
const onMessage = function(type, msg) {
if (msg.handled) return;
msg.handled = true;
switch(type) {
case "call":
return onCall(msg);
case "music":
return onMusic(msg);
case "map":
return onMap(msg);
case "alarm":
return onAlarm(msg);
case "text":
return onText(msg);
case "clearAll":
MESSAGES = [];
if (["messages", "menu"].includes(active)) showMenu();
break;
default:
E.showAlert(/*LANG*/"Unknown message type:"+"\n"+type).then(goBack);
}
};
Bangle.on("message", onMessage);
const onCall = function(msg) {
if (msg.t==="remove") {
call = undefined;
return exitScreen("call");
}
// incoming call: show it
call = msg;
buzz(call);
showCall();
};
const onAlarm = function(msg) {
if (msg.t==="remove") {
alarm = undefined;
return exitScreen("alarm");
}
alarm = msg;
buzz(alarm);
showAlarm();
};
let musicTimeout;
const onMusic = function(msg) {
const hadMusic = !!music;
if (musicTimeout) clearTimeout(musicTimeout);
musicTimeout = undefined;
if (msg.t==="remove") {
music = undefined;
if (active==="main" && hadMusic) return showMain(); // refresh menu: remove "Music" entry (if not always visible)
else return exitScreen("music");
}
music = Object.assign({}, music, msg);
// auto-close after being paused
if (music.state!=="play") musicTimeout = setTimeout(function() {
musicTimeout = undefined;
if (active==="music" && (!music || music.state!=="play")) quitApp();
}, 60*1000); // paused for 1 minute
// auto-close after "playing" way beyond song duration (because "stop" messages don't seem to exist)
else musicTimeout = setTimeout(function() {
musicTimeout = undefined;
if (active==="music" && (!music || music.state==="play")) quitApp();
}, 2*Math.max(music.dur || 0, 5*60)*1000); // playing: assume ended after twice song duration, or at least 10 minutes
if (active==="music") showMusic(); // update music screen
else if (active==="main" && !hadMusic) {
if (settings().openMusic && music.state==="play" && music.track) showMusic();
else showMain(); // refresh menu: add "Music" entry
}
};
const onMap = function(msg) {
const hadMap = !!map;
if (msg.t==="remove") {
map = undefined;
if (back==="map") back = undefined;
if (active==="main" && hadMap) return showMain(); // refresh menu: remove "Map" entry
else return exitScreen("map");
}
map = msg;
if (["map", "music"].includes(active)) showMap(); // update map screen, or switch away from music (not other screens)
else if (active==="main" && !hadMap) showMain(); // refresh menu: add "Map" entry
};
const onText = function(msg) {
require("messages").apply(msg, MESSAGES);
const mIdx = MESSAGES.findIndex(m => m.id===msg.id);
if (!MESSAGES[mIdx]) if (back==="messages") back = undefined;
if (active==="main") showMain(); // update message count
if (MESSAGES.length===0) exitScreen("messages"); // removed last message
else if (active==="messages") showMessage(messageNum);
if (msg.new) buzz(msg);
if (active!=="call") {// don't switch away from incoming call
if (active!=="messages" || messageNum===mIdx) showMessage(mIdx);
}
if (active==="messages") drawFooter(); // update footer with new number of messages
};
const getImage = function(msg, def) {
// app icons, provided by `messages` app
return require("messageicons").getImage(msg);
};
const getImageColor = function(msg, def) {
// app colors, provided by `messages` app
return require("messageicons").getColor(msg, {default: def});
};
const getIcon = function(icon) {
return require("messagegui").getIcon(icon);
};
const getIconColor = function(icon) {
return require("messagegui").getColor(icon);
};
/*
* icons should be 24x24px with 1bpp colors and transparancy
*/
const getMessageImage = function(msg) {
if (msg.img) return atob(msg.img);
if (msg.id==="music") return getIcon("Music");
if (msg.id==="back") return getIcon("Back");
const s = (msg.src || "").toLowerCase();
return getImage(s, "notification");
};
const showMap = function() {
setActive("map");
delete map.new;
let m, distance, street, target, eta;
m = map.title.match(/(.*) - (.*)/);
if (m) {
distance = m[1];
street = m[2];
} else {
street = map.title;
}
m = map.body.match(/(.*) - (.*)/);
if (m) {
target = m[1];
eta = m[2];
} else {
target = map.body;
}
let layout = new Layout({
type: "v", c: [
{type: "txt", font: fontNormal, label: target, bgCol: g.theme.bg2, col: g.theme.fg2, fillx: 1, pad: 2},
{
type: "h", bgCol: g.theme.bg2, col: g.theme.fg2, fillx: 1, c: [
{type: "txt", font: "6x8", label: "Towards"},
{type: "txt", font: fontNormal, label: street},
]
},
{
type: "h", fillx: 1, filly: 1, c: [
map.img ? {type: "img", src: () => atob(map.img), scale: 2} : {},
{
type: "v", fillx: 1, c: [
{type: "txt", font: fontNormal, label: distance || ""},
]
},
]
},
{type: "txt", font: "6x8:2", label: eta}
]
});
layout.render();
// go back on any input
setUI({
mode: "custom",
back: goBack,
btn: b => {
if (B2 || b===2) goBack();
},
swipe: dir => {
if (dir===RIGHT) showMain();
},
});
};
const toggleMusic = function() {
const mc = cmd => {
if (Bangle.musicControl) Bangle.musicControl(cmd);
};
if (!music) {
music = {state: "play"};
mc("play");
} else if (music.state==="play") {
music.state = "pause";
mc("pause");
} else {
music.state = "play";
mc("play");
}
if (layout && layout.musicIcon) {
// musicIcon/musicToggle .src returns icon based on current music.state
layout.update(layout.musicIcon);
if (layout.musicToggle) layout.update(layout.musicToggle);
layout.render();
}
};
const doMusic = function(action) {
if (!Bangle.musicControl) return;
Bangle.buzz(50);
if (action==="toggle") toggleMusic();
else Bangle.musicControl(action);
};
const showMusic = function() {
if (active!==music) setActive("music");
if (!music) music = {track: "<unknown>", artist: "<unknown>", album: "", state: "pause"};
delete music.new;
const w = Bangle.appRect.w-50; // title/album need to leave room for icon
let artist, album;
if (music.album && settings().showAlbum) {
// max 2 lines for artist/album
artist = g.setFont(fontNormal).wrapString(music.artist, w).slice(0, 2).join("\n");
album = g.wrapString(music.album, w).slice(0, 2).join("\n");
} else {
// no album: artist gets 3 lines
artist = g.setFont(fontNormal).wrapString(music.artist, w).slice(0, 3).join("\n");
album = "";
}
// place (subtitle) on a new line
let track = music.track.replace(/ \(/, "\n(");
track = g.wrapString(track, Bangle.appRect.w).slice(0, 5).join("\n");
// "unknown" n/c/dur can show up as -1
let num, dur;
if ("n" in music && music.n>0) {
num = "#"+music.n;
if ("c" in music && music.c>0) {
num += "/"+music.c;
}
num = {type: "txt", font: fontTiny, bgCol: g.theme.bg, label: num};
}
if ("dur" in music && music.dur>0) {
dur = Math.floor(music.dur/60)+":"+(music.dur%60).toString().padStart(2, "0");
dur = {type: "txt", font: fontTiny, bgCol: g.theme.bg, label: dur};
}
let info;
if (num && dur) info = {type: "h", fillx: 1, c: [{fillx: 1}, dur, {fillx: 1}, num, {fillx: 1},]};
else if (num) info = num;
else if (dur) info = dur;
else info = {};
layout = new Layout({
type: "v", c: [
{
type: "h", fillx: 1, bgCol: g.theme.bg2, col: g.theme.fg2, c: [
{
id: "musicIcon", type: "img", pad: 10, bgCol: g.theme.bg2, col: g.theme.fg2
, src: () => getIcon((music.state==="play") ? "music" : "pause")
},
{
type: "v", fillx: 1, c: [
{type: "txt", font: fontNormal, col: g.theme.fg2, bgCol: g.theme.bg2, label: artist, pad: 2, id: "artist"},
album ? {type: "txt", font: fontNormal, col: g.theme.fg2, bgCol: g.theme.bg2, label: album, pad: 2, id: "album"} : {},
]
}
]
},
{type: "txt", halign: 0, font: fontNormal, bgCol: g.theme.bg, label: track, fillx: 1, filly: 1, pad: 2, id: "track"},
settings().musicButtons ? {
type: "h", fillx: 1, c: [
B2 ? {} : {width: 4},
{
type: "btn", id: "previous", cb: () => doMusic("previous")
, src: () => getIcon("previous")
},
{fillx: 1},
{
type: "btn", id: "musicToggle", cb: () => doMusic("toggle")
, src: () => getIcon((music.state==="play") ? "pause" : "play")
},
{fillx: 1},
{
type: "btn", id: "next", cb: () => doMusic("next")
, src: () => getIcon("next")
},
B2 ? {} : {width: 4},
]
} : {},
info,
]
});
layout.render();
let options = {mode: "updown"};
// B1 with buttons: left hand side of screen is used for "previous"
if (B2 || !settings().musicButtons) options.back = goBack;
setUI(options, ud => {
if (ud) Bangle.musicControl(ud>0 ? "volumedown" : "volumeup");
else {
if (B2 || settings().musicButtons) goBack(); // B1 left-hand touch is "previous", so we need a way to go back
else doMusic("toggle");
}
});
Bangle.swipeHandler = dir => {
if (dir!==0) doMusic(dir===RIGHT ? "previous" : "next");
};
Bangle.on("swipe", Bangle.swipeHandler);
if (Bangle.touchHandler) Bangle.removeListener("touch", Bangle.touchHandler);
if (settings().musicButtons) {
// visible buttons
// left = previous, middle = toggle, right = next
if (B2) Bangle.touchHandler = (_side, xy) => {
// accept touches on the whole bottom and pick the closest button
if (xy.y<Bangle.appRect.y+Bangle.appRect.h/2) return; // ignore touches top half
if (xy.x<Bangle.appRect.w/3) doMusic("previous");
else if (xy.x>2*Bangle.appRect.w/3) doMusic("next");
else doMusic("toggle");
};
else Bangle.touchHandler = (side) => {
if (side===1) doMusic("previous");
if (side===2) doMusic("next");
if (side===3) doMusic("toggle");
};
} else {
// no buttons: touch = toggle
// B2 setUI sets touchHandler, override that (we only want up/down swipes from the UI)
Bangle.touchHandler = (side, e) => {
// B1: side 1 (left) = back, B2: only toggle for e outside widget area
if ((!B2 && side>1) || (B2 && e.y>Bangle.appRect.y)) doMusic("toggle");
};
}
Bangle.on("touch", Bangle.touchHandler);
};
let layout;
const clearStuff = function() {
delete Bangle.appRect;
layout = undefined;
setUI();
g.reset().clearRect(Bangle.appRect);
};
const setActive = function(screen, args) {
clearStuff();
if (active && screen!==active) back = active;
if (screen==="messages") messageNum = args;
active = screen;
};
/**
* Go back to previous screen, preserving history
*/
const goBack = function() {
if (back==="call" && call) showCall();
else if (back==="map" && map) showMap();
else if (back==="music" && music) showMusic();
else if (back==="messages" && MESSAGES.length) showMessage();
else if (back) showMain(); // previous screen was "main", or no longer valid
else quitApp(); // no previous screen: go back to clock
};
/**
* Leave screen, and make sure goBack() won't take us there anymore;
* @param {string} screen
*/
const exitScreen = function(screen) {
if (back===screen) back = (active==="main") ? undefined : "main";
if (active===screen) {
active = undefined;
goBack();
}
};
const showMain = function() {
setActive("main");
let grid = {"": {title:/*LANG*/"Messages", align: 0, back: load}};
if (call) grid[/*LANG*/"Incoming Call"] = {icon: "Phone", cb: showCall};
if (alarm) grid[/*LANG*/"Alarm"] = {icon: "Alarm", cb: showAlarm};
const unread = MESSAGES.filter(m => m.new).length;
if (unread) {
grid[unread+" "+/*LANG*/"New"] = {icon: "Unread", cb: () => showMessage(MESSAGES.findIndex(m => m.new))};
grid[/*LANG*/"All"+` (${MESSAGES.length})`] = {icon: "Notification", cb: showMessage};
} else {
const allLabel = MESSAGES.length+" "+(MESSAGES.length===1 ?/*LANG*/"Message" :/*LANG*/"Messages");
if (MESSAGES.length) grid[allLabel] = {icon: "Notification", cb: showMessage};
else grid[/*LANG*/"No Messages"] = {icon: "Neg", cb: load};
}
if (unread<MESSAGES.length) {
grid[/*LANG*/"Dismiss Read"] = {
icon: "Trash", cb: () => {
E.showPrompt(/*LANG*/"Are you sure?", {title:/*LANG*/"Dismiss Read Messages"}).then(isYes => {
if (isYes) {
MESSAGES.filter(m => !m.new).forEach(msg => {
Bangle.messageResponse(msg, false);
remove(msg);
});
}
showMain();
});
}
};
}
if (map) grid[/*LANG*/"Map"] = {icon: "Map", cb: showMap};
if (music || settings().alwaysShowMusic) grid[/*LANG*/"Music"] = {icon: "Music", cb: showMusic};
grid[/*LANG*/"settings"] = {icon: "settings", cb: showSettings};
showGrid(grid);
};
const clamp = function(val, min, max) {
if (val<min) return min;
if (val>max) return max;
return val;
};
/**
* Show grid of labeled buttons,
*
* items:
* {
* cb: callback,
* img: button image,
* icon: icon name, // string, use getIcon(icon) instead of img
* col: icon color, // optional: defaults to getColor(icon)
* }
* "" item is options:
* {
* title: string,
* back: callback,
* rows/cols: (optional) fit to this many columns/rows, omit for automatic fit
* align: bottom row alignment if items don't fit perfectly into a grid
* -1: left
* 1: right
* 0: left, but move final button to the right
* undefined: spread (can be unaligned with rest of grid!)
* }
* @param items
*/
const showGrid = function(items) {
clearStuff();
const options = items[""] || {},
back = options.back || items["< Back"];
const keys = Object.keys(items).filter(k => k!=="" && k!=="< Back");
let cols;
if (options.cols) {
cols = options.cols;
} else if (options.rows) {
cols = Math.ceil(keys.length/options.rows);
} else {
const rows = Math.round(Math.sqrt(keys.length));
cols = Math.ceil(keys.length/rows);
}
let l = {type: "v", c: []};
if (options.title) {
l.c.push({id: "title", type: "txt", label: options.title, font: (B2 ? "12x20" : "6x8:2"), fillx: 1});
}
const w = Bangle.appRect.w/cols, // set explicit width, because labels can stick out
bgs = [g.theme.bgH, g.theme.bg2], // background colors used for buttons
newRow = () => ({type: "h", filly: 1, c: []});
let row = newRow(),
cbs = [[]]; // callbacks for Bangle.js 2 touchHandler below
keys.forEach(key => {
const item = items[key],
label = g.setFont(fontTiny).wrapString(key, w).join("\n");
let color = "col" in item ? item.col : getIconColor(item.icon || "Unknown");
if (color && bgs.includes(g.setColor(color).getColor())) color = undefined; // make sure button is not invisible
row.c.push({
type: "v", pad: 2, width: w, c: [
{
type: "btn",
src: item.img || (() => getIcon(item.icon || "Unknown")),
col: color,
cb: B2
? undefined // We handle B2 touches below
: () => setTimeout(item.cb), // prevent MEMORY error from running cb() inside the Layout touchHandler
},
{height: 2},
{type: "txt", label: label, font: fontTiny},
]
});
if (B2) cbs[cbs.length-1].push(item.cb);
if (row.c.length>=cols) {
l.c.push(row);
row = newRow();
if (B2) cbs.push([]);
}
});
if (row.c.length) {
if (options.align!==undefined) {
const filler = {width: w*(cols-row.c.length)};
if (options.align=== -1) row.c.unshift(filler); // left
else if (options.align===1) row.c.push(filler); // right
else if (options.align===0) row.c.splice(row.c.length-1, 0, filler); // left, but final item on right
}
l.c.push(row);
}
layout = new Layout(l, {back: back});
layout.render();
if (B2) {
// override touchHandler: no need to hit buttons exactly, just pick the nearest
if (Bangle.touchHandler) Bangle.removeListener("touch", Bangle.touchHandler);
Bangle.touchHandler = (side, xy) => {
if (xy.y<=Bangle.appRect.y) return; // widgetbar: ignore
let rows = l.c.length,
y = Bangle.appRect.y, h = Bangle.appRect.h;
if (options.title) {
rows--;
y += layout.title.h;
h -= layout.title.h;
}
const r = clamp(Math.floor(rows*(xy.y-y)/h), 0, rows-1); // row (0-indexed)
let c; // column (0-indexed)
if (r<rows-1) {
// regular row
c = clamp(Math.floor(cols*(xy.x-Bangle.appRect.x)/Bangle.appRect.w), 0, cols-1);
} else {
// bottom row: layout depends on align
if (options.align=== -1) { // left
c = clamp(Math.floor(cols*(xy.x-Bangle.appRect.x)/Bangle.appRect.w), 0, cols-1);
} else if (options.align===1) { // right
c = clamp(Math.floor(cols*(xy.x-Bangle.appRect.x)/Bangle.appRect.w)+(cols-cbs[r].length), 0, cols-1);
} else if (options.align===0) { // left, but with final item on right
c = clamp(Math.floor(cols*(xy.x-Bangle.appRect.x)/Bangle.appRect.w), 0, cols-1);
if (c===cols-1) c = cbs[r].length-1; // final row=last item
else if (c>cbs[r].length-2) return; // gap before final item
} else { // spread
c = clamp(Math.floor(cbs[r].length*(xy.x-Bangle.appRect.x)/Bangle.appRect.w), 0, cols-1);
}
}
if (r<cbs.length && c<cbs[r].length) {// should always be true
Bangle.buzz(50);
cbs[r][c]();
}
};
Bangle.on("touch", Bangle.touchHandler);
}
};
const showSettings = function() {
setActive("settings");
eval(require("Storage").read("messagelist.settings.js"))(() => {
setFont();
showMain();
});
};
const showCall = function() {
setActive("call");
delete call.new;
Bangle.setLocked(false);
Bangle.setLCDPower(1);
const w = g.getWidth()-48,
lines = g.setFont(fontNormal).wrapString(call.title, w),
title = (lines.length>2) ? lines.slice(0, 2).join("\n")+"..." : lines.join("\n");
const respond = function(accept) {
Bangle.buzz(50);
Bangle.messageResponse(call, accept);
remove(call);
call = undefined;
goBack();
};
let options = {};
if (!B2) {
options.btns = [
{
label:/*LANG*/"accept",
cb: () => respond(true),
}, {
label:/*LANG*/"ignore",
cb: goBack,
}, {
label:/*LANG*/"reject",
cb: () => respond(false),
}
];
}
layout = new Layout({
type: "v", c: [
{
type: "h", fillx: 1, bgCol: g.theme.bg2, col: g.theme.fg2, c: [
{type: "img", pad: 10, src: () => getIcon("phone"), col: getIconColor("phone")},
{
type: "v", fillx: 1, c: [
{type: "txt", font: fontTiny, label: call.src ||/*LANG*/"Incoming Call", bgCol: g.theme.bg2, col: g.theme.fg2, fillx: 1, pad: 2, halign: 1},
title ? {type: "txt", font: fontNormal, label: title, bgCol: g.theme.bg2, col: g.theme.fg2, fillx: 1, pad: 2} : {},
]
},
]
},
{type: "txt", font: fontNormal, label: call.body, fillx: 1, filly: 1, pad: 2, wrap: true},
{
type: "h", fillx: 1, c: [
// button callbacks won't actually be used: setUI below overrides the touchHandler set by Layout
{type: B2 ? "btn" : "img", src: () => getIcon("Neg"), cb: () => respond(false)},
{fillx: 1},
{type: B2 ? "btn" : "img", src: () => getIcon("Pos"), cb: () => respond(true)},
]
}
]
}, options);
layout.render();
setUI({
mode: "custom",
back: goBack,
touch: (side, xy) => {
if (B2 && xy.y<Bangle.appRect.y) return;
if (side===1) respond(false);
if (side===2) respond(true);
},
btn: b => {
if (B2 || b===2) goBack();
else if (b===1) respond(true);
else respond(false);
},
swipe: dir => {
if (dir===RIGHT) showMain();
},
});
};
const showAlarm = function() {
// dismissing alarms doesn't seem to work, so this is simple */
setActive("alarm");
delete alarm.new;
Bangle.setLocked(false);
Bangle.setLCDPower(1);
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({
type: "v", c: [
{
type: "h", fillx: 1, bgCol: g.theme.bg2, col: g.theme.fg2, c: [
alarm.body ? {type: "img", pad: 10, src: () => getIcon("alarm"), col: getIconColor("alarm")} : {},
{type: "txt", font: fontNormal, label: title ||/*LANG*/"Alarm", bgCol: g.theme.bg2, col: g.theme.fg2, fillx: 1, pad: 2, halign: 1},
]
},
alarm.body
? {type: "txt", font: fontNormal, label: alarm.body, fillx: 1, filly: 1, pad: 2, wrap: true}
: {type: "img", pad: 10, scale: 3, src: () => getIcon("alarm"), col: getIconColor("alarm")},
]
});
layout.render();
setUI({
mode: "custom",
back: goBack,
btn: b => {
if (B2 || b===2) goBack();
},
swipe: dir => {
if (dir===RIGHT) showMain();
},
});
};
/**
* Send message response, and delete it from list
* @param {string|boolean} reply Response text, false to dismiss (true to open on phone)
*/
const respondToMessage = function(reply) {
const msg = MESSAGES[messageNum];
if (msg) {
Bangle.messageResponse(msg, reply);
if (reply===false) remove(msg);
}
if (MESSAGES.length<1) goBack(); // no more messages
else showMessage((msg && reply===false) ? messageNum : messageNum+1); // show next message
};
const showMessageActions = function() {
let title = MESSAGES[messageNum].title || "";
if (g.setFont(fontNormal).stringMetrics(title).width>Bangle.appRect.w-(B2 ? 0 : 20)) {
title = g.wrapString("..."+title, Bangle.appRect.w-(B2 ? 0 : 20))[0].substring(3)+"...";
}
clearStuff();
let grid = {
"": {
title: title ||/*LANG*/"Message",
back: () => showMessage(messageNum),
cols: 3, // fit all replies on first row, dismiss on bottom
}
};
// Text replies don't work (yet)
// grid[/*LANG*/"OK"] = {icon: "Ok", col: "#0f0", cb: () => respondToMessage("\u{1F44D}")}; // "Thumbs up" emoji
// grid[/*LANG*/"Nak"] = {icon: "Nak", col: "#f00", cb: () => respondToMessage("\u{1F44E}")}; // "Thumbs down" emoji
// grid[/*LANG*/"No Phone"] = {icon: "NoPhone", col: "#f0f", cb: () => respondToMessage("\u{1F4F5}")}; // "No Mobile Phones" emoji
grid[/*LANG*/"Dismiss"] = {icon: "Trash", col: "#ff0", cb: () => respondToMessage(false)};
showGrid(grid);
};
/**
* Show message
*
* @param {number} [num=0] Message to show
* @param {boolean} [bottom=false] Scroll message to bottom right away
*/
let buzzing = false, moving = false, switching = false;
let h, fh, offset;
/**
* draw (sticky) footer
*/
const drawFooter = function() {
// left hint: swipe from left for main menu
g.reset().clearRect(Bangle.appRect.x, Bangle.appRect.y2-fh, Bangle.appRect.x2, Bangle.appRect.y2)
.setFont(fontTiny)
.setFontAlign(-1, 1) // bottom left
.drawString(
"\0"+atob("CAiBACBA/EIiAnwA")+ // back
"\0"+atob("CAiBAEgkEgkSJEgA"), // >>
Bangle.appRect.x+(B2 ? 1 : 28), Bangle.appRect.y2
);
// center message count+hints: swipe up/down for next/prev message
const footer = ` ${messageNum+1}/${MESSAGES.length} `,
fw = g.stringWidth(footer);
g.setFontAlign(0, 1); // bottom center
if (B2 && messageNum>0 && offset<=0)
g.drawString("\0"+atob("CAiBAABBIhRJIhQI"), Bangle.appRect.x+Bangle.appRect.w/2-fw/2, Bangle.appRect.y2); // ^ swipe to prev
g.drawString(footer, Bangle.appRect.x+Bangle.appRect.w/2, Bangle.appRect.y2);
if (B2 && messageNum<MESSAGES.length-1 && offset>=h-(Bangle.appRect.h-fh))
g.drawString("\0"+atob("CAiBABAoRJIoRIIA"), Bangle.appRect.x+Bangle.appRect.w/2+fw/2, Bangle.appRect.y2); // v swipe to next
// right hint: swipe from right for message actions
g.setFontAlign(1, 1) // bottom right
.drawString(
"\0"+atob("CAiBABIkSJBIJBIA")+ // <<
"\0"+atob("CAiBAP8AAP8AAP8A"), // = ("hamburger menu")
Bangle.appRect.x2-(B2 ? 1 : 28), Bangle.appRect.y2
);
};
const showMessage = function(num, bottom) {
if (num<0) num = 0;
if (!num) num = 0; // no number: show first
if (num>=MESSAGES.length) num = MESSAGES.length-1;
setActive("messages", num);
if (!MESSAGES.length) {
// I /think/ this should never happen...
return E.showPrompt(/*LANG*/"No Messages", {
title:/*LANG*/"Messages",
img: require("heatshrink").decompress(atob("kkk4UBrkc/4AC/tEqtACQkBqtUDg0VqAIGgoZFDYQIIM1sD1QAD4AIBhnqA4WrmAIBhc6BAWs8AIBhXOBAWz0AIC2YIC5wID1gkB1c6BAYFBEQPqBAYXBEQOqBAnDAIQaEnkAngaEEAPDFgo+IKA5iIOhCGIAFb7RqAIGgtUBA0VqobFgNVA")),
buttons: {/*LANG*/"Ok": 1}
}).then(showMain);
}
Bangle.setLocked(false);
Bangle.setLCDPower(1);
// only clear msg.new on user input
const msg = MESSAGES[messageNum]; // message
fh = 10; // footer height
offset = 0;
let oldOffset = 0;
const move = (dy) => {
offset = Math.max(0, Math.min(h-(Bangle.appRect.h-fh), offset+dy)); // clip at message height
dy = oldOffset-offset; // real dy
// move all elements to new offset
const offsetRecurser = function(l) {
if (l.y) l.y += dy;
if (l.c) l.c.forEach(offsetRecurser);
};
offsetRecurser(layout.l);
oldOffset = offset;
draw();
};
const draw = () => {
g.reset()
.clearRect(Bangle.appRect.x, Bangle.appRect.y, Bangle.appRect.x2, Bangle.appRect.y2-fh)
.setClipRect(Bangle.appRect.x, Bangle.appRect.y, Bangle.appRect.x2, Bangle.appRect.y2-fh);
g.reset = () => g.setColor(g.theme.fg).setBgColor(g.theme.bg); // stop Layout resetting ClipRect
layout.render();
if (layout.button && h>Bangle.appRect.h-fh && offset<h-(Bangle.appRect.h-fh)) {
// not yet at bottom: draw button "disabled", i.e. in fg+bg instead of fg2/fgH+bg2/bgH
g.theme = Object.assign({}, g.theme); // allow overriding
g.theme.fg2 = g.theme.fgH = g.theme.fg;
g.theme.bg2 = g.theme.bgH = g.theme.bg;
layout.render(layout.button);
delete g.theme; // restore original
}
delete g.reset;
// draw scrollbar (simply on top of everything)
if (h>(Bangle.appRect.h-fh)) {
const sbh = (Bangle.appRect.h-fh)/h*(Bangle.appRect.h-fh), // scrollbar height
y1 = Bangle.appRect.y+offset/h*(Bangle.appRect.h-fh), y2 = y1+sbh;
g.setColor(g.theme.bg).drawLine(Bangle.appRect.x2, Bangle.appRect.y, Bangle.appRect.x2, Bangle.appRect.y2-fh);
g.setColor(g.theme.fg).drawLine(Bangle.appRect.x2, y1, Bangle.appRect.x2, y2);
}
drawFooter();
};
const buzzOnce = () => {
if (buzzing) return;
buzzing = true;
Bangle.buzz(50).then(() => setTimeout(() => {buzzing = false;}, 500));
};
layout = getMessageLayout(msg);
h = layout.l.h; // message height
if (bottom) move(h); // scrolling backwards: jump to bottom of message
else draw();
const PAGE_SIZE = Bangle.appRect.h-fh;
const // shared B1/B2 handlers
back = () => {
delete msg.new; // we mark messages as read on any input
goBack();
},
swipe = dir => {
delete msg.new;
if (dir===RIGHT) showMain();
else if (dir===LEFT) showMessageActions();
},
touch = (side, xy) => {
delete msg.new;
if (h<=Bangle.appRect.h-fh || offset>=h-(Bangle.appRect.h-fh)) { // already at bottom
// B2: check for button-press
// setUI overrides Layout listeners, so we need to check for button presses ourselves
if (B2 && layout.button) {
const b = layout.button;
// the button is at the bottom of the screen, so we accept touches all the way down
if (xy.x>=b.x && xy.y>=b.y && xy.x<=b.x+b.w /*&& xy.y<=b.y+b.h*/) return b.cb();
}
if (B2 && xy.y<Bangle.appRect.y) return;
}
// first: pagedown until we reach the bottom
if (h>Bangle.appRect.h-fh && offset<h-(Bangle.appRect.h-fh)) return move(+PAGE_SIZE);
// B1: when scrolled all the way down, treat whole right-side as button
if (!B2 && layout.button && side===2) return layout.button.cb();
// touch anywhere else:
switch(settings().onTap) {
case 1: // Dismiss
return respondToMessage(false);
case 2: // Back
return goBack();
case 3: // Do nothing
return;
case 0: // Message Menu
default:
return showMessageActions();
}
};
if (B2) { // Bangle.js 2
setUI({
mode: "custom",
back: back,
swipe: swipe,
drag: e => {
delete msg.new;
if (!switching) {
const dy = -e.dy;
if (dy>0) { // up
if (h>Bangle.appRect.h-fh && offset<h-(Bangle.appRect.h-fh)) {
moving = true; // prevent scrolling right into next message
move(dy);
} else if (messageNum<MESSAGES.length-1) { // already at bottom: show next
if (!moving) { // don't scroll right through to next message
Bangle.buzz(30);
switching = true; // don't process any more drag events until we lift our finger
showMessage(messageNum+1);
}
} else { // already at bottom of last message
buzzOnce();
}
} else if (dy<0) { // down
if (offset>0) {
moving = true; // prevent scrolling right into prev message
move(dy);
} else if (messageNum>0) { // already at top: show prev
if (!moving) { // don't scroll right through to previous message
Bangle.buzz(30);
switching = true; // don't process any more drag events until we lift our finger
showMessage(messageNum-1, true);
}
} else { // already at top of first message
buzzOnce();
}
}
}
if (!e.b) {
// touch end: we can swipe to another message (if we reached the top/bottom) or move the new message
moving = false;
switching = false;
}
},
touch: touch,
});
} else { // Bangle.js 1
setUI({
mode: "updown",
back: back,
}, dir => {
delete msg.new;
if (dir===DOWN) {
if (h>Bangle.appRect.h-fh && offset<h-(Bangle.appRect.h-fh)) {
move(+PAGE_SIZE);
} else if (messageNum<MESSAGES.length-1) { // bottom reached: show next
Bangle.buzz(30);
showMessage(messageNum+1);
} else {
buzzOnce(); // already at bottom of last message
}