Skip to content

Commit 07083dc

Browse files
Move clockinfo config to settings
1 parent 11f337f commit 07083dc

File tree

2 files changed

+109
-95
lines changed

2 files changed

+109
-95
lines changed

apps/mtnclock/app.js

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ if (! Array.isArray(data.rows)) {
55
require("Storage").writeJSON("mtnclock.json", data);
66
}
77

8-
let showingSettings = false;
9-
let clockinfosSettings = [];
108
let clockinfosMain = {};
119
let clockinfos = require("clock_info").load();
1210

@@ -236,7 +234,6 @@ function draw(color) {
236234
}
237235

238236
function setWeather() {
239-
if (showingSettings) return;
240237
var a = {};
241238
//clear day/night is default weather
242239
if ((data.code >= 800 && data.code <=802) || data.code == undefined) {
@@ -398,60 +395,6 @@ global.GB = (event) => {
398395
if (_GB) setTimeout(_GB, 0, event);
399396
};
400397

401-
function drawClkinfoSettings() {
402-
if (drawTimeout) clearTimeout(drawTimeout);
403-
g.clear();
404-
g.setColor(g.theme.fg);
405-
g.setFont("Vector", py(10)).setFontAlign(-1, -1).drawString("<Back", px(2), py(9));
406-
g.drawRect(1, 1, px(33), px(25)-1);
407-
g.setFont("Vector", py(10)).setFontAlign(0, -1).drawString("-", px(50), py(9));
408-
g.drawRect(px(33), 1, px(67), px(25)-1);
409-
g.setFont("Vector", py(10)).setFontAlign(0, -1).drawString("+", px(83), py(9));
410-
g.drawRect(px(67), 1, px(100)-1, px(25)-1);
411-
412-
data.rows.forEach(function(row, r) {
413-
let a = row.menuA;
414-
let b = row.menuB;
415-
let ci = clockinfos[a].items[b];
416-
if (clockinfosMain[a] && clockinfosMain[a][b]) {
417-
clockinfosMain[a][b] = false;
418-
ci.hide();
419-
ci.removeListener("redraw", clockinfoRedraw);
420-
}
421-
addClockinfo(r)
422-
});
423-
clockinfosMain = {};
424-
}
425-
426-
function addClockinfo(r) {
427-
let dr = data.rows[r];
428-
// Check if the saved clockinfo indices still exist
429-
let ma = (dr && dr.menuA && clockinfos[dr.menuA]) ? dr.menuA : 0;
430-
let mb = (ma && dr && dr.menuB && clockinfos[ma].items[dr.menuB]) ? dr.menuB : 0;
431-
clockinfosSettings[r] = require("clock_info").addInteractive(clockinfos, {
432-
x : 2, y: py((r+1)*25)+1, w: px(100)-4, h: py(25)-2,
433-
menuA: ma,
434-
menuB: mb,
435-
draw : (itm, info, options) => {
436-
g.reset().clearRect(options.x-1, options.y, options.x+options.w+1, options.y+options.h);
437-
if (options.focus) g.drawRect(options.x-1, options.y, options.x+options.w+1, options.y+options.h);
438-
g.setFont("Vector", py(10)).setFontAlign(-1, 0).drawString(info.text, options.x, options.y+options.h/2);
439-
}
440-
});
441-
}
442-
443-
function saveClockinfos() {
444-
data.rows = [];
445-
clockinfosSettings.forEach(function(row, r) {
446-
data.rows[r] = {
447-
menuA: row.menuA,
448-
menuB: row.menuB
449-
}
450-
row.remove();
451-
});
452-
require("Storage").writeJSON("mtnclock.json", data);
453-
}
454-
455398
var drawTimeout;
456399
var redrawTimeout;
457400

@@ -477,41 +420,6 @@ function queueDraw() {
477420
}, 60000 - (Date.now() % 60000));
478421
}
479422

480-
function checkTouchBack(xy) {
481-
return xy.x <= px(33) && xy.y < py(25);
482-
}
483-
484-
function checkTouchMinus(xy) {
485-
return xy.x > px(33) && xy.x < px(67) && xy.y < px(25);
486-
}
487-
488-
function checkTouchPlus(xy) {
489-
return xy.x >= px(67) && xy.y < px(25);
490-
}
491-
492-
Bangle.on('touch', function(b, xy) {
493-
// Bangle.js 2 supports long touch (type 2)
494-
// On other devices, any touch will show the settings
495-
if (!showingSettings && (xy.type == 2 || process.env.HWVERSION != 2)) {
496-
drawClkinfoSettings();
497-
showingSettings = true;
498-
} else if (checkTouchBack(xy)) {
499-
showingSettings = false;
500-
saveClockinfos();
501-
queueDraw();
502-
// call setWeather after a timeout because for some reason a clockinfo
503-
// can still draw for a little bit despite calling remove() on it
504-
setTimeout(setWeather, 100);
505-
} else if (checkTouchMinus(xy) && clockinfosSettings.length > 0) {
506-
let cl = clockinfosSettings[clockinfosSettings.length - 1];
507-
cl.remove();
508-
g.reset().clearRect(cl.x, cl.y, cl.x+cl.w-2, cl.y+cl.h-1);
509-
clockinfosSettings.pop();
510-
} else if (checkTouchPlus(xy) && clockinfosSettings.length < 3) {
511-
addClockinfo(clockinfosSettings.length)
512-
}
513-
});
514-
515423
queueDraw();
516424
readWeather();
517425
setWeather();

apps/mtnclock/settings.js

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,128 @@
77
var SETTINGS = Object.assign({
88
// default values
99
showWidgets: false,
10+
rows: []
1011
}, STORAGE.readJSON(FILE, true) || {});
1112

1213
function writeSettings() {
1314
STORAGE.writeJSON(FILE, SETTINGS);
1415
}
1516

17+
let showingClockinfos = false;
18+
let clockinfosSettings = [];
19+
let clockinfos = require("clock_info").load();
20+
1621
// Show the menu
17-
E.showMenu({
22+
let menu = {
1823
"" : { "title" : "Mountain Clock" },
1924
"< Back" : () => back(),
2025
'Show widgets': {
2126
value: !!SETTINGS.showWidgets, // !! converts undefined to false
22-
onchange: value => {
27+
onchange: (value) => {
2328
SETTINGS.showWidgets = value;
2429
writeSettings();
2530
}
2631
},
27-
});
32+
'Edit Clockinfos': () => {
33+
showingClockinfos = true;
34+
drawClockinfoSettings();
35+
},
36+
};
37+
38+
E.showMenu(menu);
39+
40+
function drawClockinfoSettings() {
41+
Bangle.setUI(undefined);
42+
require("widget_utils").hide();
43+
g.clear();
44+
g.setColor(g.theme.fg);
45+
g.setFont("Vector", py(10)).setFontAlign(-1, -1).drawString("<Back", px(2), py(9));
46+
g.drawRect(1, 1, px(33), px(25)-1);
47+
g.setFont("Vector", py(10)).setFontAlign(0, -1).drawString("-", px(50), py(9));
48+
g.drawRect(px(33), 1, px(67), px(25)-1);
49+
g.setFont("Vector", py(10)).setFontAlign(0, -1).drawString("+", px(83), py(9));
50+
g.drawRect(px(67), 1, px(100)-1, px(25)-1);
51+
52+
SETTINGS.rows.forEach(function(row, r) {
53+
addClockinfo(r)
54+
});
55+
}
56+
57+
function addClockinfo(r) {
58+
let dr = SETTINGS.rows[r];
59+
// Check if the saved clockinfo indices still exist
60+
let ma = (dr && dr.menuA && clockinfos[dr.menuA]) ? dr.menuA : 0;
61+
let mb = (ma && dr && dr.menuB && clockinfos[ma].items[dr.menuB]) ? dr.menuB : 0;
62+
clockinfosSettings[r] = require("clock_info").addInteractive(clockinfos, {
63+
x : 2, y: py((r+1)*25)+1, w: px(100)-4, h: py(25)-2,
64+
menuA: ma,
65+
menuB: mb,
66+
draw : (itm, info, options) => {
67+
g.reset().clearRect(options.x-1, options.y, options.x+options.w+1, options.y+options.h);
68+
if (options.focus) g.drawRect(options.x-1, options.y, options.x+options.w+1, options.y+options.h);
69+
g.setFont("Vector", py(10)).setFontAlign(-1, 0).drawString(info.text, options.x, options.y+options.h/2);
70+
}
71+
});
72+
}
73+
74+
function saveClockinfos() {
75+
SETTINGS.rows = [];
76+
clockinfosSettings.forEach(function(row, r) {
77+
SETTINGS.rows[r] = {
78+
menuA: row.menuA,
79+
menuB: row.menuB
80+
}
81+
row.remove();
82+
});
83+
console.log(JSON.stringify(SETTINGS));
84+
writeSettings();
85+
}
86+
87+
//scale x, y coords to screen
88+
function px(x) {
89+
return x*g.getWidth()/100;
90+
}
91+
92+
function py(y) {
93+
return y*g.getHeight()/100;
94+
}
95+
96+
function checkTouchBack(xy) {
97+
return xy.x <= px(33) && xy.y < py(25);
98+
}
99+
100+
function checkTouchMinus(xy) {
101+
return xy.x > px(33) && xy.x < px(67) && xy.y < px(25);
102+
}
103+
104+
function checkTouchPlus(xy) {
105+
return xy.x >= px(67) && xy.y < px(25);
106+
}
107+
108+
Bangle.on('touch', function(b, xy) {
109+
if (!showingClockinfos) return;
110+
// Bangle.js 2 supports long touch (type 2)
111+
// On other devices, any touch will show the settings
112+
if (checkTouchBack(xy)) {
113+
showingClockinfos = false;
114+
saveClockinfos();
115+
// call setWeather after a timeout because for some reason a clockinfo
116+
// can still draw for a little bit despite calling remove() on it
117+
setTimeout(() => {
118+
Bangle.setUI({
119+
mode: "custom",
120+
back: back,
121+
});
122+
require("widget_utils").show();
123+
E.showMenu(menu);
124+
}, 100);
125+
} else if (checkTouchMinus(xy) && clockinfosSettings.length > 0) {
126+
let cl = clockinfosSettings[clockinfosSettings.length - 1];
127+
cl.remove();
128+
g.reset().clearRect(cl.x, cl.y, cl.x+cl.w-2, cl.y+cl.h-1);
129+
clockinfosSettings.pop();
130+
} else if (checkTouchPlus(xy) && clockinfosSettings.length < 3) {
131+
addClockinfo(clockinfosSettings.length)
132+
}
133+
});
28134
})

0 commit comments

Comments
 (0)